其他分享
首页 > 其他分享> > 罗马数字转阿拉伯数字

罗马数字转阿拉伯数字

作者:互联网

  1. 罗马数字不是位置计数,他的缺点是表数范围小、计算难度大、表数的方式比较混乱、人从一连串字母数字中不易分析出具体的数值、表数要求太复杂。
  2. 我的学号:阿拉伯数字(20221320)罗马数字(XXCCXXMCCCXX)(上划线见下方截图)
    1. #include <stdio.h>
    2. #include <string.h>
    3. int judge(char* roma);
    4. int Match(char* temp);
    5. int main(int argc, const char * argv[]) {
    6. char roma[50];
    7. scanf("%s",roma);
    8. printf("%d",judge(roma));
    9. return 0;
    10. }
    11. int judge(char* roma)
    12. {
    13. int num=0,i,j,k=0,l=0;
    14. char temp[4]={0};
    15. int res[4]={0};
    16. for(i=0;i<strlen(roma);i=i+k)
    17. {
    18. for(j=3;j>=0;j--)
    19. {
    20. if(roma[i+j])
    21. {
    22. memset(temp, '\0', sizeof(temp));
    23. for(k=0;k<=j;k++)
    24. {
    25. temp[k]=roma[i+k];
    26. }
    27. //Match(temp);
    28. if(Match(temp)!=0)
    29. {
    30. res[l++]=Match(temp);
    31. break;
    32. }
    33. }
    34. }
    35. }
    36. for(l=0;l<4;l++)
    37. {
    38. num+=res[l];
    39. }
    40. return num;
    41. }
    42. int Match(char* temp)
    43. {
    44. char* r[50]={"M","MM","MMM",
    45. "C","CC","CCC","CD","D","DC","DCC","DCCC","CM",
    46. "X","XX","XXX","XL","L","LX","LXX","LXXX","XC",
    47. "I","II","III","IV","V","VI","VII","VIII","IX"};
    48. for(int i=0;i<30;i++)
    49. {
    50. //printf("%d",strlen(r[0]));
    51. //printf("%d",sizeof(r)/sizeof(r[0]));
    52. if(strcmp(r[i], temp)==0)
    53. {
    54. switch (i) {
    55. case 0:return 1000;
    56. case 1:return 2000;
    57. case 2:return 3000;
    58. case 3:return 100;
    59. case 4:return 200;
    60. case 5:return 300;
    61. case 6:return 400;
    62. case 7:return 500;
    63. case 8:return 600;
    64. case 9:return 700;
    65. case 10:return 800;
    66. case 11:return 900;
    67. case 12:return 10;
    68. case 13:return 20;
    69. case 14:return 30;
    70. case 15:return 40;
    71. case 16:return 50;
    72. case 17:return 60;
    73. case 18:return 70;
    74. case 19:return 80;
    75. case 20:return 90;
    76. case 21:return 1;
    77. case 22:return 2;
    78. case 23:return 3;
    79. case 24:return 4;
    80. case 25:return 5;
    81. case 26:return 6;
    82. case 27:return 7;
    83. case 28:return 8;
    84. case 29:return 9;
    85. break;
    86. default:
    87. break;
    88. }
    89. }
    90. }
    91. return 0;
    92. }
    93.  

       

       

       (程序运行时不能输上划线,故只有分开输)

标签:case,return,temp,int,阿拉伯数字,char,罗马数字,roma
来源: https://www.cnblogs.com/feng-tairui/p/16701051.html