【问题标题】:c program that converts numbers to roman numerals将数字转换为罗马数字的c程序
【发布时间】: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


【解决方案1】:

您似乎尝试使用以下方法连接数字 百 = 数字 + 0;

这只会产生数字,因为数字 + 0 = 数字。如果要加零,请将数字乘以 10。如下 百 = 数字 * 10;

对于软件设计,如果程序无法转换数字,您可能希望返回一个非零数字,因为这可以由其他调用程序/脚本进行分析。

我发现,当代码格式正确并带有缩进时,查看代码会更容易,因为这样可以更全面地了解代码流和范围。

我希望这些小技巧对你有所帮助:)。

【讨论】:

  • 那么您是否建议我不应该输入 number = number + 0 并且可能将 number = number + 1000 用于千位,将 number = number + 100 用于数百位?感谢您的回复!
  • 不,因为您使用的是十进制,您可以使用 * 10 来增加数字...您似乎在做 number = number + 0 试图在您的数字末尾添加一个零,这根本行不通。你必须做number = number * 10 来实现这一点,除非我有错误的一端......
  • 啊,好的,谢谢你(:. 如果我在其中放入一个 while/for 循环并将其递增 10,这将大大缩短我的代码。但是,它仍然不能解决诸如 25 之类的罗马数字, 26,11 等(将两个罗马数字相加)
【解决方案2】:
void dec2romanstr(int num){
    int del[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1}; // Key value in Roman counting
    char * sym[] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" }; //Symbols for key values
    char res[64] = "\0";         //result string
    int i = 0;                   //
    while (num){                 //while input number is not zero
        while (num/del[i]){      //while a number contains the largest key value possible
            strcat(res, sym[i]); //append the symbol for this key value to res string
            num -= del[i];       //subtract the key value from number
        }
        i++;                     //proceed to the next key value
    }
    puts(res);
}

【讨论】:

  • printf("%s",sym[i]) 可以用来代替将符号附加到 res 字符串
  • 纯代码答案通常是frowned upon。尝试添加explination or code comments。谢谢。
  • printf 肯定比 strcat 慢。一个好的实现不会使用任何东西,而只是移动指针
【解决方案3】:
#include <stdio.h>
#include <stdlib.h>

typedef struct radix {
    int  digit;
    char roman;
    char romanNext;
} RADIX;


char *roman(int num, char *buff){
    static RADIX table[]={
        { 1000, 'M', '?' },
        {  100, 'C', 'D' },
        {   10, 'X', 'L' },
        {    1, 'I', 'V' }
    };
    int tableSize = sizeof(table)/sizeof(RADIX);
    int i, j, n;
    char *p;

    if(num < 0 || num >=4000){
        *buff='\0';
        return NULL;
    }
    for(i=0,p=buff;i<tableSize;i++){
        n = num / table[i].digit;
        if( 1 <= n && n <=3 ){
            for(j=0;j<n;j++)
                *p++=table[i].roman;
        } else if(n == 4){
            *p++=table[i].roman;
            *p++=table[i].romanNext;
        } else if(n == 5){
            *p++=table[i].romanNext;
        } else if(6<= n && n <=8){
            *p++=table[i].romanNext;
            for(j=0;j< n-5;++j)
                *p++=table[i].roman;
        } else if(n == 9){
            *p++=table[i].roman;
            *p++=table[i-1].roman;
        }
        num -= n * table[i].digit;
    }
    *p='\0';
    return buff;
}

int main(){
    int i, number[] = {25, 13, 93, 66};
    char buff[16];

    for(i=0;i < 4;++i){
        if(roman(number[i], buff))
            printf("%d is %s\n", number[i], buff);
    }
    return 0;
}

【讨论】:

  • 感谢您对 BLUEPIXY 的建议。但是,我没有理解一半的代码,因为我们还没有在课堂上学习它,是的,因此我将无法使用它。对不起。不过谢谢你的回答! (:
【解决方案4】:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct {
    int value;
    char romanDgts[3];
} romanTable[] =    {{1000, "M"},
                    {900,   "CM"},
                    {500,   "D"},
                    {400,   "CD"},
                    {100,   "C"},
                    {90,    "XC"},
                    {50,    "L"},
                    {40,    "XL"},
                    {10,    "X"},
                    {9,     "IX"},
                    {5,     "V"},
                    {4,     "IV"},
                    {1,     "I"}};

char *intToRoman(int n)
{
    char *romanNumerals;
    int i = 0;

    romanNumerals = (char *)malloc(sizeof(char) * 10);

    while (n) {
        while (n < romanTable[i].value)
            i++;
        while(n >= romanTable[i].value) {
            strcat(romanNumerals, romanTable[i].romanDgts);
            n -= romanTable[i].value;
        }
    }

    return romanNumerals;
}

int main(int argc, char const *argv[])
{
    int number;
    char *result;

    printf("Input a decimal number: ");
    scanf("%d", &number);

    if(number < 0  || number > 3999) {
        printf("Input must be within the range from 1 to 3999.\n");
        return -1;
    }

    result = intToRoman(number);
    puts(result);

    free(result);

    return 0;
}

【讨论】:

  • 感谢 MuertoExcobito。固定。
【解决方案5】:
#include<stdio.h>
int main(){
int num,reminder,counter;
scanf("%d",&num);
for(counter=1;counter<=num/1000;counter++)
    printf("M");
reminder = num%1000;

for(counter=reminder/900;counter==1;counter++)
    printf("CM");
reminder = reminder%900;

for(counter=reminder/500;counter==1;counter++)
    printf("D");
reminder = reminder%500;

for(counter=reminder/400;counter==1;counter++)
    printf("CD");
reminder = reminder%400;

for(counter=1;counter<=reminder/100;counter++)
    printf("C");
reminder = reminder%100;

for(counter=reminder/90;counter==1;counter++)
    printf("XC");
reminder = reminder%90;

for(counter=reminder/50;counter==1;counter++)
    printf("L");
reminder = reminder%50;

for(counter=reminder/40;counter==1;counter++)
    printf("XL");
reminder = reminder%40;

for(counter=reminder/10;counter>=1;counter--)
    printf("X");
reminder = reminder%10;

for(counter=reminder/9;counter==1;counter++)
    printf("IX");
reminder = reminder%9;

for(counter=reminder/5;counter==1;counter++)
    printf("V");
reminder = reminder%5;

for(counter=reminder/4;counter==1;counter++)
    printf("IV");
reminder = reminder%4;

for(counter=reminder;counter>=1;counter--)
    printf("I");

}

很长(43 行)但很简单!......

【讨论】:

    【解决方案6】:

    这个问题确实令人生畏,至少在初学者水平上,...既然您提到只使用基本功能,那么使用我的这个解决方案可以轻松打印 1000 以下的数字:

    # include<stdio.h>
    main()
    {
      int num,r,t,z,count=1;
      char c1='I',c2='V',c3='X',c4='L',c5='C',c6='D',c7='M';
      printf("enter the number");
      scanf("%d",&num);
      r=num%10;
      z=num/100;
      t=num%100;
      while(count<=3)
      {
          if(count==2)
          {
              z=(t-r)/10;
              c7='C';
              c6='L';
              c5='X';
          }
          if(count==3)
          {
              z=r;
              c7='X';
              c6='V';
              c5='I';
          }
          switch(z)
          {
              case 0:printf("");
              break;
              case 1:printf("%c",c5);
              break;
              case 2:printf("%c%c",c5,c5);
              break;
              case 3:printf("%c%c%c",c5,c5,c5);
              break;
              case 4:printf("%c%c",c5,c6);
              break;
              case 5:printf("%c",c6);
              break;
              case 6:printf("%c%c",c6,c5);
              break;
              case 7:printf("%c%c%c",c6,c5,c5);
              break;
              case 8:printf("%c%c%c%c",c6,c5,c5,c5);
              break;
              case 9:printf("%c%c",c5,c7);
              break;
              case 10:printf("%c",c7);
              break;
          }
          count++;
      }
    }
    

    【讨论】:

    • OP 正在努力学习。你应该解释你的程序是如何工作的和/或你为什么这样做和/或原始代码中的问题是什么。
    猜你喜欢
    • 1970-01-01
    • 2012-10-27
    • 2019-01-09
    • 2011-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多