注册 登录
编程论坛 C++ Builder

[求助]设计一个能实现把小写金额转换为大写金额的类。比如5002.30,转换为“伍仟零

daluqiang 发布于 2007-05-14 23:26, 2963 次点击
设计一个能实现把小写金额转换为大写金额的类。比如5002.30,转换为“伍仟零贰元叁角整”。并设计完整的程序进行演示。
6 回复
#2
listonline2008-01-04 23:44
读取后用字符串替换操作函数Replace(),
#3
FireG2008-01-21 16:36
貌似很好玩  我试着写一下~
#4
yuki2008-02-04 14:58
希望LZ大人能先把我口袋中的钱小写变为大写的,我可以考虑一下帮您实现
#5
←…○…→2008-10-19 08:59
写了个,这题有点意思
#include <iostream.h>
#include <string.h>
#include <alloc.h>

static char* num[]={
    "零","一","二","三","四",
    "五","六","七","八","九"
};

class money{
    float number;
 public:
    money(float m){number=m;}
    ~money(){};
    void set(float m){number=m;}
    void get();
    float get(int i){return number;}
};

void money::get (){
    float temp=number;
    char str[100],tmp[3];
    long i;
    int Nrest,Nint;

    Nint=(int)temp;
    //Nrest=temp-Nint;
    if(Nint/1000000000>0){ cout<<"\n多于拾亿,自己换算!"; return; }

    for(i=1000000000;Nint/i==0;i=i/10);
    //cout<<"\ni="<<i<<"\n整数="<<Nint<<"\t小数="<<Nrest<<"temp="<<temp;
    

    while(Nint%i){
        Nint=Nint/i;
        strcpy(tmp,num[Nint/i]);
        strcat(tmp,str);
        switch(i){
            case 1000000000:strcat("拾亿",str);break;
            case  100000000:strcat(  "亿",str);break;
            case   10000000:strcat("仟万",str);break;
            case    1000000:strcat("佰万",str);break;
            case     100000:strcat("拾万",str);break;
            case      10000:strcat(  "万",str);break;
            case       1000:strcat(  "仟",str);break;
            case        100:strcat(  "佰",str);break;
            case         10:strcat(  "拾",str);break;
            case          1:strcat(  "圆",str);break;
        }
        i/=10;
    }
    cout<<endl<<str<<endl;
    /*处理小数*/
    temp=temp-(int)temp;
    if((int(temp*100))!=0){
            strcat(num[(int(temp*10))],str);
            strcat(  "角",str);
    }

        if((int(temp*10))!=0){
            strcat(num[(int(temp*10))],str);
            strcat(  "分",str);
        }
    cout<<endl<<str<<endl;
}

int main()
{
    money m(0);
    float y;

    cout<<"输入金额:¥";
    cin>>y;
    //cout<<"\ny="<<y<<endl;
    m.set(y);
    //cout<<"number="<<m.get(1);
    m.get();
    //cout<<endl<<str<<endl;

    return 0;
}
#6
←…○…→2008-10-19 09:01
啊,发错了
...
#7
←…○…→2008-10-19 09:01
#include <iostream.h>

static char* num[]={
    "零","壹","贰","叁","肆",
    "伍","陆","柒","捌","玖"
};

class money{
    double number;
 public:
    money(float m){number=m;}
    ~money(){};
    void set(float m){number=m;}
    void get();
    double get(int i){return number;}
};

void money::get (){
    double temp=number,Nrest;
    long i;
    int Nint;

    Nint=(int)temp;
    Nrest=temp-Nint;
    if(Nint/1000000000>0){ cout<<"\n多于拾亿,自己换算!"; return; }
    for(i=1000000000;Nint/i==0;i=i/10);
    //cout<<"\ni="<<i<<"\n整数="<<Nint<<"\t小数="<<Nrest<<"\ttemp="<<temp<<endl;
    

    while(Nint){
        if(Nint/i==0){
            cout<<num[Nint/i];
            for(;Nint/i==0;i=i/10);
        }
        cout<<num[Nint/i];
        
        switch(i){
            case 1000000000:cout<<"拾亿";break;
            case  100000000:cout<<  "亿";break;
            case   10000000:cout<<"仟万";break;
            case    1000000:cout<<"佰万";break;
            case     100000:cout<<"拾万";break;
            case      10000:cout<<  "万";break;
            case       1000:cout<<  "仟";break;
            case        100:cout<<  "佰";break;
            case         10:cout<<  "拾";break;
        }
        Nint=Nint-Nint/i*i;
        i/=10;
        if(!Nint) cout<<"圆";
    }
    
    
    /*处理小数*/
    Nint=Nrest*100;
    if(Nint/10){
            cout<<num[Nint/10]<<"角";
            Nint=Nint-Nint/10*10;
    }
    if(Nint)
            cout<<num[Nint]<<"分";
}

int main()
{
    money m(0);
    double y;

    cout<<"输入金额:¥";
    cin>>y;
    //cout<<"\ny="<<y<<endl;
    m.set(y);
    cout<<"大写金额:";
    m.get();
    //cout<<endl<<str<<endl;

    return 0;
}
1