sh-5.1$ cat main.cpp
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cerrno>
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 writeInfo(string file, Account *account);
bool readInfo(string file, Account *account);
int main(void) {
//short int code; // variable 'code' is used uninitialized whenever function 'main' is called (FixIt)
short int code = 0;
int number, score, operatorNum;
Account *account;
bool savable = false;
string rfile, wfile;
cout << "---欢迎来到口算小能手---" << endl;
account = NULL; // line: 61
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) { // variable 'account' is uninitialized when used here (FixIt)
(*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++; // variable 'exp' is uninitialized when used here (FixIt)
}
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; // variable 'exp' set but not used
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 (readInfo(rfile.c_str(), account)) {
cout << "读取信息成功!" << endl;
}
else {
cout << "读取文件失败,请重试。" << endl;
}
break;
case 8:
cout << "请输入要保存的路径:";
cin >> wfile;
if (writeInfo(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;
}
}
bool writeInfo(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 readInfo(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;
}
sh-5.1$ g++ -g -Wall -o main main.cpp
main.cpp: In function ‘int main()’:
main.cpp:109:43: warning: comparison of integer expressions of different signedness: ‘int’ and ‘uint’ {aka ‘unsigned in ’} [-Wsign-compare]
109 | if (score >= (*account).max_score) {
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
main.cpp:149:43: warning: comparison of integer expressions of different signedness: ‘int’ and ‘uint’ {aka ‘unsigned in ’} [-Wsign-compare]
149 | if (score >= (*account).max_score) {
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
main.cpp:178:43: warning: comparison of integer expressions of different signedness: ‘int’ and ‘uint’ {aka ‘unsigned in ’} [-Wsign-compare]
178 | if (score >= (*account).max_score) {
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
main.cpp:218:43: warning: comparison of integer expressions of different signedness: ‘int’ and ‘uint’ {aka ‘unsigned in ’} [-Wsign-compare]
218 | if (score >= (*account).max_score) {
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
main.cpp:301:43: warning: comparison of integer expressions of different signedness: ‘int’ and ‘uint’ {aka ‘unsigned in ’} [-Wsign-compare]
301 | if (score >= (*account).max_score) {
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
main.cpp:374:43: warning: comparison of integer expressions of different signedness: ‘int’ and ‘uint’ {aka ‘unsigned in ’} [-Wsign-compare]
374 | if (score >= (*account).max_score) {
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
main.cpp: In function ‘bool add(uint, uint)’:
main.cpp:429:37: warning: right operand of comma operator has no effect [-Wunused-value]
429 | cout << "%d + %d = " << a, b;
| ^
main.cpp: In function ‘bool sub(uint, uint)’:
main.cpp:446:37: warning: right operand of comma operator has no effect [-Wunused-value]
446 | cout << "%d - %d = " << a, b;
| ^
main.cpp: In function ‘bool mcl(uint, uint)’:
main.cpp:463:37: warning: right operand of comma operator has no effect [-Wunused-value]
463 | cout << "%d × %d = " << a, b;
| ^
main.cpp: In function ‘bool div(uint, uint)’:
main.cpp:480:37: warning: right operand of comma operator has no effect [-Wunused-value]
480 | cout << "%d ÷ %d = " << a, b;
| ^
/usr/bin/ld: /tmp/ccd3nG4I.o: in function `main':
/tmp/main.cpp:385: undefined reference to `readInfo(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, Account*)'
/usr/bin/ld: /tmp/main.cpp:398: undefined reference to `writeInfo(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, Account*)'
collect2: error: ld returned 1 exit status
sh-5.1$ clang++ -g -Wall -o main main.cpp
main.cpp:315:46: warning: variable 'exp' set but not used [-Wunused-but-set-variable]
uint exp; // variable 'exp' set but not used
^
main.cpp:321:65: warning: variable 'exp' is uninitialized when used here [-Wuninitialized]
exp += 3; // 我都懒得给你注释了
^~~
main.cpp:315:49: note: initialize the variable 'exp' to silence this warning
uint exp; // variable 'exp' set but not used
^
= 0
main.cpp:248:65: warning: variable 'exp' is uninitialized when used here [-Wuninitialized]
exp++; // variable 'exp' is uninitialized when used here (FixIt)
^~~
main.cpp:241:49: note: initialize the variable 'exp' to silence this warning
uint exp;
^
= 0
main.cpp:429:36: warning: expression result unused [-Wunused-value]
cout << "%d + %d = " << a, b;
^
main.cpp:446:36: warning: expression result unused [-Wunused-value]
cout << "%d - %d = " << a, b;
^
main.cpp:463:37: warning: expression result unused [-Wunused-value]
cout << "%d × %d = " << a, b;
^
main.cpp:480:37: warning: expression result unused [-Wunused-value]
cout << "%d ÷ %d = " << a, b;
^
7 warnings generated.
/usr/bin/ld: /tmp/main-52f47d.o: in function `main':
<unknown>:385: undefined reference to `readInfo(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, Account*)'
/usr/bin/ld: <unknown>:398: undefined reference to `writeInfo(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, Account*)'
clang-14: error: linker command failed with exit code 1 (use -v to see invocation)
sh-5.1$