C++如何从加法乘法练习模块返回多个统计值到main并展示汇总数据
实现方案
以下是针对需求的具体修改步骤和完整可运行代码:
- 调整练习函数的参数设计:将
sums()、products()改为接收3个无符号整型引用参数,分别对应正确题数、错误题数、总题数,函数内部直接修改传入的外部变量,无需额外返回值即可实现统计数据回传 - 主函数中新增持久化统计变量:分别定义加法、乘法练习的三类统计变量,初始值设为0,每次调用对应练习函数时传入变量引用,实现多轮练习的数据累加
- 修复原有逻辑bug:原代码菜单中3对应退出,主循环判断条件误写为
option !=6,同步修正为option !=3;同时修正菜单中"Pratice"的拼写错误 - 新增退出前统计展示逻辑:用户选择退出后,分别打印加法、乘法模块的统计数据,再计算汇总数据,按表格格式输出
完整修改后代码
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <string> #include <iostream> #include <cmath> #include <cstdlib> #include <iomanip> using namespace std; // 函数原型修改,新增引用参数用于回传统计数据 int menu(); void sums(unsigned int &correct, unsigned int &wrong, unsigned int &total); void products(unsigned int &correct, unsigned int &wrong, unsigned int &total); int main() { srand(time(0)); int option; // 定义加法、乘法练习的统计变量,初始值为0 unsigned int add_correct = 0, add_wrong = 0, add_total = 0; unsigned int mul_correct = 0, mul_wrong = 0, mul_total = 0; do { option = menu(); switch (option) { case 1: { sums(add_correct, add_wrong, add_total); break; } case 2: { products(mul_correct, mul_wrong, mul_total); break; } case 3: { cout << endl << "正在退出程序,统计数据如下:" << endl; // 打印统计表格 cout << "==================================================================" << endl; cout << left << setw(16) << "练习类型" << setw(12) << "正确题数" << setw(12) << "错误题数" << setw(12) << "总题数" << setw(10) << "正确率" << endl; double add_rate = add_total == 0 ? 0 : (double)add_correct / add_total * 100; cout << left << setw(16) << "加法练习" << setw(12) << add_correct << setw(12) << add_wrong << setw(12) << add_total << fixed << setprecision(2) << add_rate << "%" << endl; double mul_rate = mul_total == 0 ? 0 : (double)mul_correct / mul_total * 100; cout << left << setw(16) << "乘法练习" << setw(12) << mul_correct << setw(12) << mul_wrong << setw(12) << mul_total << fixed << setprecision(2) << mul_rate << "%" << endl; cout << "------------------------------------------------------------------" << endl; unsigned int total_correct = add_correct + mul_correct; unsigned int total_wrong = add_wrong + mul_wrong; unsigned int total_all = add_total + mul_total; double total_rate = total_all == 0 ? 0 : (double)total_correct / total_all * 100; cout << left << setw(16) << "总计" << setw(12) << total_correct << setw(12) << total_wrong << setw(12) << total_all << fixed << setprecision(2) << total_rate << "%" << endl; cout << "==================================================================" << endl; break; } default: cout << "无效选项,请重新选择" << endl; } } while (option != 3); return 0; } int menu() { cout << endl << "请选择操作选项:" << endl; cout << "1) 加法练习" << endl; cout << "2) 乘法练习" << endl; cout << "3) 退出程序" << endl; int option; cin >> option; return option; } void sums(unsigned int &quantity_correct_answers, unsigned int &quantity_wrong_answers, unsigned int &quantity_total_questions) { string keepgoing; do { const int minValue = 10; const int maxValue = 99; int y = (rand() % (maxValue - minValue + 1)) + minValue; int x = (rand() % (maxValue - minValue + 1)) + minValue; cout << "请问 " << x << " + " << y << " = ?" << endl; int answer; cin >> answer; if (answer == (x + y)) { cout << "太棒了!回答正确!" << endl; ++quantity_correct_answers; ++quantity_total_questions; } else { cout << "很遗憾,回答错误。" << endl; ++quantity_wrong_answers; ++quantity_total_questions; } cout << "当前场次:正确:" << quantity_correct_answers << " 错误:" << quantity_wrong_answers << " 总题数:" << quantity_total_questions << endl; cout << "是否继续练习?输入y继续,输入n返回菜单:" << endl; cin >> keepgoing; } while (keepgoing == "y"); } void products(unsigned int &quantity_correct_answers, unsigned int &quantity_wrong_answers, unsigned int &quantity_total_questions) { string keepgoing; do { const int minValueOne = 0; const int maxValueOne = 9; const int minValueTwo = 10; const int maxValueTwo = 99; int y = (rand() % (maxValueOne - minValueOne + 1)) + minValueOne; int x = (rand() % (maxValueTwo - minValueTwo + 1)) + minValueTwo; cout << "请问 " << x << " × " << y << " = ?" << endl; int answer; cin >> answer; if (answer == (x * y)) { cout << "太棒了!回答正确!" << endl; ++quantity_correct_answers; ++quantity_total_questions; } else { cout << "很遗憾,回答错误。" << endl; ++quantity_wrong_answers; ++quantity_total_questions; } cout << "当前场次:正确:" << quantity_correct_answers << " 错误:" << quantity_wrong_answers << " 总题数:" << quantity_total_questions << endl; cout << "是否继续练习?输入y继续,输入n返回菜单:" << endl; cin >> keepgoing; } while (keepgoing == "y"); }
运行效果示例
用户完成多轮练习选择退出后,会输出类似以下格式的统计表格:
================================================================== 练习类型 正确题数 错误题数 总题数 正确率 加法练习 5 1 6 83.33% 乘法练习 3 2 5 60.00% ------------------------------------------------------------------ 总计 8 3 11 72.73% ==================================================================
内容的提问来源于stack exchange,提问作者user17312577
相关产品推荐
相关产品推荐

