gpt4 book ai didi

万一无法完成除法计算。商为0.0。有没有办法可以将 int 更改为 float?

转载 作者:行者123 更新时间:2023-11-30 20:14:48 25 4
gpt4 key购买 nike

// 2.cpp : Sample Program with a menu
//Write a program that will use a menu



#include "stdafx.h"
#include<stdio.h>

#define Pi 3.14159

int main(void)
{
int digit1;
int choice;
int a, b, c; //user input for choice 3 & 4
int a1, a2;// User input for Choice 1
int divi;
int divisor;

/*Menu*/

printf("***** MENU *****\n");
printf(" 1 - Greater Than, Less Than, Or Equal?\n");
printf("\t In this selection, you will enter two integers. \n");
printf("\t The program will return'Greater Than' if the first\n");
printf("\t integer entered is less than the second,'Less than'\n");
printf("\t if the first integer is greater than the second, or\n");
printf("\t'Equal' if the two integers entered are equal\n");
printf(" 2 - Evenly Divisible\n");
printf("\t In this slection, you will enter two integers. \n");
printf("\t The program will test if the the first integer\n");
printf("\t is evenly divisible by the second. The program\n");
printf("\t will then return its result and display\n");
printf("\t the quotient rounded to the nearest thousandth\n");

printf(" 3 - Calculations with 2 integers\n");
printf(" 4 - Calculations with 3 integers\n");
printf(" 5 - Calculations with circles\n");
printf(" 6 - Quit\n");

printf("Please enter your choice: ");
scanf("%i",&choice);

printf("\n\n");

switch(choice)
{
case 1: //Greater than, less than, or equal

printf("Please Enter two integers: \n");
scanf("%i %i", &a1, &a2);
if (a1<a2)
printf("Greater Than\n");
else if (a1>a2)
printf("Less Than\n");
else if (a1=a2)
printf("Equal\n");

break;

case 2: //Equally Divisible

我需要这部分代码的帮助。商为 0.000。为什么?这些情况会导致它无法接收整数吗?我尝试用大括号本地化整数。我做错了什么?

    printf("Please Enter two integers: \n");
{
int divi;
int divisor;

scanf("%i %i", &divi, &divisor);
float modQuotient = divi%divisor;

if (modQuotient!=0)
{
printf("%i is not evenly divisible by %i\n", divi, divisor);
printf("The quotient is %.3f\n", divi/divisor);
}
else
{
printf("%i is evenly divisible by %i\n", divi, divisor);
printf("The quotient is %.3f\n", divi/divisor);
}


}



break;

/*case 3: /*Calculations with 2 integers*/


case 4: /*Calculations with 3 integers*/
printf(" You have selected to do some calculations with 2

integers\n");
printf("Please enter your 3 integers: ");
scanf("%i%i%i",&a, &b, &c);

printf("The average of %i, %i and %i is %g\n\n",a, b, c, (a+b+c)/3.0);
break;

case 5: /*Calculations with circles*/
float radius;

printf("Please enter the radius of a circle: ");
scanf ("%g", &radius);
printf("\nThe diameter of a circle with a radius of %g units is %.2f units\n\n",

radius, radius*2);
printf("The circumference is of the circle with a radius of %g

units is %.2f units\n\n", radius, Pi*radius*2);
printf("The area of a circle with a radius of %g units is %.2f

square units\n\n", radius, Pi*radius*radius);
break;

case 6: /*Quit*/
printf("Thank you. Good bye\n\n");
break;

default: /*Invalid Entry*/
printf("Invalid Entry...\n");
break;
}

printf("The program will now end. Have a great day!\n");


return 0;
}

最佳答案

两个int的模是另一个int。此外,除以零会导致未定义的行为。所以你应该写:

if ( divisor == 0 )
exit(EXIT_FAILURE); // or some other error handling

int modQuotient = divi % divisor;

如果您要 printf'ing modQuotient,请记住使用 %i%d

行中:

printf("The quotient is %.3f\n", divi/divisor);

有问题。 dividivisor都是int,因此它们的二元运算结果也是int。使用 printf 时,它不会对参数进行任何转换来匹配格式说明符 - 您必须自己手动确保匹配。因此,此代码通过使用 %f 尝试打印 int 导致未定义的行为。

如果要进行浮点除法,则必须至少使其中一个操作数为浮点型,例如(为了清楚起见分成两行):

float quot = (float)divi / divisor;
printf("The quotient is %.3f\n", quot);

注意。这段代码有一个逻辑错误:

else if (a1=a2)
printf("Equal\n");

= 运算符表示赋值。此行将 a2 更改为等于 a1。比较相等性的运算符是 ==

关于万一无法完成除法计算。商为0.0。有没有办法可以将 int 更改为 float?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24272342/

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