zoukankan      html  css  js  c++  java
  • 四则运算3

    一、设计思路

      第三版程序在第二版上增加了用户填写答案和自动批改答案功能,并且还能显示一共答对了多少题。

      

    二、程序源代码

    #include <stdlib.h>
    #include <iostream.h>
    #include <conio.h>
    #include <time.h>
     using namespace std;
    void Display1(int,int,int,int,int,int);
    void Display2(int [],int,int,int);
    int main()
    {
        int a = 0;
        int b = 0;
        int mode = 0;//0:加  1:减   2:乘 3:除 
        int i = 0; 
        int result = 0;
        int score = 0;
        int answer = 0;
        int c[7]={0,40,1,100,1,1,4};//是否定制,出题数量、是否有乘除、数值上限、数值下限,是否有余数,每行个数。
        srand((unsigned)time( NULL ) ); //初始化随机数发生器,使得每次运行生成的随机数不同
        printf("---------------------------------------------------------------------------
    ");
    	printf("                         欢迎来到二柱子出题系统!                          
    ");
        printf("               本系统默认生成10道100以内数字的四则运算题目,每行4道        
    ");
    	printf("                        是否定制?是输入1,否输入0:");
        cin>>c[0];
        if(c[0]==0)
    	{   
    	printf("----------------------------题目打印如下------------------------------------
    "); 
            Display1(a,b,mode,result,score,answer) ;
        }   
    	else 
    	{
        cout<<"                             请输入出题数量:";
    	cin>>c[1];
    	cout<<"                    是否含乘除?是输入1,否输入0:";
    	cin>>c[2];
    	cout<<"                              数值上限:"; 
    	cin>>c[3];
    	cout<<"                              数值下限:";
    	cin>>c[4]; 
    	cout<<"                    是否含余数?是输入1,否输入0:";
    	cin>>c[5]; 
    	cout<<"                             每行题目数:";
    	cin>>c[6]; 
    	printf("----------------------------题目打印如下------------------------------------
    "); 
        Display2(c,a,b,mode);	
        } 
    	printf("--------------------题目打印完毕,谢谢使用----------------------------------
    "); 
        cout<<endl;
    	return 0;   
    }
     void Display1(int a,int b,int mode,int result,int score,int answer)
    	{
    	 for(int i=0;i<10;i++) //做三十题
            {  
            a = rand() % 100; //生成一个0~99之间的随机数
            b = rand() % 100;  //生成一个0~99之间的随机数
            mode = rand() % 4; //生成一个0~3之间的随机数,代表运算符
            printf("<%d>%d",i+1, a);   //打印算式
            switch(mode) //确定运算符
            {  
            case 0:   
                printf("+%d =  ", b ); 
    			result= a + b;   //选择了+运算的正确答案  
                break;  
            case 1:    
                printf("-%d =  ", b );
    		    result= a - b;   //选择了-运算的正确答案   
                break;  
            case 2:   
                printf("*%d =  ", b ); 
    			 result= a * b;   //选择了*运算的正确答案  
                break;  
            case 3:   
                printf("/%d =  ", b );
    			result= a / b;   //选择了/运算的正确答案   
                break;  
            default:  
                printf("somethingis wrong!
    ");   
                break;       
            } 
             scanf("%d", &answer); //输入答案
            if(answer == result) //与正确答案一致
            {  
                score+= 10;  //加分
                printf("Right
    "); 
            } 
            else 
            {  
                printf("Wrong,the right answer is:%d
    ",result); //错开
            }      
    		}
    		 printf("Yourscore is: %d
    
    
    ", score);//显示十道题的得分	
    	} 
    	 void Display2(int c[],int a,int b,int mode)
    	{
    		  for(int i=0;i<c[1];i++) //定制题数 
                {  
                  a = rand() % (c[3]-c[4]+1)+c[4]; //生成限定的随机数
                  b = rand() % (c[3]-c[4]+1)+c[4];  //生成限定的随机数
                   if(c[2]==1)
    			   {      	
                    mode = rand() % 4; //生成一个0~3之间的随机数,代表运算符          
                 switch(mode) //确定运算符
                 {  
             case 0:   
                printf("%d+%d =  ",a, b );   
                break;  
             case 1:    
                printf("%d-%d =  ",a, b );   
                break;  
              case 2:   
                printf("%d*%d =  ",a, b );   
                break;  
              case 3: 
    		if(c[5] == 1)                 //判断选择是否有余数,如果能有余数
             {
    		 printf("%d/%d =  ",a, b ); 
             }
             else if(a%b!=0)                         //如果不能有余数
               {
                       i = i-1;
               }
    		else 
    		{
    		 printf("%d/%d =  ",a, b ); 
             }       
              break;  
            default:  
                printf("somethingis wrong!
    ");   
                break;       
                }
                if((i+1)%c[6]== 0)              //判断是否换行
             {
                  printf("
    ");            
             }
    		 else
    		 	{
     					
                   for(int j = 0;j<3;j++)
                   {
                         printf(" ");
                   }                               
    		    }
          } 
            else{
            	mode = rand() % 2; //生成一个0~1之间的随机数,代表运算符
            	 printf("%d", a);   //打印算式
            switch(mode) //确定运算符
            {  
            case 0:   
                printf("+%d =  
    ", b );   
                break;  
            case 1:    
                printf("-%d =  
    ", b );   
                break; 
    			default:  
                printf("somethingis wrong!
    ");   
                break;       
            } 
            if(i%c[6]== 0)              //判断是否换行
             {
                  printf("
    ");            
             }
    		 else
    		 	{
     					
                   for(int j = 0;j<3;j++)
                   {
                         printf(" ");
                   }                               
    		    }	
            }
    		} 	
    	} 
    

      运行截图:

    五、实验体会总结

      这次的程序可以说在第二个的基础上稍加改动,我对第一个函数修改了部分参数,输出10道题,主要是体现功能

  • 相关阅读:
    华为机试题01背包问题
    丑数
    动态规划(1)
    Linux 后台启动 Redis
    redis.exceptions.ResponseError: MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk.
    SQLServer从渣仔到小白
    cmder 增强型命令行工具
    总结在部署分布式爬虫环境过程中常见的若干问题
    【pymongo.errors】Cursor not found
    浅析scrapy与scrapy_redis区别
  • 原文地址:https://www.cnblogs.com/huazongzong/p/4357676.html
Copyright © 2011-2022 走看看