Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <unistd.h>
- #include <termios.h>
- #include <fstream>
- using namespace std;
- char _getch(){
- char buf=0;
- struct termios old={0}; fflush(stdout); if(tcgetattr(0, &old)<0)
- perror("tcsetattr()"); old.c_lflag&=~ICANON; old.c_lflag&=~ECHO; old.c_cc[VMIN]=1; old.c_cc[VTIME]=0; if(tcsetattr(0, TCSANOW, &old)<0)
- perror("tcsetattr ICANON"); if(read(0, &buf, 1)<0)
- perror("read()"); old.c_lflag|=ICANON; old.c_lflag|=ECHO;
- if(tcsetattr(0, TCSADRAIN, &old)<0)
- perror ("tcsetattr ~ICANON"); return buf;
- }
- struct USER
- {
- char name[13];
- char passwd[11];
- double cash;
- int login_count;
- int sim_datei;
- int hold[5];
- };
- struct STOCK
- {
- char name[10];
- int count;
- int *date;
- double *open, *high, *low, *close, *adj;
- int *vol;
- };
- STOCK read_stock(char name[])
- {
- STOCK s;
- char filename[1000]="/Users/maoyunqi/Desktop/Programming/final/";
- strcat(filename,name);
- strcat(filename,".csv");
- ifstream fin;
- fin.open(filename);
- s.count = 0;
- char temp[1000];
- while (!fin.eof())
- {
- fin.getline(temp, 1000);
- s.count++;
- }
- fin.close();
- s.date = new int[s.count];
- s.open = new double[s.count];
- s.high = new double[s.count];
- s.low = new double[s.count];
- s.close = new double[s.count];
- s.adj = new double[s.count];
- s.vol = new int[s.count];
- int i;
- char tmp;
- fin.open(filename);
- for (i = 0; i < s.count; i++)
- {
- fin >> s.date[i];
- fin >> tmp;
- fin >> s.open[i];
- fin >> tmp;
- fin >> s.high[i];
- fin >> tmp;
- fin >> s.low[i];
- fin >> tmp;
- fin >> s.close[i];
- fin >> tmp;
- fin >> s.adj[i];
- fin >> tmp;
- fin >> s.vol[i];
- }
- fin.close();
- return s;
- }
- void delete_stock(STOCK s)
- {
- delete[] s.vol;
- delete[] s.adj;
- delete[] s.close;
- delete[] s.low;
- delete[] s.high;
- delete[] s.open;
- delete[] s.date;
- }
- void inputName(char name[], int j)
- {
- char x;
- int c = 0, i;
- do {
- system("clear");
- if (j == 1)
- cout << "Username: ";
- else if (j == 0)
- cout << "Password: ";
- if (j == 1)
- {
- for (i = 0; i < c; i++)
- cout << name[i];
- }
- else if (j == 0)
- {
- for (i = 0; i < c; i++)
- cout << "*";
- }
- x = _getch();
- if (isalnum(x))
- {
- if (c == 12 && j == 1)
- c--;
- else if (c == 10 && j == 0)
- c--;
- name[c] = x;
- c++;
- }
- if (x == 8 && c > 0)//8是後退
- c--;
- } while (x != 13 || c == 0);
- name[c] = '\0';
- }
- USER login()
- {
- char name1[13];
- char passwd1[11];
- USER userlist[1000];
- int userno, i, j;
- ifstream fin;
- fin.open("/Users/maoyunqi/Desktop/Programming/final/userlist.txt");
- fin >> userno;
- for (i = 0; i < userno; i++)
- {
- fin >> userlist[i].name;
- fin >> userlist[i].passwd;
- fin >> userlist[i].cash;
- fin >> userlist[i].login_count;//登入此系統次數
- fin >> userlist[i].sim_datei;
- for (j = 0; j < 5; j++)
- fin >> userlist[i].hold[j];
- }
- fin.close();
- do {
- system("clear");
- inputName(name1, 1);
- inputName(passwd1, 0);
- for (i = 0; i < userno; i++)
- {
- if (strcmp(userlist[i].name, name1) == 0)
- {
- if (strcmp(userlist[i].passwd, passwd1) == 0)
- {
- userlist[i].login_count++;
- return userlist[i];
- }
- else
- {
- cout << "Incorrect password!!!!!!" << endl;
- cout << "Press any key" << endl;
- _getch();
- break;
- }
- }
- }
- if (i == userno)
- {
- char passwd2[11];
- cout << "Confirm your password: ";
- cin >> passwd2;
- if (strcmp(passwd1, passwd2) == 0)
- {
- strcpy(userlist[userno].name, name1);
- strcpy(userlist[userno].passwd, passwd1);
- userlist[userno].login_count = 1;
- userlist[userno].cash = 10000;
- userlist[userno].sim_datei = 2;
- userlist[userno].hold[0] = 0;
- userlist[userno].hold[1] = 0;
- userlist[userno].hold[2] = 0;
- userlist[userno].hold[3] = 0;
- userlist[userno].hold[4] = 0;
- ofstream fout;
- fout.open("/Users/maoyunqi/Desktop/Programming/final/userlist.txt");
- fout << userno + 1 << endl;
- for (j = 0; j <= userno; j++)
- {
- fout << userlist[j].name << endl;
- fout << userlist[j].passwd << endl;
- fout << userlist[j].cash << endl;
- fout << userlist[j].login_count << endl;
- fout << userlist[j].sim_datei << endl;
- fout << userlist[j].hold[0] << " " << userlist[j].hold[1] << " " << userlist[j].hold[2] << " " << userlist[j].hold[3] << " " << userlist[j].hold[4] << endl;
- }
- fout.close();
- return userlist[userno];
- }
- }
- } while (true);
- }
- void mainUI(int select, USER one, STOCK s[])
- {
- char left[7];
- char right[7];
- int i;
- for (i = 0; i < 7; i++)
- {
- left[i] = ' ';
- right[i] = ' ';
- }
- left[select] = '[';
- right[select] = ']';
- system("clear");
- cout << endl << endl << endl;
- cout << " ╔ ═ ╗ ╔ ╗ ╔ ╗ ╔ ═ ═ ═ ╗" << endl;
- cout << " ║ █ ║ ╔ ◢◣ ◢◣ ╗ ║ ███ ║" << endl;
- cout << " ║ █ ║ ║ █ ◥◤ █ ║ ║ █ ═ ╣" << endl;
- cout << " ║ █ ║ ║ █ ╔ ╗ █ ║ ║ ███ ║" << endl;
- cout << " ║ █ ║ ║ █ ║ ║ █ ║ ║ █ ╔ ═ ╝" << endl;
- cout << " ╚ ═ ╝ ╚ ═ ╝ ╚ ═ ╝ ╚ ═ ╝" << endl << endl << endl << endl;
- cout << " " << one.name << " 您好,今日日期為 " << s[0].date[one.sim_datei] << endl;
- cout << " 你目前現金有 " << one.cash << " 元,總資產為 " << one.cash << " 元" << endl;
- cout << "=============================================================================" << endl;
- cout << " " << left[1] << "1" << right[1] << " 投資決策" << endl;
- cout << " " << left[2] << "2" << right[2] << " 數獨遊戲" << endl;
- cout << " " << left[3] << "3" << right[3] << " 寂寞聊天室" << endl;
- cout << " " << left[4] << "4" << right[4] << " 猜數字" << endl;
- cout << " " << left[5] << "5" << right[5] << " 富爸爸" << endl;
- cout << " " << left[6] << "6" << right[6] << " 變更密碼" << endl;
- cout << " " << left[0] << "0" << right[0] << " 離開系統" << endl;
- cout << "=============================================================================" << endl << endl;
- }
- void print(int M[9][9])
- {
- int r, c;
- for (r = 0; r < 9; r++)
- {
- for (c = 0; c < 9; c++)
- {
- if (M[r][c] > 0)
- cout << M[r][c] << " ";
- else
- cout << " ";
- }
- cout << endl;
- }
- }
- void updateZ(int Z[9][9][10], int r, int c, int num)
- {
- int x, y;
- for (x = 0; x < 9; x++)
- Z[r][x][num] = 0;
- for (y = 0; y < 9; y++)
- Z[y][c][num] = 0;
- for (y = r / 3 * 3; y <= r / 3 * 3 + 2; y++)
- {
- for (x = c / 3 * 3; x <= c / 3 * 3 + 2; x++)
- {
- Z[y][x][num] = 0;
- }
- }
- Z[r][c][num] = 1;
- }
- int findZ(int Z[9][9][10], int r, int c)
- {
- int i, count, num;
- count = 0;
- for (i = 1; i <= 9; i++)
- {
- if (Z[r][c][i] == 1)
- {
- count++;
- num = i;
- }
- }
- if (count == 1)
- return num;
- else
- return 0;
- }
- void solve(int Q[9][9], int A[9][9])
- {
- int Z[9][9][10];
- int r, c, i;
- for (r = 0; r < 9; r++)
- {
- for (c = 0; c < 9; c++)
- {
- if (Q[r][c] > 0)
- {
- A[r][c] = Q[r][c];
- for (i = 1; i <= 9; i++)
- Z[r][c][i] = 0;
- Z[r][c][Q[r][c]] = 1;
- }
- else
- {
- A[r][c] = 0;
- for (i = 1; i <= 9; i++)
- Z[r][c][i] = 1;
- }
- }
- }
- for (r = 0; r < 9; r++)
- {
- for (c = 0; c < 9; c++)
- {
- if (Q[r][c] > 0)
- {
- updateZ(Z, r, c, Q[r][c]);
- }
- }
- }
- int flag = 1, num;
- while (flag == 1)
- {
- flag = 0;
- for (r = 0; r < 9; r++)
- {
- for (c = 0; c < 9; c++)
- {
- if (A[r][c] == 0)
- {
- num = findZ(Z, r, c);
- if (num > 0)
- {
- flag = 1;
- A[r][c] = num;
- updateZ(Z, r, c, num);
- }
- }
- }
- }
- }
- }
- void printUI(int M[9][9], int rs, int cs)
- {
- int r, c;
- for (r = 0; r < 9; r++)
- {
- for (c = 0; c < 9; c++)
- {
- if (r == rs && c == cs && M[r][c] > 0)
- cout << "[" << M[r][c] << "]";
- else if (M[r][c] > 0)
- cout << " " << M[r][c] << " ";
- else if (r == rs && c == cs && M[r][c] == 0)
- cout << "[" << " " << "]";
- else
- cout << " " << " " << " ";
- }
- cout << endl;
- }
- }
- bool compare(int M[9][9], int M1[9][9])
- {
- int r, c;
- for (r = 0; r < 9; r++)
- {
- for (c = 0; c < 9; c++)
- {
- if (M[r][c] != M1[r][c])
- return false;
- }
- }
- return true;
- }
- int Sudoku_main()
- {
- int Q[9][9], a, b;
- srand(time(NULL));
- ifstream fin;
- char filename[] = "/Users/maoyunqi/Desktop/Programming/final/Q1.txt";
- filename[43] = '1' + rand() % 3;
- fin.open(filename);
- for (a = 0; a < 9; a++)
- {
- for (b = 0; b < 9; b++)
- fin >> Q[a][b];
- }
- fin.close();
- int A[9][9], A1[9][9];
- int r = 0, c = 0;
- char x, y;
- int i, j;
- for (i = 0; i < 9; i++)
- {
- for (j = 0; j < 9; j++)
- {
- A[i][j] = Q[i][j];
- }
- }
- do {
- system("clear");
- printUI(A, r, c);
- x = _getch();
- if (x >= '0'&&x <= '9')
- {
- if (Q[r][c] == 0)
- A[r][c] = x - '0';
- }
- if (x == -32)
- {
- y = _getch();
- switch (y)
- {
- case 72:
- r = (r + 8) % 9;
- break;
- case 75:
- c = (c + 8) % 9;
- break;
- case 77:
- c = (c + 10) % 9;
- break;
- case 80:
- r = (r + 10) % 9;
- break;
- }
- }
- } while (x != 13);
- solve(Q, A1);
- if (compare(A, A1))
- {
- cout << "Correct" << endl;
- _getch();
- return 1;
- }
- else
- {
- cout << "Try again" << endl;
- _getch();
- return 0;
- }
- //return 1 win
- //return 0 lose
- }
- void Chat_main(USER one)
- {
- ifstream fin;
- ofstream fout;
- char str[1000];
- char name[21];
- strcpy(name, one.name);
- do {
- system("clear");
- fin.open("/Users/maoyunqi/Desktop/Programming/final/msg.txt");
- while (!fin.eof())
- {
- fin.getline(str, 1000);
- cout << str << endl;
- }
- fin.close();
- cout << "===================================" << endl;
- cin.getline(str, 1000);
- if (strcmp(str, "EXIT") != 0 && strlen(str) != 0)
- {
- fout.open("/Users/maoyunqi/Desktop/Programming/final/msg.txt", std::ofstream::app);
- fout << name << ":" << str << endl;
- fout.close();
- }
- } while (strcmp(str, "EXIT") != 0);
- }
- void split(int n, int A[])
- {
- A[0] = n / 1000;
- n %= 1000;
- A[1] = n / 100;
- n %= 100;
- A[2] = n / 10;
- A[3] = n % 10;
- }
- int legal(int A[])
- {
- int out = 1;
- int i, j;
- if (A[0] >= 10)
- return 0;
- for (i = 0; i < 3; i++)
- {
- for (j = i + 1; j < 4; j++)
- {
- if (A[i] == A[j])
- out = 0;
- }
- }
- return out;
- }
- void generate(int A[])
- {
- //method 3
- do {
- split(rand() % 10000, A);
- } while (legal(A) == 0);
- }
- void input(int G[], int tag)
- {
- int n;
- do {
- if (tag == 0)
- cout << "Please input your answer:";
- else
- cout << "Please Guess a number:";
- cin >> n;
- split(n, G);
- } while (legal(G) == 0);
- }
- void compare(int G[], int A[], int AB[])
- {
- int i, j;
- AB[0] = 0;
- for (i = 0; i < 4; i++)
- {
- if (G[i] == A[i])
- AB[0]++;
- }
- AB[1] = 0;
- for (i = 0; i < 4; i++)
- {
- for (j = 0; j < 4; j++)
- {
- if (G[i] == A[j])
- AB[1]++;
- }
- }
- AB[1] -= AB[0];//?
- }
- int XAXB_main()
- {
- int cA[4], hA[4], cG[4], hG[4], cAB[2], hAB[2], tA[4], tAB[2];
- int AP[10000], i;
- int count = 0;
- srand(time(NULL));
- generate(cA);
- input(hA, 0); // human input hA
- for (i = 0; i < 10000; i++)
- {
- split(i, tA);
- if (legal(tA) == 1)
- AP[i] = 1;
- else
- AP[i] = 0;
- }
- do
- {
- input(hG, 1);
- compare(hG, cA, hAB); //AB[0]->A AB[1]->B
- count++;
- cout << "(H)" << count << ":" << hAB[0] << "A" << hAB[1] << "B" << endl;
- // computer guess -> cG
- for (i = 0; i < 10000; i++)
- {
- if (AP[i] == 1)
- {
- split(i, cG);
- cout << "computer guess:" << cG[0] << cG[1] << cG[2] << cG[3] << endl;
- break;
- }
- }
- compare(cG, hA, cAB);
- cout << "(C)" << count << ":" << cAB[0] << "A" << cAB[1] << "B" << endl;
- //update AP
- for (i = 0; i < 10000; i++)
- {
- if (AP[i] == 1)
- {
- split(i, tA);
- compare(cG, tA, tAB);
- if (tAB[0] != cAB[0] || tAB[1] != cAB[1])
- AP[i] = 0;
- }
- }
- } while (hAB[0] != 4 && cAB[0] != 4);
- _getch(); //
- if (hAB[0] == 4 && cAB[0] == 4)
- {
- cout << "平手" << endl;
- _getch();
- return 2;
- }
- else if (hAB[0] == 4)
- {
- cout << "電腦輸了" << endl;
- _getch();
- return 1;
- }
- else
- {
- cout << "電腦贏了" << endl;
- _getch();
- return 0;
- }
- //return 1 win
- //return 0 lose
- return 0;
- }
- USER Change_password(USER one)
- {
- char passwd1[11];
- char passwd2[11];
- char passwd3[11];
- system("clear");
- cout << "password:";
- cin >> passwd1;
- while (strcmp(one.passwd, passwd1) == 0)
- {
- cout << "new password:";
- cin >> passwd2;
- cout << "confirm your password:";
- cin >> passwd3;
- if (strcmp(passwd2, passwd3) == 0)
- {
- strcpy(one.passwd, passwd3);
- cout << "success" << endl;
- break;
- }
- else
- break;
- }
- return one;
- }
- void SaveUser(USER one)
- {
- USER userlist[1000];
- int userno, i, j;
- ifstream fin;
- //讀取userlist.txt放入userlist[]中
- fin.open("/Users/maoyunqi/Desktop/Programming/final/userlist.txt");
- fin >> userno;
- for (i = 0; i < userno; i++)
- {
- fin >> userlist[i].name;
- fin >> userlist[i].passwd;
- fin >> userlist[i].cash;
- fin >> userlist[i].login_count;
- fin >> userlist[i].sim_datei;
- for (j = 0; j < 5; j++)
- fin >> userlist[i].hold[j];
- if (strcmp(userlist[i].name, one.name) == 0) //讀到目前login的使用者,覆蓋他的資料
- {
- strcpy(userlist[i].passwd, one.passwd);
- userlist[i].cash = one.cash;
- userlist[i].login_count = one.login_count;
- for (j = 0; j < 5; j++)
- userlist[i].hold[j] = one.hold[j];
- }
- }
- fin.close();
- //更新完後寫回userlist.txt
- ofstream fout;
- fout.open("/Users/maoyunqi/Desktop/Programming/final/userlist.txt");
- fout << userno << endl;
- for (i = 0; i < userno; i++)
- {
- fout << userlist[i].name << endl;
- fout << userlist[i].passwd << endl;
- fout << userlist[i].cash << endl;
- fout << userlist[i].login_count << endl;
- fout << userlist[i].sim_datei << endl;
- fout << userlist[i].hold[0] << " " << userlist[i].hold[1] << " " << userlist[i].hold[2] << " " << userlist[i].hold[3] << " " << userlist[i].hold[4] << endl;
- }
- fout.close();
- }
- USER investment(USER one, STOCK s[])
- {
- int i, j;
- system("clear");
- cout << "Your Cash:" << one.cash << endl;
- for (i = 0; i < 5; i++)
- {
- cout << s[i].name << " ";
- for (j = -2; j <= 0; j++)
- {
- cout << s[i].date[one.sim_datei + j] << " ";
- cout << s[i].close[one.sim_datei + j] << " ";
- }
- cout << "Hold: " << one.hold[i] << endl;
- }
- char company[10];
- do {
- cout << "Please tell me which company do you want to invest:" << endl;
- cin >> company;
- if (strcmp(company, "EXIT") == 0)
- break;
- else
- {
- bool ok = true;//是否輸入正確股票名稱
- for (i = 0; i < 5; i++)
- {
- if (strcmp(s[i].name, company) == 0)
- {
- ok = false;
- char x, y;
- do {
- system("clear");
- cout << "Your Cash:" << one.cash << endl;
- cout << "Hold: " << one.hold[i] << endl;
- cout << "Please press 右鍵 or 左鍵 to change the hold or enter to exit" << endl;
- x = _getch();
- if (x == -32)
- {
- y = _getch();
- switch (y)
- {
- case 77:
- if (one.cash >= s[i].close[one.sim_datei])
- {
- one.hold[i]++;
- one.cash -= s[i].close[one.sim_datei];
- }
- break;
- case 75:
- if (one.hold[i] > 0)
- {
- one.hold[i]--;
- one.cash += s[i].close[one.sim_datei];
- }
- break;
- }
- }
- } while (x != 13);
- }
- }
- if (ok)
- cout << "!!!!!!!!!! Wrong !!!!!!!!!!" << endl;
- }
- } while (true);
- //_getwch();
- return one;
- }
- int main()
- {
- char ch; do{
- ch = _getch();
- if(ch >= '0'&& ch <= '9')
- std::cout << ch;
- } while(ch != '\n');
- USER one;
- STOCK s[5];
- char name1[]="AAPL",name2[]="AMZN",name3[]="FB",name4[]="GOOGLE",name5[]="MSFT";
- s[0] = read_stock(name1);
- s[1] = read_stock(name2);
- s[2] = read_stock(name3);
- s[3] = read_stock(name4);
- s[4] = read_stock(name5);
- one = login();
- int select = 1;
- char c1, c2;
- do
- {
- mainUI(select, one, s);
- c1 = _getch();
- if (c1 == -32)
- {
- c2 = _getch();
- if (c2 == 72)
- select = (select + 6) % 7;
- if (c2 == 80)
- select = (select + 1) % 7;
- if (c2 == 77)
- c1 = 13;
- }
- if (c1 >= '0'&&c1 <= '6')
- select = c1 - '0';
- if (c1 == 13)
- {
- if (select == 1) //投資決策
- {
- one = investment(one, s);
- }
- else if (select == 2) //數獨
- {
- int win = Sudoku_main();
- if (win == 1)
- one.cash += 5000;
- else
- one.cash -= 5000;
- }
- else if (select == 3) //聊天室
- {
- Chat_main(one);
- }
- else if (select == 4) //猜數字
- {
- int win = XAXB_main();
- if (win == 1)
- one.cash += 5000;
- else if (win == 0)
- one.cash += 1000;
- else
- one.cash -= 100;
- }
- else if (select == 5) //富爸爸
- {
- one.cash += 1000;
- }
- else if (select == 6) //變更密碼
- {
- one = Change_password(one);
- }
- else // select == 0
- {
- break;
- }
- }
- } while (true);
- SaveUser(one); //把user狀態存回userlist.txt
- delete_stock(s[0]);
- delete_stock(s[1]);
- delete_stock(s[2]);
- delete_stock(s[3]);
- delete_stock(s[4]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement