在C中直接输入整数

编程入门 行业动态 更新时间:2024-10-11 19:24:56
本文介绍了在C中直接输入整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在上C语言入门课程,我想知道是否有一种方法可以直接输入整数并将它们平均在一起?我试图使我的程序尽可能整洁.

I'm in a beginner C course and I was wondering if there's a way to input integers straight across and averages them together? I'm trying to make my program nice and tidy as possible.

我想像这样直接输入整数:

I want to input integers straight across like:

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

Enter the temperatures and Enter 00 when finished:

60 80 97 42 Average is: 69.75

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

I don't want to input integers like shown below:

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

Enter the temperatures and Enter 00 when finished: 75

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

Enter the temperatures and Enter 00 when finished: 80

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

Enter the temperatures and Enter 00 when finished: 46

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

Enter the temperatures and Enter 00 when finished: 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中直接输入整数

本文发布于:2023-11-02 18:18:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1553081.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:整数   直接输入

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!