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

时间:2015-02-18 17:56:37

标签: 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!

*/

3 个答案:

答案 0 :(得分:2)

要打印结果,您可以使用&#34;格式说明符&#34;就像在scanf()中一样。

使用时

scanf("%d", &age);

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

例如:

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

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

此处printf()" "(第一个参数)内的任何内容打印到监视器。这样做%dage的值替换。

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

Age is 25
对于 INTERGER 变量,

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

另外一个例子:

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

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

您可以在C&#34;中使用google,&#34;格式说明符;找出C中使用的所有数据类型的格式说明符。

答案 1 :(得分:0)

您没有说出每个描述对应的数字。无论如何要显示双重使用printf(“%f”,doubleValue);

// Print output
printf("Statistics:\n");
printf("   Average %f\n",normalAverage );

类似地使用%s表示字符串,%c表示字符,或%d表示整数

答案 2 :(得分:0)

printf("%f", normal_average);  /*double */