Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. //cifre_romane1
  2. #include <fstream>
  3. #include <cstring>
  4. using namespace std;
  5. ifstream in("cifre_romane1.in");
  6. ofstream out("cifre_romane1.out");
  7. char s[1000];
  8. int v[1000],nr,dim;
  9. bool test=true;
  10. int main()
  11. {
  12. int i,l;
  13. in>>s;
  14. l=strlen(s);
  15. for(i=0; i<l; i++)
  16. {
  17. switch(s[i])
  18. {
  19. case 'I':
  20. v[i]=1;
  21. break;
  22. case 'V':
  23. v[i]=5;
  24. break;
  25. case 'X':
  26. v[i]=10;
  27. break;
  28. case 'L':
  29. v[i]=50;
  30. break;
  31. case 'C':
  32. v[i]=100;
  33. break;
  34. case 'D':
  35. v[i]=500;
  36. break;
  37. case 'M':
  38. v[i]=1000;
  39. break;
  40. }
  41. }
  42. for(i=0; i<l-1; i++)
  43. if(v[i]<v[i+1])
  44. nr-=v[i];
  45. else
  46. nr+=v[i];
  47. nr+=v[l-1];
  48.  
  49. int a,b,c,d,n;
  50. n=nr;
  51. a=n%10;
  52. n/=10;
  53. b=n%10;
  54. n/=10;
  55. c=n%10;
  56. n/=10;
  57. d=n%10;
  58. for(int i=1; i<=d; i++)
  59. {
  60. if(s[dim]!='M')
  61. test=false;
  62. dim++;
  63. }
  64. if(1<=c && c<=3)
  65. {
  66. for(int i=1; i<=c; i++)
  67. {
  68. if(s[dim]!='C')
  69. test=false;
  70. dim++;
  71. }
  72. }
  73. else if(c==4)
  74. {
  75. if(s[dim]!='C')
  76. test=false;
  77. dim++;
  78. if(s[dim]!='D')
  79. test=false;
  80. dim++;
  81. }
  82. else if(5<=c && c<=8)
  83. {
  84. if(s[dim]!='D')
  85. test=false;
  86. dim++;
  87. for(int i=1; i<=c-5; i++)
  88. {
  89. if(s[dim]!='C')
  90. test=false;
  91. dim++;
  92. }
  93. }
  94. else if(c==9)
  95. {
  96. if(s[dim]!='C')
  97. test=false;
  98. dim++;
  99. if(s[dim]!='M')
  100. test=false;
  101. dim++;
  102. }
  103. if(1<=b && b<=3)
  104. {
  105. for(int i=1; i<=b; i++)
  106. {
  107. if(s[dim]!='X')
  108. test=false;
  109. dim++;
  110. }
  111. }
  112. else if(b==4)
  113. {
  114. if(s[dim]!='X')
  115. test=false;
  116. dim++;
  117. if(s[dim]!='L')
  118. test=false;
  119. dim++;
  120. }
  121. else if(5<=b && b<=8)
  122. {
  123. if(s[dim]!='L')
  124. test=false;
  125. dim++;
  126. for(int i=1; i<=b-5; i++)
  127. {
  128. if(s[dim]!='X')
  129. test=false;
  130. dim++;
  131. }
  132. }
  133. else if(b==9)
  134. {
  135.  
  136. if(s[dim]!='X')
  137. test=false;
  138. dim++;
  139. if(s[dim]!='C')
  140. test=false;
  141. dim++;
  142. }
  143. if(1<=a && a<=3)
  144. {
  145. for(int i=1; i<=a; i++)
  146. {
  147. if(s[dim]!='I')
  148. test=false;
  149. dim++;
  150. }
  151. }
  152. else if(a==4)
  153. {
  154. if(s[dim]!='I')
  155. test=false;
  156. dim++;
  157. if(s[dim]!='V')
  158. test=false;
  159. dim++;
  160. }
  161. else if(5<=a && a<=8)
  162. {
  163. if(s[dim]!='V')
  164. test=false;
  165. dim++;
  166. for(int i=1; i<=a-5; i++)
  167. {
  168. if(s[dim]!='I')
  169. test=false;
  170. dim++;
  171. }
  172. }
  173. if(test)
  174. out<<nr;
  175. else
  176. out<<"Numar invalid";
  177. in.close();
  178. out.close();
  179. return 0;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement