gpt4 book ai didi

c - 在 while 循环中将值存储在数组中

转载 作者:行者123 更新时间:2023-11-30 20:35:47 24 4
gpt4 key购买 nike

因此,程序应该根据用户给出的初始温度计算温度,然后根据风、湿度和条件进行更改。首先从用户处获取起始温度,然后显示一个选择菜单,包括输入风速、输入湿度、输入条件、显示当前温度、显示初始温度修改后的最后 1000 个温度以及显示总和所有温度。

我已经完成了大部分工作,但我在如何存储和显示最后 1000 个温度方面遇到了问题。我应该在这个程序中使用一个数组,但我不确定如何使用它来获得我需要的东西。请帮忙。

#define _CRT_SECURE_NO_WARNINGS
#define CLS system("cls")
#define PAUSE system("pause")
#include <stdio.h>
#include <stdlib.h>

// PROTOTYPING FUNCTIONS
void askTemp();
float convertC(float f);
void displayMenu();
char getChoice();
char getCondChoice();
float getHumid();
float getTemp();
float getWind();

main() {
// INITIALIZE VARIABLES
float temp;
float wind = 5;
float humid = 20;
int trump = 0;
char choice = 'A';
int num;
char cond = 'S';
char condChoice;
float celsius;
int i = 0;
int j = 0;
int k = 0;
float temp2 = 0;
float wind2 = 0;
float humid2 = 0;
int x = 0;

askTemp();
temp = getTemp();

do{
displayMenu();
choice = getChoice();

switch (choice){
case 'W': // Get Wind
wind = getWind();
break;
case 'H': // Get the humidity
humid = getHumid();
break;
case 'C': // Enter subswitch statement to get condition
condChoice = getCondChoice();
switch (condChoice){ // gets condition
case 'S':
cond = 'S';
break;
case 'C':
cond = 'C';
break;
case 'R':
cond = 'R';
break;
}// END SWITCH
case 'T': // display current temperature

if (temp != temp2 || wind != wind2 || humid != humid2){ // apply degree changes based on humid, wind, and cond
if (wind > 100)
temp = temp - (temp * .03);
if (wind < 10)
temp = temp + (temp * .025);
if (humid > 75)
temp = temp + (temp * .058933);
if (humid < 20)
temp = temp - (temp * .0162);
if (cond == 'S' && wind > 30)
temp = temp + (temp * .01);
if (cond == 'S' && wind == 0)
temp = temp + (temp * .005);
if (cond == 'R')
temp = temp - (temp * .02);
if (cond == 'C' && humid > 75)
temp = temp + (temp * .01);
if (cond == 'C' && humid < 75)
temp = temp + (temp * .005);
if (wind > 30 && humid > 75 && cond == 'S')
temp = temp;
wind2 = wind; // assigned variables so program does not apply condition changes multiple times for same temp
humid2 = humid;
temp2 = temp;
}// END IF
celsius = convertC(temp2);
printf("The Current temperature is %.1ff/%.1fc degrees Celsius\n", temp2, celsius);
PAUSE;
break;
case 'P': // Display previous 1000 temperatures

break;
case 'A': // Display average of all temps entered
break;
}// END SWITCH

} while (choice != 'Q');



} // END MAIN

void askTemp(){
printf("What is the current temperature: \n");
return;
}// end askTemp

void displayMenu(){
CLS;
printf("W. Enter Wind Speed\n");
printf("H. Enter Humidity\n");
printf("C. conditions\n");
printf("T. Current Temperature\n");
printf("P. Previous Temps\n");
printf("A. Average Temperature\n");
printf("Q. Quit\n");
return;
} // end currentTemp

char getChoice(){
char result;
scanf("%c", &result);
result = toupper(result);
return result;
}// end getChoice

char getCond(){
char result;


}// end getCond

char getCondChoice(){
char result;
printf("S for sunny, C for cloudy, R for rainy\n");
scanf(" %c", &result);
result = toupper(result);
do{
if (result != 'S' && result != 'C' && result != 'R'){
printf("Invalid input try again\n");
scanf(" %c", &result);
result = toupper(result);
}
} while (result != 'S' && result != 'C' && result != 'R');
return result;
}// end getCond

float getHumid(){
float result;
scanf("%f", &result);
return result;
}// end getHumid

float getTemp(){
float result;
scanf("%f", &result);
do{

if (result < -50 || result > 150){
printf("Invalid temperature try again\n");
scanf("%f", &result);
}
} while (result < -50 || result > 150);
return result;
}// end getTemp

float getWind(){
float result;
scanf("%f", &result);
return result;
}// end getWind

float convertC(float f){
float t;
t = (f - 32) / 1.8;
return(t);
}// end convert

最佳答案

您声明一个包含 1000 个 float 的数组,如下所示:

float saved_temperatures[1000];

您可以通过索引数组来存储值,并将其像变量一样使用。

saved_temperatures[i] = temp;
++i;

i 是一个索引变量,您应该确保它从 0 开始并且始终小于 1000。saved_Temperatures 的有效索引范围是 0..999 .

for (int i = 0; i < 1000; ++i) {
printf("index %d temperature %f\n", i, saved_temperatures[i]);
}

关于c - 在 while 循环中将值存储在数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38109814/

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