current position:Home>The running result is not expected (c + +)

The running result is not expected (c + +)

2022-02-02 23:20:41 CSDN Q & A

img


Why not + and - But other symbols ? Pictured

#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(" The first %d subject :",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 Summary of results :\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("[ answer :");   if(test[i].opt=='+'){    printf("%d]",test[i].n1+test[i].n2);    }else{    printf("%d]",test[i].n1-test[i].n2);    }   } } printf(" Total number of topics :%d, Correct number :%d",N,counter); printf(" Accuracy rate :%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;}



Refer to the answer 1:

get_rand_opt There is no call ,get_input Change to the following

void  get_input(A test[], int n) {	int i;	for (i = 0; i < n; i++) {		test[i].opt = get_rand_opt();		printf(" The first %d subject :", i + 1);		printf("%d %c %d= ", test[i].n1, test[i].opt, test[i].n2);		scanf("%d", &test[i].input);	}}



Refer to the answer 2:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202022320391707.html

Random recommended