个人作业1——四则运算题目生成程序(基于控制台)

一、题目描述:

  1. 使用 -n 参数控制生成题目的个数,例如
    Myapp.exe -n 10 -o Exercise.txt
    将生成10个题目。

  2. 使用 -r 参数控制题目中数值(自然数、真分数和真分数分母)的范围,例如
    Myapp.exe -r 10
    将生成10以内(不包括10)的四则运算题目。该参数可以设置为1或其他自然数。该参数必须给定,否则程序报错并给出帮助信息。

  3. 生成的题目中如果存在形如e1 ÷ e2的子表达式,那么其结果应是真分数。

  4. 每道题目中出现的运算符个数不超过3个。

  5. 程序一次运行生成的题目不能重复,即任何两道题目不能通过有限次交换+和×左右的算术表达式变换为同一道题目。例如,23 + 45 = 和45 + 23 = 是重复的题目,6 × 8 = 和8 × 6 = 也是重复的题目。3+(2+1)和1+2+3这两个题目是重复的,由于+是左结合的,1+2+3等价于(1+2)+3,也就是3+(1+2),也就是3+(2+1)。但是1+2+3和3+2+1是不重复的两道题,因为1+2+3等价于(1+2)+3,而3+2+1等价于(3+2)+1,它们之间不能通过有限次交换变成同一个题目。
    生成的题目存入执行程序的当前目录下的Exercises.txt文件,格式如下:

    1. 四则运算题目1
    2. 四则运算题目2
      ……

    其中真分数在输入输出时采用如下格式,真分数五分之三表示为3/5,真分数二又八分之三表示为2’3/8。

  6. 在生成题目的同时,计算出所有题目的答案,并存入执行程序的当前目录下的Answers.txt文件,格式如下:
    1. 答案1
    2. 答案2

特别的,真分数的运算如下例所示:1/6 + 1/8 = 7/24。
7. 程序应能支持一万道题目的生成。
8. 程序支持对给定的题目文件和答案文件,判定答案中的对错并进行数量统计,并会输出所有题目中重复的题目,输入参数如下:
Myapp.exe -e .txt -a .txt -o Grade.txt
统计结果输出到文件Grade.txt,格式如下:

Correct: 5 (1, 3, 5, 7, 9)
Wrong: 5 (2, 4, 6, 8, 10)
Repeat:2
RepeatDetail:
(1) 2,45+32 Repeat 3,32+45
(2) 5,3+(2+1) Repeat 7,1+2+3

解释:
Correct: 5 ----5道题目正确,正确的题号 1,3,5,7,9
Wrong:5 -----5道题目错误,错误的题号 2,4,6,8,10
Repeat:2 2---组题目重复
(1) 第一组 题号2,题目 45+32 与题号3的题目重复,题号3为 32+45
(2)第二组 题号5,题目 3+(2+1) 与题号7的题目重复,题号7为 1+2+3

其中“:”后面的数字5表示对/错的题目的数量,括号内的是对/错题目的编号。为简单起见,假设输入的题目都是按照顺序编号的符合规范的题目。

二、功能设计
主要实现以下几个功能:
1.随机生成整数或者分数,随机生成符号,括号,进行拼接,形成等式
2.对所形成的等式进行转换,从中缀表达式转换为后缀表达式
3.对后缀表达式进行计算
4.对等式进行查重
5.对提供的答案文件进行判断,找出错误和正确的题号和数量
6.对上面功能所产生的题目和答案进行写入文件

三、设计实现

主要类有:
Expression:操作数与操作符基类
NumberExpression:操作数类
AdditionExpression:加法类
SubtractionExpression:减法类
MultiplicationExpression:乘法类
DivisionExpression:除法类
Generator:生成题目和答案

大致思路如上图


前一阵子在研究Ruby早期的源代码,看到了基于表达式的四则运算实现方式(不过现在由于速度问题已经改为基于字节码),于是就想尝试看看。
实现了无分数的版本,加入分数之后出了一些问题,表达式计算出来的结果经常出错,还在想办法当中。查重还未实现。还有由于使用的是基于char的vector存储,有的时候会碰到计算出如40的数字显示成ascll的'(',这个问题也未解决。

四、代码说明
主要代码有以下:
1.加法计算(其余类似)


Expression* add_fraction(Expression* a,Expression* b){
		if(a->isFraction()&&b->isFraction()){
			int fraction_int = a->get_v_fraction_int()+b->get_v_fraction_int();
			int common_multiple = find_common_multiple(a->get_v_actual_denom(),b->get_v_actual_denom());
			a->set_v_actual(b->get_v_actual_denom()*a->get_v_actual());
			b->set_v_actual(a->get_v_actual_denom()*b->get_v_actual());
			int temp_actual = a->get_v_actual()+b->get_v_actual()+fraction_int*common_multiple;
			return (new NumberExpression(temp_actual,common_multiple));
		}
		else if(!a->isFraction()&&!b->isFraction()){
			return (new NumberExpression(a->get_v_actual()+b->get_v_actual()));
		}else{
			if(a->isFraction()){
				if(a->get_v_actual_denom()!=0){
					a->set_v_actual(a->get_v_actual_denom()*a->get_v_fraction_int()+a->get_v_actual());
				}
				b->set_v_actual(a->get_v_actual_denom()*b->get_v_actual());
				b->set_v_actual_denom(a->get_v_actual_denom());
				return (new NumberExpression(a->get_v_actual()+b->get_v_actual(),a->get_v_actual_denom()));
			}else{
				if(b->get_v_actual_denom()!=0){
					b->set_v_actual(b->get_v_actual_denom()*b->get_v_fraction_int()+b->get_v_actual());
				}
				a->set_v_actual(b->get_v_actual_denom()*a->get_v_actual());
				a->set_v_actual_denom(b->get_v_actual_denom());
				return (new NumberExpression(a->get_v_actual()+b->get_v_actual(),a->get_v_actual_denom()));
			}
		}
	}

