【人民币转换】完整版

人民币转换

http://www.nowcoder.com/questionTerminal/00ffd656b9604d1998e966d555005a4b

VS完整版

#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<cmath>
#include<unordered_map>
#include<algorithm>
#include<stack>
using namespace std;


vector<string> table = { "零","壹","贰","叁","肆","伍","陆","柒","捌","玖" };
vector<string> table_1 = { "元","万","亿" ,"万","亿" };
vector<string> table_2 = { "仟"," ","拾","佰" };

int main()
{    
    double moy;
    while (cin >> moy) {
        cout << "人民币";
        string ans;
        string money = to_string(moy);
        int index = money.find('.');
        string money_l = money.substr(0, index);
        string money_r = money.substr(index + 1);
        if (money_l == "0") {
            if (money_r[0] == '0'&&money_r[1] == '0')
                cout << "零元整";
            else if (money_r[1] == '0')
                cout << table[money_r[0] - '0'] << "角";
            else if (money_r[0] == '0')
                cout << "零" << table[money_r[1] - '0'] << "分";
            else
                cout << table[money_r[0] - '0'] << "角" << table[money_r[1] - '0'] << "分";
            cout << endl;
            continue;
        }
        while (money_l.size()) {             
            int tb_1 = money_l.size() / 4;
            int tb_2 = money_l.size() % 4;
            if (tb_2 != 1) {
                if (money_l[0] == '0') {
                    if (money_l[1] == '0')
                    {
                        money_l = money_l.substr(1);
                        continue;
                    }
                    else {
                        cout << table[money_l[0] - '0'];
                    }                        
                }
                else {
                    if(money_l[0] - '0'==1&&tb_2==2&&ans.size()==0)
                        cout<< table_2[tb_2];
                    else {
                        cout << table[money_l[0] - '0'] << table_2[tb_2];
                        ans = table_2[tb_2];
                    }
                }                    
            }
            else {
                if (money_l[0] == '0') {
                    if (ans != "亿") {
                        cout << table_1[tb_1];
                        ans = table_1[tb_1];
                    }
                    else if(ans == "亿"&&tb_1==0)
                    {
                        cout << table_1[tb_1];
                    }
                    else if (ans == "亿"&&table_1[tb_1]== "亿")
                    {
                        cout << table_1[tb_1];
                        ans = table_1[tb_1];
                    }
                }                    
                else {
                    cout << table[money_l[0] - '0'] << table_1[tb_1];
                    ans = table_1[tb_1];
                }                    
            }            
            money_l = money_l.substr(1);
        }
        if (money_r[0] == '0'&&money_r[1] == '0')
            cout << "整";
        else if (money_r[1] == '0')
            cout << table[money_r[0] - '0'] << "角";
        else if (money_r[0] == '0')
            cout <<  table[money_r[1] - '0'] << "分";
        else
            cout << table[money_r[0] - '0'] << "角" << table[money_r[1] - '0'] << "分";
        cout << endl;
    }        
    system("pause");
    return 0;
}
全部评论

相关推荐

11-01 20:03
已编辑
门头沟学院 算法工程师
Amazarashi66:这种也是幸存者偏差了,拿不到这个价的才是大多数
点赞 评论 收藏
分享
2 1 评论
分享
牛客网
牛客企业服务