您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

将以下内容翻译为中文:C程序将罗马数字转换为十进制数字

2024/4/23 22:24:50发布13次查看
给出以下是一个将罗马数字转换为十进制数字的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
"); 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<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 decimaldecimal:.........roman1............ i5............ v10............ x50............ l100............ c500............ d1000............ menter a roman numeral: mthe decimal equivalent of roman numeral m is 1000
以上就是将以下内容翻译为中文:c程序将罗马数字转换为十进制数字的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product