2.生成算式


void generate(int begin,int end){
		int num = 0;
		cout<<"请输入要生成的题目数量"<>num;
		Expression* ea;
		Expression* eb;
		Expression* ex;
		Expression* ey;
		float operator_choice = 0.0;
		float fraction_choice = 0.0;
		ofstream out_question("question.txt"),out_answer("answer.txt");  
		for(int i=0;ipush_char('(');
					ea = new AdditionExpression(new NumberExpression(rand_int_range(begin,end)),new NumberExpression(rand_int_range(begin,end)));
					ea->push_char(')');
				}else if(operator_choice<0.5){
					ea->push_char('(');
					ea = new SubtractionExpression(new NumberExpression(rand_int_range(begin,end)),new NumberExpression(rand_int_range(begin,end)));
					ea->push_char(')');
				}else if(operator_choice<0.75){
					ea = new MultiplicationExpression(new NumberExpression(rand_int_range(begin,end)),new NumberExpression(rand_int_range(begin,end)));
				}else{
					ea = new DivisionExpression(new NumberExpression(rand_int_range(begin,end)),new NumberExpression(rand_int_range(begin,end)));
				}
			}
			else{
				if(operator_choice<0.25){
					ea->push_char('(');
					ea = new AdditionExpression(new NumberExpression(rand_int_range(begin,end),rand_int_range(begin,end)),new NumberExpression(rand_int_range(begin,end)));
					ea->push_char(')');
				}
				else if(operator_choice<0.5){
					ea->push_char('(');
					ea = new SubtractionExpression(new NumberExpression(rand_int_range(begin,end)),new NumberExpression(rand_int_range(begin,end),rand_int_range(begin,end)));
					ea->push_char(')');
				}else if(operator_choice<0.75){
					ea = new MultiplicationExpression(new NumberExpression(rand_int_range(begin,end),rand_int_range(begin,end)),new NumberExpression(rand_int_range(begin,end)));
				}else{
					ea = new DivisionExpression(new NumberExpression(rand_int_range(begin,end)),new NumberExpression(rand_int_range(begin,end),rand_int_range(begin,end)));
				}
			}
			ex = ea->get_value_o();
			operator_choice = rand_float();
			fraction_choice = rand_float();
			Expression* t_1 = new NumberExpression(rand_int_range(begin,end));
			Expression* t_2 = new NumberExpression(rand_int_range(begin,end),rand_int_range(begin,end));
			if(fraction_choice<0.75){
				if(operator_choice<0.25){
					eb->push_char('(');
					eb = new AdditionExpression(ea,t_1);
					ey = new AdditionExpression(ex,t_1);
					eb->push_char(')');
				}
				else if(operator_choice<0.5){
					eb->push_char('(');
					eb = new SubtractionExpression(ea,t_1);
					ey = new SubtractionExpression(ex,t_1);
					eb->push_char(')');
				}else if(operator_choice<0.75){
					eb = new MultiplicationExpression(ea,t_1);
					ey = new MultiplicationExpression(ex,t_1);
				}else{
					eb = new DivisionExpression(ea,t_1);
					ey = new DivisionExpression(ex,t_1);
				}
			}else{
				if(operator_choice<0.25){
					eb->push_char('(');
					eb = new AdditionExpression(ea,t_2);
					ey = new AdditionExpression(ex,t_2);
					eb->push_char(')');
				}
				else if(operator_choice<0.5){
					eb->push_char('(');
					eb = new SubtractionExpression(ea,t_2);
					ey = new SubtractionExpression(ex,t_2);
					eb->push_char(')');
				}else if(operator_choice<0.75){
					eb = new MultiplicationExpression(ea,t_2);
					ey = new MultiplicationExpression(ex,t_2);
				}else{
					eb = new DivisionExpression(ea,t_2);
					ey = new DivisionExpression(ex,t_2);
				}
			}
		        vector temp = eb->evaluate();
			for(char i : temp){
			if(i=='+'||i=='-'||i=='*'||i=='\\'||i=='('||i==')'||i=='^'){
				out_question<

3.算式和答案生成存放在本地文件查看(可以看出有些答案有问题)

六、PSD统计:

七、代码地址
https://gitee.com/ClothoSword/Arithmetic

posted @ 2018-03-31 22:03  ClothoSword  阅读(179)  评论(0编辑  收藏  举报