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

标签 c

// 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/

相关文章:

c - 如何在一串参数中包含变量名称,例如 : execlp ("ls" ,"ls" ,"/VARIABLE_NAME", Null);?

c - UDP 多客户端聊天服务器

c - 使用数组创建堆时出错

c - 为什么将结构地址转换为 int 指针、取消引用并将其用作语句中的 LVALUE 会使我的微 Controller 崩溃?

c - 如何使用原始套接字 ping 本地主机?

c - 如何安全地使用指针索引数组

c++ - 你如何将带有字符串变量的字符串传递给C++中的函数

C 用户输入数组用作 if 语句

c - int variable++ 不会递增超过 1 或将重置为 0

c - Multi pthreads Segmentation fault (core dumped) 无法检测到问题