-5
這裏是我現在擁有的代碼。就目前而言,它可以完成它所需的一切,以返回必要的相應值。但是,我缺少的一件事是返回與溫度相關的索引號相對應的月份的函數。創建一個字符串函數返回一個月
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdlib>
using namespace std;
const int NUM_COLS = 2;
const int NUM_ROWS = 12;
void getData(ifstream& tempFile, double temps[][NUM_COLS]);
double averageHigh(double temps[][NUM_COLS], int NUM_ROWS);
double averageLow(double temps[][NUM_COLS], int NUM_ROWS);
double indexHighTemp(double temps[][NUM_COLS], int NUM_ROWS);
double indexLowTemp(double temps[][NUM_COLS], int NUM_ROWS);
string monthForTemp(double temps[][NUM_COLS], int NUM_ROWS);
/*
*
*/
int main(int argc, char** argv)
{
ifstream tempFile;
double temps[NUM_ROWS][NUM_COLS] = {0.0};
tempFile.open("Ch8_ex9Data.txt");
if (!tempFile)
{
cout << "File does not exist, contact system admin" << endl;
return 1;
}
getData (tempFile, temps);
double highAverage = averageHigh(temps, NUM_ROWS);
cout << "Average high is: " << highAverage << endl;
double lowAverage = averageLow(temps, NUM_ROWS);
cout << "Average low is: " << lowAverage << endl;
int indexHiTemp = indexHighTemp(temps, NUM_ROWS);
cout << "High temp index is: " << indexHiTemp
<< " which is " << temps[indexHiTemp][0]
<< " degrees F" << endl;
int indexLoTemp = indexLowTemp(temps, NUM_ROWS);
cout << "Low temp index is: " << indexLoTemp
<< " which is " << temps[indexLoTemp][1]
<< " degrees F" << endl;
return 0;
}
void getData(ifstream& tempFile, double temps[][NUM_COLS])
{
cout << fixed << showpoint << setprecision(1);
double hiTemp, loTemp;
tempFile >> hiTemp >> loTemp;
int rowIndex = 0;
while(tempFile)
{
temps[rowIndex][0] = hiTemp;
temps[rowIndex][1] = loTemp;
rowIndex++;
tempFile >> hiTemp >> loTemp;
}
}
double averageHigh(double temps[][NUM_COLS], int NUM_ROWS)
{
double avgHigh = 0.0;
double sum = 0.0;
int i;
for (i = 0; i < NUM_ROWS; i++)
{
sum += temps[i][0];
//cout << temps[i][0] << endl;
}
avgHigh = sum/NUM_ROWS;
return avgHigh;
}
double averageLow(double temps[][NUM_COLS], int NUM_ROWS)
{
double avgLow = 0.0;
double sum = 0.0;
int i;
for (i = 0; i < NUM_ROWS; i++)
{
sum += temps[i][1];
//cout << temps[i][1] << endl;
}
avgLow = sum/NUM_ROWS;
return avgLow;
}
double indexHighTemp(double temps[][NUM_COLS], int NUM_ROWS)
{
cout << fixed << showpoint << setprecision(1);
double index = 0.0;
double maxHighTemp = 0.0;
for (int i = 0; i < NUM_ROWS; i++)
{
if (temps[i][0] > maxHighTemp)
{
maxHighTemp = temps[i][0];
index = i;
}
}
return index;
}
double indexLowTemp(double temps[][NUM_COLS], int NUM_ROWS)
{
cout << fixed << showpoint << setprecision(1);
double index = 0.0;
double maxLowTemp = 0.0;
string month = " ";
for (int i = 0; i < NUM_ROWS; i++)
{
if (temps[i][0] < maxLowTemp)
{
maxLowTemp = temps[i][1];
index = i;
}
}
return index, month;
}
string monthForTemp(double temps[][NUM_COLS], NUM_ROWS)
{
string month = " ";
switch (index)
{
case 1 :
month = "January";
break;
case 2 :
month = "February";
break;
case 3 :
month = "March";
break;
case 4 :
month = "April";
break;
case 5 :
month = "May";
break;
case 6 :
month = "June";
break;
case 7 :
month = "July";
break;
case 8 :
month = "August";
break;
case 9 :
month = "September";
break;
case 10 :
month = "October";
break;
case 11 :
month = "November";
break;
case 12 :
month = "December";
break;
}
return month;
}
我需要的行和列值傳遞給字符串函數,但我不能完全似乎得到它的權利。任何指針?
謝謝。
您的代碼不會編譯,並且您不會將月份'索引'傳遞給函數。 –
對。沒有最後的功能,並且沒有「迴歸月」;儘管如此,indexLowTemp的一部分。我試圖找到一種方法將變量傳遞到字符串函數中並獲得一個月作爲響應。 – Ronave4C
這裏顯示的結果: 平均高61.3 平均低40.7 高溫度指數:6是78.2華氏度 低溫指數爲:0是23.7華氏度 我應該讓地方indexLowTemp和indexHighTemp函數也返回一個字符串?或者它應該是一個單獨的功能? – Ronave4C