鱼C论坛

 找回密码
 立即注册
12
返回列表 发新帖
楼主: 小凯2013

switch 语句报错

[复制链接]
 楼主| 发表于 2022-12-28 11:06:28 | 显示全部楼层
那还不是为了初始化结构体指针
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-28 11:47:58 | 显示全部楼层
小凯2013 发表于 2022-12-28 11:06
那还不是为了初始化结构体指针

初始化了吗?没有吧?
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;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-12-28 11:48:54 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-12-29 08:29:22 | 显示全部楼层
小凯2013 发表于 2022-12-25 09:27
实际上有511行的,我怕鱼C论坛炸掉
所以我就扔了个模型。

9000行也不会炸
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-29 18:07:35 | 显示全部楼层
小凯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后面括号里东西的类型和后面常量类型是否相同
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-29 18:08:33 | 显示全部楼层
小凯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
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-31 09:07:39 | 显示全部楼层
小凯2013 发表于 2022-12-25 19:22
源码【不许侵权】:

你这 readme 也不是 markdown 啊
而且条款不是 License 吗,你见过那个条款不让私自编译的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2023-1-1 15:33:13 | 显示全部楼层
liuzhengyuan 发表于 2022-12-31 09:07
你这 readme 也不是 markdown 啊
而且条款不是 License 吗,你见过那个条款不让私自编译的

今天你就见到了!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 0 反对 1

使用道具 举报

发表于 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;
        }
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-7 21:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表