因此程序应该从用户给出的初始温度计算温度,然后根据风,湿度和条件改变温度。首先从用户获得起始温度,然后显示一个选项菜单,包括输入风速,输入湿度,输入条件,显示当前温度,显示最初温度修改后的最后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
答案 0 :(得分:0)
你声明一个1000 float
的数组,如下所示:
float saved_temperatures[1000];
通过索引到数组中来存储值,并将其用作变量。
saved_temperatures[i] = temp;
++i;
i
是一个索引变量,您应该确保它从零开始并始终小于1000. saved_temperatures
的有效索引范围是0..999。
for (int i = 0; i < 1000; ++i) {
printf("index %d temperature %f\n", i, saved_temperatures[i]);
}