显示其名称的最高值

时间:2012-02-23 04:49:40

标签: c++

我需要一个能够通过以下值的函数,并使用其名称打印出最高值。找到20的最高值并不难,但我无法找到显示值名称的方法。谢谢!

示例:

North: 5
South: 10
West: 15
East :20

输出:

Winner is East with $20 in sales!

这是我到目前为止所得到的

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

double northeast, northwest, southeast, southwest;
// Function prototype
double getSales(string);
void findHighest();

int main()
{
    northeast = getSales("Northeast");
    northwest = getSales("Northwest");
    southeast = getSales("Southeast");
    southwest = getSales("Southwest");


    return 0;
}

//Function getSales
double getSales(string name)
{   cout << "What is the quarterly sales figure for " << name << "? ";
    double sales;
    cin >> sales;
    while (sales < 0)
    {
        cout << "Please enter a positive value ";
        cin >> sales;
    }
    return sales;
}

// Function getHighest
void getHighest()
{



}

3 个答案:

答案 0 :(得分:2)

使用struct来存储字符串值和double值。     字符串将提供名称并将销售额翻倍。

struct sample
{
    string name;
    double sales;
};

void main()
{
    sample array[4];
    array[0].name = "NE";
    array[1].name = "NW";
    array[2].name = "SE";
    array[3].name = "SW";
    int i =0;
    while(i<4)
    {
       cout<<array[i].name;
       cin>>array[i].sales;
    }
    sample greatest;
    greatest.sales = array[0].sales;
    greatest.name = array[0].name;
    for(i = 0; i<4,i++)
    {
       if(ar[i+1].sales > array[i].sales)
       {
          greatest.sales = array[i+1].sales; 
          greatest.name = array[i+1].name;
       }
    }
    cout<<"greatest"<<greatest.name<<greatest.sales;
}

答案 1 :(得分:0)

您可以声明地图变量map<string, double> b

northeast = getSales("Northeast");
b.insert(pair<string,double>("Northeast",northeast));

northwest = getSales("Northwest");
b.insert(pair<string,double>("Northwest",northwest));

然后添加你的getHighest方法,如下所示:

void getHighest()
{
    map<string,double>::iterator i;
    string name;
    double value = b.begin()->second;

    for ( i = b.begin(); i != b.end(); i++ )
    {
        if ( value < i->second ) 
        {
            name = i->first;
            value = i->second;
        }
    }
    cout << name << " " << value << endl;
}

我希望它能帮到你

答案 2 :(得分:-1)

感谢您的回复。以下是我正在寻找的答案。

#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

double NE, NW, SE, SW;
// Function prototype
double getSales(string);
void getHighest(double,double,double,double);

int main()
{
    NE = getSales("Northeast");
    NW = getSales("Northwest");
    SE = getSales("Southeast");
    SW = getSales("Southwest");

    cout << setprecision(2) << fixed;
    getHighest(NE, NW, SE, SW);

    return 0;
}

//Function getSales
double getSales(string name)
{   cout << "What is the quarterly sales figure for " << name << "? ";
    double sales;
    cin >> sales;
    while (sales < 0)
    {
        cout << "Please enter a positive value for " << name << " ";
        cin >> sales;
    }
    return sales;
}

// Function getHighest
void getHighest(double NE, double NW,
                 double SE, double SW)
{
    if (NE > SE && NE > NW && NE > SW)
        cout << "\nNortheast is the highest grossing division with $" << NE << endl;
    if (NW > NE && NW > SE && NW > SW)                      
        cout << "\nNorthwest is the highest grossing division with $" << NW << endl;
    if (SE > SW && SE > NW && SE > NE)                        
        cout << "\nSoutheast is the highest grossing division with $" << SE << endl;
    if (SW > SE && SW > NW && SW > NE)                        
        cout << "\nSouthwest is the highest grossing division with $" << SW << endl;

}