四则运算

/*
**file: .c
**by: 李炫宗
**time: 2015.03.27
**function: 小学生四则运算考试模拟系统
**华南师范大学增城学院
*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <string.h>

#define TIME_OUT 60000 //定义时间毫秒
#define TOTAL 20 //总题数

int total;//题目总数
int yes = 0;//答对题数
int start;//开始时间
int stop;//结束时间
int answer;//答案
int user_answer;//用户答案寄存
char user_name[20];//用户姓名

int init();
int main_body();//
int print_math();//输出问题
int do_question();//回答问题
int random(int x);//返回0-x之间的随机数
int see_grade();//查看成绩
int copyRight();
int getName();
int writeEile();

int main(){
srand(clock());//随机函数初始化
getName();
while(1){
int i;
init();
i = getch();
fflush(stdin);//清除缓冲区
if(i == '1') {
yes = 0;
main_body();see_grade();
}
else if(i == '2') see_grade();
else if(i == '3') break;
}
copyRight();
system("pause");
return 0;
}

int main_body(){
total = TOTAL;
yes = 0;
print_math();
while(total >= 0 ){
stop = clock();//获取当前时间
if(total == 0){ //给最后一题等待时间
if(do_question() || stop - start > TIME_OUT ) total--;
}else if(stop - start > TIME_OUT) print_math();//如果时间超过,就输出下一题
else if(do_question()) print_math();
}
return 1;
}

int print_math(){

char c[] = {'+','-','*','/'};
char *pc;
int x;
int y;
int r;

x = random(100);
y = random(100);
r = random(4);
start = clock();//时间初始化
user_answer = 0;//用户答案初始化
pc = c;
pc += r;
switch(r){
case 0:
answer = x + y;
if(answer > 100){
x = x % 50;
y = y % 50;
answer = x + y;
}
break;
case 1:
answer = x - y;
break;
case 2:
x = x % 10;
answer = x * y;
if(answer >100 ){
y = y % 10;
answer = x * y;
}
break;
case 3:
if(y == 0) y++;
answer = x / y;
x = answer * y;
break;
}
system("cls");
printf("第%d题\n",TOTAL - total + 1);
printf("输入数字后请用回车确认\n");
printf("结果保留整数\n");
printf("%d%c%d=\n",x,*pc,y);
total--;
return 1;
}

int do_question(){
if(kbhit()){
int ch;
ch = getch();
putch(ch);
if(ch >= '0' && ch <= '9') {
user_answer = user_answer * 10 + ch - '0';
return 0;
}else if( ch == 13){
if(user_answer == answer) yes++;//验证答案
return 1;
}else if(ch == 45){
answer = -answer;
}else if(ch == 8){
user_answer = 0;
printf("\r \r");
return 0;
}else {
getchar();//丢弃其他字符
return 0;
}
}
return 0;
}
int random(int x){
int i;
i = rand();
return (int)((float) i / RAND_MAX * x);
}
int init(){
system("cls");
printf("小学生四则运算考试模拟系统\n");
printf(" | \n");
printf(" | \n");
printf(" 1.开始 | 2.查看分数 \n");
printf(" | \n");
printf("------------+-------------\n");
printf(" | \n");
printf(" 3.退出 | \n");
printf(" | \n");
printf(" | \n");
printf("请选择(输入数字):\n");
return 1;
}

int see_grade(){
system("cls");
printf("天才的%s\n",user_name);
printf("您的上一次答题情况\n");
printf("答对题数%d\n",yes);
printf("得分为:%d分\n",yes * 100 / TOTAL);//百分制
printf("满分为100分\n");
printf("按任意键返回\n");
writeFile();
while(!kbhit());
fflush(stdin);
return 1;
}

int copyRight(){
system("cls");
printf("本软件天才嗟启凵编写\n");
printf("感谢您的使用!\n");
return 0;
}

int getName(){
printf("欢迎您使用小学生四则运算考试模拟系统\n");
printf("请输入您的姓名:\n");
scanf("%s",user_name);
fflush(stdin);
return 1;
}

int writeFile(){
char ch[100];
char num[10];
FILE * write;
write = fopen("data.txt","w");
if(write == NULL){
perror("data.txt");
return 0;
}
strcpy(ch,user_name);
strcat(ch,"\n您的成绩如下\n您共作对");
itoa(yes,num,10);
strcat(ch,num);
strcat(ch,"道题\n您的得分为");
itoa(yes * 100 / TOTAL , num, 10);
strcat(ch,num);
strcat(ch,"分\n满分为100分\n");
fputs(ch,write);
fclose(write);
return 1;
}

你可能感兴趣的:(四则运算)