2012-02-23 53 views
0

我需要一個函數,它將經過下面的值並以其名稱打印出最高值。要找到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() 
{ 



} 
+0

請顯示您到目前爲止的內容。該解決方案可能是對現有代碼的直接修改。 – 2012-02-23 04:50:46

+0

數值如何存儲?一個'map '? – 2012-02-23 04:50:56

+0

你想做什麼?清楚。 – 2012-02-23 04:52:15

回答

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

} 
+0

這是一個糟糕的解決方案。如果兩個地區的銷售額相同,會發生什麼情況?另外,如果你有100個地區,你會怎麼做? – 2012-02-23 20:18:51

+0

你是對的,但這是我書中的練習,我們不能使用結構或數組,因爲它還沒有被覆蓋。 – Onat 2012-02-28 02:27:38

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

我希望它會幫助你

+0

如果您切換地圖的鍵和值,getHighest函數被簡化爲僅僅解除引用end()。 – bitmask 2012-02-23 06:45:59

+0

@bitmask:解引用'end()'是未定義的行爲,它應該是'* - end()'或'* rbegin()'。 :) – Xeo 2012-02-23 13:37:18

+0

@Xeo:是的,當然你是對的 - 一定是分心了。很好的抓住:) – bitmask 2012-02-23 14:47:53

2

使用結構來存儲字符串值和雙值。 字符串將給出名稱和銷售額的兩倍。

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; 
} 
+0

+1使用結構,避免使用標準的模板庫函數。 – CppLearner 2012-02-23 10:43:35

+0

STL函數更適合用於高級程序,對於基本的和不重要的結構來說是足夠好的。您可以隨時修改陣列以vetor和結構映射或多地圖 – 2012-02-23 10:55:14