我正在尝试用文字打印 01-99,但我有点成功。
这是源代码:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) {
char a, b;
char *digit1;
char *digit2;
// get digit character by character
scanf("%c%c", &a, &b);
switch(a) {
case '1':
switch(b) {
case '0':
digit1 = "ten";
break;
case '1':
digit1 = "eleven";
break;
case '2':
digit1 = "twelve";
break;
case '3':
digit1 = "thirteen";
break;
case '4':
digit1 = "fourteen";
break;
case '5':
digit1 = "fifteen";
break;
case '6':
digit1 = "sixteen";
break;
case '7':
digit1 = "seventeen";
break;
case '8':
digit1 = "eighteen";
break;
case '9':
digit1 = "nineteen";
break;
default:
digit1 = "";
break;
}
break;
case '2':
digit1 = "twenty-";
break;
case '3':
digit1 = "thirty-";
break;
case '4':
digit1 = "forty-";
break;
case '5':
digit1 = "fifty-";
break;
case '6':
digit1 = "sixty-";
break;
case '7':
digit1 = "seventy-";
break;
case '8':
digit1 = "eighty-";
break;
case '9':
digit1 = "ninty-";
break;
default:
digit1 = "";
}
switch(b) {
case '1':
digit2 = "one";
break;
case '2':
digit2 = "two";
break;
case '3':
digit2 = "three";
break;
case '4':
digit2 = "four";
break;
case '5':
digit2 = "five";
break;
case '6':
digit2 = "six";
break;
case '7':
digit2 = "seven";
break;
case '8':
digit2 = "eight";
break;
case '9':
digit2 = "nine";
break;
case '0':
digit2 = "\b";
break;
default:
digit2 = strcpy(digit1, "\b");
}
if (a != 1) {
printf("%s%s\n", digit1, digit2);
}
else {
printf("%s\n", digit1);
}
return 0;
}
我从 20-99 年到现在都成功打印了。但是有一个错误。如果我输入 20、30、40... 中的任何一个,-
则不会被删除,因为它应该是因为我曾经\b
删除过它。