zoukankan      html  css  js  c++  java
  • 软件工程课后作业一之30道随机四则运算程序3

    一、题目要求:

           用户输入答案,程序自动判定对错,最后给出总共对/错的数量。

    二、设计思想:

           在原程序基础上增加四则运算结果并用result保存起来,之后用户输入一个结果并与之比较,相等则显示正确,答错则显示正确答案。

    三、代码:

    #include<iostream.h>
    #include<stdlib.h>
    #include<time.h>
    int answer=0;
    int score=0;
    int result=0;
    void display(int number,int mul,int rage,int neg)//先自定义输出函数
    {
    	int first[1000],second[1000],opt[1000];//分别代表第一个数、第二数和运算符
    	for(int i=0;i<number;i++)//随机两个生成运算操作数
    	{  
    		int m=1;//避免题目重复并初始化
    		first[i]=rand()%rage;//随机产生第一个数
    		second[i]=rand()%rage;//随机产生第二个数
    		if(mul==0)//没有乘除法
    		{	
    			opt[i]=rand()%2;//随机生成0~1的数字,分别表示加减
    		}
    		if(mul==1)
    		{
    			opt[i]=rand()%4;//随机生成0~3的数字,分别表示加减乘除
    		}
    		for(int j=0;j<i;j++)
    		{
    			if(first[j]==first[i]&&second[j]==second[i]&&opt[j]==opt[i])//比较新产生的算式与原来的是否重复
    			{
    				i=i-1;
    				m=0;
    			}
    		}
    		while(m)//若算式都不重复则输出
    		{
    			switch(opt[i])
    			{
    			case 0:
    				cout<<first[i]<<"+"<<second[i]<<"=";result=first[i]+second[i];break;
    				
    			case 1:
    				if(neg==0)//0表示减法结果不允许出现负数,1表示允许出现负数
    				{
    					int temp;//中间变量
    					if(first[i]<second[i])
    					{
    						temp=first[i];
    						first[i]=second[i];
    						second[i]=temp;
    					}
    					cout<<first[i]<<"-"<<second[i]<<"=";
    					result=first[i]-second[i];
    					break;
    				}
    				else
    				{
    					cout<<first[i]<<"-"<<second[i]<<"=";
    					result=first[i]-second[i];
    					break;
    				}
    			case 2:
    				cout<<first[i]<<"*"<<second[i]<<"=";
    				result=first[i]*second[i];
    				break;
    			case 3:
    				if(second[i]==0)//避免分母为零的处理
    				{   
    					i=i-1;
    					break;
    				}
    				else
    				{
    							cout<<first[i]<<"/"<<second[i]<<"=";
    							result=first[i]/second[i];
    							break;
    				}
    				break;
    			}
    			cin>>answer;
    			if(answer==result)
    			{
    				score+=1;
    				cout<<"right"<<endl;
    				break;
    			}
    			else
    			{
    				cout<<"wrong!the answer is:"<<result<<endl;
    				break;
    			}
    		}
    	}
    	cout<<"你答对的题数是:"<<score<<endl;
    }
    void main()
    {
    	srand((unsigned) time(NULL));//初始化随机数发生器,使得每次运行生成的随机数不同
    	int number;//定制题目数量
    	int mul;//是否需要乘除法
    	int rage;//控制数值范围
    	int neg;//控制减法结果是否允许有负数
    	int remainder;//控制除法是否允许有余数
    	cout<<"请输入要打印的题目数量:";
    	cin>>number;
    	if(number>0)
    	{
    		cout<<endl;
    		cout<<"请输入正整数的数值范围(即最大数):";
    		cin>>rage;
    		cout<<endl;
    		cout<<"是否有乘除法(0表示没有;1表示有):";
    		cin>>mul;
    		cout<<endl;
    		cout<<"减法有无负数(0表示没有;1表示有):";
    		cin>>neg;
    		cout<<endl;
    	}
    	else
    	{
    		cout<<"输入错误,请重新输入!"<<endl;
            cout<<"请输入要打印的题目数量:";
    		cin>>number;
    		cout<<endl;
            cout<<"请输入正整数的数值范围(即最大数):";
    		cin>>rage;
    		cout<<endl;
    		cout<<"是否有乘除法(0表示没有;1表示有):";
    		cin>>mul;
    		cout<<endl;
    		cout<<"减法有无负数(0表示没有;1表示有):";
    		cin>>neg;
    		cout<<endl;
    	}
    	display(number,mul,rage,neg);//调用函数
    }
    

      四、测试及截图:

    五、实验总结:

    实验实现了能自动回答用户输入答案的正误,但是当涉及到有余数的四则运算时有些缺陷,其他的方面基本实现。

    六、三个日志表格:

  • 相关阅读:
    反转链表 16
    CodeForces 701A Cards
    hdu 1087 Super Jumping! Jumping! Jumping!(动态规划)
    hdu 1241 Oil Deposits(水一发,自我的DFS)
    CodeForces 703B(容斥定理)
    poj 1067 取石子游戏(威佐夫博奕(Wythoff Game))
    ACM 马拦过河卒(动态规划)
    hdu 1005 Number Sequence
    51nod 1170 1770 数数字(数学技巧)
    hdu 2160 母猪的故事(睡前随机水一发)(斐波那契数列)
  • 原文地址:https://www.cnblogs.com/2015tan/p/4356409.html
Copyright © 2011-2022 走看看