自動生成簡單四則運算的C語言程序


  該程序是在博客園里面找的,具體是誰的找了半天沒找到,無法提供它原本的鏈接。由於自己寫的過於簡單,且有一些功能暫時無法實現,所以就找了一個來應付作業,望原諒。在這個程序的源碼中我改了一個錯誤的地方,源碼中有這樣一個隨機數發生器的初始化函數的語句:“srand((unsigned)time(NULL))”。srand函數是隨機數發生器的初始化函數。但是正確的寫法應該是:srand(unsigned( time(NULL)));為了防止隨機數每次重復,常常使用系統時間來初始化,即使用time函數來獲得系統時間,它的返回值為從 00:00:00 GMT, January 1, 1970 到現在所持續的秒數,然后將time_t型數據轉化為(unsigned)型再傳給srand函數,即: srand((unsigned) time(&t)); 還有一個經常用法,不需要定義time_t型t變量,即: srand((unsigned) time(NULL)); 直接傳入一個空指針,因為你的程序中往往並不需要經過參數獲得的t數據。所以在源碼中他並沒有定義需要的time_t型t變量,導致程序無法運行。改后的代碼如下(https://github.com/Jackchenyu/four-arithmetic-operation/tree/Dev):

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>

int count_s();
void over();

void main()
{
    int i;
    printf("\n\t\t\t || 歡迎進入小學四則運算系統 ||\n");
    printf("\n\t\t\t1  開始做題\n");
    printf("\n\t\t\t2  退出\n");
    printf("\n\t請輸入您的選擇: \n");
    scanf("%d",&i);
    if(i==1){
        count_s();
    }
    else if(i==2)
    {
        over();
    }else{
        printf("\n\t輸入錯誤,請重新輸入:d%",i);
        return;
    }
}

void over()
{
    printf("\n\t\t歡迎再次使用,謝謝!");
}

int count_s()
{
    int i=0;
     int n=0;
     int x=0;
     int t;
     char a;
     int b, c;
     float result;
     printf("/******請輸入要出的題目數量:\n");
     scanf("%d",&n);
     srand((unsigned) time(NULL));
     while(x<n)
     {
            a = rand() % 4;
             b = rand() % 100;
             c = rand() % 100;
         switch(t)
         {
         case 0:
             printf("%d + %d = ?\n", b, c);
             break;
         case 1:
             printf("%d - %d = ?\n", b, c);
             break;
         case 2:
             printf("%d * %d = ?\n", b, c);
             break;
         case 3:
             printf("%d / %d = ?\n", b, c);
             break;
         }
 
         i++;
         while(i>=n)
         {
             printf("\n\t一共 %d 題\n",i);
             printf("\n\t\t繼續?[Y/N]\n");
             fflush(stdin);
             scanf("%c",&a);
             if(a=='Y'||a=='y')
             {
                 printf("/*****請輸入要出的題目數量\n");
                 scanf("%d",&n);
                 i=0;
                 break;
              }
             printf("歡迎再次使用使用!\n");
             fflush(stdin);
             getchar();
             return 0;
         }
     }
}

 

如果有什么不對的地方請多多指教。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM