64,646
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <String>
using namespace std;
void outhead(const char *ch, int len, int head, int tail)//转换输出整数部分
void outtail(const char *ch, int len);
void change(const char *ch, int len, int head,int tail) //转换输出小数部分
{
if(head > 8)
{
outhead(ch, head-8,0,head-8);
cout<<"亿";
outhead(ch+head-8,4,0,4);
cout<<"";
}
else if(head > 4)
{
outhead(ch, head -4,0,head - 4);
cout<<"万";
}
outhead(ch+head-4,4,0,4);//问题应该是出在这里的
if(tail > 0)
{
cout<<"点";
outtail(ch+head+1,tail);
}
cout<<"元"<<endl;
}
void outhead(const char *ch, int len, int head,int tail)
{
if(len <= 0)
return;
int i = head;
int j = tail-1;
bool zero = false;
int flag = false;
while(ch[i] == '0')
{
i++;
flag = true;
}
while(ch[j] == '0')
{
j--;
flag = true;
}
if(flag)
{
outhead(ch,len,i,j+1);
return;
}
for(i=head; i < tail; i++)
{
if(ch[i] == '0')
{
if(!zero)
{
zero = true;
cout<<"零";
}
}
else
{
zero = false;
switch(ch[i])
{
case '1':
cout<<"壹";
break;
case '2':
cout<<"贰";
break;
case '3':
cout<<"叁";
break;
case '4':
cout<<"肆";
break;
case '5':
cout<<"伍";
break;
case '6':
cout<<"陆";
break;
case '7':
cout<<"柒";
break;
case '8':
cout<<"捌";
break;
case '9':
cout<<"玖";
break;
default:
break;
}
switch(len-i)//仟百拾输出在这里
{
case 4:
cout<<"仟";
break;
case 3:
cout<<"佰";
break;
case 2:
cout<<"拾";
break;
default:
break;
}
}
}
}
void outtail(const char *ch, int len)
{
for(int i = 0; i < len; i++)
switch(ch[i])
{
case '0':
cout<<"零";
break;
case '1':
cout<<"壹";
break;
case '2':
cout<<"贰";
break;
case '3':
cout<<"叁";
break;
case '4':
cout<<"肆";
break;
case '5':
cout<<"伍";
break;
case '6':
cout<<"陆";
break;
case '7':
cout<<"柒";
break;
case '8':
cout<<"捌";
break;
case '9':
cout<<"玖";
break;
default:
break;
}
}
void main()
{
char s[15];
char in;
int intnum = 0;
int tailnum = 0;
int len = 0;
bool head1 = true;
cout <<"输入式样XXXXXX.XX" <<endl;
cout <<"请输入金额(元)以#结束:" <<endl;
while(true)
{
cin >> in;
if((in >= '0' && in <= '9') || in == '.')
{
s[len] = in;
len ++;
if(len == 15)break;
if(in == '.')
{
if(head1)
{
head1 = false;
}
else
{
cout<<"input error!"<<endl;
return;
}
}
else if(head1)
{
intnum++;
}
else
{
tailnum++;
}
}
else break;
}
if(s[0] == '0')
{
cout<< "input error!"<<endl;
return;
}
cout << "人民币:"<< endl;
change(s, len, intnum, tailnum);
}
输入式样XXXXXX.XX
请输入金额(元)以#结束:
20#
人民币:
贰拾元
Press any key to continue
#include <iostream>
#include <String>
using namespace std;
void outhead(const char *ch, int len, int head, int tail);//转换输出整数部分
void outtail(const char *ch, int len);
void change(const char *ch, int len, int head,int tail) //转换输出小数部分
{
if(head > 8)
{
outhead(ch, head-8,0,head-8);
cout<<"亿";
outhead(ch+head-8,4,0,4);
cout<<"";
}
else if(head > 4)
{
outhead(ch, head -4,0,head - 4);
cout<<"万";
}
outhead(ch+head-4,4,0,4);//问题应该是出在这里的
if(tail > 0)
{
cout<<"点";
outtail(ch+head+1,tail);
}
cout<<"元"<<endl;
}
void outhead(const char *ch, int len, int head,int tail)
{
if(len <= 0)
return;
int i = head;
int j = tail-1;
bool zero = false;
int flag = false;
while(ch[i] == '0')
{
i++;
flag = true;
}
while(ch[j] == '0')
{
j--;
flag = true;
}
if(flag)
{
outhead(ch,len,i,j+1);
return;
}
for(i=head; i < tail; i++)
{
if(ch[i] == '0')
{
if(!zero)
{
zero = true;
cout<<"零";
}
}
else
{
zero = false;
switch(ch[i])
{
case '1':
cout<<"壹";
break;
case '2':
cout<<"贰";
break;
case '3':
cout<<"叁";
break;
case '4':
cout<<"肆";
break;
case '5':
cout<<"伍";
break;
case '6':
cout<<"陆";
break;
case '7':
cout<<"柒";
break;
case '8':
cout<<"捌";
break;
case '9':
cout<<"玖";
break;
default:
zero = true;//这里!!!!!!!!!!!!!
break;
}
if(!zero)//这里!!!!!!!!!!!!!!!!!
switch(len-i)//仟百拾输出在这里
{
case 4:
cout<<"仟";
break;
case 3:
cout<<"佰";
break;
case 2:
cout<<"拾";
break;
default:
break;
}
}
}
}
void outtail(const char *ch, int len)
{
for(int i = 0; i < len; i++)
switch(ch[i])
{
case '0':
cout<<"零";
break;
case '1':
cout<<"壹";
break;
case '2':
cout<<"贰";
break;
case '3':
cout<<"叁";
break;
case '4':
cout<<"肆";
break;
case '5':
cout<<"伍";
break;
case '6':
cout<<"陆";
break;
case '7':
cout<<"柒";
break;
case '8':
cout<<"捌";
break;
case '9':
cout<<"玖";
break;
default:
break;
}
}
void main()
{
char s[15];
char in;
int intnum = 0;
int tailnum = 0;
int len = 0;
bool head1 = true;
cout <<"输入式样XXXXXX.XX" <<endl;
cout <<"请输入金额(元)以#结束:" <<endl;
while(true)
{
cin >> in;
if((in >= '0' && in <= '9') || in == '.')
{
s[len] = in;
len ++;
if(len == 15)break;
if(in == '.')
{
if(head1)
{
head1 = false;
}
else
{
cout<<"input error!"<<endl;
return;
}
}
else if(head1)
{
intnum++;
}
else
{
tailnum++;
}
}
else break;
}
if(s[0] == '0')
{
cout<< "input error!"<<endl;
return;
}
cout << "人民币:"<< endl;
change(s, len, intnum, tailnum);
}