创建一个字符串函数以返回一个月

时间:2016-12-12 02:22:17

标签: c++

这是我目前拥有的代码。就目前而言,它可以完成所需的一切,以返回必要的相应值。然而,我遗漏的一件事是一个函数,它将返回与温度相关的索引号对应的月份。

#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;
}

我需要将列和行值传递给字符串函数,但我似乎无法正确使用它。有什么指针吗?

感谢。

1 个答案:

答案 0 :(得分:0)

string monthForTemp(double temps[][NUM_COLS], NUM_ROWS)

声明不正确,无法执行任务。它应该看起来像

string monthForTemp(int index)

但要做到这一点,indexHighTemp和其余的索引返回函数需要更改为

int indexHighTemp(double temps[][NUM_COLS], int NUM_ROWS) //returns int
{
    cout << fixed << showpoint << setprecision(1);
    int index = 0; // index is int
    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用作索引。您不能从数组中获取3.1459的项目。此外,由于iint,因此index成为double没有多大意义。它最好是一个浮点表示的整数,在最坏的情况下(这里不是问题,因为数字很小)它会被floating point imprecision损坏。

这是Head into X-Y territory的一个很好的理由,并提出了一个完全不同的解决方案:

用数组替换monthForTemp函数。例如:

std::string monthForTemp[] = 
{
    "January", 
    "February", 
    "March", 
    // rest of months go here 
};

可以用作

cout << "High temp month is: " << monthForTemp[indexHiTemp]  
     << " which is " << temps[indexHiTemp][0]
     << " degrees F" << endl;

假设indexHighTemp已被修改为按上述建议返回int