C 在while循环中存储数组中的值
因此,该程序应该根据用户给定的初始温度计算温度,然后根据风、湿度和条件进行更改。首先从用户处获取起始温度,然后显示选项菜单,包括输入风速、输入湿度、输入条件、显示当前温度、显示初始温度修改后的最后1000个温度以及显示所有温度的总和 我已经做了大部分,但是我在如何存储和显示最后1000个温度方面遇到了问题。我应该在这个程序中使用一个数组,但我不知道如何使用它来获得我需要的。请帮忙C 在while循环中存储数组中的值,c,arrays,do-while,C,Arrays,Do While,因此,该程序应该根据用户给定的初始温度计算温度,然后根据风、湿度和条件进行更改。首先从用户处获取起始温度,然后显示选项菜单,包括输入风速、输入湿度、输入条件、显示当前温度、显示初始温度修改后的最后1000个温度以及显示所有温度的总和 我已经做了大部分,但是我在如何存储和显示最后1000个温度方面遇到了问题。我应该在这个程序中使用一个数组,但我不知道如何使用它来获得我需要的。请帮忙 #define _CRT_SECURE_NO_WARNINGS #define CLS system("cls")
#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
\define\u CRT\u SECURE\u NO\u警告
#定义CLS系统(“CLS”)
#定义暂停系统(“暂停”)
#包括
#包括
//原型功能
void askTemp();
浮点数c(浮点数f);
void displayMenu();
char getChoice();
char getCondChoice();
浮球(湿球);
float getTemp();
浮动风();
main(){
//初始化变量
浮子温度;
浮风=5;
浮子湿度=20;
int特朗普=0;
字符选择='A';
int-num;
char cond='S';
角色选择;
浮动摄氏度;
int i=0;
int j=0;
int k=0;
浮点数2=0;
浮动风2=0;
浮标湿度d2=0;
int x=0;
askTemp();
temp=getTemp();
做{
显示菜单();
choice=getChoice();
开关(选择){
案例“W”://获取风
wind=getWind();
打破
案例'H'://获取湿度
潮湿=潮湿();
打破
案例'C'://输入subswitch语句以获取条件
condChoice=getCondChoice();
开关(condChoice){//获取条件
案例S:
cond='S';
打破
案例“C”:
cond='C';
打破
案例“R”:
cond='R';
打破
}//终端开关
案例“T”://显示当前温度
如果(temp!=temp2 | | |风!=wind2 | | |潮湿!=humid2){//根据湿度、风和条件应用度更改
如果(风>100)
温度=温度-(温度*.03);
如果(风力<10)
温度=温度+(温度*.025);
如果(湿度>75)
温度=温度+(温度*.058933);
如果(湿度<20)
温度=温度-(温度*.0162);
如果(cond=='S'和&wind>30)
温度=温度+(温度*.01);
如果(cond='S'&&wind==0)
温度=温度+(温度*.005);
如果(cond=='R')
温度=温度-(温度*.02);
如果(条件='C'&湿度>75)
温度=温度+(温度*.01);
如果(条件='C'&湿度<75)
温度=温度+(温度*.005);
如果(风>30且湿度>75&&cond=='S')
温度=温度;
wind2=wind;//已分配变量,因此程序不会对同一温度多次应用条件更改
湿度2=潮湿;
temp2=temp;
}//如果结束
摄氏度=摄氏度(温度2);
printf(“当前温度为%.1ff/%.1fc摄氏度\n”,温度2,摄氏度);
暂停;
打破
案例“P”://显示以前的1000个温度
打破
案例“A”://显示输入的所有临时值的平均值
打破
}//终端开关
}while(choice!=“Q”);
}//末端总管
void askTemp(){
printf(“当前温度是多少:\n”);
返回;
}//结束askTemp
void displayMenu(){
CLS;
printf(“W.输入风速\n”);
printf(“H.输入湿度\n”);
printf(“C.条件”);
printf(“T.当前温度\n”);
printf(“P.以前的临时工”);
printf(“A.平均温度\n”);
printf(“Q.Quit\n”);
返回;
}//结束当前温度
char getChoice(){
字符结果;
scanf(“%c”、&result);
结果=toupper(结果);
返回结果;
}//结束选择
char getCond(){
字符结果;
}//结束秒
char getCondChoice(){
字符结果;
printf(“S表示晴天,C表示多云,R表示下雨\n”);
scanf(“%c”、&result);
结果=toupper(结果);
做{
如果(结果!='S'&&result!='C'&&result!='R'){
printf(“无效输入重试\n”);
scanf(“%c”、&result);
结果=toupper(结果);
}
}while(result!=“S”&&result!=“C”&&result!=“R”);
返回结果;
}//结束秒
浮点数(){
浮动结果;
scanf(“%f”,和结果);
返回结果;
}//结束加湿
浮点getTemp(){
浮动结果;
scanf(“%f”,和结果);
做{
如果(结果<-50 | |结果>150){
printf(“无效温度重试\n”);
scanf(“%f”,和结果);
}
}而(结果<-50 | |结果>150);
返回结果;
}//end getTemp
float getWind(){
浮动结果;
scanf(“%f”,和结果);
返回结果;
}//结束格特温
浮点数转换器C(浮点数f){
浮动t;
t=(f-32)/1.8;
返回(t);
}//端转换
您声明了一个1000个浮点数组,如下所示:
float saved_temperatures[1000];
通过索引到数组中并将其作为变量使用,可以在其中存储值
saved_temperatures[i] = temp;
++i;
i
是一个索引变量,您应该确保它从零开始,并且始终小于1000。保存的温度的有效索引范围为0..999
for (int i = 0; i < 1000; ++i) {
printf("index %d temperature %f\n", i, saved_temperatures[i]);
}
for(int i=0;i<1000;++i){
printf(“索引%d温度%f\n”,i,保存的温度[i]);
}
请注意,在C中,除了用于convertC()的原型之外,所有的“原型”都只是函数声明,而不是原型。他们说虚构是存在的,但与参数列表无关(只是它不是一个变量列表)。使