【发布时间】:2014-04-24 12:38:43
【问题描述】:
我的作业是编写一个简单的 c 程序,将数字转换为罗马数字。我被允许使用的唯一功能是 printf 和 scanf。我已经编写了代码,但是我不知道如何打印出诸如“25、13、93、66”等数字。任何帮助将不胜感激!谢谢你! (下面是写好的代码)
int main(int arg, char *argv[]){
int thousand, hundred, ten, single;
int number;
printf("Enter a decimal number: ");
scanf("%d", &number);
thousand = number + 0;
if (thousand == 1000) {
printf("%d is represented as M in Roman numerals. \n", number);
} else if (thousand == 2000) {
printf("%d is represented as MM in Roman numerals. \n", number);
} else if (thousand == 3000)
printf("%d is represented as MMM in Roman numerals. \n",number);
hundred = number + 0;
if (hundred == 100) {
printf("%d is represented as C in Roman numerals. \n",number);
} else if (hundred == 200) {
printf("%d is represented as CC in Roman numerals. \n",number);
} else if (hundred == 300) {
printf("%d is represented as CCC in Roman numerals. \n",number);
} else if (hundred == 400) {
printf("%d is represented as CD in Roman numerals. \n",number);
} else if (hundred == 500) {
printf("%d is represented as D in Roman numerals. \n",number);
} else if (hundred == 600) {
printf("%d is represented as DC in Roman numerals. \n",number);
} else if (hundred == 700) {
printf("%d is represented as DCC in Roman numerals. \n",number);
} else if (hundred == 800) {
printf("%d is represented as DCCC in Roman numerals. \n",number);
} else if (hundred == 900)
printf("%d is represented as CM in Roman numerals. \n",number);
ten = number + 0;
if (ten == 10) {
printf("%d is represented as X in Roman numerals. \n",number);
} else if (ten == 20) {
printf("%d is represented as XX in Roman numerals. \n",number);
} else if (ten == 30) {
printf("%d is represented as XXX in Roman numerals. \n",number);
} else if (ten == 40) {
printf("%d is represented as XL in Roman numerals. \n",number);
} else if (ten == 50) {
printf("%d is represented as L in Roman numerals. \n",number);
} else if (ten == 60) {
printf("%d is represented as LX in Roman numerals. \n",number);
} else if (ten == 70) {
printf("%d is represented as LXX in Roman numerals. \n",number);
} else if (ten ==80) {
printf("%d is represented as LXXX in Roman numerals. \n",number);
} else if (ten == 90)
printf("%d is represented as XC in Roman numerals. \n",number);
single = number + 0;
if (single == 1) {
printf("%d is represented as I in Roman numerals. \n",number);
} else if (single == 2) {
printf("%d is represented as II in Roman numerals. \n",number);
} else if (single == 3) {
printf("%d is represented as III in Roman numerals. \n",number);
} else if (single == 4) {
printf("%d is represented as IV in Roman numerals. \n",number);
} else if (single == 5) {
printf("%d is represented as V in Roman numerals. \n",number);
} else if (single == 6) {
printf("%d is represented as VI in Roman numerals. \n",number);
} else if (single == 7) {
printf("%d is represented as VII in Roman numerals. \n",number);
} else if (single ==8) {
printf("%d is represented as VIII in Roman numerals. \n",number);
} else if (single == 9)
printf("%d is represented as IX in Roman numerals. \n",number);
if (number <= 0) {
printf("%d can not be represented in Roman numerals. \n", number);
} else if (number > 3000)
printf("%d can not be represented in Roman numerals. \n", number);
return 0;
}
【问题讨论】:
-
您可能应该使用 while/for 循环。从个位数开始,然后逐步向上,边走边添加相关的数字。
-
考虑与您的代码保持一致的风格。考虑为所有单语句 if 块使用花括号
{ ... },或者在存在且只有单个语句时不使用它们。
标签: c