gpt4 book ai didi

c - 在C中直接输入整数

转载 作者:行者123 更新时间:2023-11-30 21:02:12 25 4
gpt4 key购买 nike

我正在学习 C 初学者类(class),我想知道是否有一种方法可以直接输入整数并将它们平均在一起?我正在努力使我的程序尽可能漂亮和整洁。

我想直接输入整数,例如:

输入温度并在完成后输入 00:

 60 80 97 42

Average is: 69.75

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

输入温度并在完成后输入 00:75

输入温度并在完成后输入 00:80

输入温度并在完成后输入 00:46

输入温度并在完成后输入 00:91

平均值为:73

最佳答案

#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;
}

关于c - 在C中直接输入整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31664158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com