下面這個程式在我機子上測試沒有問題,但上傳上去后就有問題,我估計是編碼的原因。麻煩哪位仁兄幫我測試一下,并告知我測試結果,萬分感謝!
程式碼如下:
#include <iostream>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;
}