gpt4 book ai didi

c - 除以小数时无法得到正确答案

转载 作者:太空宇宙 更新时间:2023-11-04 02:37:16 25 4
gpt4 key购买 nike

这是我的第一门编码类(class)。当我在选择中选择数字 3 时出现错误。也就是说,如果我将 12 除以 2,程序会给出正确的输出,但是,如果我将 10.4 除以 2,程序输出就会进入循环,直到我停止程序。

#include <stdio.h>

int main() {
/* variable definition: */

int intValue, menuSelect, results;
float shrink;

intValue = 1;
// While a positive number
while (intValue > 0) {
printf("Enter a positive Integer\n: ");
scanf("%d", &intValue);
{
printf("Enter 1 to calculate Square, 2 to Calculate Cube, or 3 to divide input by 2, to end program enter a negative integer.\n: ");
scanf("%d", &menuSelect);
if (menuSelect == 1) {
// Call the Square Function
results = Square(intValue);
printf("Square of %d is %d\n", intValue, results);
} else
if (menuSelect == 2) {
// Call the Cube function
results = Cube(intValue);
printf("Cube of %d is %d\n", intValue, results);
} else
if (menuSelect == 3) {
// Call the Divisor function
results = Shrink (intValue);
printf("The quotient of %d is %d\n", intValue, results);
} else
printf("Invalid menu item, only 1, 2 or 3 is accepted\n");
}
}
return 0;
}

/* function returning the Square of a number */
int Square(int value) {
return value * value;
}

/* function returning the Cube of a number */
int Cube(int value) {
return value * value * value;
}

/* function returning the quotient of a number */
int Shrink(int value) {
return (double)value / 2;
}

最佳答案

首先是什么

floatshrink;

在 main 的第二行,你的程序甚至不应该编译。

你遇到的问题是

1)你不能在你的程序中输入 float 。您的变量都是整数,而您只扫描整数。

int intValue, menuSelect, results;

//(...)

scanf("%d", &intValue);

2) 您的函数只返回整数,因此无论如何您都会丢失小数部分。

int Shrink(int value)
{
return (double)value/2;

}

3) 不要忘记你的标题!!编译器需要知道会发生什么

double Square(double value);

/* function returning the Cube of a numnber */
double Cube(double value);

/* function returning the quotient of a number */
double Shrink(double value);

你们最后遇到了一些关于缩进的基本问题,别担心,你们很快就会掌握的这是更正后的版本

#include <stdio.h>
#include <stdlib.h>


double Square(double value);

/* function returning the Cube of a numnber */
double Cube(double value);

/* function returning the quotient of a number */
double Shrink(double value);


int main (void )
{
/* variable definition: */

float numer_input,results;
int menuSelect;
//floatshrink; (no idea what this is)

numer_input = 1; //you could use a do while statement to avoid this
// While a positive number
while ( numer_input> 0)
{
printf ("Enter a positive Integer\n: ");
scanf("%f", &numer_input);


printf ("Enter 1 to calculate Square, 2 to Calculate Cube, or 3 to divide input by 2, to end program enter a negative integer.\n: ");
scanf("%d", &menuSelect);
if (menuSelect == 1)
{
// Call the Square Function
results = Square(numer_input);
printf("Square of %f is %f\n",numer_input, results);
}
else if (menuSelect == 2)
{
// Call the Cube function
results = Cube(numer_input);
printf("Cube of %f is %f\n",numer_input, results);
}
else if (menuSelect == 3)
{
// Call the Divisor function
results = Shrink (numer_input);
printf("The quotient of %f is %f\n", numer_input, results);
}
else
printf("Invalid menu item, only 1, 2 or 3 is accepted\n");

}

return 0;
}

/* function returning the Square of a number */
double Square(double value){
return value*value;
}

/* function returning the Cube of a numnber */
double Cube(double value){
return value*value*value;
}

/* function returning the quotient of a number */
double Shrink(double value){
return (double)value/2;
}

最后,我建议使用 switch 语句,因为代码看起来比 if 和 else if 更清晰,但这是个人喜好问题。

关于c - 除以小数时无法得到正确答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35834073/

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