1. 程式人生 > >個人專案3:四則運算

個人專案3:四則運算

個人專案3:可以在之前基礎上實現線上答題;

答題結束後可以判斷對錯;

可以將錯題儲存起來;

設計思路:利用while語句實現選擇不同種類的答題專案;同樣利用while實現對錯的判斷。

 

出現問題:

不知道怎樣將錯題儲存起來;

要防止在進行選擇答題種類和繼續答題時因按錯鍵導致程式出現錯誤;

 

 #include <stdlib.h>

#include <stdio.h>

#include <time.h>

int question_get();

int type;

void main( void )

{

   int answer,n;

   srand( (unsigned)time( NULL ) );

 

     loop:  printf( "請選擇要進行測試的題目種類:" );

 

            printf( "\n1.加法運算\n2.減法運算\n3.乘法運算\n4.除法運算\n5.退出運算\n" );

 

            printf("\t\t\t請選擇(1-5):");

 

            scanf( "%d", &type );

  

   while( 1 )

      {   

         int temp;

         int flag;

         answer = question_get();

         printf( "請回答:\n" );

         scanf( "%d", &temp );

         while( temp!=answer )

            {

            printf( "\n答案錯誤,重做\n" );

              scanf( "%d", &temp );

             }

         printf( "\n答案正確,很好\n" );

 

             printf( "繼續請按1,退出請按0\n" );

           scanf( "%d", &flag );

 

         while( flag!=0&&flag!=1 )

 

   {

       printf( "按其它鍵無效\n" );

             scanf( "%d", &flag );

   }

         if( flag==0 )

         break;

 

          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("\t\t\t退出系統\n"); /*結束程式*/

       system("pause");

       exit(0);

      }

   else if( type==0||type>5 )

      {

        printf("\t\t\t輸入錯誤,請輸入1-5內的數字\n");

        printf("\t\t\t請選擇(1-5):");

        scanf( "%d", &type );

        goto loop;

       }

 }

 

 

結果: