2014-01-24 87 views
0

关于本程序

我有一个显示菜单的程序。该菜单包含一系列项目(字符串)和价格(双重类型值)。该程序没有其他目的,但显示一个菜单。C++转换为货币值

问题

当然,默认情况下,双精度型值以小数显示。这在这种情况下很有用,因为我们列出了物品的价格;然而,通常价格最初始于美元符号($)。据我所知, iomanip标题包含“put_money”&“put_money”函数,但这些不需要做我需要他们做的事情。我需要的是菜单中的每个价格都与美元符号一起显示在实际值旁边。例如:

我所需的输出是这样的:

Please select an item to purchase: 

______________________________________________________ 

Item: Prices: Item: Prices: Item: Prices: 
______________________________________________________ 

Water  $100  Milk  $200 Juice  $300 
    Wine  $400 Bread  $500 Apple  $600 
    Tuna  $700 Steak  $800 Bandage  $900 
Med-Kit $1000 Splint $1100 Thread $1200 

我目前的计划会产生这样:

Please select an item to purchase: 

______________________________________________________ 

Item: Prices: Item: Prices: Item: Prices: 
______________________________________________________ 

Water  100  Milk  200 Juice  300 
    Wine  400 Bread  500 Apple  600 
    Tuna  700 Steak  800 Bandage  900 
Med-Kit 1000 Splint  1100 Thread  1200 

有没有美元符号添加到所有的更有效方式我的价值,而不必手动添加每个单独的美元符号?我只是觉得手动添加它们可能在一个大型项目中很乏味(当然不是这样,但可能类似)。

我的代码

#include <iostream> 
#include <iomanip> 
#include <string> 

int main(void) { 


unsigned short int width = 10; 
unsigned short int prec = 6; 

float prices[12] = {100.00, 200.00, 300.00, 400.00, 500.00, 600.00, 700.00, 800.00, 900.00, 1000.00, 1100.00, 1200.00}; 

std::string items[12] = {"Water", "Milk", "Juice", "Wine", "Bread", "Apple", "Tuna", "Steak", "Bandage", "Med-Kit", "Splint","Thread"}; 

std::cout << "\nPlease select an item to purchase:\n\n"; 

std::cout << "_________________________________________________________________\n\n"; 

std::cout << std::setw(width) << "Item:" << std::setw(width) << "Prices:" <<std::setw(width) << "Item:" <<std::setw(width) << "Prices;" <<std::setw(width) << "Item:" <<std::setw(width) << "Prices:" << "\n"; 

std::cout << "_________________________________________________________________\n\n"; 

std::cout << std::setw(width) <<items[0] <<std::setw(width) << prices[0] <<std::setw(width) << items[1] <<std::setw(width) << prices[1] <<std::setw(width) << items[2] <<std::setw(width) << prices[2] << "\n"; 
std::cout << std::setw(width) <<items[3] <<std::setw(width) << prices[3] <<std::setw(width) << items[4] <<std::setw(width) << prices[4] <<std::setw(width) << items[5] <<std::setw(width) << prices[5] << "\n"; 
std::cout << std::setw(width) <<items[6] <<std::setw(width) << prices[6] <<std::setw(width) << items[7] <<std::setw(width) << prices[7] <<std::setw(width) << items[8] <<std::setw(width) << prices[8] << "\n"; 
std::cout << std::setw(width) <<items[9] <<std::setw(width) << prices[9] <<std::setw(width) << items[10] <<std::setw(width) << prices[10] <<std::setw(width) << items[11] <<std::setw(width) << prices[11] << "\n"; 

std::cin.get(); 
std::cin.get(); 

return 0; 



} 

请原谅的代码过长。感谢大家提前帮助。

+0

我不是要求检查我的代码,但有助于显示货币值。@JustSid – Jake2k13

+0

尝试'std :: cout << std :: setw(width)<< items [0] << std :: setw(width)<<“$”<< prices [0] << std :: setw (width)<< items [1] << std :: setw(width)<<“$”<< prices [1] << std :: setw(width)<< items [2] << std :: setw (width)<<“$”<< prices [2] <<“\ n”;' –

+0

这可行,但我想知道是否有更有效的方法,而不是手动将其添加到每个值。这很容易变成一个单调乏味且压倒一切的过程。但这是一个很好的建议。 @Ankit B – Jake2k13

回答

4

为什么不创建class来存储项目? 例如,你可以有:

class Item 
{ 
    private: 
    double price; 
    string item; 

    public: 
    void display() 
    { 
     cout << "$" << price; 
    } 
}; 

所以你调用display功能显示在货币价值的价格。

+0

好主意,所以将价格数组存储在私人范围内,稍后使用对象和函数调用它们?我仍然在学习C++ – Jake2k13

+0

你可以有一个'Item'数组,例如在你的'main'函数中,你可以有'Item items [12]'。如果您希望程序能够修改它,您还可以在类Item中包含setter/getter函数。 – rcs

3

其实std::put_money可以并且会做你正在问什么如果你正确使用它。

有两部分。首先,你必须选择一个区域。默认的“C”语言环境根本没有定义要使用的货币符号。通常你会希望使用无名的区域,这样的事情:

std::cout.imbue(std::locale("")); 

这个选(并使用)该用户配置他们的环境中的语言环境,所以(比如)一个典型的美国用户将获得一些美国英语区域使用美元符号作为货币符号,但是典型的德国用户将获得欧元符号作为货币符号的德语区域设置。这也会影响数字的格式。下面的示例输出使用逗号作为千位分隔符和句点(句号)作为小数点分隔符。许多欧洲国家的用户(例如)应该期望这两个国家被颠倒过来。

然后,您需要设置流的showbase标志。对于其他数字,这会告诉数据流显示已转换数字的基数,因此(例如)如果您打印出十六进制数字,则会在前面加上0x(或0X)。当你写钱时,它会重新表示是否显示货币符号。

您通常希望将其与您已收到的有关创建类(或结构)以存储有关项目的数据的建议结合使用。而不是一个名为display的函数,我会创建一个operator<<的超载来显示一个项目。

请注意,货币值存储为long double s。在一个典型的情况下,它将作为正常使用中最小面额的货币(例如在美国,整数个便士)的整数存储。因此,初始化为100.0实际上意味着值为$1.00

随着这(和离开了你的头的代码),创建和显示的表可能是这个样子:

#include <iostream> 
#include <iomanip> 
#include <string> 
#include <vector> 

unsigned short int width=10; 
unsigned short int prec=6; 

class item { 
    std::string name; 
    long double price; 

public: 
    item(std::string const &name, double price): name(name), price(price) {} 

    friend std::ostream &operator<<(std::ostream &os, item const &i) { 
     return os<<std::setw(width) << i.name 
       <<std::setw(width) << std::showbase << std::put_money(i.price); 
    } 
}; 

int main(void) { 
    std::vector<item> items { 
     {"water", 10000}, 
     {"Milk", 20000}, 
     {"Juice", 30000}, 
     {"Wine", 40000}, 
     {"Bread", 60000}, 
     {"Apple", 70000}, 
     {"Tuna", 80000}, 
     {"Steak", 90000}, 
     {"Bandage", 100000}, 
     {"Med-Kit", 110000}, 
     {"Splint", 120000}, 
     {"Thread", 130000} 
    }; 

    std::cout.imbue(std::locale("")); 

    unsigned count=0; 

    for (auto &&item:items) { 
     std::cout<<item; 
     if (++count==3) { 
      std::cout<<"\n"; 
      count=0; 
     } 
    } 
} 

结果:

water $100.00  Milk $200.00  Juice $300.00 
    Wine $400.00  Bread $600.00  Apple $700.00 
    Tuna $800.00  Steak $900.00 Bandage $1,000.00 
    Med-Kit $1,100.00 Splint $1,200.00 Thread $1,300.00 

如果您决定做请注意,它不会像在要写入的项目前插入$一样简单。问题在于,当你写出值时,你几乎需要明确地设置宽度和精度。当你这样做,你很快就会发现,$被插入之前人数为填充,所以(例如)你的第一个项目将最终看起来是这样的:

water$ 100.00 

...而不是将货币符号放在您几乎可以肯定需要的数字旁边。它可以防止有足够的额外的工作:

std::ostringstream temp; 

temp << '$' << std::setprecision(your_precision) << value; 

std::cout << std::setw(your_width) << temp.str(); 

虽然这可能是开放的说法,使用std::put_money是多一点的工作比你想,你的时候得到的格式,甚至相当接近对自己来说是正确的,它仍然还有很多工作要做(并且增加对国际化的最小支持也是相当多的工作)。


  1. 不过请注意,,,虽然符号根据用户的语言环境自动选择,没有试图将值转换,所以如果我进入1美元,并显示该值在德语区域,它会显示为1欧元(反之亦然)。从技术上讲,我不相信标准实际上需要这样做,但这是我见过的所有编译器实际上都是这样做的。该标准要求get_moneyput_money是对称的,所以如果用户输入$100.00并且你用get_money读取它,存储所产生的任何内容,然后用put_money将其写回,那么应该得到$100.00作为结果。