當前位置:編程學習大全網 - 編程語言 - 初學者,c語言編程序

初學者,c語言編程序

/*這個是翻譯數字的方法*/

char TranslateNum(int num){

switch(num){

case 0: return '零';

case 1: return '壹';

case 2: return '貳';

case 3: return '三';

case 4: return '肆';

case 5: return '伍';

case 6: return '陸';

case 7: return '柒';

case 8: return '捌';

case 9: return '玖';

default: return '錯';

}

}

/*這個時翻譯數位的方法*/

char TranslateCount(int count){

switch(count % 4){

case 0: {

if (count == 0) return '空';

else if(count == 4) return '萬';

else if (count == 8) return '億';

case 1: return '拾';

case 2: return '佰';

case 3: return '仟';

}

}

/*拼接翻譯結果的方法,將tempResult添加到result的左邊*/

void AddToLeft(char tempResult[], char result[]){

int i = 0, j = 2;

/*每次只翻譯1位數,翻譯結果拾兩個字符,分別拾數字和位

所以tempResult[2]就是結尾,從這裏開始添加result*/

while(result[i] != '\0')

tempResult[j++] = result[i++];

/*設置結尾*/

tempResult[j] = '\0';

result = tempResult;

}

void main(){

/*這裏給妳寫死了,方便妳理解,如果妳想自己輸入的話改成調用scanf函數就可以了*/

long int num = 1002300;

/*記錄當前位數及該位上的數字*/

int count = 0, tempNum;

/*這是大寫結果的字符數組和緩沖數組*/

char result[20] = “元整”, tempResult[20];

while(num != 0){

/*獲取當前翻譯的數位上的數字,比如

第壹次執行時count=0,tempNum就是個位上的數

第二次執行時count=1,tempNum就是十位上的數*/

tempNum = 1002300 % 10;

/*把當前翻譯結果存到臨時數組中*/

tempResult[0] = TranslateNum(tempResult);

tempResult[1] = TranslateCount(count);

tempResult[2] = '\0';

/*把tempResult添加到result的左邊*/

AddToLeft(tempResult, result);

/*當前位翻譯完畢,準備翻譯下壹位數*/

num /= 10;

count++;

}

/*輸出結果*/

int i = 0;

while(result[i] != '\0'){

if(result == '空')

i++;

else

printf("%c", result[i++];

}

getch();

}

/*這段代碼看似挺多,其實很簡單,只是需要考慮很多種翻譯情況,有什麽問題可以再問我,我是用記事本寫的,有什麽妳不能處理的BUG的話,把錯誤信息貼給我*/

  • 上一篇:學計算機程序設計,是不是非得要學好英語
  • 下一篇:如何編程修改本機的IP地址?
  • copyright 2024編程學習大全網