1. 程式人生 > >自動生成簡單四則運算的C語言程序

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

程序 輸入 oid break 運行 scan 由於 小學 and

  該程序是在博客園裏面找的,具體是誰的找了半天沒找到,無法提供它原本的鏈接。由於自己寫的過於簡單,且有一些功能暫時無法實現,所以就找了一個來應付作業,望原諒。在這個程序的源碼中我改了一個錯誤的地方,源碼中有這樣一個隨機數發生器的初始化函數的語句:“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變量,導致程序無法運行。改後的代碼如下:

#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;
}
}
}

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

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