1. 程式人生 > >二柱子線上答題

二柱子線上答題

小組成員:滕達


程式程式碼

#include <stdio.h> #include <ctype.h> #include <time.h> #include <stdlib.h> #include <windows.h> #define IDNO_LENGTH 6 #define CALC_KIND 4 char getSignal(); int getResult(int,int,char); void takeTest(); void falsePrint(int); void correctPrint(int
); //main函式 void main() { while(1) { takeTest(); } } //獲取隨機運算子 char getSignal() { char signal[CALC_KIND]={'+','-','*','/'}; srand((unsigned)time(NULL)); return signal[rand()%4]; } //獲取隨機數 int random(double start, double end) { return (int)(start+(end-start)*rand()/(RAND_MAX+ 1.0
)); } //運算 int getResult(int num1,int num2,char signal) { int res; switch(signal) { case '+': res=num1+num2;break; case '-': res=num1-num2;break; case '*': res=num1*num2;break; case '/': res=num1/num2;break; default: res=0; printf("運算子不符合規則\n"); } return res; } //列出題目 void takeTest() {
int giveRes; int num1; int num2; char signal; int printFlag; srand((unsigned)time(NULL)); signal=getSignal(); num1=random(0,100); num2=random(1,100); if(signal=='-') { if(num1<num2) { int temp; temp=num1; num1=num2; num2=temp; } } if(signal=='/') { if(num2==0) { int temp; temp=num1; num1=num2; num2=temp; } } printf("%d%c%d=",num1,signal,num2); scanf("%d",&giveRes); fflush(stdin); if(getResult(num1,num2,signal)==giveRes) { printFlag=random(1,3); correctPrint(printFlag); } else { printFlag=random(1,3); falsePrint(printFlag); printf("正確答案:%d\n",getResult(num1,num2,signal)); } } //回答正確列印資訊 void correctPrint(int num) { switch(num) { case 1: printf("You're right!\n");break; case 2: printf("It's OK!\n");break; case 3: printf("Correct!\n");break; default: printf("Powerful!\n");break; } } //回答錯誤列印資訊 void falsePrint(int num) { switch(num) { case 1: printf("You're lost!");break; case 2: printf("Failure!");break; case 3: printf("Come on!");break; default: printf("Incorrect!");break; } }
執行結果截圖