Advertisement
a53

Cifre_romane2

a53
Nov 28th, 2020
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define LMAX 1000
  3. using namespace std;
  4. char roman[LMAX]="";
  5.  
  6. void Roman(int numar) /// Tranforma numar in scrierea romana
  7. {
  8. int num[]={1,4,5,9,10,40,50,90,100,400,500,900,1000,4000,5000,9000,10000,40000,50000,90000,100000,400000,500000,900000,1000000};
  9. char sym[25][7]={"I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M",
  10. "M(V)","(V)","M(X)","(X)","(X)(L)","(L)","(X)(C)","(C)","(C)(D)","(D)","(C)(M)","(M)"};
  11. int i=24;
  12. while(numar>0)
  13. {
  14. int div=numar/num[i];
  15. numar%=num[i];
  16. while(div--)
  17. strcat(roman,sym[i]);
  18. --i;
  19. }
  20. }
  21.  
  22. int main()
  23. {
  24. int c;
  25. ifstream f("cifre_romane2.in");
  26. f>>c;
  27. ofstream g("cifre_romane2.out");
  28. if(c==1) /// Vom afisa numarul cu cifre romane
  29. {
  30. int a;
  31. f>>a;
  32. Roman(a);
  33. g<<roman;
  34. }
  35. else /// Vom afisa numarul cu cifre arabe
  36. {
  37. char nr[LMAX],NR[LMAX];
  38. f>>NR;
  39. int Lnr=0,LNR=strlen(NR);
  40. for(int i=0;i<LNR;++i)
  41. if(i&&NR[i-1]=='(')
  42. switch(NR[i])
  43. {
  44. case 'V' : nr[Lnr++]='0';break;
  45. case 'X' : nr[Lnr++]='1';break;
  46. case 'L' : nr[Lnr++]='2';break;
  47. case 'C' : nr[Lnr++]='3';break;
  48. case 'D' : nr[Lnr++]='4';break;
  49. case 'M' : nr[Lnr++]='5';break;
  50. }
  51. else
  52. if(NR[i]!='('&&NR[i]!=')')
  53. nr[Lnr++]=NR[i];
  54. nr[Lnr]='\0';
  55. int na=0,v=0;
  56. char C[]="IVXLCDM012345"; /// Inlocuim (V), (X), (L), (C), (D), (M) cu cifrele 0 1 2 3 4 5 si memoram in nr
  57. for(int i=0;i<Lnr;++i)
  58. {
  59. switch(nr[i])
  60. {
  61. case 'I' : v=1;break;
  62. case 'V' : v=5;break;
  63. case 'X' : v=10;break;
  64. case 'L' : v=50;break;
  65. case 'C' : v=100;break;
  66. case 'D' : v=500;break;
  67. case 'M' : v=1000;break;
  68. case '0' : v=5000;break;
  69. case '1' : v=10000;break;
  70. case '2' : v=50000;break;
  71. case '3' : v=100000;break;
  72. case '4' : v=500000;break;
  73. case '5' : v=1000000;break;
  74. }
  75. if(i==Lnr-1)
  76. na+=v;
  77. else
  78. if(strchr(C,nr[i+1])-C<=strchr(C,nr[i])-C)
  79. na+=v;
  80. else
  81. na-=v;
  82. }
  83. Roman(na);
  84. bool ok=true;
  85. for(int i=0;i<LNR;++i)
  86. if(NR[i]!=roman[i])
  87. {
  88. ok=false;
  89. break;
  90. }
  91. if(ok)
  92. g<<na<<'\n';
  93. else
  94. g<<"Numar invalid\n";;
  95. }
  96. return 0;
  97. }
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement