算法
步骤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 decimalDecimal:.........Roman1............ I5............ V10............ X50............ L100............ C500............ D1000............ MEnter a Roman numeral: MThe Decimal equivalent of Roman Numeral M is 1000
登录后复制
以上就是将以下内容翻译为中文:C程序将罗马数字转换为十进制数字的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2583543.html