yd______ 2021-12-17 14:06 采纳率: 100%
浏览 20
已结题

运行结果不是预期想要的(c++)

img


为什么不是+和- 而是其它符号?如图


#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;
}
  • 写回答

1条回答 默认 最新

  • 书山客 2021-12-17 14:17
    关注

    get_rand_opt没有调用,get_input改成下面这样

    void  get_input(A test[], int n) {
    	int i;
    	for (i = 0; i < n; i++) {
    		test[i].opt = get_rand_opt();
    		printf("第%d题目:", i + 1);
    		printf("%d %c %d= ", test[i].n1, test[i].opt, test[i].n2);
    		scanf("%d", &test[i].input);
    	}
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 12月25日
  • 已采纳回答 12月17日
  • 创建了问题 12月17日

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应