Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //cifre_romane1
- #include <fstream>
- #include <cstring>
- using namespace std;
- ifstream in("cifre_romane1.in");
- ofstream out("cifre_romane1.out");
- char s[1000];
- int v[1000],nr,dim;
- bool test=true;
- int main()
- {
- int i,l;
- in>>s;
- l=strlen(s);
- for(i=0; i<l; i++)
- {
- switch(s[i])
- {
- case 'I':
- v[i]=1;
- break;
- case 'V':
- v[i]=5;
- break;
- case 'X':
- v[i]=10;
- break;
- case 'L':
- v[i]=50;
- break;
- case 'C':
- v[i]=100;
- break;
- case 'D':
- v[i]=500;
- break;
- case 'M':
- v[i]=1000;
- break;
- }
- }
- for(i=0; i<l-1; i++)
- if(v[i]<v[i+1])
- nr-=v[i];
- else
- nr+=v[i];
- nr+=v[l-1];
- int a,b,c,d,n;
- n=nr;
- a=n%10;
- n/=10;
- b=n%10;
- n/=10;
- c=n%10;
- n/=10;
- d=n%10;
- for(int i=1; i<=d; i++)
- {
- if(s[dim]!='M')
- test=false;
- dim++;
- }
- if(1<=c && c<=3)
- {
- for(int i=1; i<=c; i++)
- {
- if(s[dim]!='C')
- test=false;
- dim++;
- }
- }
- else if(c==4)
- {
- if(s[dim]!='C')
- test=false;
- dim++;
- if(s[dim]!='D')
- test=false;
- dim++;
- }
- else if(5<=c && c<=8)
- {
- if(s[dim]!='D')
- test=false;
- dim++;
- for(int i=1; i<=c-5; i++)
- {
- if(s[dim]!='C')
- test=false;
- dim++;
- }
- }
- else if(c==9)
- {
- if(s[dim]!='C')
- test=false;
- dim++;
- if(s[dim]!='M')
- test=false;
- dim++;
- }
- if(1<=b && b<=3)
- {
- for(int i=1; i<=b; i++)
- {
- if(s[dim]!='X')
- test=false;
- dim++;
- }
- }
- else if(b==4)
- {
- if(s[dim]!='X')
- test=false;
- dim++;
- if(s[dim]!='L')
- test=false;
- dim++;
- }
- else if(5<=b && b<=8)
- {
- if(s[dim]!='L')
- test=false;
- dim++;
- for(int i=1; i<=b-5; i++)
- {
- if(s[dim]!='X')
- test=false;
- dim++;
- }
- }
- else if(b==9)
- {
- if(s[dim]!='X')
- test=false;
- dim++;
- if(s[dim]!='C')
- test=false;
- dim++;
- }
- if(1<=a && a<=3)
- {
- for(int i=1; i<=a; i++)
- {
- if(s[dim]!='I')
- test=false;
- dim++;
- }
- }
- else if(a==4)
- {
- if(s[dim]!='I')
- test=false;
- dim++;
- if(s[dim]!='V')
- test=false;
- dim++;
- }
- else if(5<=a && a<=8)
- {
- if(s[dim]!='V')
- test=false;
- dim++;
- for(int i=1; i<=a-5; i++)
- {
- if(s[dim]!='I')
- test=false;
- dim++;
- }
- }
- if(test)
- out<<nr;
- else
- out<<"Numar invalid";
- in.close();
- out.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement