|
发表于 2023-1-31 13:50:09
|
显示全部楼层
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <cerrno>
- #include <bits/extc++.h>
- using namespace std;
- typedef unsigned int uint, *puint;
- typedef const char cchar, *pcchar;
- #define randint(a, b) ((rand() % (b-a+1)) + a)
- #define MAX_LEVEL 15
- struct Account {
- string name = "Player";
- uint max_score = 0;
- uint score = 0;
- uint level = 0;
- uint exp = 0;
- long long atid = randint(100000000000000, 999999999999999);
- };
- bool add(uint a, uint b);
- bool sub(uint a, uint b);
- bool mcl(uint a, uint b);
- bool div(uint a, uint b);
- bool mwriteInfo(string file, Account *account);
- bool mreadInfo(string file, Account *account);
- bool mywriteInfo(pcchar file, Account *account) {
- FILE *fp = NULL;
-
- if ((fp = fopen(file, "w")) == NULL) {
- cout << "打开文件失败:[Errno %d]: %s" << errno, strerror(errno);
- return false;
- }
-
- fwrite(account, sizeof(Account), 1, fp);
- return true;
- }
- bool myreadInfo(pcchar file, Account *account) {
- FILE *fp = NULL;
- char choose;
-
- if ((fp = fopen(file, "r")) == NULL) {
- cout << "打开文件失败:[Errno %d]: %s" << errno, strerror(errno);
- return false;
- }
-
- cout << "确定覆盖现在的信息吗?【Y/n】";
- cin >> choose;
-
- switch (choose) {
- case 'Y':
- case 'y':
- break;
- case 'N':
- case 'n':
- cout << "用户取消了操作。" << endl;
- return true;
-
- default:
- cout << "请认真输入!" << endl;
- return true;
- }
-
- fread(account, sizeof(Account), 1, fp);
- return true;
- }
- int main(void) {
- short int code;
- int number, score, operatorNum;
- Account *account;
- bool savable = false;
- string rfile, wfile;
-
- cout << "---欢迎来到口算小能手---" << endl;
-
- while (code != 0) {
- cout << "请选择模式:" << endl;
- cout << "1 -- 纯加法[+1-3]" << endl;
- cout << "2 -- 纯减法[+1-3]" << endl;
- cout << "3 -- 纯乘法[+2-4]" << endl;
- cout << "4 -- 纯除法[+2-4]" << endl;
- cout << "5 -- 混合运算[作对一题就+1]" << endl;
- cout << "6 -- 死亡算法[作对一题就+3]" << endl;
- cout << "7 -- 导入数据[+0]" << endl;
- cout << "8 -- 保存数据[+0]" << endl;
- cout << "9 -- 查看账户信息[+0]" << endl;
- cout << "0 -- Exit退出[+1]" << endl;
- cout << "注:200积分升一级,共15级qwq!" << endl;
- cout << "请输入指令代码:";
- cin >> code;
- if ((*account).exp >= 200 && (*account).level < 15) {
- (*account).exp -= 200;
- (*account).level += 1;
- }
-
- switch (code) {
- case 0:
- if ((*account).score > 0) {
- if (savable) {
- exit(EXIT_SUCCESS);
- }
- else {
- char choose;
-
- cout << "你还没有保存信息哦!" << endl;
- cout << "确定要退出吗?【N/y】";
- cin >> choose;
-
- switch (choose) {
- case 'Y':
- case 'y':
- exit(EXIT_SUCCESS);
- break;
-
- case 'N':
- case 'n':
- break;
- }
- }
- }
- else {
- cout << "别想钻空子!加分不是白加的!" << endl;
- }
- code = 10;
- break;
-
- case 1:
- cout << "请输入题目数:";
- cin >> number;
-
- for (int count = number;count > 0;count--) {
- if (add(randint(100, 999), randint(100, 999))) {
- score++;
- }
- }
-
- (*account).score += score;
-
- if (score >= (*account).max_score) {
- (*account).max_score = score;
- }
-
- if (score >= number/5) {
- (*account).exp += 1;
- }
- else if (score >= number/3) {
- (*account).exp += 2;
- }
- else if (score >= number/1) {
- (*account).exp += 3;
- }
-
- score = 0;
- break;
-
- case 2:
- cout << "请输入题目数:";
- cin >> number;
-
- uint a, b;
-
- while (true) {
- a = randint(100, 999);
- b = randint(100, 999);
-
- if (a >= b) {
- break;
- }
- }
-
- for (int count = number;count > 0;count--) {
- if (sub(a, b)) {
- score++;
- }
- }
-
- (*account).score += score;
-
- if (score >= (*account).max_score) {
- (*account).max_score = score;
- }
-
- if (score >= number/5) {
- (*account).exp += 1;
- }
- else if (score >= number/3) {
- (*account).exp += 2;
- }
- else if (score >= number/1) {
- (*account).exp += 3;
- }
-
- score = 0;
- break;
-
- case 3:
- cout << "请输入题目数:";
- cin >> number;
-
- for (int count = number;count > 0;count--) {
- if (mcl(randint(100, 999), randint(100, 999))) {
- score++;
- }
- }
-
- (*account).score += score;
-
- if (score >= (*account).max_score) {
- (*account).max_score = score;
- }
-
- if (score >= number/5) {
- (*account).exp += 2;
- }
- else if (score >= number/3) {
- (*account).exp += 3;
- }
- else if (score >= number/1) {
- (*account).exp += 4;
- }
-
- score = 0;
- break;
-
- case 4:
- cout << "请输入题目数:";
- cin >> number;
-
- uint c, d;
-
- while (true) {
- c = randint(100, 999);
- d = randint(100, 999);
-
- if (c % d == 0) {
- break;
- }
- }
-
- for (int count = number;count > 0;count--) {
- if (div(randint(c, d), randint(c, d))) {
- score++;
- }
- }
-
- (*account).score += score;
-
- if (score >= (*account).max_score) {
- (*account).max_score = score;
- }
-
- if (score >= number/5) {
- (*account).exp += 2;
- }
- else if (score >= number/3) {
- (*account).exp += 3;
- }
- else if (score >= number/1) {
- (*account).exp += 4;
- }
-
- score = 0;
- break;
-
- case 5:
- cout << "请输入题目数:";
- cin >> number;
-
- for (int count = number;count > 0;count--) {
- operatorNum = randint(1, 4);
- uint exp;
-
- switch (operatorNum) {
- case 1:
- if (add(randint(100, 999), randint(100, 999))) {
- score++;
- exp++;
- }
-
- break;
-
- case 2:
- uint a, b;
-
- while (true) {
- a = randint(100, 999);
- b = randint(100, 999);
-
- if (a >= b) {
- break;
- }
- }
-
- if (sub(a, b)) {
- score++;
- exp++;
- }
-
- break;
-
- case 3:
- if (mcl(randint(100, 999), randint(100, 999))) {
- score++;
- exp++;
- }
-
- break;
-
- case 4:
- uint c, d;
-
- while (true) {
- c = randint(100, 999);
- d = randint(100, 999);
-
- if (c % d == 0) {
- break;
- }
- }
-
- if (div(c, d)) {
- score++;
- exp++;
- }
- }
- }
-
- (*account).score += score;
-
- if (score >= (*account).max_score) {
- (*account).max_score = score;
- }
-
- score = 0;
- break;
-
- case 6:
- cout << "请输入题目数:";
- cin >> number;
-
- for (int count = number;count > 0;count--) {
- operatorNum = randint(1, 4);
- uint exp;
-
- switch (operatorNum) {
- case 1:
- if (add(randint(10000, 99999), randint(10000, 99999))) {
- score++;
- exp += 3;
- }
-
- break;
-
- case 2:
- uint a, b;
-
- while (true) {
- a = randint(10000, 99999);
- b = randint(10000, 99999);
-
- if (a >= b) {
- break;
- }
- }
-
- if (sub(a, b)) {
- score++;
- exp += 3;
- }
-
- break;
-
- case 3:
- if (mcl(randint(10000, 99999), randint(10000, 99999))) {
- score++;
- exp += 3;
- }
-
- break;
-
- case 4:
- uint e, f;
-
- while (true) {
- e = randint(10000, 99999);
- f = randint(10000, 99999);
-
- if (e % f == 0) {
- break;
- }
- }
-
- if (div(e, f)) {
- score++;
- exp += 3;
- }
- }
- }
-
- (*account).score += score;
-
- if (score >= (*account).max_score) {
- (*account).max_score = score;
- }
-
- score = 0;
- break;
-
- case 7:
- cout << "请输入文件路径:";
- cin >> rfile;
-
- if (myreadInfo(rfile.c_str(), account)) {
- cout << "读取信息成功!" << endl;
- }
- else {
- cout << "读取文件失败,请重试。" << endl;
- }
-
- break;
-
- case 8:
- cout << "请输入要保存的路径:";
- cin >> wfile;
-
- if (mywriteInfo(wfile.c_str(), account)) {
- cout << "保存文件成功!" << endl;
- savable = true;
- }
- else {
- cout << "保存文件失败,请重试。" << endl;
- }
-
- break;
-
- case 9:
- cout << "Account Information:" << endl;
- cout << "Name(姓名):" << (*account).name << endl;
- cout << "Max-Score(最高分数):" << (*account).max_score << endl;
- cout << "Score(累计分数):" << (*account).score << endl;
- cout << "Level(等级):" << (*account).score << endl;
- cout << "Exp(经验):" << (*account).score << endl;
- cout << "ATID(账户ID):" << (*account).atid << endl;
-
- break;
-
- }
- }
-
- return 0;
- }
- bool add(uint a, uint b) {
- uint answer = a + b;
- uint userinput;
-
- cout << "%d + %d = " << a, b;
- cin >> userinput;
-
- if (userinput == answer) {
- cout << "答对啦!" << endl;
- return true;
- }
- else {
- cout << "答错啦!" << endl;
- return false;
- }
- }
- bool sub(uint a, uint b) {
- uint answer = a - b;
- uint userinput;
-
- cout << "%d - %d = " << a, b;
- cin >> userinput;
-
- if (userinput == answer) {
- cout << "答对啦!" << endl;
- return true;
- }
- else {
- cout << "答错啦!" << endl;
- return false;
- }
- }
- bool mcl(uint a, uint b) {
- uint answer = a * b;
- uint userinput;
-
- cout << "%d × %d = " << a, b;
- cin >> userinput;
-
- if (userinput == answer) {
- cout << "答对啦!" << endl;
- return true;
- }
- else {
- cout << "答错啦!" << endl;
- return false;
- }
- }
- bool div(uint a, uint b) {
- uint answer = a / b;
- uint userinput;
-
- cout << "%d ÷ %d = " << a, b;
- cin >> userinput;
-
- if (userinput == answer) {
- cout << "答对啦!" << endl;
- return true;
- }
- else {
- cout << "答错啦!" << endl;
- return false;
- }
- }
复制代码 |
|