c - 我不明白如何在简单计算器中输入或划分 float 和 int

标签 c calculator

这就是任务

Input: 1 1 
Output: 2

Input: -1 -1
Output: -2

Input: 1.1 2.2
Output: 3.3

我知道如何完成 3 项任务中的 2 项,但我不知道如何包含 float当我有 int 时的数字数字。

最佳答案

您可以使用 %g 来获取您提到的预期输出。

#include <stdio.h>

int main()
{
    float num1 = 0;
    float num1 = 0;
    float tot = 0;

    printf("Enter number 1 = ");
    scanf("%f", &num1);

    printf("Enter number 2 = ");
    scanf("%f", &num2);

    tot = num1 + num2;

    printf("Output = %g", tot);

    return 0;
}
input : 2, 2
output :4

input : 5.5, 5
output: 10.5

关于c - 我不明白如何在简单计算器中输入或划分 float 和 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58373619/

相关文章:

c - 如何在内存 hexdump 中找到变量的地址?

c - 插入排序代码使用insert和insertsort!

Java 图形用户界面计算器

android - Android 中的触控计算器

c - c中ruby Exception类对象的扩展是什么?

c - 对于函数指针 func_ptr,(*func_ptr)() 调用该函数。但是为什么 (****func_ptr)() 或 (***func_ptr)() 有效?

c - 第二次调用后重新分配失败

c++ - 当一个功能完成另一个功能打开时我该怎么办?

java - 查找默认计算器(跨平台)

java - Java 后缀计算器中的 StackOverFlowError