Home > Backend Development > C++ > Translate the following into Chinese: C program to convert Roman numerals to decimal numbers

Translate the following into Chinese: C program to convert Roman numerals to decimal numbers

WBOY
Release: 2023-09-05 21:53:05
forward
725 people have browsed it

Translate the following into Chinese: C program to convert Roman numerals to decimal numbers

Given the following is a C language algorithm to convert Roman numerals to decimal numbers:

Algorithm

Step 1 - Start

Step 2 - Read roman numerals at runtime

Step 3 - Length: = strlen(roman)

Step 4 - For i = 0 to length -1

Step 4.1 - switch(roman[i])

Step 4.1.1 - case 'm':

Step 4.1.2 - case 'M':

Step 4.1.2.1 - d[i]: =1000

Step 4.1.3 - case 'd':

Step 4.1.4 - case 'D':

Step 4.1.4.1 - d[i]: =500

Step 4.1.5 - case 'c':

Step 4.1.6 - case 'C':

Step 4.1.6.1 - d[i]: =100

Step 4.1.7 - case 'l':

Step 4.1.8 - case 'L':

Step 4.1.8.1 - d[i]: =50

Step 4.1.9 - case 'x':

Step 4.1.10 - case 'X':

Step 4.1.10.1 - d[i]: =10

Step 4.1.11 - case 'v':

Step 4.1.12 - case 'V':

Step 4.1.12.1 - d[i]: =5

Step 4.1.13 - case 'i':

Step 4.1.14 - case 'I':

Step 4.1.14.1 - d[i]: =1

Step 5 - For i = 0 to length-1

Step 5.1 - If (i==length-1) or (d[ i]>=d[i 1])

Step 5.1.1 - deci = d[i]

Step 5.2 - Otherwise

Step 5.2.1 - deci - = d[i]

Step 6 - Print the decimal equivalent of the Roman numerals

Step 7 - Stop the

program

The following is the conversion of the Roman numerals to C program for decimal numbers:

#include <stdio.h>
#include <conio.h>
main(){
   char roman[30];
   int deci=0;
   int length,i,d[30];
   printf("The Roman equivalent to decimal</p><p>");
   printf("Decimal:.........Roman</p><p>");
   printf("%5d............%3c</p><p>",1,&#39;I&#39;);
   printf("%5d............%3c</p><p>",5,&#39;V&#39;);
   printf("%5d............%3c</p><p>",10,&#39;X&#39;);
   printf("%5d............%3c</p><p>",50,&#39;L&#39;);
   printf("%5d............%3c</p><p>",100,&#39;C&#39;);
   printf("%5d............%3c</p><p>",500,&#39;D&#39;);
   printf("%5d............%3c</p><p>",1000,&#39;M&#39;);
   printf("Enter a Roman numeral:");
   scanf("%s",roman);
   length=strlen(roman);
   for(i=0;i<length;i++){
      switch(roman[i]){
         case &#39;m&#39;:
         case &#39;M&#39;: d[i]=1000; break;
         case &#39;d&#39;:
         case &#39;D&#39;: d[i]= 500; break;
         case &#39;c&#39;:
         case &#39;C&#39;: d[i]= 100; break;
         case &#39;l&#39;:
         case &#39;L&#39;: d[i]= 50; break;
         case &#39;x&#39;:
         case &#39;X&#39;: d[i]= 10; break;;
         case &#39;v&#39;:
         case &#39;V&#39;: d[i]= 5; break;
         case &#39;i&#39;:
         case &#39;I&#39;: d[i]= 1;
      }
   }
   for(i=0;i<length;i++){
      if(i==length-1 || d[i]>=d[i+1])
         deci += d[i];
      else
         deci -= d[i];
   }
   printf("The Decimal equivalent of Roman numeral %s is %d", roman, deci);
}
Copy after login

Output

When the above program is executed, it produces the following result −

The Roman equivalent to decimal
Decimal:.........Roman
1............ I
5............ V
10............ X
50............ L
100............ C
500............ D
1000............ M
Enter a Roman numeral: M
The Decimal equivalent of Roman Numeral M is 1000
Copy after login

The above is the detailed content of Translate the following into Chinese: C program to convert Roman numerals to decimal numbers. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template