c将数字转换为罗马数字的程序

时间:2014-04-24 12:38:44

标签: c

我的作业是写一个简单的程序,将数字转换成罗马数字。我可以使用的唯一功能是printf和scanf。我已经编写了代码,但是我不知道如何打印出诸如" 25,13,​​93,66和#34;等任何帮助将不胜感激!谢谢! (以下是书面代码)

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;
   }

6 个答案:

答案 0 :(得分:1)

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

这只会导致数字,数字+ 0 =数字。如果要添加零,请将数字乘以10.如下所示 百=数字* 10;

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

我发现在使用缩进格式正确格式化代码时更容易查看代码,因为这样可以更全面地了解代码流以及范围落在何处。

我希望这些小技巧可以帮助:)。

答案 1 :(得分:1)

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);
}

答案 2 :(得分:0)

#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;
}

答案 3 :(得分:0)

#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;
}

答案 4 :(得分:0)

#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行),但简单对!! !!

答案 5 :(得分:-1)

问题确实令人生畏,至少在初学者水平上......因为你提到只使用基本功能,所以使用我的解决方案可以轻松打印 1000 以下的数字:< / p>

# 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++;
  }
}