C 编程 - 在 main() 之外的函数中收集数据
C Programming - Collecting data in a function outside of main()
编写一个程序,要求用户输入每日降雨量。您的程序将需要接受 5 次每日降雨量输入。只允许非负降雨量。当用户输入负数时,告诉他们该数字无效,他们应该输入另一个有效值。
计算总降雨量和平均降雨量。求最大日降雨量和最小日降雨量
使用信息性消息输出总值、平均值、最大值和最小值。
main中不能发生以下情况:
- 正在接受用户输入
- 计算总数或平均值
- 最大或最小的判断
- 输出结果
=============================================
现在我只是想弄清楚如何输入 5 个数字,到目前为止我有这段代码,但它让我输入了无数次。我已经在这个项目上工作了几个小时,所以任何建议都会很棒。
#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;
}
您可以创建一个结构来存储您的数据并执行操作。
类似于:
#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
在输入错误的情况下很痛苦最好是读取一行然后解析它,检查 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;
}
}
正如你所说的递归,它实际上会创建一个无限循环,实际上,为此你也不需要它,你可以这样做:
Running sample(评论更改)
#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;
}
编写一个程序,要求用户输入每日降雨量。您的程序将需要接受 5 次每日降雨量输入。只允许非负降雨量。当用户输入负数时,告诉他们该数字无效,他们应该输入另一个有效值。
计算总降雨量和平均降雨量。求最大日降雨量和最小日降雨量
使用信息性消息输出总值、平均值、最大值和最小值。
main中不能发生以下情况:
- 正在接受用户输入
- 计算总数或平均值
- 最大或最小的判断
- 输出结果
=============================================
现在我只是想弄清楚如何输入 5 个数字,到目前为止我有这段代码,但它让我输入了无数次。我已经在这个项目上工作了几个小时,所以任何建议都会很棒。
#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;
}
您可以创建一个结构来存储您的数据并执行操作。
类似于:
#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
在输入错误的情况下很痛苦最好是读取一行然后解析它,检查 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;
}
}
正如你所说的递归,它实际上会创建一个无限循环,实际上,为此你也不需要它,你可以这样做:
Running sample(评论更改)
#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;
}