为什么不是+和- 而是其它符号?如图
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define N 5
struct a{
int n1;
int n2;
char opt;
int input;
};
typedef struct a A;
void init_test(A test[],int n);
void get_input(A test[],int n);
void summary(A test[],int n);
int check_answer(A test);
int get_rand_number(int min,int max);
char get_rand_opt();
void swap(int * x,int * y);
int main(){
A test[N];
init_test(test,N);
get_input(test,N);
summary(test,N);
return 0;
}
void init_test(A test[],int n){
int i;
srand(time(NULL));
for(i=0;i<n;i++){
test[i].n1=get_rand_number(0,99);
if(test[i].opt=='+'){
test[i].n2=get_rand_number(0,99-test[i].n1);
}else{
test[i].n2=get_rand_number(0,99);
if(test[i].n1<test[i].n2){
swap(&test[i].n1,&test[i].n2);
}
}
}
}
void get_input(A test[],int n){
int i;
for(i=0;i<n;i++){
printf("第%d题目:",i+1);
printf("%d %c %d= ",test[i].n1,test[i].opt,test[i].n2);
scanf("%d",&test[i].input);
}
}
void summary(A test[],int n){
int i;
int is_right=0;
int counter=0;
printf("\n成绩汇总:\n");
for(i=0;i<n;i++){
printf("%d %c %d= ",test[i].n1,test[i].opt,test[i].n2);
printf("%d",test[i].input);
is_right=check_answer(test[i]);
counter+=is_right;
if(is_right){
printf("\n");
}else{
printf("[答案:");
if(test[i].opt=='+'){
printf("%d]",test[i].n1+test[i].n2);
}else{
printf("%d]",test[i].n1-test[i].n2);
}
}
}
printf("题目总数:%d,答对数量:%d",N,counter);
printf("正确率:%f%%\n",counter*100.0/N);
}
int check_answer(A test){
int is_right=0;
if(test.opt=='+'){
if(test.input==test.n1+test.n2){
is_right=1;
}
}else{
if(test.input==test.n1-test.n2){
is_right=1;
}
}
return is_right;
}
int get_rand_number(int min,int max){
int n=rand()%(max-min+1)+min;
return n;
}
char get_rand_opt(){
char opt;
if(rand()%2==0){
opt='+';
}else{
opt='-';
}
return opt;
}
void swap(int * x,int * y)
{
int a;
a=*x;
*x=*y;
*y=a;
}