C++编程题 请高手帮忙!!把程序按要求补完整!!
/*实训1:四则运算测试软件
由计算机出10道题(1位数加减乘除题,每次考试有各类题目,每道题用随机数产生),学生答题。
答题完毕后,打印出各题目及正确答案、学生答案、正确与否,统计正确与错误个数。
打印出测试实际所用的时间。
要求:不能有重题;减法保证不出负数;除数保证能够除尽。
*/
#include <stdio.h>
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#define N 10
class expression
{
int operand1;//操作数1
int operand2;//操作数2
char operater;//运算符
int right_result;//正确结果
int calculate_result;//答题结果
int mark;//计算结果的正确标记(1 正确 0 错误)
public:
int get_operand1(){return operand1;}
int get_operand2(){return operand2;}
char get_operater(){return operater;}
int get_calculate_result(){return calculate_result;}
int get_right_result(){return right_result;}
int get_mark(){ return mark;}
void set_calculate_result(int x){ calculate_result=x; }
int set();//产生一个运算式
int judge();//判断计算结果是否正确,设置mark,返回mark
void print();//只打印运算式和等号
};
int expression::set()//产生一个运算式,产生的运算式正确返回1,产生的运算式错误返回0
{
int oper,t,flag;//flag如果为1则产生的运算式正确,如果为0则产生的运算式错误
operand1=1+rand()%9; //产生1-9数字
operand2=1+rand()%9;
oper=1+rand()%4; //产生1-4数字
switch(oper)
{
case 1://加
printf("两个数的加法运算:");
calculate_result=operand1+operand2;
break;
case 2://减
printf("两个数的减法运算:");
calculate_result=operand1-operand2;
break;
case 3://乘
printf("两个数的乘法运算:");
calculate_result=operand1*operand2;
break;
case 4://除
printf("两个数的除法运算:");
calculate_result=operand1/operand2;
break;
default:
flag=0;//产生的运算式错误,运算符不在 + -* /之内
}
return flag;
}
void expression::print()//只打印运算式和等号
{
if(oper==1)
printf("%d+%d=%d",operand1,operand2,calculate);
else if(oper==2)
printf("%d-%d=%d",operand1,operand2,calculate);
else if(oper==3)
printf("%d*%d=%d",operand1,operand2,calculate);
else
printf("%d/%d=%d",operand1,operan2,calculate);
}
int expression::judge()//判断计算结果是否正确,设置mark,返回mark
{
}
void main()
{
} 经测试后发现,你上面 随机数产生函数 似乎有规律 ,每次运行时产生的都是相同的一组数,第一次总是6和9。当要满足你所说的要求,不一定会产生那样的随机数。比如:如果产生的减法运算结果小于0,就不出题,继续让程序产生减法运算,但是我试了一下。我让程序的前三道题都是减法,可以实现;但是要在此基础上让减法的运算结果为零,编程代码可以实现,但就是产生不了,结果是程序陷入死循环中,我想这是由于随机数的产生有问题,这方面我没仔细研究过。下面是我的修改,基本实现了你的要求,可以直接运行,你可以参考,并作出改进。
#include <stdio.h>
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#define N 10
class expression
{
int operand1;//操作数1
int operand2;//操作数2
char operater;//运算符
float right_result;//正确结果
float calculate_result;//答题结果
int mark;//计算结果的正确标记(1 正确 0 错误)
int flag;
int nCha;
public:
expression();
int get_flag();
int get_operand1(){return operand1;}
int get_operand2(){return operand2;}
char get_operater(){return operater;}
float get_calculate_result(){return calculate_result;}
float get_right_result(){return right_result;}
int get_mark(){ return mark;}
void set_calculate_result(float x){ calculate_result=x; }
int set();//产生一个运算式
void judge();//判断计算结果是否正确,设置mark,返回mark
void print();//只打印运算式和等号
};
expression::expression()
{
nCha=0;
}
int expression::get_flag()
{
return flag;
}
int expression::set()//产生一个运算式,产生的运算式正确返回1,产生的运算式错误返回0
{
//int oper,t,flag;//flag如果为1则产生的运算式正确,如果为0则产生的运算式错误
operand1=1+rand()%9; //产生1-9数字
operand2=1+rand()%9;
aa: int oper=1+rand()%4; //产生1-4数字
/* if(nCha<3) //此段代码是让前三个运算式都是减法
{
if(oper==2)
nCha++;
else
goto aa;
}
*/
switch(oper)
{
case 1://加
printf("两个数的加法运算:");
right_result=(float)(operand1+operand2);
operater='+';
flag=1;
break;
case 2://减
right_result=float(operand1-operand2);
if(right_result<0) //如果减法运算小于0,则对调操作数。如果方案改为重新产生运算式,
//就会产生死循环
{
int x;
x=operand1;
operand1=operand2;
operand2=x;
right_result=-right_result;
}
printf("两个数的减法运算:");
operater='-';
flag=1;
break;
case 3://乘
printf("两个数的乘法运算:");
right_result=float(operand1*operand2);
operater='*';
flag=1;
break;
case 4://除
printf("两个数的除法运算:");
right_result=operand1/(float)operand2;
operater='/';
flag=1;
break;
default:
flag=0;//产生的运算式错误,运算符不在 + -* /之内
}
return flag;
}
void expression::print()//只打印运算式和等号
{
/*if(oper==1)
printf("%d %c %d =",operand1,calculate,operand2);
else if(oper==2)
printf("%d %c %d =",operand1,calculate,operand2);
else if(oper==3)
printf("%d %c %d =",operand1,calculate,operand2);
else
printf("%d %c %d =",operand1,calculate,operand2);
*/
printf("%d %c %d =",operand1,operater,operand2);
}
void expression::judge()//判断计算结果是否正确,设置mark,返回mark
{
cin>>calculate_result;
if(operater=='/')//除法运算有时不能精确,在此保证千分之一的准确性就行
{
float a=get_right_result()-calculate_result;
if(-0.001<a&&a<0.001)
mark=1;
else
mark=0;
}
else
{
if(get_right_result()==calculate_result)
mark=1;
else
mark=0;
}
}
void main()
{
int nError=0,nRight=0;//计录结果数目
expression ex;
for(int i=0;i<N;i++)
{
printf("第%d道题:",i);
if(!ex.set())
{
printf("运算式产生错误,系统退出。");
break;
}
ex.print();
ex.judge();
if(ex.get_mark())
{
printf("回答正确!");
printf("\n正确结果是%f",ex.get_right_result());
printf("\n你的回答是%f",ex.get_calculate_result());
nRight+=1;
}
else
{
printf("\n回答错误!");
printf("\n正确结果是%f",ex.get_right_result());
printf("\n你的回答是%f",ex.get_calculate_result());
nError+=1;
}
printf("\n\n");
}
if(ex.get_flag())
{
printf("\n\n正确答题数目是%d个",nRight);
printf("\n错误答题数目是%d个\n",nError);
}
}
[[it] 本帖最后由 xunxunmimi 于 2008-6-15 15:56 编辑 [/it]]
[[it] 本帖最后由 xunxunmimi 于 2008-6-15 15:59 编辑 [/it]]
页:
[1]
