Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 用其名称显示最高值_C++ - Fatal编程技术网

C++ 用其名称显示最高值

C++ 用其名称显示最高值,c++,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

我需要一个函数,将通过下面的值,并打印出其名称的最高值。不难找到最高值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()
{



}
#包括“stdafx.h”
#包括
#包括
使用名称空间std;
双东北、西北、东南、西南;
//功能原型
双重销售(字符串);
void findHighest();
int main()
{
东北=getSales(“东北”);
西北=getSales(“西北”);
东南=getSales(“东南”);
西南=getSales(“西南”);
返回0;
}
//函数getSales
双getSales(字符串名称)
{销售部;
}
退货;
}
//函数getHighest
void getHighest()
{
}

您可以声明映射变量
map b

northeast=getSales(“东北”);
b、 插入(成对(“东北”,东北));
西北=getSales(“西北”);
b、 插入(成对(“西北”,西北));
然后添加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;
}
void getHighest()
{
迭代器i;
字符串名;
双值=b.开始()->秒;
for(i=b.begin();i!=b.end();i++)
{
如果(值秒)
{
name=i->first;
值=i->秒;
}
}

不能使用结构来存储字符串值和双精度值。 字符串将给出名称并使销售额加倍。

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;
}
struct示例
{
字符串名;
双重销售;
};
void main()
{
样本数组[4];
数组[0]。name=“NE”;
数组[1]。name=“NW”;
数组[2]。name=“SE”;
数组[3]。name=“SW”;
int i=0;

谢谢你的回答。下面是我想要的答案

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

}
#包括“stdafx.h”
#包括
#包括
#包括
使用名称空间std;
双东北,西北,东南,西南;
//功能原型
双重销售(字符串);
最高无效(双倍,双倍,双倍,双倍);
int main()
{
NE=getSales(“东北”);
NW=getSales(“西北”);
SE=getSales(“东南”);
SW=getSales(“西南”);

无法显示到目前为止您所获得的。解决方案可能是对现有代码的直接修改。值是如何存储的?a
map
?您想做什么?请清楚。这听起来像是家庭作业。如果是,请将其标记为这样。如果您切换map的键和值,则
getHighest
函数是reduced仅取消引用
end()
@bitmask:取消引用
end()
是未定义的行为,应该是
*--end()
*rbegin()
:)@Xeo:是的,当然你是对的——一定是分心了。很好的陷阱:)+1用于使用struct,避免使用标准模板库函数。STL函数更适合于高级程序,对于基本和琐碎的程序struct就足够了。你可以随时将数组修改为vetor,将struct修改为map或multi-map这是一个糟糕的解决方案假设两个地区都有相同的最高销售额,会发生什么?另外,如果你有100个地区,你会怎么做?你是对的,但这是我书中的一个练习,我们不能使用struct或array,因为它还没有被介绍。
#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;

}