?? 氣象信息.cpp
字號:
//天氣
#include<stdlib.h> //exit()函數的定義
#include<iostream.h>
#include<fstream.h> //IO所需庫
#define FILE_IN "weather.in"
#define FILE_OUT "report.out"
float FindAve(float[],int);
void HighLow(float[],float[],float*,float*,int);
void main()
{
float high[31],low[31],high_ave,low_ave,hottest,coldest;
char date[25],location[100];
int total_days,i=0;
//先定義輸入輸出流
ifstream input; //輸入流對象
ofstream output; //輸入流對象
//打開文件用于讀數據,如果文件不在,不要創建文件
input.open(FILE_IN,ios::in|ios::nocreate);
//檢查文件是否打開。另外一種方法是利用fail函數
if(!input)
{
cout<<"\n未找到輸入文件"<<FILE_IN;
cout<<"\n退出程序,再見!\n";
exit(1);
}
output.open(FILE_OUT,ios::out);
cout<<"\n文件找到,繼續執行……";
//讀入兩行,并存入字符串中
input.getline(date,24);
input.getline(location,49);
cout<<location<<endl;
while(!input.eof()) //一直讀到文件結束
{
input>>high[i]>>low[i]; //文件指針位置必須無誤,不能出現非預期數據
++i;
}
total_days=i-1; //溫度記錄的天數為i
cout<<"\n讀數據完畢"<<endl;
high_ave = FindAve(high,total_days);
low_ave = FindAve(low,total_days);
HighLow(high,low,&hottest,&coldest,total_days);
//向輸出文件寫數據
output<<"Monthly Weather Report\n\nLocation:\t"<<location
<<"\nDate:"<<date;
output<<"\n\nAverage Temperatures:High "<<high_ave
<<", low "<<low_ave;
output<<"\n\nHottest Temperature:\t"<<hottest
<<"\nColdest Temperature:\t"<<coldest;
input.close();
output.close();
}
float FindAve(float x[],int total)
{
float sum=0.0;
int i;
for(i=0;i<total;++i)
{
sum+=x[i];
}
return (sum/total);
}
void HighLow(float high[],float low[],float *h,float *l,int total)
{
*h=high[0],*l=low[0];
int i;
//遍歷兩個數組,尋找最大值……
for(i=0;i<total;++i)
{
if(high[i]>*h)*h=high[i];
if(low[i]<*l)*l=low[i];
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -