C语言输出操作求助
代码如下,输出时怎么实现 问题 | 正确答案 | 你的答案#include <stdio.h>
#include <ctype.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#define ID_LENGTH 6
#define CALC_KIND 4
#define CALC_NUM 10
bool check(char id[]);
char getSignal();
int getResult(int,int,char);
void takeTest();
//main函数
int main(){
printf("请输入用户的ID号:");
char id[ID_LENGTH];
do{
scanf("%s",&id);
fflush(stdin);
} while(check(id)==false);
printf("1.开始测试\t2.检查分数\t3.退出\n");
int choose;
do{
printf("选择:");
scanf("%d",&choose);
fflush(stdin); //清空缓冲区,通常是为了确保不影响后面的数据读取(例如在读完一个字符串后紧接着又要读取一个字符)
} while(choose!=1 && choose!=2 && choose!=3);
if(choose==1){
takeTest();
}else if(choose==2){
FILE *fp;
char ch;
if((fp=fopen("record.txt","rt+"))==NULL){
printf("\n你还没做过题目测试");
getchar();
exit(1);
}
ch=fgetc(fp);
while(ch!=EOF){
putchar(ch);
ch=fgetc(fp);
}
fclose(fp);
}else{
exit(0);
}
}
//获取随机运算符
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;
}
//列出10道题
void takeTest(){
printf("开始答题\n");
clock_t startTime;
startTime=clock();
int index;
int giveRes;
int num1;
int num2;
int score=0;
char signal;
for(index=0;index<CALC_NUM;index++){
srand((unsigned)time(NULL));
signal=getSignal();
do{
num1=random(0,100);
num2=random(1,100);
if(num1<num2&&(signal=='-'||signal=='/')){ //给小学生的题,减和除运算保证大减小和大除以小
int d=num1;
num1=num2;
num2=d;
}
if(signal=='/') //对于除法,调整被除数,以保证能整除
num1=(num1/num2)*num2;
}while((num1+num2)>=100||num1*num2>=100);
printf("%d%c%d=",num1,signal,num2);
scanf("%d",&giveRes);
fflush(stdin);
/*if(getResult(num1,num2,signal)==giveRes){
printf("correct\n");
score+=10;
}else{
printf("wrong\n");
}*/
}
FILE *fp=fopen("record.txt","w");
fprintf(fp,"分数为%d",score);
fclose(fp);
printf("解10道题用了%lf秒\n",(double)(clock()- startTime)/CLOCKS_PER_SEC);
}
//检测id是否合规则,合规则返回true
bool check(char id[]){
int index;
bool flag;
flag=true;
for(index=0;index<ID_LENGTH;index++){
if(index<2){
if(isalpha(id[index])!=1){
flag=false;
printf("ID不符合规则,重新输入:");
break;
}
}else{
if(!isdigit(id[index]) || strlen(id) != 6){
flag=false;
printf("ID不符合规则,重新输入:");
break;
}
}
}
return flag;
}