gpt4 book ai didi

C 编程作业 - 数学导师 - 除法和余数逻辑 - 我可以用更好的方法来做吗?

转载 作者:行者123 更新时间:2023-11-30 17:27:06 24 4
gpt4 key购买 nike

所以我为 C 编程作业编写了这个程序。它使用切换菜单条目在加法、减法、乘法和除法中随机生成的问题之间进行选择。

我制作它时遇到的唯一问题是除法部分。如何处理随机生成的不能均匀划分的数字。所以我想,“好吧,甚至不要将小数点作为 float 处理,只需直接整数。然后使用两个随机生成的数字的 mod 来输入余数(如果有的话)。”

这就是我的想法。

void division(){

int a,b;
int ans, uans;
int remain, uremain;

a=rand()%(100-1+1)+1;
b=rand()%(100-1+1)+1;

printf("What is\n");
printf(" %.0f\n",a);
printf(" ÷ %.0f\n",b);
printf("========\n");
printf("?= ");

scanf("%d",&uans);

printf("\n Remainder = ");

scanf("%d",&uremain);

ans = a / b;
remain = a % b;


if(ans==uans && remain==uremain){
printf("\n\nCorrect!.......\n\n");
printf("\n\nThe answer was %d with a remainder of %.0f\n",ans, remain);
}
else{
printf("\n\nIncorrect!......\n\n");
printf("\n\nThe answer was %d with a remainder of %d\n",ans, remain);
}
}

是否有更好的方法来做到这一点,以解决如果没有余数则可能不必输入余数的问题,我假设我可以执行类似 if(remain > 0) 的操作来查看是否有余数然后添加它,但只是想知道其他人的想法。

对于上下文,这里是完整的程序

/*****************************

CS 50 - Programing in C
Math tutor

Write a program that displays a menu as shown in the sample run.
You can enter 1, 2, 3, or 4 for choosing an addition, subtraction, multiplication,
or division test. After a test is finished, the menu is redisplayed.
You may choose another test or enter 5 to exit the system.

Each test generates two random single-digit numbers to form a question for addition,
subtraction, multiplication, or division.

For a subtraction such as number1 – number2,

number1 is greater than or equal to number2.

For a division question such as number1 / number2, number2 is not zero.

******************************/


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include <time.h>

int displaymenu();
void addition();
void subtraction();
void multiplication();
void division();


int main(){

int com;
srand(time(NULL));

do{
com = displaymenu();
switch(com){
case 1:
addition();
break;
case 2:
subtraction();
break;
case 3:
multiplication();
break;
case 4:
division();
break;
case 5: printf("Have a nice day\n");
}
}while(com != 5);

//needed for all basic programs to run for the professor
system("pause");

return(0);
}

int displaymenu(){

int choice;

printf("*-----------------------*\n");
printf("| MATH TUTOR |\n");
printf("*-----------------------*\n");
printf("* 1. Addition *\n");
printf("* 2. Subtration *\n");
printf("* 3. Multiplication *\n");
printf("* 4. Division *\n");
printf("* 5. EXIT *\n");
printf("*-----------------------*\n");
printf("Menu Choice: ");
scanf("%d",&choice);
return choice;
}

void addition(){

int a,b, ans;

a=rand()%(100-1+1)+1;
b=rand()%(100-1+1)+1;

printf("What is\n");
printf(" %d\n",a);
printf(" + %d\n",b);
printf("========\n");
printf("?= ");
scanf("%d",&ans);

if((a+b)==ans){
printf("\n\nCorrect!.......\n\n");
}
else{
printf("\n\nIncorrect!......\n\n");
printf("\n\nThe answer was %d\n",a+b);
}
}

void subtraction(){

int a,b, ans;

a=rand()%(100-1+1)+1;
b=rand()%(100-1+1)+1;

printf("What is\n");
printf(" %d\n",a);
printf(" - %d\n",b);
printf("========\n");
printf("?= ");
scanf("%d",&ans);

if((a-b)==ans){
printf("\n\nCorrect!.......\n\n");
}
else{
printf("\n\nIncorrect!......\n\n");
printf("\n\nThe answer was %d\n",a-b);
}
}

void multiplication(){

int a,b, ans;

a=rand()%(100-1+1)+1;
b=rand()%(100-1+1)+1;

printf("What is\n");
printf(" %d\n",a);
printf(" x %d\n",b);
printf("========\n");
printf("?= ");
scanf("%d",&ans);

if((a*b)==ans){
printf("\n\nCorrect!.......\n\n");
}
else{
printf("\n\nIncorrect!......\n\n");
printf("\n\nThe answer was %d\n",a*b);
}
}

void division(){

int a,b;
int ans, uans;
int remain, uremain;

a=rand()%(100-1+1)+1;
b=rand()%(100-1+1)+1;

ans = a / b;
remain = a % b;

printf("What is\n");
printf(" %d\n",a);
printf(" ÷ %d\n",b);
printf("========\n");
printf("?= ");
scanf("%d",&uans);


if(remain > 0){
printf("\nRemainder = ");
scanf("%d",&uremain);
}



if(ans==uans && remain==uremain){
printf("\n\nCorrect!.......\n");
printf("\n\nThe answer was %d with a remainder of %d\n",ans, remain);
}
else{
printf("\n\nIncorrect!......\n");
printf("\n\nThe answer was %d with a remainder of %d\n\n\n\n",ans, remain);
}
}

最佳答案

您可以像乘法问题一样生成除法问题,但使用乘积和其中一个数字来解决问题,另一个数字作为解决方案。

也就是说,像乘法一样生成ab,然后问a*b是什么> 除以 a(这样 b 就是答案)。

关于C 编程作业 - 数学导师 - 除法和余数逻辑 - 我可以用更好的方法来做吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26514638/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com