实现随机出题进行测验
#include <stdio.h> #include <ctype.h> #include <time.h> #include <stdlib.h> #include <stdio.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); int 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;break; } return res; } void takeTest() { int giveRes,num1,num2,printFlag; char signal; 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; } } }