[英]C Programming - Collecting data in a function outside of main()
Write a program that asks the user to enter daily rainfall amounts.编写一个程序,要求用户输入每日降雨量。 Your program will need to accept 5 daily rainfall inputs.
您的程序需要接受 5 个每日降雨量输入。 Only allow non-negative rainfall amounts.
只允许非负降雨量。 When the user enters a negative number, tell them that the number is invalid, and that they should enter another, valid value.
当用户输入负数时,告诉他们该数字无效,他们应该输入另一个有效值。
Calculate the total rainfall and the average rainfall.计算总降雨量和平均降雨量。 Determine the largest daily rainfall and the smallest daily rainfall.
确定最大日降雨量和最小日降雨量。
Output the total, average, largest, and smallest values using informative messages.使用信息性消息输出总数、平均值、最大值和最小值。
The following things cannot happen in main:以下事情不能在 main 中发生:
============================================= ==============================================
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5 // have the user enter it 5 times
double CollectRainfall() {
double amount;
double rainfall[SIZE];
int i;
printf("Enter a rainfall amount: \n"); // enter amount
scanf_s("%lf", &amount);
for (i = 0; i < SIZE; i++) {
rainfall[i] = CollectRainfall();
while (amount < 0.0) { // if it's a negative number
printf("The number is invalid.\n"); // display error message if a negative # was entered
printf("Enter another rainfall amount: \n");
}
}
}
int main() {
CollectRainfall();
return 0;
}
You can create a struct to store you data and do the operation.您可以创建一个结构来存储数据并执行操作。
Something like:就像是:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5 // have the user enter it 5 times
typedef struct data {
double rainfall[SIZE];
double average;
double min;
double max;
} data_t;
static void collectRainfall(double rainfall[SIZE]) {
for (int i = 0; i < SIZE; i++) {
double amount;
printf("Enter a rainfall amount: \n"); // enter amount
scanf("%lf", &amount);
while (amount < 0.0) { // if it's a negative number
printf("The number is invalid.\n"); // display error message if a negative # was entered
printf("Enter a rainfall amount: \n"); // enter amount
scanf("%lf", &amount);
}
rainfall[i] = amount;
}
}
static void compute(data_t *data) {
data->min = data->rainfall[0];
data->max = data->rainfall[0];
data->average = data->rainfall[0];
for (int i = 1; i < SIZE; i++) {
double rainfall = data->rainfall[i];
if (rainfall > data->max) {
data->max = rainfall;
}
if (rainfall < data->min) {
data->min = rainfall;
}
data->average += rainfall;
}
data->average /= SIZE;
}
static void display(data_t *data) {
printf("min %f, max %f, average %f\n",
data->min, data->max, data->average);
}
int main() {
data_t data;
collectRainfall(data.rainfall);
compute(&data);
display(&data);
return 0;
}
scanf
is a pain in case of bad input best is to read a line then parse it, check if strtod
is ok scanf
在输入错误的情况下很痛苦,最好是读取一行然后解析它,检查strtod
是否正常
static void collectRainfall(double rainfall[SIZE]) {
for (int i = 0; i < SIZE; i++) {
char str[32];
double amount = -1;
printf("Enter a rainfall amount [%d/%d]: \n", i , SIZE);
while (42) {
char *res = fgets(str, sizeof(str), stdin);
if (res && (amount = strtod(str, &res)) >= 0 && res != str)
break;
printf("The number is invalid.\n");
printf("Enter a rainfall amount [%d/%d]: \n", i , SIZE);
}
rainfall[i] = amount;
}
}
As said the recursion as you have it will essentially create an infinite loop, and really, for this you dont need it either, you can do something like:如前所述,递归实际上将创建一个无限循环,实际上,为此您也不需要它,您可以执行以下操作:
Running sample (commented changes)运行示例(注释更改)
#include <stdio.h>
#include <stdlib.h>
#define SIZE 5 // have the user enter it 5 times
void CollectRainfall() { //no return needed
double rainfall[SIZE], sum = 0, max = 0, min = 0;
int i;
for (i = 0; i < SIZE; i++)
{
printf("Enter a rainfall amount: \n"); // enter amount
scanf("%lf", &rainfall[i]); //save values into the array
while (rainfall[i] < 0.0)
{ // if it's a negative number
printf("The number is invalid.\n"); // display error message if a negative # was entered
printf("Enter another rainfall amount: \n");
i--; // iterate back to replace negative number
}
}
printf("Values:");
for (i = 0, min = rainfall[i]; i < SIZE; i++)
{
printf(" %.2lf", rainfall[i]); // print all values
sum += rainfall[i]; // sum values
if(rainfall[i] > max){ //max value
max = rainfall[i];
}
if(min > rainfall[i]){ //min value
min = rainfall[i];
}
}
printf("\nSum: %.2lf", sum); // print sum
printf("\nMax: %.2lf", max); // print max
printf("\nMin: %.2lf", min); // print min
printf("\nAverage: %.2lf", sum / SIZE); //print average
}
int main() {
CollectRainfall();
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.