昨天寝室的讨论到这个题目,都在动手写,我也写了一个(任意位的):
#include <iostream>
#include <string>
using namespace std;
int main(){
const int size = 12;
const string s1[] = {"零","一","二","三","四","五","六","七","八","九"};
const string s2[] = {"","十","百","千"};
int money,a[size],beg = 0,end = 0;
cin >> money;
while (cin.fail()){
cin.clear();
cin.ignore();
cin >> money;
}
for (int i = 0;i < size;++i){
a[i] = money % 10;
money /= 10;
}
//count 0 at begin
for (int i = size-1;i >= 0;--i){
if (a[i] != 0)
break;
++beg;
}
//count 0 at end
for (int i = 0; i < size;++i){
if (a[i] != 0)
break;
++end;
}
//dispaly
for (int i = size-1-beg;i >= end;--i){
//process the situation when 0 at middle
if (i < size-1){
if (a[i+1] == 0 && a[i] == 0);
else
cout << s1[a[i]];
}
else
cout << s1[a[i]];
cout << (a[i] == 0 ? "" : s2[i%4]);
if (i == 4) cout << "万";
if (i == 8) cout << "亿";
}
cout << "元" << endl;
}
vc++6.0运行要报错的哦,改下i的定义就好,我用2005写的!
有个bug怎么也改不对(输入10001001),请教指点,按我的思路!
[此贴子已经被作者于2007-5-25 10:03:02编辑过]