
给出以下是一个将罗马数字转换为十进制数字的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 <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);
}当上述程序被执行时,它产生以下结果 −
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
以上就是将以下内容翻译为中文:C程序将罗马数字转换为十进制数字的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号