c - 我需要添加什么才能将输出显示给用户?

标签 c

我需要添加什么才能将输出显示给用户?我不确定需要添加什么 printf 语句才能使其工作。我知道如何向用户显示文本,但不知道如何显示计算结果。

        #include <stdio.h>
        #define NORMAL_ROD 10
        #define SENTINEL 0


           int main(void)
{
    //Variable declarations
    int normal_count = 0;           
    int normal_total = 0;          
    int long_count = 0;
    int long_total = 0;
    int short_count = 0;
    int short_total = 0 ;
    double normal_average;          
    double long_average;
    double short_average;
    double overall_average;
    int overall_count =0;
    int overall_total= 0;



    int rodLength = 1;


    printf("Rod Manufacturing Quality Control\n");


    while(1)
    {
        printf("Input Rod Length: ");   
        scanf("%i", &rodLength);        
        if(rodLength == SENTINEL) break;

        overall_count = overall_count + 1;
        overall_total = overall_total + rodLength;

        if(rodLength == NORMAL_ROD)
        {
            normal_count = normal_count + 1;
            normal_total = normal_total + rodLength;
        }
        else if(rodLength > NORMAL_ROD)
        {
            long_count = long_count +1;
            long_total = long_total + rodLength;
        }
        else
        {
            short_count = short_count +1;
            short_total = short_total = rodLength;
        }
    }

    // Average calcs
    normal_average = (double)normal_total / normal_count;
    short_average = (double)short_total / short_count;
    long_average = (double)long_total / long_count;
    overall_average = (double)overall_total / overall_count;
    /*
    ...
    ...
    ...
    */

    // Print output
    printf("Statistics:\n");
    printf("Desc        Number        Total        Average\n");



    return 0;
}

/*

Output here!

*/

最佳答案

要打印结果,您可以使用“格式说明符”,如 scanf() 中。

当您使用时

scanf("%d", &age);

这里%d是整数的格式说明符。您也可以类似地对 printf() 使用格式说明符。

例如:

如果您将年龄存储在整数变量age中,那么您可以像这样打印它:

printf("Age is: %d",age);

这里 printf()""(第一个参数)内的内容打印到监视器。执行此操作时,%d 会被 age 的值替换。

因此,如果age等于25,那么将显示如下:

Age is 25

%dINTERGER 变量的格式说明符。每种数据类型都有自己的格式说明符。

还有一个前任:

printf("Age is %d , weight is %d", age, weight);

所以基本上您需要在 printf() 语句中使用格式说明符来打印存储在变量中的值。如果您将结果存储在变量中,则可以使用带有正确格式说明符的这些变量名称。

您可以通过谷歌搜索“C 中的格式说明符”来查找 C 中使用的所有数据类型的格式说明符。

关于c - 我需要添加什么才能将输出显示给用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28590528/

相关文章:

c - 在 c 中使用标记化

C 数组 = 比 memcpy() 更快的数组

将 C char 数组转换为 Matlab 字符串 [Matlab Coder]

c++ - 如何在 hp-ux 中使用 gcc 创建共享库?

c - 使用 openssl 在客户端/服务器中进行 DES 加密/解密

ios - NSUserDefaults 给声明阴影局部变量警告

c - 在 main() 中的 pthread_join 之后执行 pthread_exit 有好处吗?

c - 如何获取C盘操作系统信息

c - scanf 没有响应?

c - 检查字符串卡住的函数