zoukankan      html  css  js  c++  java
  • 第二次作业:编写一个四则运算的"软件"

    • - 题目:
    • 请编写一个能自动生成小学四则运算题目的 “软件”。
    • 让程序能接受用户输入答案,并判定对错。
    • 最后给出总共 对/错 的数量。

    需求分析:

    ●基本功能

    ●实现100以内的加法

    ●实现100以内的减法

    ●实现100以内的乘法

    ●实现100以内的除法

     

    设计:

    ●程序由主函数和子函数构成

    ●首先选择要进行测试的题目种类,让运用者进入开始做题,题目随机产生(1表示加法运算,2表示减法,3表示乘法,4表示除法运算,5表示退出系统)

    ●int question_get() 用于系统计算四种运算的值,temp表示用户计算出的值,系统会自动将answer与temp的值进行比较,从而会输出相应的提示语。最后输出总共回答了题的数目,答对了几道,答错了几道。

    代码实现:

    #include <stdlib.h> 
    #include <stdio.h>
    #include <time.h>
    
    int question_get();
    int type;
    
    
    void main( void ) 
    {
    	int r=0;
    	int e=0;
    	int answer;
    	int n; 
    	srand( (unsigned)time( NULL ) );
    loop:printf( "请选择要进行测试的题目种类:" );
         printf( "
    1.加法运算
    2.减法运算
    3.乘法运算
    4.除法运算
    5.退出运算
    " );
         printf("			请选择(1-5):");
    	 scanf( "%d", &type ); 
    	 
    	 while( 1 )
    	 {
    		 int temp; 
    		 int flag; 
    		 answer = question_get(); 
    		 printf( "请回答:
    " ); 
    		 scanf( "%d", &temp ); 
    		 
    		 while( temp!=answer ) 
    		 {
    			 e++;
    			 printf( "
    答案错误,再接再厉,请您重做
    " ); 
    			 
    			 scanf( "%d", &temp );
    			 
    		 }
    		 r++;
    		 printf( "
    恭喜你!回答正确
    " ); 
    		 
    		 printf( "若继续请按1,退出请按0
    " ); 
    		 scanf( "%d", &flag ); 
    		 
    		 while( flag!=0&&flag!=1 ) 
    		 {
    			 printf( "按其它键无效
    " );
    			 scanf( "%d", &flag ); 
    		 }
    		 if( flag==0 ) 
    			 break; 
    		 else
    			 printf("您共回答了%d道题,其中正确的有%d道,错误的有%d道
    ",e+r,r,e);
    		 goto loop;
    	 }
    } 
    
    int question_get() 
    {
    	int a,b,c; 
    loop: if( type==1 ) 
    	  { 
    		  a=rand()%99;
    		  b=99-a;
    		  b=rand()%b;
    		  printf( "%d + %d = ?", a, b ); 
    		  return (a+b);
    		  
    	  }
    	  else if( type==2 )
    	  {
    		  b=rand()%99;
    		  c=99-b;
    		  c=rand()%c;
    		  printf( "%d - %d = ?", b+c, b );
    		  return(c);
    	  }
    	  else if( type==3 )
    	  {
    		  a=rand()%10;
    		  b=50-a; 
    		  b=rand()%b; 
    		  printf( "%d * %d = ?", a, b );
    		  return(a*b);
    	  }
    	  else if( type==4 )
    	  {
    		  b=rand()%50;
    		  c=100/b; 
    		  while( 1 ) 
    		  {
    			  c=rand()%c;
    			  if( c!=0 )
    				  break;
    		  }
    		  printf( "%d / %d = ?", b*c, b );
    		  return(c);
    	  }
    	  else if( type==5 )
    	  {
    		  printf("			退出系统
    "); 
    		  system("pause"); 
    		  exit(0);
    	  }
    	  else if( type==0||type>5 ) 
    	  {
    		  printf("			输入错误,请输入1-5内的数字
    "); 
    		  printf("			请选择(1-5):"); 
    		  scanf( "%d", &type ); 
    		  goto loop;
    	  } 
    	  
    }
    

     

    程序截图:

    加法界面:

    减法界面:

    乘法界面:

    除法界面:

    分析与总结:

    ●PSP耗时与总结

        总结:

        一定要多动脑子去思考,应该怎么设计,为什么要这样设计,一定要有一个清晰的思路再去写代码。要先想好思路再动手写,想的过程很重要,然后再去实现。

    PSP

    Personal Software Process Stage

    Time(h)

    Time(%)

    ●Design

    ●具体设计

    6

    11.5

    ●Coding

    ●具体编码

    24

    46.2

              ●Code Review

    ●代码复审

    5

    9.6

    ●Test

    ●测试(自测,修改代码)

    13

    25

    ●Postmortem & Process

    Improvement Plan

    ●事后总结,并提出过程改进计划

    4

    7.7

  • 相关阅读:
    FJNU 1151 Fat Brother And Geometry(胖哥与几何)
    FJNU 1157 Fat Brother’s ruozhi magic(胖哥的弱智术)
    FJNU 1159 Fat Brother’s new way(胖哥的新姿势)
    HDU 3549 Flow Problem(最大流)
    HDU 1005 Number Sequence(数列)
    Tickets(基础DP)
    免费馅饼(基础DP)
    Super Jumping! Jumping! Jumping!(基础DP)
    Ignatius and the Princess IV(基础DP)
    Keywords Search(AC自动机)
  • 原文地址:https://www.cnblogs.com/0606tangjie/p/4413454.html
Copyright © 2011-2022 走看看