作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我为 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);
}
}
最佳答案
您可以像乘法问题一样生成除法问题,但使用乘积和其中一个数字来解决问题,另一个数字作为解决方案。
也就是说,像乘法
一样生成a
和b
,然后问a*b
是什么> 除以 a
(这样 b
就是答案)。
关于C 编程作业 - 数学导师 - 除法和余数逻辑 - 我可以用更好的方法来做吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26514638/
前言。我正在尝试使用 Google Colab 来教学生 Python。问题是,没有很好的工具来可视化代码执行。我尝试通过将 Python Tutor 与 Google Colab 集成来使用它,即创
我有 3 个实体:导师、学生、类(class) 导师教授许多类(class) 学生可以被分配到由多名导师教授的许多类(class) 导师和学生需要能够登录系统 我想使用下表来表示此设计: users
我是一名优秀的程序员,十分优秀!