
给出以下是一个将罗马数字转换为十进制数字的C语言算法:
算法
步骤1 - 开始
步骤2 - 在运行时读取罗马数字
步骤3 - 长度: = strlen(roman)
步骤4 - 对于i = 0到长度-1
步骤4.1 - switch(roman[i])
步骤4.1.1 - case 'm':
步骤4.1.2 - case 'M':
步骤4.1.2.1 - d[i]: =1000
步骤4.1.3 - case 'd':
步骤4.1.4 - case 'D':
步骤4.1.4.1 - d[i]: =500
步骤4.1.5 - case 'c':
步骤4.1.6 - case 'C':
步骤4.1.6.1 - d[i]: =100
步骤4.1.7 - case 'l':
步骤4.1.8 - case 'L':
步骤4.1.8.1 - d[i]: =50
步骤4.1.9 - case 'x':
步骤4.1.10 - case 'X':
步骤4.1.10.1 - d[i]: =10
步骤4.1.11 - case 'v':
步骤4.1.12 - case 'V':
步骤4.1.12.1 - d[i]: =5
步骤4.1.13 - case 'i':
步骤4.1.14 - case 'I':
步骤4.1.14.1 - d[i]: =1
步骤5 - 对于i = 0到长度-1
步骤5.1 - 如果(i==长度-1)或(d[i]>=d[i+1])
步骤5.1.1 - deci += d[i]
步骤5.2 - 否则
步骤5.2.1 - deci -= d[i]
步骤6 - 打印罗马数字的十进制等价物
步骤7 - 停止
程序
以下是将罗马数字转换为十进制数字的C程序:
#include#include main(){ char roman[30]; int deci=0; int length,i,d[30]; printf("The Roman equivalent to decimal "); printf("Decimal:.........Roman
"); printf("%5d............%3c
",1,'I'); printf("%5d............%3c
",5,'V'); printf("%5d............%3c
",10,'X'); printf("%5d............%3c
",50,'L'); printf("%5d............%3c
",100,'C'); printf("%5d............%3c
",500,'D'); printf("%5d............%3c
",1000,'M'); printf("Enter a Roman numeral:"); scanf("%s",roman); length=strlen(roman); for(i=0;i
=d[i+1]) deci += d[i]; else deci -= d[i]; } printf("The Decimal equivalent of Roman numeral %s is %d", roman, deci); }
输出
当上述程序被执行时,它产生以下结果 −
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










