MIOJ #17 小写数字转大写数字

描述

实现一个算法,可以将小写数字转换成大写数字。

输入

输入一个整数。范围在0~450亿之间。

输出

输出对应的大写数字,以“元整”结尾。 大写数字要符合汉语读写习惯。

输入样例

0
5
233
1001
40607
8900000000

输出样例

零元整
伍元整
贰佰叁拾叁元整
壹仟零壹元整
肆万零陆佰零柒元整
捌拾玖亿元整

AC代码:

#include <bits/stdc++.h>
#include<vector>
#include<stack>
#include<sstream>
using namespace std;

char* num[] = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
string func1(string s)
{
    bool isZero = false;
    int k = 0;
    /*while(s[k]-'0'==0)
    {
        s[k]='*';
        k++;
    }*/
    k = s.size()-1;
    while(s[k]-'0'==0)
    {
        s[k]='*';
        k--;
    }
    for(int i = 0; i<s.size(); i++)
    {
        if(s[i]-'0' != 0)
        {
            isZero = false;
        }
        else if(s[i]-'0' == 0 && isZero == true)
        {
            s[i] = '*';
        }
        else if(s[i]-'0' == 0 && isZero == false)
        {
            isZero = true;
        }
    }
    return s;
}


string func(string s)
{
    string result = "";
    int  k = 0;
    for(int i = s.size()-1; i>=0; i--)
    {
        if(s[i] =='*')
        {
            k++;
            continue;
        }
        if(s[i]-'0' >= 0 && s[i]-'0' <= 9)
        {
            if(k == 1 &&s[i]-'0' != 0)
            {
                result ="拾" +result;
            }
            else if(k == 2 &&s[i]-'0' != 0)
            {
                result ="佰" +result;
            }
            else if(k == 3 &&s[i]-'0' != 0)
            {
                result ="仟" +result;
            }
            result =  num[s[i]-'0'] + result ;
            k++;
        }

    }
    return result;

}



int main()
{
    //  char *unit[] = {"十","百","千","万","亿"};仟佰拾
    string s;
    bool isZero = false;
    while(cin>>s)
    {
        string s1 ="";
        string s2 = "";
        string s3 = "";
        string result = "";
        if(s == "0")
        {
            cout<<"零元整"<<endl;
            continue;
        }
        if(s.length()> 0 && s.length()<=4)
        {
            s1 = func1(s);
            s1 = func(s1);
            cout<<s1;
        }
        else if(s.length()> 4 && s.length()<=8)
        {
            s1 = s.substr(s.length()-4,4);
            s1 = func1(s1);
            s1 = func(s1);
            s2 = s.substr(0,s.length()-4);
            s2 = func1(s2);
            s2 = func(s2);
            cout<<s2<<"万";
            cout<<s1;
        }
        else if(s.length()> 8 && s.length()<=12)
        {
            int num = s.length();
            s1 = s.substr(s.length()-4,4);
            s1 = func1(s1);
            s1 = func(s1);
            s2 = s.substr(s.length()-8,4);
            s2 = func1(s2);
            s2 = func(s2);
            s3 = s.substr(0,s.length()-8);
            s3 = func1(s3);
            s3 = func(s3);
            cout<<s3<<"亿";
            if(s2!="")
            {
                cout<<s2<<"万";
            }
            cout<<s1;
        }
        cout<<"元整"<<endl;
    }
}

 

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注