| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1412 人关注过本帖
标题:这是一个小学数学出题器,但是VS运行时提示代码有错误,有人能帮助解决吗?
只看楼主 加入收藏
YBSyuo
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2022-4-20
结帖率:50%
收藏
已结贴  问题点数:10 回复次数:10 
这是一个小学数学出题器,但是VS运行时提示代码有错误,有人能帮助解决吗?
这是CHATGPT给出的代码。
用的编译器是VS2022,出现的问题包括但不限于:

1:    应输入预处理器指令
2: 应输入标识符
3: 语法错误
4:等等等等 等等等等

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <random>
#include <fstream>
#include <sstream>
#include <cmath>
using namespace std;
// 加法、减法、乘法、除法有预置的数值可选
vector<int> add_list = { 0, 20, 30, 50, 100, 300, 500, 1000 };
vector<int> sub_list = { 10, 20, 30, 50, 100, 300, 500, 1000 };
vector<int> mul_list = { 10, 20, 30, 50, 100, 300, 500, 1000 };
vector<int> div_list = { 10, 20, 30, 50, 100, 300, 500, 1000 };
// 进位加法
vector<string> carry_add(int num) {
    vector<string> res;
    for (int i = 0; i < num; i++) {
        int a = rand() % !n(MISSING)um;
        int b = rand() % !((MISSING)num - a);
        stringstream ss;
        ss << a << " + " << b << " = ";
        res.push_back(ss.str());
    }
    return res;
}
// 退位减法
vector<string> carry_sub(int num) {
    vector<string> res;
    for (int i = 0; i < num; i++) {
        int a = rand() % !n(MISSING)um;
        int b = rand() % !a(MISSING);
        stringstream ss;
        ss << a << " - " << b << " = ";
        res.push_back(ss.str());
    }
    return res;
}
// 随机进位与退位加法和减法
vector<string> random_carry_add_sub(int num) {
    vector<string> res;
    for (int i = 0; i < num; i++) {
        int a = rand() % !n(MISSING)um;
        int b = rand() % !n(MISSING)um;
        int c = rand() % !; (MISSING)
            stringstream ss;
        if (c == 0) {
            ss << a << " + " << b << " = ";
        }
        else {
            ss << a << " - " << b << " = ";
        }
        res.push_back(ss.str());
    }
    return res;
}
// 乘法
vector<string> mul(int num) {
    vector<string> res;
    for (int i = 0; i < num; i++) {
        int a = rand() % !n(MISSING)um;
        int b = rand() % !n(MISSING)um;
        stringstream ss;
        ss << a << " * " << b << " = ";
        res.push_back(ss.str());
    }
    return res;
}
// 除法
vector<string> div(int num) {
    vector<string> res;
    for (int i = 0; i < num; i++) {
        int a = rand() % !n(MISSING)um;
        int b = rand() % !n(MISSING)um;
        int c = a * b;
        stringstream ss;
        ss << c << " / " << b << " = ";
        res.push_back(ss.str());
    }
    return res;
}
// 生成PDF
void generate_pdf(vector<string>& questions) {
    ofstream out("questions.pdf");
    for (auto& q : questions) {
        out << q << endl;
    }
    out.close();
}
// 生成xlsx
void generate_xlsx(vector<string>& questions) {
    ofstream out("questions.xlsx");
    for (auto& q : questions) {
        out << q << endl;
    }
    out.close();
}
// 打印
void print(vector<string>& questions) {
    for (auto& q : questions) {
        cout << q << endl;
    }
}
int main() {
    // 加法
    vector<string> add_questions;
    for (auto& num : add_list) {
        vector<string> tmp = carry_add(num);
        add_questions.insert(add_questions.end(), tmp.begin(), tmp.end());
    }
    // 减法
    vector<string> sub_questions;
    for (auto& num : sub_list) {
        vector<string> tmp = carry_sub(num);
        sub_questions.insert(sub_questions.end(), tmp.begin(), tmp.end());
    }
    // 随机进位与退位加法和减法
    vector<string> random_add_sub_questions;
    for (auto& num : add_list) {
        vector<string> tmp = random_carry_add_sub(num);
        random_add_sub_questions.insert(random_add_sub_questions.end(), tmp.begin(), tmp.end());
    }
    // 乘法
    vector<string> mul_questions;
    for (auto& num : mul_list) {
        vector<string> tmp = mul(num);
        mul_questions.insert(mul_questions.end(), tmp.begin(), tmp.end());
    }
    // 除法
    vector<string> div_questions;
    for (auto& num : div_list) {
        vector<string> tmp = div(num);
        div_questions.insert(div_questions.end(), tmp.begin(), tmp.end());
    }
    // 打乱题目
    random_shuffle(add_questions.begin(), add_questions.end());
    random_shuffle(sub_questions.begin(), sub_questions.end());
    random_shuffle(random_add_sub_questions.begin(), random_add_sub_questions.end());
    random_shuffle(mul_questions.begin(), mul_questions.end());
    random_shuffle(div_questions.begin(), div_questions.end());
    // 合并题目
    vector<string> questions;
    questions.insert(questions.end(), add_questions.begin(), add_questions.end());
    questions.insert(questions.end(), sub_questions.begin(), sub_questions.end());
    questions.insert(questions.end(), random_add_sub_questions.begin(), random_add_sub_questions.end());
    questions.insert(questions.end(), mul_questions.begin(), mul_questions.end());
    questions.insert(questions.end(), div_questions.begin(), div_questions.end());
    // 生成PDF
    generate_pdf(questions);
    // 生成xlsx
    generate_xlsx(questions);
    // 打印
    print(questions);
    return 0;
}
搜索更多相关主题的帖子: string end int vector num 
2023-02-13 21:39
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:10 
你的问题是什么?你不讲出来的话,别人无法改

从 ChatGPT 给出的代码来看,撇明显地错误不谈,逻辑上也不通顺。
vector<string> carry_add(int num) 是产生 num结果在 [0,num-1] 区间的加法。逻辑不同的地方是:数量 与 范围 竟然用了同一个参数;
vector<string> carry_sub(int num) 是产生 num被减数在 [0,num-1] 区间的减法。同上;
vector<string> random_carry_add_sub(int num) 代码缺失,我怀疑应当是 int c = rand() % 2;,但这个函数存在的意义是什么我想不通;
……
函数 generate_pdf 与 generate_xlsx 就有点儿搞笑了,仅仅改了个扩展名。
2023-02-14 08:55
YBSyuo
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2022-4-20
收藏
得分:0 
回复 2楼 rjsp
版主大人都说不通,那这个3.0版本的AI应该是I的有限,暂时不够适用了,还得靠人脑呐!

我确实被这些代码整懵了,感谢版主大人回复,给我脑袋里乱麻给斩断了,不然还得纠结问题出在哪。
——————————————
——————————————
这是另一份功能相同的代码,但有一个报错:  void type6_calc(int num1, int num2, int num3, int num4, int type) {    C1075    “{”: 未找到匹配令牌        196行

似乎差一步就能成功运行,我试错后没找到办法解决,不知道是代码本身逻辑问题,还是编译器问题。

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
// 加法、减法、乘法、除法有预置的数值可选
int add_num[8] = { 10, 20, 30, 50, 100, 300, 500, 1000 };
int sub_num[8] = { 10, 20, 30, 50, 100, 300, 500, 1000 };
int mul_num[8] = { 10, 20, 30, 50, 100, 300, 500, 1000 };
int div_num[8] = { 10, 20, 30, 50, 100, 300, 500, 1000 };
// 题型
int type1[2] = { 1, 2 }; // 两个数计算
int type2[3] = { 1, 2, 3 }; // 三个数连续计算
int type3[4] = { 1, 2, 3, 4 }; // 四个数平衡式
int type4[5] = { 1, 2, 3, 4, 5 }; // 题目填空在算式中间
int type5[6] = { 1, 2, 3, 4, 5, 6 }; // 题目是不等式
int type6[7] = { 1, 2, 3, 4, 5, 6, 7 }; // 题目是符号填空
// 题型1: 两个数计算
void type1_calc(int num1, int num2, int type) {
    int result;
    switch (type) {
    case 1:
        result = num1 + num2;
        cout << num1 << " + " << num2 << " = " << result << endl;
        break;
    case 2:
        result = num1 - num2;
        cout << num1 << " - " << num2 << " = " << result << endl;
        break;
    case 3:
        result = num1 * num2;
        cout << num1 << " * " << num2 << " = " << result << endl;
        break;
    case 4:
        result = num1 / num2;
        cout << num1 << " / " << num2 << " = " << result << endl;
        break;
    }
}
// 题型2: 三个数连续计算
void type2_calc(int num1, int num2, int num3, int type) {
    int result;
    switch (type) {
    case 1:
        result = num1 + num2 + num3;
        cout << num1 << " + " << num2 << " + " << num3 << " = " << result << endl;
        break;
    case 2:
        result = num1 - num2 - num3;
        cout << num1 << " - " << num2 << " - " << num3 << " = " << result << endl;
        break;
    case 3:
        result = num1 * num2 * num3;
        cout << num1 << " * " << num2 << " * " << num3 << " = " << result << endl;
        break;
    case 4:
        result = num1 / num2 / num3;
        cout << num1 << " / " << num2 << " / " << num3 << " = " << result << endl;
        break;
    }
}
// 题型3: 四个数平衡式
void type3_calc(int num1, int num2, int num3, int num4, int type) {
    int result;
    switch (type) {
    case 1:
        result = num1 + num2 - num3 + num4;
        cout << num1 << " + " << num2 << " - " << num3 << " + " << num4 << " = " << result << endl;
        break;
    case 2:
        result = num1 - num2 + num3 - num4;
        cout << num1 << " - " << num2 << " + " << num3 << " - " << num4 << " = " << result << endl;
        break;
    case 3:
        result = num1 * num2 / num3 * num4;
        cout << num1 << " * " << num2 << " / " << num3 << " * " << num4 << " = " << result << endl;
        break;
    case 4:
        result = num1 / num2 * num3 / num4;
        cout << num1 << " / " << num2 << " * " << num3 << " / " << num4 << " = " << result << endl;
        break;
    }
}
// 题型4: 题目填空在算式中间
void type4_calc(int num1, int num2, int num3, int num4, int type) {
    int result;
    switch (type) {
    case 1:
        result = num1 + num2;
        cout << num1 << " + ____ = " << result << endl;
        break;
    case 2:
        result = num1 - num2;
        cout << num1 << " - ____ = " << result << endl;
        break;
    case 3:
        result = num1 * num2;
        cout << num1 << " * ____ = " << result << endl;
        break;
    case 4:
        result = num1 / num2;
        cout << num1 << " / ____ = " << result << endl;
        break;
    case 5:
        result = num1 + num2 + num3;
        cout << num1 << " + ____ + " << num3 << " = " << result << endl;
        break;
    case 6:
        result = num1 - num2 - num3;
        cout << num1 << " - ____ - " << num3 << " = " << result << endl;
        break;
    case 7:
        result = num1 * num2 * num3;
        cout << num1 << " * ____ * " << num3 << " = " << result << endl;
        break;
    case 8:
        result = num1 / num2 / num3;
        cout << num1 << " / ____ / " << num3 << " = " << result << endl;
        break;
    case 9:
        result = num1 + num2 - num3 + num4;
        cout << num1 << " + ____ - " << num3 << " + " << num4 << " = " << result << endl;
        break;
    case 10:
        result = num1 - num2 + num3 - num4;
        cout << num1 << " - ____ + " << num3 << " - " << num4 << " = " << result << endl;
        break;
    case 11:
        result = num1 * num2 / num3 * num4;
        cout << num1 << " * ____ / " << num3 << " * " << num4 << " = " << result << endl;
        break;
    case 12:
        result = num1 / num2 * num3 / num4;
        cout << num1 << " / ____ * " << num3 << " / " << num4 << " = " << result << endl;
        break;
    }
}
// 题型5: 题目是不等式
void type5_calc(int num1, int num2, int num3, int num4, int type) {
    int result;
    switch (type) {
    case 1:
        result = num1 + num2;
        cout << num1 << " + ____ > " << result << endl;
        break;
    case 2:
        result = num1 - num2;
        cout << num1 << " - ____ < " << result << endl;
        break;
    case 3:
        result = num1 * num2;
        cout << num1 << " * ____ > " << result << endl;
        break;
    case 4:
        result = num1 / num2;
        cout << num1 << " / ____ < " << result << endl;
        break;
    case 5:
        result = num1 + num2 + num3;
        cout << num1 << " + ____ + " << num3 << " > " << result << endl;
        break;
    case 6:
        result = num1 - num2 - num3;
        cout << num1 << " - ____ - " << num3 << " < " << result << endl;
        break;
    case 7:
        result = num1 * num2 * num3;
        cout << num1 << " * ____ * " << num3 << " > " << result << endl;
        break;
    case 8:
        result = num1 / num2 / num3;
        cout << num1 << " / ____ / " << num3 << " < " << result << endl;
        break;
    case 9:
        result = num1 + num2 - num3 + num4;
        cout << num1 << " + ____ - " << num3 << " + " << num4 << " > " << result << endl;
        break;
    case 10:
        result = num1 - num2 + num3 - num4;
        cout << num1 << " - ____ + " << num3 << " - " << num4 << " < " << result << endl;
        break;
    case 11:
        result = num1 * num2 / num3 * num4;
        cout << num1 << " * ____ / " << num3 << " * " << num4 << " > " << result << endl;
        break;
    case 12:
        result = num1 / num2 * num3 / num4;
        cout << num1 << " / ____ * " << num3 << " / " << num4 << " < " << result << endl;
        break;
    }
}
// 题型6: 题目是符号填空
void type6_calc(int num1, int num2, int num3, int num4, int type) {
    int result;
    switch (type) {
    case 1:
        result = num1 + num2;
        cout << num1 << " ____ " << num2 << " = " << result << endl;
        break;
    case 2:
        result = num1 - num2;
        cout << num1 << " ____ " << num2 << " = " << result << endl;
        break;
    case 3:
        result = num1 * num2;
        cout << num1 << " ____ " << num2 << " = " << result << endl;
        break;
    case 4:
        result = num1 / num2;
        cout << num1 << " ____ " << num2 << " = " << result << endl;
        break;
    case 5:
        result = num1 + num2 + num3;
        cout << num1 << " ____ " << num2 << " + " << num3 << " = " << result << endl;
        break;
    case 6:
        result = num1 - num2 - num3;
        cout << num1 << " ____ " << num2 << " - " << num3 << " = " << result << endl;
        break;
    case 7:
        result = num1 * num2 * num3;
        cout << num1 << " ____ " << num2 << " * " << num3 << " = " << result << endl;
        break;
    case 8:
        result = num1 / num2 / num3;
        cout << num1 << " ____ " << num2 << " / " << num3 << " = " << result << endl;
        break;
    case 9:
        result = num1 + num2 - num3 + num4;
        cout << num1 << " ____ " << num2 << " - " << num3 << " + " << num4 << " = " << result << endl;
        break;
    case 10:
        result = num1 - num2 + num3 - num4;
        cout << num1 << " ____ " << num2 << " + " << num3 << " - " << num4 << " = " << result << endl
            ; ;
    };
2023-02-14 09:49
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:0 
你两段代码的功能不相同,那你说一说你具体的需求是什么嘛!
我根据你第一段代码,连蒙带猜:
程序代码:
#include <iostream>
#include <type_traits>
#include <random>
#include <algorithm>
#include <vector>

class foo
{
public:
    foo( std::mt19937& random_gen ) : random_gen_{random_gen}
    {
    }

    struct exp
    {
        unsigned a, b;
        char opt;
    };

    using operation_data_type = std::remove_cvref_t< decltype(exp::a) >;

    exp gen_plus( operation_data_type n ) const
    {
        operation_data_type a = random( n );
        operation_data_type b = random( n-a );
        return { a, b, '+' };
    }

    exp gen_minus( operation_data_type n ) const
    {
        operation_data_type a = random( n );
        operation_data_type b = random( a );
        return { a, b, '-' };
    }

    exp gen_multiplies( operation_data_type n ) const
    {
        operation_data_type a = random( n );
        operation_data_type b = random( n );
        return { a, b, '*' };
    }

    exp gen_divides( operation_data_type n ) const
    {
        operation_data_type c = random( n );
        operation_data_type b = 1 + random(n-1);
        return { c*b, b, '/' };
    }

private:
    operation_data_type random( operation_data_type max ) const // 产生范围 [0,max] 区间的随机数
    {
        return std::uniform_int_distribution<operation_data_type>{0,max}( random_gen_ );
    }

private:
    std::mt19937& random_gen_;
};

int main( void )
{
    // 产生 10条百以内的加法、减法、乘法、除法(对于加减除法而言,是指结果在百以内;对于乘法而言,是指操作数在百以内)
    const size_t range = 100;
    const size_t num_plus = 10;
    const size_t num_minus = 10;
    const size_t num_multiplies = 10;
    const size_t num_divides = 10;

    std::vector<foo::exp> exps;
    exps.reserve( num_plus+num_minus+num_multiplies+num_divides );

    std::mt19937 random_gen{ std::random_device{}() };
    foo f( random_gen );
    for( size_t i=0; i!=num_plus; ++i )
        exps.push_back( f.gen_plus(range) );
    for( size_t i=0; i!=num_minus; ++i )
        exps.push_back( f.gen_minus(range) );
    for( size_t i=0; i!=num_multiplies; ++i )
        exps.push_back( f.gen_multiplies(range) );
    for( size_t i=0; i!=num_divides; ++i )
        exps.push_back( f.gen_divides(range) );
    std::shuffle( std::begin(exps), std::end(exps), random_gen ); // 打乱题目

    // 输出
    for( const auto& e : exps )
        std::cout << e.a << ' ' << e.opt << ' ' << e.b << " =\n";
}
2023-02-14 11:32
YBSyuo
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2022-4-20
收藏
得分:0 
回复 4楼 rjsp
就是一个可以出算式题目的软件
图片附件: 游客没有浏览图片的权限,请 登录注册


小学生用的
2023-02-14 12:41
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:0 
你这还是几乎什么都没说?

第一操作数的值范围是多少?
第二操作数的值范围是多少?
结果的值范围是多少?
操作符是不是只有加、减两种?
题目必须输出到xlsx这种私有格式吗?如果回答是,那又是一堆的问题...(略)
输出题目一定是10行3列吗?
……
2023-02-15 08:32
YBSyuo
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2022-4-20
收藏
得分:0 
回复 6楼 rjsp
斑竹大人,别说您勒,我自己都晕头转向了。已经决定暂时放弃这个,改用其它工具凑合用用。无论如何给版主添麻烦了,花了好些时间在这些代码上,真是对版主这样有着极度的编程热情的人抱有崇高的敬意,希望有一天我也能将代码化入血液里……小白敬上!!!
2023-02-15 17:15
pvm2000
Rank: 6Rank: 6
等 级:贵宾
威 望:24
帖 子:180
专家分:312
注 册:2022-12-22
收藏
得分:0 
拿这个代码找Chatgpt修改
2023-02-16 07:57
pvm2000
Rank: 6Rank: 6
等 级:贵宾
威 望:24
帖 子:180
专家分:312
注 册:2022-12-22
收藏
得分:0 
你以为ChatGPT是编程高手,其实它东抄一段西抄几句
2023-02-16 07:59
YBSyuo
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2022-4-20
收藏
得分:0 
回复 8楼 pvm2000
改了还错,啊哈哈哈哈……
2023-02-17 18:47
快速回复:这是一个小学数学出题器,但是VS运行时提示代码有错误,有人能帮助解决 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027629 second(s), 11 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved