那还不是为了初始化结构体指针
初始化了吗?没有吧?
C++有自己的new/delete你不用,你偏要用C的malloc/free
那也可以呀,只是你得自己调用对象的构造函数和析构函数
问题是你调用了吗?
#include <iostream>
#include <string>
#include <cstdlib>
using std::cout, std::endl;
using std::string;
using std::malloc, std::free;
struct test_t {
string str;
int value;
};
int main() {
test_t *t = (test_t *)malloc(sizeof(*t)); // 申请内存
new(&t->str) string(); // 创建string对象
t->str = "1234";
t->value = 1234;
cout << t->str << endl;
cout << t->value << endl;
t->str.~basic_string(); // 销毁string对象
free(t); // 释放内存
return 0;
}
?{:10_327:} 小凯2013 发表于 2022-12-25 09:27
实际上有511行的,我怕鱼C论坛炸掉
所以我就扔了个模型。
9000行也不会炸{:10_256:} 小凯2013 发表于 2022-12-25 09:39
现在变这样子了:
C:\Program Files\Embarcadero\Dev-Cpp\TDM-GCC-64\x86_64-w64-mingw32\bin\ld.exe C:%u ...
你看看switch后面括号里东西的类型和后面常量类型是否相同 小凯2013 发表于 2022-12-25 09:39
现在变这样子了:
C:\Program Files\Embarcadero\Dev-Cpp\TDM-GCC-64\x86_64-w64-mingw32\bin\ld.exe C:%u ...
你read和write的问题,不是switch 小凯2013 发表于 2022-12-25 19:22
源码【不许侵权】:
你这 readme 也不是 markdown 啊
而且条款不是 License 吗,你见过那个条款不让私自编译的 liuzhengyuan 发表于 2022-12-31 09:07
你这 readme 也不是 markdown 啊
而且条款不是 License 吗,你见过那个条款不让私自编译的
今天你就见到了! #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 << "打开文件失败:: %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 << "打开文件失败:: %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;
}
}
页:
1
[2]