Home > Article > Backend Development > 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:
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
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,'I'); printf("%5d............%3c</p><p>",5,'V'); printf("%5d............%3c</p><p>",10,'X'); printf("%5d............%3c</p><p>",50,'L'); printf("%5d............%3c</p><p>",100,'C'); printf("%5d............%3c</p><p>",500,'D'); printf("%5d............%3c</p><p>",1000,'M'); printf("Enter a Roman numeral:"); scanf("%s",roman); length=strlen(roman); for(i=0;i<length;i++){ switch(roman[i]){ case 'm': case 'M': d[i]=1000; break; case 'd': case 'D': d[i]= 500; break; case 'c': case 'C': d[i]= 100; break; case 'l': case 'L': d[i]= 50; break; case 'x': case 'X': d[i]= 10; break;; case 'v': case 'V': d[i]= 5; break; case 'i': case 'I': 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); }
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
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!