English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
所有分類

例如:4567
執行出來是肆仟伍佰陸拾柒仟柒仟仟元整
後面為什麼會出現仟柒仟仟
#include
#include
#include

int main(int argc, char *argv[])
{
int i ,size ;
char number[15];
printf("請輸入一阿拉伯數字金額(最多15位)n");
scanf("%s", number);
size=strlen(number);

for( i = 0 ; i < 15 ; i++ ) {

if( number[i] == '9')
printf("玖");
if( number[i] == '8' )
printf("捌");
if( number[i] == '7' )
printf("柒");
if( number[i] == '6' )
printf("陸");
if( number[i] == '5' )
printf("伍");
if( number[i] == '4' )
printf("肆");
if( number[i] == '3' )
printf("參");
if( number[i] == '2' )
printf("貳");
if( number[i] == '1' )
printf("壹");
if( number[i] == '0' ){
if( size % 4 == 3 )
if( number[i + 1] != '0' || number[i + 2] != '0' )
printf("零");
if( size % 4 == 2 && number[i-1] != '0' )
if( number[i + 1] != '0' )
printf("零");
}
if( size % 4 == 1 ) {
if( size / 4 == 1 )
if( number[i] != '0' || number[i - 1] != '0' || number[i - 2] != '0' || number[i - 3] != '0' )
printf("萬");
if( size / 4 == 2 )
if( number[i] != '0' || number[i - 1] != '0' || number[i - 2] != '0' || number[i - 3] != '0' )
printf("億");
if( size / 4 == 3 )
if( number[i] != '0' || number[i - 1] != '0' || number[i - 2] != '0' || number[i - 3] != '0' )
printf("兆");
}
if( number[i] != '0' ) {
if( size % 4 == 2 )
printf("拾") ;
if( size % 4 == 3 )
printf("佰") ;
if( size % 4 == 0 )
printf("仟") ;
}
--size;
}
printf("元整n");
system("PAUSE");
return 0;
}

2006-12-23 06:02:23 · 2 個解答 · 發問者 ? 1 in 電腦與網際網路 程式設計

2 個解答

輸入的數字不一定有15位但你卻用for( i = 0 ; i < 15 ; i++ )
從number[5]開始到number[14]是放什麼東西,我們都不確定
「肆仟伍佰陸拾柒 仟柒仟仟」
得到仟是因為number[5]或許根本不是文字所以它不等於'9'~'0'
number[i] != '0'當然也就成立
然後不知道什麼亂碼%4=0這也是有可能

這邊給你我的寫法:

#include
#include
#include

int main(int argc, char *argv[]){
char number[15];
printf("請輸入一阿拉伯數字金額(最多15位):\n");
scanf("%s", number);
int size=strlen(number);//decreasing
int size1=size;//always 4

for(int i=0; i if( number[i] == '9' ) printf("玖");
else if( number[i] == '8' ) printf("捌");
else if( number[i] == '7' ) printf("柒");
else if( number[i] == '6' ) printf("陸");
else if( number[i] == '5' ) printf("伍");
else if( number[i] == '4' ) printf("肆");
else if( number[i] == '3' ) printf("參");
else if( number[i] == '2' ) printf("貳");
else if( number[i] == '1' ) printf("壹");
else if( number[i] == '0' ){
if( size%4==3 ){
if( number[i+1]!='0' || number[i+2]!='0' )
printf("零");
}
else if(i!=0){
if( size%4==2 && number[i-1]!='0' ){
if( number[i+1]!='0' )
printf("零");
}
}
}

if(number[i]!='0'){
if( size%4==2 ) printf("拾");
else if( size%4==3 ) printf("佰");
else if( size%4==0 ) printf("仟");
}
if(size%4==1){
if(size==13) printf("兆");
else if(size==9){
if(size>12 & number[i]=='0' && number[i-1]=='0' && number[i-2]=='0' && number[i-3]=='0'){}
else printf("億");
}
else if(size==5){
if(size1>8 && number[i]=='0' && number[i-1]=='0' && number[i-2]=='0' && number[i-3]=='0'){}
else printf("萬");
}
}
}
printf("元整");
system("PAUSE");
return 0;
}

2006-12-24 17:22:49 · answer #1 · answered by Tosh 6 · 0 0

for( i = 0 ; i < 15 ; i++ ) {
if( number[i] == 0 ) break; // 加個檢查字串是否結束的動作
if( number[i] == '9')
....

或是改成
for( i = 0 ; i < strlen(number) ; i++ ) {
....

2006-12-24 08:50:38 · answer #2 · answered by elastic 7 · 0 0

fedest.com, questions and answers