mirror of
https://github.com/tiennm99/CTDL-GT.git
synced 2026-06-09 16:13:01 +00:00
Init
This commit is contained in:
+27
@@ -0,0 +1,27 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
bool isLeapYear(int n)
|
||||
{
|
||||
if ((n % 4 == 0 && n % 100 != 0) || n % 400 == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
cout << "Nhap so nam:" << endl;
|
||||
cin >> n;
|
||||
if (isLeapYear(n))
|
||||
{
|
||||
cout << "YES";
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "NO";
|
||||
}
|
||||
cout << endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
void nhapMatran(int *arr, int r, int c)
|
||||
{
|
||||
for (int i = 0; i < r; i++)
|
||||
{
|
||||
for (int j = 0; j < c; j++)
|
||||
{
|
||||
cout << "Nhap phan tu tai hang " << i + 1 << ", cot " << j + 1 << ":";
|
||||
cin >> *(arr + r*i + j);
|
||||
}
|
||||
}
|
||||
}
|
||||
void hienthiMatran(int *arr, int r, int c)
|
||||
{
|
||||
for (int i = 0; i < r; i++)
|
||||
{
|
||||
for (int j = 0; j < c; j++)
|
||||
{
|
||||
cout << *(arr + r * i + j) << "\t";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
void nhanMatran(int *a, int ar, int ac, int *b, int br, int bc, int *c)
|
||||
{
|
||||
for (int i = 0; i < ar; i++)
|
||||
{
|
||||
for (int j = 0; j < bc; j++)
|
||||
{
|
||||
*(c + ar * i + j) = 0;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < ar; i++)
|
||||
{
|
||||
for (int j = 0; j < bc; j++)
|
||||
{
|
||||
for (int k = 0; k < ac; k++)
|
||||
{
|
||||
*(c + ar * i + j) += (*(a + ar * i + k)) * (*(b + ac * k + j));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
const int ar = 3;
|
||||
const int ac = 3;
|
||||
const int br = 3;
|
||||
const int bc = 3;
|
||||
int a[ar][ac] =
|
||||
{
|
||||
{1, 2, 3},
|
||||
{4, 5, 6},
|
||||
{7, 8, 9}
|
||||
};
|
||||
int b[br][bc] =
|
||||
{
|
||||
{1, 2, 3},
|
||||
{4, 5, 6},
|
||||
{7, 8, 9}
|
||||
};
|
||||
int c[ar][bc];
|
||||
/*
|
||||
cout << "Nhap vao ma tran A[" << ar << "][" << ac << "]:" << endl;
|
||||
nhapMatran(*a, ar, ac);
|
||||
cout << "Nhap vao ma tran B[" << br << "][" << bc << "]:" << endl;
|
||||
nhapMatran(*b, br, bc);
|
||||
*/
|
||||
cout << "Ma tran A:" << endl;
|
||||
hienthiMatran(*a, ar, ac);
|
||||
cout << "Ma tran B:" << endl;
|
||||
hienthiMatran(*b, ar, ac);
|
||||
cout << "Ma tran C = A*B:" << endl;
|
||||
nhanMatran(*a, ar, ac, *b, br, bc, *c);
|
||||
hienthiMatran(*c, ar, bc);
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
void nhapMatran(int **arr, int r, int c)
|
||||
{
|
||||
for (int i = 0; i < r; i++)
|
||||
{
|
||||
for (int j = 0; j < c; j++)
|
||||
{
|
||||
cout << "Nhap phan tu tai hang " << i + 1 << ", cot " << j + 1 << ":";
|
||||
cin >> arr[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
void hienthiMatran(int **arr, int r, int c)
|
||||
{
|
||||
for (int i = 0; i < r; i++)
|
||||
{
|
||||
for (int j = 0; j < c; j++)
|
||||
{
|
||||
cout << arr[i][j] << "\t";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
bool nhanMatran(int **a, int ar, int ac, int **b, int br, int bc, int **c, int &cr, int &cc)
|
||||
{
|
||||
if (ac == br)
|
||||
{
|
||||
for (int i = 0; i < cr; i++)
|
||||
{
|
||||
for (int j = 0; j < cc; j++)
|
||||
{
|
||||
c[i][j] = 0;
|
||||
for (int k = 0; k < ac; k++)
|
||||
{
|
||||
c[i][j] += a[i][k] * b[k][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int **a, **b, **c;
|
||||
int ar, ac, br, bc, cr, cc;
|
||||
cout << "Nhap vao so hang cua ma tran A:";
|
||||
cin >> ar;
|
||||
cout << "Nhap vao so cot cua ma tran A:";
|
||||
cin >> ac;
|
||||
a = new int*[ar];
|
||||
for (int i = 0; i < ar; i++)
|
||||
{
|
||||
a[i] = new int[ac];
|
||||
}
|
||||
nhapMatran(a, ar, ac);
|
||||
cout << "Nhap vao so hang cua ma tran B:";
|
||||
cin >> br;
|
||||
cout << "Nhap vao so cot cua ma tran B:";
|
||||
cin >> bc;
|
||||
b = new int*[br];
|
||||
for (int i = 0; i < br; i++)
|
||||
{
|
||||
b[i] = new int[bc];
|
||||
}
|
||||
nhapMatran(b, br, bc);
|
||||
cr = ar;
|
||||
cc = bc;
|
||||
c = new int*[cr];
|
||||
for (int i = 0; i < cr; i++)
|
||||
{
|
||||
c[i] = new int[cc];
|
||||
}
|
||||
cout << "Ma tran A[" << ar << "][" << ac << "]:" << endl;
|
||||
hienthiMatran(a, ar, ac);
|
||||
cout << "Ma tran B[" << br << "][" << bc << "]:" << endl;
|
||||
hienthiMatran(b, ar, ac);
|
||||
cout << "Ma tran C = A*B:" << endl;
|
||||
if (nhanMatran(a, ar, ac, b, br, bc, c, cr, cc))
|
||||
{
|
||||
hienthiMatran(c, cr, cc);
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "Khong the nhan hai ma tran voi kich co " << ar << "*" << ac << " va " << br << "*" << bc << "!" << endl;
|
||||
}
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
long pow(int n, int m)
|
||||
{
|
||||
long p = 1;
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
p *= n;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int n, m;
|
||||
long p;
|
||||
cout << "Nhap co so: ";
|
||||
cin >> n;
|
||||
cout << "Nhap so mu: ";
|
||||
cin >> m;
|
||||
p = pow(n, m);
|
||||
cout << "n ^ m = " << p << endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
long pow(int n, int m)
|
||||
{
|
||||
if (m == 0)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return n * pow(n, m - 1);
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int n, m;
|
||||
long p;
|
||||
cout << "Nhap co so: ";
|
||||
cin >> n;
|
||||
cout << "Nhap so mu: ";
|
||||
cin >> m;
|
||||
p = pow(n, m);
|
||||
cout << "n ^ m = " << p << endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
int a(string s1, string s2)
|
||||
{
|
||||
if (s1 == s2)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
int l = (s1.length() < s2.length()) ? s1.length() : s2.length();
|
||||
for (int i = 0; i < l; i++)
|
||||
{
|
||||
if (s1[i] != s2[i])
|
||||
{
|
||||
return i + 1;
|
||||
}
|
||||
}
|
||||
return l + 1;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
string s1, s2;
|
||||
int n;
|
||||
cout << "Nhap chuoi dau tien:" << endl;
|
||||
getline(cin, s1);
|
||||
cout << "Nhap chuoi thu hai:" << endl;
|
||||
getline(cin, s2);
|
||||
n = a(s1, s2);
|
||||
cout << "Ket qua la: " << n << endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
char* b(string s, char c)
|
||||
{
|
||||
int l = (int)s.length();
|
||||
for (int i = 0; i < l; i++)
|
||||
{
|
||||
if (s[i] == c)
|
||||
{
|
||||
return &s[i];
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
string s;
|
||||
char c;
|
||||
char* ptr;
|
||||
cout << "Nhap chuoi:" << endl;
|
||||
getline(cin, s);
|
||||
cout << "Nhap ki tu:" << endl;
|
||||
cin >> c;
|
||||
ptr = b(s, c);
|
||||
if (ptr != 0)
|
||||
{
|
||||
cout << "Dia chi cua ki tu la: " << (void *) ptr << endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "Khong tim thay ki tu trong chuoi!" << endl;
|
||||
}
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
char* c(string s1, string s2)
|
||||
{
|
||||
int l1 = (int)s1.length();
|
||||
int l2 = (int)s2.length();
|
||||
if (l1 > l2)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
for (int i = 0; i <= l2 - l1; i++)
|
||||
{
|
||||
int j = 0;
|
||||
while (j < l1)
|
||||
{
|
||||
if (s2[i + j] != s1[j])
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
j++;
|
||||
}
|
||||
}
|
||||
if (j == l1)
|
||||
{
|
||||
return &s2[i];
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
string s1, s2;
|
||||
char* ptr;
|
||||
cout << "Nhap chuoi thu nhat:" << endl;
|
||||
getline(cin, s1);
|
||||
cout << "Nhap chuoi thu hai:" << endl;
|
||||
getline(cin, s2);
|
||||
ptr = c(s1, s2);
|
||||
if (ptr != 0)
|
||||
{
|
||||
cout << "Dia chi can biet la: " << (void *)ptr << endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "Chuoi 1 khong phai la chuoi con cua chuoi 2!" << endl;
|
||||
}
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
using namespace std;
|
||||
class candidate
|
||||
{
|
||||
public:
|
||||
int id;
|
||||
char* name;
|
||||
private:
|
||||
float math, physics, chemistry;
|
||||
public:
|
||||
candidate(int id, char* name, float math, float physics, float chemistry)
|
||||
{
|
||||
this->id = id;
|
||||
this->name = name;
|
||||
this->math = math;
|
||||
this->physics = physics;
|
||||
this->chemistry = chemistry;
|
||||
}
|
||||
~candidate()
|
||||
{
|
||||
id = 0;
|
||||
delete[] name;
|
||||
name = NULL;
|
||||
math = 0;
|
||||
physics = 0;
|
||||
chemistry = 0;
|
||||
}
|
||||
float total_grade()
|
||||
{
|
||||
return (math + physics + chemistry);
|
||||
}
|
||||
};
|
||||
int main()
|
||||
{
|
||||
candidate *t;
|
||||
float tongdiem;
|
||||
int id;
|
||||
char *name = new char[256];
|
||||
float math, physics, chemistry;
|
||||
cout << "Nhap id: " << endl;
|
||||
cin >> id;
|
||||
cout << "Nhap ten: " << endl;
|
||||
cin.clear();
|
||||
cin.ignore(999, '\n');
|
||||
cin.getline(name, 256);
|
||||
cout << "Nhap diem Toan: " << endl;
|
||||
cin >> math;
|
||||
cout << "Nhap diem Ly: " << endl;
|
||||
cin >> physics;
|
||||
cout << "Nhap diem Hoa: " << endl;
|
||||
cin >> chemistry;
|
||||
t = new candidate(id, name, math, physics, chemistry);
|
||||
tongdiem = t->total_grade();
|
||||
cout << "Tong diem: " << setprecision(4) << tongdiem << endl;
|
||||
delete t;
|
||||
t = NULL;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#define MEET_DISTANCE 50 //Fix from 10 to 50
|
||||
#define MEET_TIME 5000 //Fix from 4000 to 5000
|
||||
using namespace std;
|
||||
struct Position
|
||||
{
|
||||
double latitude, longitude;
|
||||
unsigned long long time;
|
||||
Position* next;
|
||||
Position()
|
||||
{
|
||||
latitude = 0;
|
||||
longitude = 0;
|
||||
time = 0;
|
||||
next = NULL;
|
||||
}
|
||||
};
|
||||
void append(Position* &start, Position* new_position)
|
||||
{
|
||||
if (start == NULL)
|
||||
{
|
||||
start = new_position;
|
||||
return;
|
||||
}
|
||||
Position* temp = start;
|
||||
while (temp->next != NULL)
|
||||
{
|
||||
temp = temp->next;
|
||||
}
|
||||
temp->next = new_position;
|
||||
}
|
||||
Position* readFromFile(string filename)
|
||||
{
|
||||
fstream file;
|
||||
file.open(filename, ios::in);
|
||||
if (!file.is_open())
|
||||
{
|
||||
cout << "Loi doc file!" << endl;
|
||||
return NULL;
|
||||
}
|
||||
string line;
|
||||
Position* start, *new_position;
|
||||
start = NULL;
|
||||
while (getline(file, line))
|
||||
{
|
||||
replace(line.begin(), line.end(), ',', ' ');
|
||||
new_position = new Position;
|
||||
stringstream ss;
|
||||
ss.str(line);
|
||||
double time;
|
||||
ss >> new_position->latitude >> new_position->longitude >> time;
|
||||
new_position->time = (unsigned long long) time;
|
||||
append(start, new_position);
|
||||
}
|
||||
file.close();
|
||||
return start;
|
||||
}
|
||||
void writeToFile(string filename, Position* start)
|
||||
{
|
||||
fstream file;
|
||||
file.open(filename, ios::out);
|
||||
Position* temp = start;
|
||||
while (temp != NULL)
|
||||
{
|
||||
file << setprecision(10) << temp->latitude << "," << temp->longitude << "," << temp->time << endl;
|
||||
temp = temp->next;
|
||||
}
|
||||
file.close();
|
||||
}
|
||||
void findMin(Position* start, Position* &preMin, Position* &min)
|
||||
{
|
||||
if (start == NULL)
|
||||
{
|
||||
preMin = NULL;
|
||||
min = NULL;
|
||||
return;
|
||||
}
|
||||
Position* temp = start;
|
||||
preMin = NULL;
|
||||
min = start;
|
||||
while (temp->next != NULL)
|
||||
{
|
||||
if (temp->next->time < min->time)
|
||||
{
|
||||
preMin = temp;
|
||||
min = preMin->next;
|
||||
}
|
||||
temp = temp->next;
|
||||
}
|
||||
}
|
||||
void sortList(Position* &start)
|
||||
{
|
||||
if (start == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (start->next == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Position *newList, *preMin, *min;
|
||||
newList = NULL;
|
||||
while (start != NULL)
|
||||
{
|
||||
findMin(start, preMin, min);
|
||||
append(newList, min);
|
||||
if (min == start)
|
||||
{
|
||||
start = min->next;
|
||||
}
|
||||
else
|
||||
{
|
||||
preMin->next = min->next;
|
||||
}
|
||||
min->next = NULL;
|
||||
}
|
||||
start = newList;
|
||||
}
|
||||
double distance(Position* pos_1, Position* pos_2)
|
||||
{
|
||||
return pow(10, 5)*sqrt(pow(pos_1->latitude - pos_2->latitude, 2) + pow(pos_1->longitude - pos_2->longitude, 2));
|
||||
}
|
||||
double loopLengthOfPath(Position* start)
|
||||
{
|
||||
if (start == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
double length = 0;
|
||||
Position* temp = start;
|
||||
while (temp->next != NULL)
|
||||
{
|
||||
length += distance(temp, temp->next);
|
||||
temp = temp->next;
|
||||
}
|
||||
return length;
|
||||
}
|
||||
double recursionLengthOfPath(Position* start)
|
||||
{
|
||||
if (start == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
if (start->next == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
double length = distance(start, start->next);
|
||||
return length + recursionLengthOfPath(start->next);
|
||||
}
|
||||
bool checkMeeting(Position* start_1, Position* start_2)
|
||||
{
|
||||
if (start_1 == NULL || start_2 == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
Position* temp_1 = start_1;
|
||||
Position* temp_2 = start_2;
|
||||
while (abs((long long) (temp_1->time - temp_2->time)) > MEET_TIME || distance(temp_1, temp_2) > MEET_DISTANCE)
|
||||
{
|
||||
if (temp_1->time > temp_2->time)
|
||||
{
|
||||
temp_2 = temp_2->next;
|
||||
}
|
||||
else
|
||||
{
|
||||
temp_1 = temp_1->next;
|
||||
}
|
||||
if (temp_1 == NULL || temp_2 == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
Position* start = readFromFile("position.csv");
|
||||
Position* start_2 = readFromFile("position_2.csv");
|
||||
sortList(start);
|
||||
sortList(start_2);
|
||||
cout << loopLengthOfPath(start) << endl;
|
||||
cout << recursionLengthOfPath(start_2) << endl;
|
||||
string out = checkMeeting(start, start_2) ? "YES" : "NO";
|
||||
cout << out << endl;
|
||||
writeToFile("sorted.csv", start);
|
||||
writeToFile("sorted_2.csv", start_2);
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,275 @@
|
||||
#include <algorithm>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct ProductCategory
|
||||
{
|
||||
string ID;
|
||||
string name;
|
||||
ProductCategory *next;
|
||||
};
|
||||
|
||||
struct Product
|
||||
{
|
||||
string ID;
|
||||
string name;
|
||||
int selling_price;
|
||||
int importing_price;
|
||||
double quantity;
|
||||
ProductCategory *product_category;
|
||||
Product *next;
|
||||
};
|
||||
|
||||
void releaseCategory(ProductCategory* &category_head)
|
||||
{
|
||||
if (category_head != NULL)
|
||||
{
|
||||
ProductCategory* temp = category_head;
|
||||
category_head = category_head->next;
|
||||
delete temp;
|
||||
releaseCategory(category_head);
|
||||
}
|
||||
}
|
||||
|
||||
void releaseProduct(Product* &head)
|
||||
{
|
||||
if (head != NULL)
|
||||
{
|
||||
Product* temp = head;
|
||||
head = head->next;
|
||||
delete temp;
|
||||
releaseProduct(head);
|
||||
}
|
||||
}
|
||||
|
||||
void appendCategory(ProductCategory* &category_head, ProductCategory* newcategory)
|
||||
{
|
||||
if (category_head == NULL)
|
||||
{
|
||||
category_head = newcategory;
|
||||
return;
|
||||
}
|
||||
ProductCategory *temp = category_head;
|
||||
while (temp->next != NULL) temp = temp->next;
|
||||
temp->next = newcategory;
|
||||
}
|
||||
|
||||
void appendProduct(Product* &head, Product* new_product)
|
||||
{
|
||||
if (head == NULL)
|
||||
{
|
||||
head = new_product;
|
||||
return;
|
||||
}
|
||||
Product *temp = head;
|
||||
while (temp->next != NULL) temp = temp->next;
|
||||
temp->next = new_product;
|
||||
}
|
||||
|
||||
void findProductCategory(ProductCategory* category_head, string ID, ProductCategory* &result)
|
||||
{
|
||||
result = category_head;
|
||||
while (result != NULL)
|
||||
{
|
||||
if (result->ID == ID)
|
||||
return;
|
||||
result = result->next;
|
||||
}
|
||||
}
|
||||
|
||||
void readFromFile(string filename, Product* &head, ProductCategory* &category_head)
|
||||
{
|
||||
releaseCategory(category_head);
|
||||
releaseProduct(head);
|
||||
ifstream ifs;
|
||||
ifs.open(filename, ifstream::in);
|
||||
if (!ifs.is_open())
|
||||
{
|
||||
cout << "Loi doc file!" << endl;
|
||||
return;
|
||||
}
|
||||
string line;
|
||||
while (getline(ifs, line))
|
||||
{
|
||||
replace(line.begin(), line.end(), ',', ' ');
|
||||
Product *new_product = new Product;
|
||||
string ID, name;
|
||||
istringstream iss;
|
||||
iss.str(line);
|
||||
iss >> new_product->ID
|
||||
>> new_product->name
|
||||
>> new_product->selling_price
|
||||
>> new_product->importing_price
|
||||
>> new_product->quantity
|
||||
>> ID
|
||||
>> name;
|
||||
new_product->next = NULL;
|
||||
findProductCategory(category_head, ID, new_product->product_category);
|
||||
if (new_product->product_category == NULL)
|
||||
{
|
||||
ProductCategory *new_category = new ProductCategory;
|
||||
new_category->ID = ID;
|
||||
new_category->name = name;
|
||||
new_category->next = NULL;
|
||||
appendCategory(category_head, new_category);
|
||||
new_product->product_category = new_category;
|
||||
}
|
||||
appendProduct(head, new_product);
|
||||
}
|
||||
ifs.close();
|
||||
}
|
||||
|
||||
void writeProductToFile(string filename, Product* head)
|
||||
{
|
||||
ofstream ofs;
|
||||
ofs.open(filename, ofstream::out);
|
||||
Product* temp = head;
|
||||
while (temp != NULL)
|
||||
{
|
||||
ofs << setprecision(10)
|
||||
<< temp->ID << ","
|
||||
<< temp->name << ","
|
||||
<< temp->selling_price << ","
|
||||
<< temp->importing_price << ","
|
||||
<< temp->quantity << ","
|
||||
<< temp->product_category->ID << ","
|
||||
<< temp->product_category->name
|
||||
<< endl;
|
||||
temp = temp->next;
|
||||
}
|
||||
ofs.close();
|
||||
}
|
||||
|
||||
void printAllCategories(Product* head)
|
||||
{
|
||||
int cnt = 0;
|
||||
ProductCategory *category_head = NULL;
|
||||
Product *temp = head;
|
||||
while (temp != NULL)
|
||||
{
|
||||
ProductCategory *result = NULL;
|
||||
findProductCategory(category_head, temp->product_category->ID, result);
|
||||
if (result == NULL)
|
||||
{
|
||||
result = new ProductCategory;
|
||||
result->ID = temp->product_category->ID;
|
||||
result->name = temp->product_category->name;
|
||||
result->next = NULL;
|
||||
appendCategory(category_head, result);
|
||||
++cnt;
|
||||
}
|
||||
temp = temp->next;
|
||||
}
|
||||
cout << cnt;
|
||||
if (cnt > 1) cout << " categories";
|
||||
else cout << " category";
|
||||
cout << endl;
|
||||
ProductCategory *tmp = category_head;
|
||||
while (tmp != NULL)
|
||||
{
|
||||
cout << "----------------" << endl;
|
||||
cout << "ID: " << tmp->ID << endl;
|
||||
cout << "Name: " << tmp->name << endl;
|
||||
tmp = tmp->next;
|
||||
}
|
||||
releaseCategory(category_head);
|
||||
}
|
||||
|
||||
bool deleteProducts(Product* &head, double quantity)
|
||||
{
|
||||
int cnt = 0;
|
||||
Product *temp, *tmp;
|
||||
while (head != NULL)
|
||||
{
|
||||
if (head->quantity < quantity)
|
||||
{
|
||||
tmp = head;
|
||||
head = head->next;
|
||||
delete tmp;
|
||||
++cnt;
|
||||
}
|
||||
else break;
|
||||
}
|
||||
if (head != NULL)
|
||||
{
|
||||
temp = head;
|
||||
while (temp->next != NULL)
|
||||
{
|
||||
if (temp->next->quantity < quantity)
|
||||
{
|
||||
tmp = temp->next;
|
||||
temp->next = tmp->next;
|
||||
delete tmp;
|
||||
++cnt;
|
||||
continue;
|
||||
}
|
||||
temp = temp->next;
|
||||
}
|
||||
}
|
||||
return (cnt > 0);
|
||||
}
|
||||
|
||||
double sellingFromFile(Product* &head, string filename)
|
||||
{
|
||||
ifstream ifs;
|
||||
double price = 0;
|
||||
ifs.open(filename, ios::in);
|
||||
if (!ifs.is_open())
|
||||
{
|
||||
cout << "Loi doc file" << endl;
|
||||
return price;
|
||||
}
|
||||
string line;
|
||||
while (getline(ifs, line))
|
||||
{
|
||||
replace(line.begin(), line.end(), ',', ' ');
|
||||
istringstream iss;
|
||||
string ID;
|
||||
double quantity;
|
||||
iss.str(line);
|
||||
iss >> ID >> quantity;
|
||||
Product *temp = head;
|
||||
while (temp != NULL)
|
||||
{
|
||||
if (temp->ID == ID)
|
||||
{
|
||||
if (temp->quantity > quantity)
|
||||
{
|
||||
price += quantity * temp->selling_price;
|
||||
temp->quantity -= quantity;
|
||||
}
|
||||
else
|
||||
{
|
||||
price += temp->quantity * temp->selling_price;
|
||||
temp->quantity = 0;
|
||||
}
|
||||
break;
|
||||
}
|
||||
temp = temp->next;
|
||||
}
|
||||
}
|
||||
ifs.close();
|
||||
return price;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
Product *head = NULL;
|
||||
ProductCategory *category_head = NULL;
|
||||
readFromFile("ProductIn.csv", head, category_head);
|
||||
double m = sellingFromFile(head, "SellingProduct.csv");
|
||||
cout << "Tong so tien: " << m << endl;
|
||||
printAllCategories(head);
|
||||
bool b = deleteProducts(head, 5);
|
||||
if (b)
|
||||
cout << "Delete thanh cong!" << endl;
|
||||
else
|
||||
cout << "Delete khong thanh cong!" << endl;
|
||||
writeProductToFile("ProductOut.csv", head);
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
C001,One,30,30,9.3,CO,Odd
|
||||
C002,Two,12,10,8,CE,Even
|
||||
C003,Three,25,25,8.5,CO,Odd
|
||||
C004,Four,20,16,7.7,CE,Even
|
||||
C005,Five,10,5,5.6,CO,Odd
|
||||
C006,Six,21,20,7.4,CE,Even
|
||||
C007,Seven,26,21,5.8,CO,Odd
|
||||
C008,Eight,27,27,5.6,CE,Even
|
||||
C009,Nine,23,21,7,CO,Odd
|
||||
C010,Ten,23,19,9.4,CE,Even
|
||||
|
@@ -0,0 +1,7 @@
|
||||
C001,One,30,30,9.3,CO,Odd
|
||||
C002,Two,12,10,7,CE,Even
|
||||
C003,Three,25,25,5.5,CO,Odd
|
||||
C006,Six,21,20,7.4,CE,Even
|
||||
C007,Seven,26,21,5.8,CO,Odd
|
||||
C009,Nine,23,21,7,CO,Odd
|
||||
C010,Ten,23,19,5.4,CE,Even
|
||||
|
@@ -0,0 +1,9 @@
|
||||
C002,1
|
||||
C003,3
|
||||
C004,5
|
||||
C005,7
|
||||
C008,9
|
||||
C009,0
|
||||
C010,2
|
||||
C010,2
|
||||
CXXX,5
|
||||
|
+228
@@ -0,0 +1,228 @@
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct Node {
|
||||
int data;
|
||||
Node *next;
|
||||
};
|
||||
|
||||
struct Stack {
|
||||
Node *sTop;
|
||||
int sCount;
|
||||
};
|
||||
|
||||
struct Queue {
|
||||
Node *qFront;
|
||||
Node *qRear;
|
||||
int qCount;
|
||||
};
|
||||
|
||||
void createStack(Stack *&s)
|
||||
{
|
||||
if (s != nullptr) {
|
||||
while (s->sTop != nullptr) {
|
||||
Node *temp = s->sTop;
|
||||
s->sTop = s->sTop->next;
|
||||
delete temp;
|
||||
}
|
||||
s->sCount = 0;
|
||||
delete s;
|
||||
}
|
||||
s = new Stack;
|
||||
s->sTop = nullptr;
|
||||
s->sCount = 0;
|
||||
}
|
||||
|
||||
void pushStack(Stack *&s, int data)
|
||||
{
|
||||
if (s == nullptr)
|
||||
return;
|
||||
Node *temp = new Node;
|
||||
if (temp == nullptr)
|
||||
return;
|
||||
temp->data = data;
|
||||
temp->next = s->sTop;
|
||||
s->sTop = temp;
|
||||
s->sCount++;
|
||||
}
|
||||
|
||||
void popStack(Stack *&s)
|
||||
{
|
||||
if (s == nullptr)
|
||||
return;
|
||||
if (s->sCount == 0)
|
||||
return;
|
||||
Node *temp = s->sTop;
|
||||
s->sTop = s->sTop->next;
|
||||
delete temp;
|
||||
s->sCount--;
|
||||
}
|
||||
|
||||
int topStack(Stack *&s)
|
||||
{
|
||||
if (s == nullptr)
|
||||
return 0;
|
||||
if (s->sCount == 0)
|
||||
return 0;
|
||||
return s->sTop->data;
|
||||
}
|
||||
|
||||
bool isStackEmpty(Stack *s)
|
||||
{
|
||||
if (s == nullptr)
|
||||
return true;
|
||||
return (s->sCount == 0);
|
||||
}
|
||||
|
||||
bool isStackFull(Stack *s)
|
||||
{
|
||||
Node *temp = new Node;
|
||||
if (temp != nullptr) {
|
||||
delete temp;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void clearStack(Stack *&s)
|
||||
{
|
||||
while (!isStackEmpty(s))
|
||||
popStack(s);
|
||||
}
|
||||
|
||||
int sizeOfStack(Stack *s)
|
||||
{
|
||||
if (s == nullptr)
|
||||
return -1;
|
||||
return s->sCount;
|
||||
}
|
||||
|
||||
void createQueue(Queue *&q)
|
||||
{
|
||||
if (q != nullptr) {
|
||||
while (q->qRear != nullptr) {
|
||||
Node *temp = q->qRear;
|
||||
q->qRear = q->qRear->next;
|
||||
delete temp;
|
||||
}
|
||||
while (q->qFront != nullptr) {
|
||||
Node *temp = q->qFront;
|
||||
q->qFront = q->qFront->next;
|
||||
delete temp;
|
||||
}
|
||||
}
|
||||
q = new Queue;
|
||||
q->qCount = 0;
|
||||
q->qFront = nullptr;
|
||||
q->qRear = nullptr;
|
||||
}
|
||||
|
||||
void enqueue(Queue *&q, int data)
|
||||
{
|
||||
if (q == nullptr)
|
||||
return;
|
||||
Node *temp = new Node;
|
||||
if (temp == nullptr)
|
||||
return;
|
||||
temp->next = q->qRear;
|
||||
temp->data = data;
|
||||
q->qRear = temp;
|
||||
if (q->qCount == 0)
|
||||
q->qFront = temp;
|
||||
q->qCount++;
|
||||
}
|
||||
|
||||
void dequeue(Queue *&q)
|
||||
{
|
||||
if (q == nullptr)
|
||||
return;
|
||||
if (q->qCount == 0)
|
||||
return;
|
||||
Node *temp = nullptr;
|
||||
if (q->qCount == 1) {
|
||||
q->qRear = nullptr;
|
||||
} else {
|
||||
temp = q->qRear;
|
||||
while (temp->next != q->qFront)
|
||||
temp = temp->next;
|
||||
}
|
||||
delete q->qFront;
|
||||
q->qFront = temp;
|
||||
q->qCount--;
|
||||
}
|
||||
|
||||
int queueFront(Queue *q)
|
||||
{
|
||||
if (q == nullptr)
|
||||
return 0;
|
||||
if (q->qCount == 0)
|
||||
return 0;
|
||||
return q->qFront->data;
|
||||
}
|
||||
|
||||
int queueRear(Queue *q)
|
||||
{
|
||||
if (q == nullptr)
|
||||
return 0;
|
||||
if (q->qCount == 0)
|
||||
return 0;
|
||||
return q->qRear->data;
|
||||
}
|
||||
|
||||
bool isQueueEmpty(Queue *q)
|
||||
{
|
||||
if (q == nullptr)
|
||||
return true;
|
||||
return (q->qCount == 0);
|
||||
}
|
||||
|
||||
bool isQueueFull(Queue *q)
|
||||
{
|
||||
Node *temp = new Node;
|
||||
if (temp != nullptr) {
|
||||
delete temp;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void clearQueue(Queue *&q)
|
||||
{
|
||||
while (!isQueueEmpty(q))
|
||||
dequeue(q);
|
||||
}
|
||||
|
||||
int sizeOfQueue(Queue *q)
|
||||
{
|
||||
if (q == nullptr)
|
||||
return -1;
|
||||
return q->qCount;
|
||||
}
|
||||
|
||||
void stackToQueue(Stack *&s, Queue *&q)
|
||||
{
|
||||
createQueue(q);
|
||||
while (!isStackEmpty(s)) {
|
||||
int x = topStack(s);
|
||||
popStack(s);
|
||||
enqueue(q, x);
|
||||
}
|
||||
}
|
||||
|
||||
void queueToStack(Queue *&q, Stack *&s)
|
||||
{
|
||||
createStack(s);
|
||||
if (!isQueueEmpty(q)) {
|
||||
Node *temp = q->qRear;
|
||||
while (temp != nullptr) {
|
||||
pushStack(s, temp->data);
|
||||
temp = temp->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,399 @@
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct Node {
|
||||
int data;
|
||||
Node *next;
|
||||
Node()
|
||||
{
|
||||
this->data = 0;
|
||||
this->next = nullptr;
|
||||
}
|
||||
Node(int data)
|
||||
{
|
||||
this->data = data;
|
||||
this->next = nullptr;
|
||||
}
|
||||
Node(int data, Node *next)
|
||||
{
|
||||
this->data = data;
|
||||
this->next = next;
|
||||
}
|
||||
~Node() {}
|
||||
};
|
||||
|
||||
enum ErrorCode {
|
||||
SUCCESS,
|
||||
FAIL,
|
||||
OVERFLOW,
|
||||
UNDERFLOW
|
||||
};
|
||||
|
||||
class Stack
|
||||
{
|
||||
private:
|
||||
Node *sTop;
|
||||
int sCount;
|
||||
public:
|
||||
Stack()
|
||||
{
|
||||
this->sTop = nullptr;
|
||||
this->sCount = 0;
|
||||
}
|
||||
~Stack()
|
||||
{
|
||||
if (!this->isEmpty())
|
||||
this->clear();
|
||||
}
|
||||
ErrorCode create();
|
||||
ErrorCode push(int data);
|
||||
ErrorCode pop();
|
||||
ErrorCode top(int &data);
|
||||
bool isEmpty();
|
||||
bool isFull();
|
||||
ErrorCode clear();
|
||||
int size();
|
||||
};
|
||||
|
||||
ErrorCode Stack::create()
|
||||
{
|
||||
if (!this->isEmpty())
|
||||
return this->clear();
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
ErrorCode Stack::push(int data)
|
||||
{
|
||||
if (this->isFull())
|
||||
return OVERFLOW;
|
||||
Node *temp = new Node(data, this->sTop);
|
||||
this->sTop = temp;
|
||||
temp = nullptr;
|
||||
this->sCount++;
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
ErrorCode Stack::pop()
|
||||
{
|
||||
if (this->isEmpty())
|
||||
return UNDERFLOW;
|
||||
Node *temp = this->sTop;
|
||||
this->sTop = this->sTop->next;
|
||||
delete temp;
|
||||
temp = nullptr;
|
||||
this->sCount--;
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
ErrorCode Stack::top(int &data)
|
||||
{
|
||||
if (this->isEmpty()) {
|
||||
data = 0;
|
||||
return FAIL;
|
||||
}
|
||||
data = this->sTop->data;
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
bool Stack::isEmpty()
|
||||
{
|
||||
return (this->sCount == 0);
|
||||
}
|
||||
|
||||
bool Stack::isFull()
|
||||
{
|
||||
Node *temp = new Node;
|
||||
if (temp != nullptr) {
|
||||
delete temp;
|
||||
temp = nullptr;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
ErrorCode Stack::clear()
|
||||
{
|
||||
while (!this->isEmpty()) {
|
||||
this->pop();
|
||||
}
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
int Stack::size()
|
||||
{
|
||||
return this->sCount;
|
||||
}
|
||||
|
||||
class Queue
|
||||
{
|
||||
private:
|
||||
Node *qFront;
|
||||
Node *qRear;
|
||||
int qCount;
|
||||
public:
|
||||
Queue()
|
||||
{
|
||||
this->qFront = nullptr;
|
||||
this->qRear = nullptr;
|
||||
this->qCount = 0;
|
||||
}
|
||||
~Queue()
|
||||
{
|
||||
if (!this->isEmpty())
|
||||
this->clear();
|
||||
}
|
||||
ErrorCode create();
|
||||
ErrorCode enqueue(int data);
|
||||
ErrorCode dequeue();
|
||||
ErrorCode queueFront(int &data);
|
||||
ErrorCode queueRear(int &data);
|
||||
bool isFull();
|
||||
bool isEmpty();
|
||||
ErrorCode clear();
|
||||
int size();
|
||||
};
|
||||
|
||||
ErrorCode Queue::create()
|
||||
{
|
||||
if (!this->isEmpty())
|
||||
return this->clear();
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
ErrorCode Queue::enqueue(int data)
|
||||
{
|
||||
if (this->isFull())
|
||||
return OVERFLOW;
|
||||
Node *temp = new Node(data, this->qRear);
|
||||
qRear = temp;
|
||||
temp = nullptr;
|
||||
if (this->isEmpty()) {
|
||||
qFront = qRear;
|
||||
}
|
||||
this->qCount++;
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
ErrorCode Queue::dequeue()
|
||||
{
|
||||
if (this->isEmpty())
|
||||
return UNDERFLOW;
|
||||
Node *temp = nullptr;
|
||||
if (this->qCount == 1) {
|
||||
this->qRear = nullptr;
|
||||
} else {
|
||||
temp = this->qRear;
|
||||
while (temp->next != this->qFront)
|
||||
temp = temp->next;
|
||||
}
|
||||
delete this->qFront;
|
||||
this->qFront = temp;
|
||||
this->qCount--;
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
ErrorCode Queue::queueFront(int &data)
|
||||
{
|
||||
if (this->isEmpty()) {
|
||||
data = 0;
|
||||
return FAIL;
|
||||
}
|
||||
data = this->qFront->data;
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
ErrorCode Queue::queueRear(int &data)
|
||||
{
|
||||
if (this->isEmpty()) {
|
||||
data = 0;
|
||||
return FAIL;
|
||||
}
|
||||
data = this->qRear->data;
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
bool Queue::isEmpty()
|
||||
{
|
||||
return (this->qCount == 0);
|
||||
}
|
||||
|
||||
bool Queue::isFull()
|
||||
{
|
||||
Node *temp = new Node;
|
||||
if (temp != nullptr) {
|
||||
delete temp;
|
||||
temp = nullptr;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
ErrorCode Queue::clear()
|
||||
{
|
||||
while (!this->isEmpty())
|
||||
this->dequeue();
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
int Queue::size()
|
||||
{
|
||||
return this->qCount;
|
||||
}
|
||||
|
||||
void stackToQueue(Stack &s, Queue &q)
|
||||
{
|
||||
q.create();
|
||||
while (!s.isEmpty()) {
|
||||
int data;
|
||||
s.top(data);
|
||||
q.enqueue(data);
|
||||
s.pop();
|
||||
}
|
||||
}
|
||||
|
||||
void queueToStack(Queue &q, Stack &s)
|
||||
{
|
||||
s.create();
|
||||
Queue qTemp;
|
||||
Stack sTemp;
|
||||
while (!q.isEmpty()) {
|
||||
int data;
|
||||
q.queueFront(data);
|
||||
sTemp.push(data);
|
||||
qTemp.enqueue(data);
|
||||
q.dequeue();
|
||||
}
|
||||
while (!qTemp.isEmpty()) {
|
||||
int data;
|
||||
qTemp.queueFront(data);
|
||||
q.enqueue(data);
|
||||
qTemp.dequeue();
|
||||
}
|
||||
while (!sTemp.isEmpty()) {
|
||||
int data;
|
||||
sTemp.top(data);
|
||||
s.push(data);
|
||||
sTemp.pop();
|
||||
}
|
||||
}
|
||||
|
||||
void printStack(Stack &s) //print Stack from top to base
|
||||
{
|
||||
Stack sTemp;
|
||||
while (!s.isEmpty()) {
|
||||
int data;
|
||||
s.top(data);
|
||||
cout << data << " ";
|
||||
sTemp.push(data);
|
||||
s.pop();
|
||||
}
|
||||
while (!sTemp.isEmpty()) {
|
||||
int data;
|
||||
sTemp.top(data);
|
||||
s.push(data);
|
||||
sTemp.pop();
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
void printQueue(Queue &q) //print Queue from front to rear
|
||||
{
|
||||
Stack sTemp;
|
||||
Queue qTemp;
|
||||
while (!q.isEmpty()) {
|
||||
int data;
|
||||
q.queueFront(data);
|
||||
cout << data << " ";
|
||||
sTemp.push(data);
|
||||
qTemp.enqueue(data);
|
||||
q.dequeue();
|
||||
}
|
||||
while (!qTemp.isEmpty()) {
|
||||
int data;
|
||||
qTemp.queueFront(data);
|
||||
q.enqueue(data);
|
||||
qTemp.dequeue();
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
string errorCodeString[] = {
|
||||
"SUCCESS",
|
||||
"FAIL",
|
||||
"OVERFLOW",
|
||||
"UNDERFLOW"
|
||||
};
|
||||
string output;
|
||||
ErrorCode errorCode;
|
||||
int data;
|
||||
cout << "Init Stack:" << endl;
|
||||
Stack s;
|
||||
Queue q;
|
||||
Stack sTemp;
|
||||
Queue qTemp;
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
s.push(i);
|
||||
printStack(s);
|
||||
}
|
||||
cout << "Size of Stack: " << s.size() << endl;
|
||||
output = s.isFull() ? "YES" : "NO";
|
||||
cout << "Is Stack full?: " << output << endl;
|
||||
cout << "Clear and print Stack:" << endl;
|
||||
s.clear();
|
||||
printStack(s);
|
||||
output = s.isEmpty() ? "YES" : "NO";
|
||||
cout << "Is Stask empty?: " << output << endl;
|
||||
errorCode = s.pop();
|
||||
cout << "ErrorCode when pop Stack again: "
|
||||
<< errorCodeString[errorCode] << endl;
|
||||
errorCode = s.top(data);
|
||||
cout << "ErrorCode when top an empty Stack: "
|
||||
<< errorCodeString[errorCode] << endl;
|
||||
cout << "Init Queue:" << endl;
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
q.enqueue(i);
|
||||
printQueue(q);
|
||||
}
|
||||
cout << "Size of Queue: " << q.size() << endl;
|
||||
output = q.isFull() ? "YES" : "NO";
|
||||
cout << "Is Queue full?: " << output << endl;
|
||||
cout << "Clear and print Queue:" << endl;
|
||||
q.clear();
|
||||
printQueue(q);
|
||||
output = q.isEmpty() ? "YES" : "NO";
|
||||
cout << "Is Queue empty?: " << output << endl;
|
||||
errorCode = q.dequeue();
|
||||
cout << "ErrorCode when dequeue Queue again: "
|
||||
<< errorCodeString[errorCode] << endl;
|
||||
errorCode = q.queueFront(data);
|
||||
cout << "ErrorCode when queueFront an empty Queue: "
|
||||
<< errorCodeString[errorCode] << endl;
|
||||
errorCode = q.queueRear(data);
|
||||
cout << "ErrorCode when queueRear an empty Queue: "
|
||||
<< errorCodeString[errorCode] << endl;
|
||||
cout << "Init Stack and Queue again:" << endl;
|
||||
for (int i = 0; i < 10; i++) {
|
||||
s.push(i);
|
||||
q.enqueue(i);
|
||||
}
|
||||
cout << "Stack: ";
|
||||
printStack(s);
|
||||
cout << "Queue: ";
|
||||
printQueue(q);
|
||||
cout << "Use stackToQueue to create new Queue:" << endl;
|
||||
stackToQueue(s, qTemp);
|
||||
cout << "New Queue: ";
|
||||
printQueue(qTemp);
|
||||
cout << "Stack after: ";
|
||||
printStack(s);
|
||||
cout << "Use queueToStack to create new Stack" << endl;
|
||||
queueToStack(q, sTemp);
|
||||
cout << "New Stack: ";
|
||||
printStack(sTemp);
|
||||
cout << "Queue after: ";
|
||||
printQueue(q);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
# CTDL-GT
|
||||
Code trong mon Cau truc du lieu va giai thuat (HK181)
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
bool isPrime(int num) {
|
||||
if (num < 2) {
|
||||
return false;
|
||||
}
|
||||
int i;
|
||||
for (i = 2; i*i <= num; i++) {
|
||||
if (num % i == 0) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void onePrime(int * arr, int n) {
|
||||
if (n == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (isPrime(arr[n - 1]))
|
||||
{
|
||||
cout << arr[n - 1] << endl;
|
||||
return;
|
||||
}
|
||||
onePrime(arr, n - 1);
|
||||
}
|
||||
void allPrime(int * arr, int n) {
|
||||
if (n == 0)
|
||||
{
|
||||
cout << endl;
|
||||
return;
|
||||
}
|
||||
if (isPrime(arr[n - 1]))
|
||||
{
|
||||
cout << arr[n - 1] << " ";
|
||||
}
|
||||
allPrime(arr, n - 1);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int arr[] = {1, 3, 5, 7, 9};
|
||||
onePrime(arr, 5);
|
||||
allPrime(arr, 5);
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
struct node {
|
||||
int data;
|
||||
node *next = NULL;
|
||||
};
|
||||
int myMaxFunc(node* head, int maxVal) {
|
||||
if (head == NULL)
|
||||
{
|
||||
return maxVal;
|
||||
}
|
||||
if (head->data > maxVal)
|
||||
{
|
||||
maxVal = head->data;
|
||||
}
|
||||
return myMaxFunc(head->next, maxVal);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
node *a, *b, *c, *d, *e, *f, *g;
|
||||
node *head;
|
||||
g = new node();
|
||||
g->data = 7;
|
||||
g->next = NULL;
|
||||
f = new node();
|
||||
f->data = 2;
|
||||
f->next = g;
|
||||
e = new node();
|
||||
e->data = 100;
|
||||
e->next = f;
|
||||
d = new node();
|
||||
d->data = 3;
|
||||
d->next = e;
|
||||
c = new node();
|
||||
c->data = 9;
|
||||
c->next = d;
|
||||
b = new node();
|
||||
b->data = 5;
|
||||
b->next = c;
|
||||
a = new node();
|
||||
a->data = -11;
|
||||
a->next = b;
|
||||
head = new node();
|
||||
//head->data = 0;
|
||||
head->next = a;
|
||||
cout << myMaxFunc(head, INT_MIN) << endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
void draw_ch(int k)
|
||||
{
|
||||
if (k > 1)
|
||||
{
|
||||
cout << "_";
|
||||
draw_ch(k - 1);
|
||||
}
|
||||
}
|
||||
void draw_num(int k, int n)
|
||||
{
|
||||
if (k <= n)
|
||||
{
|
||||
cout << k;
|
||||
draw_num(k + 1, n);
|
||||
}
|
||||
}
|
||||
void draw1Line(int k, int n, bool b)
|
||||
{
|
||||
draw_ch(k);
|
||||
draw_num(k, n);
|
||||
cout << endl;
|
||||
if (b == 0)
|
||||
{
|
||||
if (k > n / 2 + 1)
|
||||
{
|
||||
draw1Line(k - 1, n, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (n % 2)
|
||||
{
|
||||
draw1Line(k + 1, n, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
draw1Line(k, n, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (k >= n)
|
||||
{
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
draw1Line(k + 1, n, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
void recursiveTriangle(int N)
|
||||
{
|
||||
draw1Line(N, N, 0);
|
||||
}
|
||||
void loopTriangle(int N)
|
||||
{
|
||||
int k, i;
|
||||
k = N;
|
||||
for (; k > N/2; k--)
|
||||
{
|
||||
i = 1;
|
||||
for (; i < k; i++)
|
||||
{
|
||||
cout << "_";
|
||||
}
|
||||
for (; i <= N; i++)
|
||||
{
|
||||
cout << i;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
if (N % 2)
|
||||
{
|
||||
k+= 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
k++;
|
||||
}
|
||||
for (; k <= N; k++)
|
||||
{
|
||||
i = 1;
|
||||
for (; i < k; i++)
|
||||
{
|
||||
cout << "_";
|
||||
}
|
||||
for (; i <= N; i++)
|
||||
{
|
||||
cout << i;
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
cin >> n;
|
||||
recursiveTriangle(n);
|
||||
loopTriangle(n);
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
struct node
|
||||
{
|
||||
int data;
|
||||
node* next = NULL;
|
||||
};
|
||||
void printAll(node* head)
|
||||
{
|
||||
if (head != NULL)
|
||||
{
|
||||
cout << head->data << endl;
|
||||
printAll(head->next);
|
||||
}
|
||||
}
|
||||
void fun2(node* head)
|
||||
{
|
||||
if (head == NULL)
|
||||
return;
|
||||
printf("%d ", head->data);
|
||||
if (head->next != NULL)
|
||||
fun2(head->next->next);
|
||||
printf("%d ", head->data);
|
||||
}
|
||||
int main()
|
||||
{
|
||||
node *a, *b, *c, *d, *e, *f, *g;
|
||||
node *head;
|
||||
g = new node();
|
||||
g->data = 17;
|
||||
g->next = NULL;
|
||||
f = new node();
|
||||
f->data = 5;
|
||||
f->next = g;
|
||||
e = new node();
|
||||
e->data = 13;
|
||||
e->next = f;
|
||||
d = new node();
|
||||
d->data = 6;
|
||||
d->next = e;
|
||||
c = new node();
|
||||
c->data = 3;
|
||||
c->next = d;
|
||||
b = new node();
|
||||
b->data = 5;
|
||||
b->next = c;
|
||||
a = new node();
|
||||
a->data = -11;
|
||||
a->next = b;
|
||||
head = new node();
|
||||
head->next = a;
|
||||
//printAll(head);
|
||||
fun2(c);
|
||||
//printAll(head);
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
struct node
|
||||
{
|
||||
int data;
|
||||
node* next = NULL;
|
||||
};
|
||||
void printAll(node* head)
|
||||
{
|
||||
if (head != NULL)
|
||||
{
|
||||
cout << head->data << " ";
|
||||
printAll(head->next);
|
||||
}
|
||||
}
|
||||
void printAll2(node* head)
|
||||
{
|
||||
if (head == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
node* temp = head;
|
||||
do
|
||||
{
|
||||
cout << temp->data << " ";
|
||||
temp = temp->next;
|
||||
} while (temp != head);
|
||||
}
|
||||
void func1(node* head) {
|
||||
node* temp = head;
|
||||
while (temp != NULL) {
|
||||
if (temp->next == NULL) {
|
||||
temp->next = head;
|
||||
return;
|
||||
}
|
||||
temp = temp->next;
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
node *a, *b, *c, *d, *e, *f, *g;
|
||||
node *head;
|
||||
g = new node();
|
||||
g->data = 7;
|
||||
g->next = NULL;
|
||||
f = new node();
|
||||
f->data = 2;
|
||||
f->next = g;
|
||||
e = new node();
|
||||
e->data = 100;
|
||||
e->next = f;
|
||||
d = new node();
|
||||
d->data = 3;
|
||||
d->next = e;
|
||||
c = new node();
|
||||
c->data = 9;
|
||||
c->next = d;
|
||||
b = new node();
|
||||
b->data = 5;
|
||||
b->next = c;
|
||||
a = new node();
|
||||
a->data = -11;
|
||||
a->next = b;
|
||||
head = new node();
|
||||
head->next = a;
|
||||
printAll(head);
|
||||
func1(head);
|
||||
printAll2(NULL);
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
struct node {
|
||||
int data;
|
||||
node *next = NULL;
|
||||
};
|
||||
node* deleteNth(node* head, int n)
|
||||
{
|
||||
if (head == NULL || n < 0)
|
||||
{
|
||||
return head;
|
||||
}
|
||||
if (n == 0)
|
||||
{
|
||||
node *temp = head;
|
||||
head = head->next;
|
||||
delete temp;
|
||||
}
|
||||
else
|
||||
{
|
||||
node *pre = head, *cur = head->next;
|
||||
for (int i = 1; i < n; i++)
|
||||
{
|
||||
pre = cur;
|
||||
cur = cur->next;
|
||||
if (cur == NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (cur != NULL)
|
||||
{
|
||||
pre->next = cur->next;
|
||||
delete cur;
|
||||
cur = NULL;
|
||||
}
|
||||
}
|
||||
return head;
|
||||
}
|
||||
void printAll(node* head)
|
||||
{
|
||||
if (head != NULL)
|
||||
{
|
||||
cout << head->data << " ";
|
||||
printAll(head->next);
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
node *a, *b, *c, *d, *e, *f, *g;
|
||||
node *head;
|
||||
g = new node();
|
||||
g->data = 7;
|
||||
g->next = NULL;
|
||||
f = new node();
|
||||
f->data = 2;
|
||||
f->next = g;
|
||||
e = new node();
|
||||
e->data = 100;
|
||||
e->next = f;
|
||||
d = new node();
|
||||
d->data = 3;
|
||||
d->next = e;
|
||||
c = new node();
|
||||
c->data = 9;
|
||||
c->next = d;
|
||||
b = new node();
|
||||
b->data = 5;
|
||||
b->next = c;
|
||||
a = new node();
|
||||
a->data = -11;
|
||||
a->next = b;
|
||||
head = new node();
|
||||
//head->data = 0;
|
||||
head->next = a;
|
||||
printAll(head);
|
||||
cout << endl;
|
||||
head = deleteNth(head, 7);
|
||||
printAll(head);
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
Write a function:
|
||||
1. Append a node to linked list.
|
||||
2. Prepend a node to linked list.
|
||||
3. Search and return pointer to node if existed otherwise, return NULL.
|
||||
*/
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
struct node
|
||||
{
|
||||
int data;
|
||||
node* next;
|
||||
};
|
||||
void appendList(node* &head, int data)
|
||||
{
|
||||
node* newNode = new node;
|
||||
newNode->data = data;
|
||||
newNode->next = NULL;
|
||||
if (head == NULL)
|
||||
{
|
||||
head = newNode;
|
||||
return;
|
||||
}
|
||||
node* temp = head;
|
||||
while (temp->next != NULL)
|
||||
{
|
||||
temp = temp->next;
|
||||
}
|
||||
temp->next = newNode;
|
||||
}
|
||||
void prependList(node* &head, int data)
|
||||
{
|
||||
node* newNode = new node;
|
||||
newNode->data = data;
|
||||
if (head == NULL)
|
||||
{
|
||||
newNode->next = NULL;
|
||||
head = newNode;
|
||||
return;
|
||||
}
|
||||
newNode->next = head;
|
||||
head = newNode;
|
||||
}
|
||||
node* searchNode(node* head, int val)
|
||||
{
|
||||
node* temp = head;
|
||||
while (temp != NULL)
|
||||
{
|
||||
if (temp->data == val)
|
||||
{
|
||||
return temp;
|
||||
}
|
||||
temp = temp->next;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
node* searchLastNode(node* head, int val)
|
||||
{
|
||||
node* temp = head;
|
||||
node* pos = NULL;
|
||||
while (temp != NULL)
|
||||
{
|
||||
if (temp->data == val)
|
||||
{
|
||||
pos = temp;
|
||||
}
|
||||
temp = temp->next;
|
||||
}
|
||||
return pos;
|
||||
}
|
||||
void printAll(node* head)
|
||||
{
|
||||
if (head != NULL)
|
||||
{
|
||||
cout << head->data << " ";
|
||||
printAll(head->next);
|
||||
}
|
||||
}
|
||||
int main()
|
||||
{
|
||||
node* head = NULL;
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
prependList(head, i);
|
||||
}
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
prependList(head, i);
|
||||
}
|
||||
cout << searchLastNode(head, 8);
|
||||
printAll(head);
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct Node
|
||||
{
|
||||
int data;
|
||||
Node *next;
|
||||
};
|
||||
|
||||
void appendList(Node* &head, int data)
|
||||
{
|
||||
Node* newNode = new Node;
|
||||
newNode->data = data;
|
||||
newNode->next = NULL;
|
||||
if (head == NULL)
|
||||
{
|
||||
head = newNode;
|
||||
return;
|
||||
}
|
||||
Node* temp = head;
|
||||
while (temp->next != NULL)
|
||||
{
|
||||
temp = temp->next;
|
||||
}
|
||||
temp->next = newNode;
|
||||
}
|
||||
|
||||
void makeCircularLinkedList(Node* head) {
|
||||
Node* temp = head;
|
||||
while (temp != NULL) {
|
||||
if (temp->next == NULL) {
|
||||
temp->next = head;
|
||||
return;
|
||||
}
|
||||
temp = temp->next;
|
||||
}
|
||||
}
|
||||
|
||||
void printSingleLinkedList(Node* head)
|
||||
{
|
||||
if (head != NULL)
|
||||
{
|
||||
cout << head->data << " ";
|
||||
printSingleLinkedList(head->next);
|
||||
}
|
||||
}
|
||||
|
||||
void printCircularLinkedList(Node* head)
|
||||
{
|
||||
if (head == NULL) return;
|
||||
Node* temp = head;
|
||||
do
|
||||
{
|
||||
cout << temp->data << " ";
|
||||
temp = temp->next;
|
||||
} while (temp != head);
|
||||
}
|
||||
|
||||
void printList(Node* head)
|
||||
{
|
||||
if (head == NULL) return;
|
||||
Node *temp = head;
|
||||
do
|
||||
{
|
||||
cout << temp->data << " ";
|
||||
temp = temp->next;
|
||||
} while (temp != NULL && temp != head);
|
||||
}
|
||||
|
||||
Node* searchList(Node* head, int data)
|
||||
{
|
||||
Node* temp = head;
|
||||
while (temp != NULL)
|
||||
{
|
||||
if (temp->data == data)
|
||||
{
|
||||
return temp;
|
||||
}
|
||||
temp = temp->next;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Node* deleteNth(Node* head, int n)
|
||||
{
|
||||
if (head == NULL || n < 0)
|
||||
{
|
||||
return head;
|
||||
}
|
||||
if (n == 0)
|
||||
{
|
||||
Node *temp = head;
|
||||
head = head->next;
|
||||
delete temp;
|
||||
}
|
||||
else
|
||||
{
|
||||
Node *pre = head, *cur = head->next;
|
||||
for (int i = 1; i < n; i++)
|
||||
{
|
||||
pre = cur;
|
||||
cur = cur->next;
|
||||
if (cur == NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (cur != NULL)
|
||||
{
|
||||
pre->next = cur->next;
|
||||
delete cur;
|
||||
cur = NULL;
|
||||
}
|
||||
}
|
||||
return head;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
Node* head = NULL;
|
||||
Node* search = NULL;
|
||||
for (int i = 0; i < 10; ++i)
|
||||
printSingleLinkedList(head);
|
||||
cout << endl;
|
||||
printList(head);
|
||||
cout << endl;
|
||||
search = searchList(head, 5);
|
||||
cout << (void*)search << endl;
|
||||
makeCircularLinkedList(head);
|
||||
printCircularLinkedList(head); {
|
||||
appendList(head, i);
|
||||
}
|
||||
cout << endl;
|
||||
printList(head);
|
||||
cout << endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user