在C中直接输入整数

时间:2015-07-27 22:08:27

标签: c average

我正处于初级C课程中,我想知道是否有一种方法可以直接输入整数并将它们平均在一起?我试图让我的程序尽可能干净整洁。

我想直接输入整数,如:

输入温度,完成后输入00:

 60 80 97 42

 Average is: 69.75

我不想输入如下所示的整数:

输入温度,完成后输入00:75

输入温度,完成后输入00:80

输入温度,完成后输入00:46

输入温度,完成后输入00:91

平均值是:73

6 个答案:

答案 0 :(得分:2)

#include <stdio.h>
#include <string.h>

int main(void){
    char input[64];
    double ave = 0.0, value;
    int count = 0;

    printf("Enter the temperatures and Enter 00 when finished:\n");
    while(1){
        if(1==scanf("%63s", input)){
            if(strcmp(input, "00") == 0)
                break;
            if(1==sscanf(input, "%lf", &value))
                ave += (value - ave) / ++count;
        }
    }
    if(count)
        printf("Average is: %g\n", ave);
    else
        printf("Input one or more values\n");

    return 0;
}

答案 1 :(得分:0)

我相信您应该从输入00改为终止条件,如输入x。 所以,你的代码看起来像这样::

int n;
int sum = 0, count = 0;
while(scanf("%d", &n)) {
    sum = sum + n;
    count++;
}
printf("%lf", double(sum/count));

scanf返回成功获取的输入数。由于n被声明为int,因此每当您输入一些整数值时,scanf将返回1,如果您输入的某些值不是int类型例如,如果您输入xchar} scanf将返回0,因为x不是整数,这样您就可以计算平均值。

答案 2 :(得分:0)

使用scanf函数,任何空格字符都被视为每个整数的输入结束。因此,在循环中使用scanf,您可以在同一行中连续输入值。

如果您希望每次必须修改代码以使用while循环并具有动态分配的数组时,它可以用于不同数量的条目,因为大小未知。然后检查转义序列,如00.

所有值都存储在一个数组中,您可以在其中进行平均计算

#include <stdio.h>

#define NUM_OF_ENTRIES 5

int main()
{
    printf("Enter numbers: ");
    int i = 0;
    int value_set[NUM_OF_ENTRIES];
    for (i = 0; i < NUM_OF_ENTRIES; i++ )
    {
        scanf("%d", &value_set[i]);
    }

答案 3 :(得分:0)

代码可以使用scanf("%d", &number)来读取整数。问题是"%d"在扫描'\n'之前首先扫描并丢弃包含int的前导空格。需要'\n'知道何时停止,因为OP希望&#34;直接输入整数&#34;。因此,代码应该首先一次查找空格一个字符。找到行尾'\n'后,扫描完成。

使用这种方法,数字计数没有实际限制。

#include <ctype.h>
#include <stdio.h>

double Line_Average(void) {
  double sum = 0;
  unsigned long long count = 0;

  while (1) {
    int ch;
    while (isspace(ch = fgetc(stdin)) && ch != '\n')
      ;
    if (ch == '\n' || ch == EOF) {
      break; // End-of-line or End-if file detected.
      }
    ungetc(ch, stdin); // Put back character for subsequent `scanf()`

    int data;
    if (scanf("%d", &data) != 1) {
       break; // Bad data
    }
    sum += data;
    count++;
  }
  return sum/count;
}

// sample usage
puts("Enter the temperatures");
double Average = Line_Average();
printf("Average is: %.2f\n", Average);

答案 4 :(得分:0)

一种可能性:

  double sum = 0;
  double val;
  size_t count = 0;

  char follow;

  while( scanf( "%lf%c", &val, &follow ) == 2 )
  {
    sum += val;
    count++;
    if ( follow == '\n' )
      break;
  }

  printf( "average = %f\n", sum/count );

这将读取每个数字加上紧跟其后的字符,直到它看到换行符或非数字字符串。它并不完美;如果你输入一个数字后跟一个空格后跟一个换行符,那么它就不会破坏循环。但它应该给你一些想法。

答案 5 :(得分:-1)

因为你没有发布代码。我已经给出了一个示例代码...从这里你可以用一些调整来构建你需要的东西

#include<stdio.h>

main()
{
   int one, two, thr, four, five, avg;
    printf("\nEnter the temperatures and Enter 00 when finished:");
    scanf ("%d %d %d %d %d", &one, &two, &thr, &four, &five);
    avg=(one+two+thr+four+five)/5;
    printf("Average value is %d", avg);
}
相关问题