# 谁来帮我测试一下这个程序？

Sarlanori 2008-03-07 03:26:29

``````#include <iostream>
#include <string>
using namespace std;

string convert(char number);
int main()
{
string x;
int count = 0;
string output;
while(cin >> x)
{
count = x.length();
for(int i = 0;i < x.length();i++)
{
switch(count)
{
//十亿位
case 10:
if(x[i] != '0')
{
output += convert(x[i]);
output += "拾";
}
break;
//亿位
case 9:
if(x[i] != '0')
{
output += convert(x[i]);
output += "億";
}
else
{
output += "億";
}
break;
//千万位
case 8:
if(x[i] != '0')
{
output += convert(x[i]);
output += "仟";
}
else if(x[i] == '0' &&
(x[i + 1] != '0' || x[i + 2] != '0'
|| x[i + 3] != '0' || x[i + 4] != '0' || x[i + 5] != '0'
|| x[i + 6] != '0'))
{
output += "零";
}
break;
//百万位
case 7:
if(x[i] != '0')
{
output += convert(x[i]);
output += "佰";
}
else if(x[i] == '0' &&
(x[i + 1] != '0' || x[i + 2] != '0'
|| x[i + 3] != '0' || x[i + 4] != '0' || x[i + 5] != '0'))
{
output += "零";
}
break;
//十万位
case 6:
if(x[i] != '0')
{
output += convert(x[i]);
output += "拾";
}
else if(x[i] == '0' && (x[i + 1] != '0' || x[i + 2] != '0' ||
x[i + 3] != '0' || x[i + 4] != '0'))
{
output += "零";
}
break;
//万位
case 5:
if(x[i] != '0')
{
output += convert(x[i]);
output += "萬";
}
else
{
output += "萬";
}
break;
//千位
case 4:
if(x[i] != '0')
{
output += convert(x[i]);
output += "仟";
}
else if(x[i] == '0' && (x[i + 1] != '0' || x[i + 2] != '0'
|| x[i + 3] != '0'))
{
output += "零";
}
break;
//百位
case 3:
if(x[i] != '0')
{
output += convert(x[i]);
output += "佰";
}
else if(x[i] == '0' && (x[i + 1] != '0' || x[i + 2] != '0'))
{
output += "零";
}
break;
//十位
case 2:
if(x[i] != '0')
{
output += convert(x[i]);
output += "拾";
}
else if(x[i] == '0' && x[i + 1] != '0')
{
output += "零";
}
break;
//个位
case 1:
if(x[i] != '0')
{
output += convert(x[i]);
}
else if(x.length() == 1)
{
output += convert(x[i]);
}
break;
}
count--;
}
int j;
for(int k = 0;k < output.length();k++)
{
if((j = output.find("零零零")) != -1) output.erase(j,4);
if((j = output.find("零零")) != -1) output.erase(j,2);
if((j = output.find("億萬")) != -1) output.erase(j + 2,2);
if((j = output.find("零拾")) != -1) output.erase(j,2);
if((j = output.find("零萬")) != -1) output.erase(j,2);
if((j = output.find("零仟")) != -1) output.erase(j,2);
if((j = output.find("零佰")) != -1) output.erase(j,2);
if((j = output.find("零億")) != -1) output.erase(j,2);
}
cout << output << endl;
output = "";
}
return 0;
}

string convert(char number)
{
string str;
switch(number)
{
case '0':
str = "零";
break;
case '1':
str = "壹";
break;
case '2':
str = "貳";
break;
case '3':
str = "參";
break;
case '4':
str = "肆";
break;
case '5':
str = "伍";
break;
case '6':
str = "陸";
break;
case '7':
str = "柒";
break;
case '8':
str = "捌";
break;
case '9':
str = "玖";
break;
default:
cout << "ERROR!\n";
exit(1);
}
return str;
}
``````
...全文
79 7 打赏 收藏 转发到动态 举报

7 条回复

Sarlanori 2008-03-07
• 打赏
• 举报

p0303230 2008-03-07
• 打赏
• 举报

``````count = (int)x.length();
for(i = 0; i< x.length(); i++)
{
if(x[i] == '0') count--;
else break;
}
for(i;i < x.length();i++)
{``````

Sarlanori 2008-03-07
• 打赏
• 举报

hai040，luwenzuo,p0303230:我写的时候就没考虑判断非法数据，比如开头有零的情况，而且对输入数据的要求是整数>=0且<=2147483647。现在的问题是我上传到一个ACM网站后它给出了如下的错误：
``````與正確輸出不相符(line:11)

``````

p0303230 2008-03-07
• 打赏
• 举报

p0303230 2008-03-07
• 打赏
• 举报

00100000问题更大

• 打赏
• 举报

``````
/*
4887989756445546

546546

564546

546

5456489897879987987879897987

5646544564879987798

564456458798794

546548978794456546

989784564566458456654987879

4565648989789787

54465654978

897

*/
``````
hai040 2008-03-07
• 打赏
• 举报

64,867

c++ 技术论坛（原bbs）

• 近7日
• 近30日
• 至今

1. 请不要发布与C++技术无关的贴子
2. 请不要发布与技术无关的招聘、广告的帖子
3. 请尽可能的描述清楚你的问题，如果涉及到代码请尽可能的格式化一下