C ++: 914 855 caractères
#include<map>
#include<string>
#include<iostream>
#include<sstream>
#define I istream
#define T(C) if(C)throw int(1);
#define X(c,v,f,m) D[c]=v;P[c]=D[f];M[c]=m;
#define S second
using namespace std;typedef map<char,int>R;R D,P,M;struct U{U():t(0),l(0),a(0){}int t,l,a;operator int(){return t+l;}I&d(I&s){char c,b;s>>c;if(c=='('){s>>c>>b;T(b!=')')c+=32;}if(s){R::iterator f=D.find(c);T(f==D.end())if(P[c]==l){l=f->S-l;a=0;}else{T(l&&(f->S>l))a=l==f->S?a+1:1;T(a>M[c])t+=l;l=f->S;}}return s;}};I&operator>>(I&s,U&d){return d.d(s);}int main(){D[' ']=-1;X(73,1,32,3)X(86,5,73,1)X(88,10,73,3)X(76,50,88,1)X(67,100,88,3)X(68,500,67,1)X(77,1000,67,3)X(118,5000,77,1)X(120,10000,77,3)X(108,50000,120,1)X(99,100000,120,3)X(100,500000,99,1)X(109,1000000,99,3)string w;while(cin>>w){try{stringstream s(w);U c;while(s>>c);cout<<c<<"\n";}catch(int x){cout<<"0\n";}}}
Il pourrait être encore compressé.
> ./a.exe
III
3
IIII
0
XVI
16
(C)(D)(L)MMI
452001
Formatage légèrement plus agréable: 1582 caractères
#include<map>
#include<string>
#include<iostream>
#include<sstream>
#define I istream
#define T(C) if(C)throw int(1);
#define X(c,v,f,m) D[c]=v;P[c]=D[f];M[c]=m;
#define S second
using namespace std;
typedef map<char,int> R;
R D,P,M;
struct U
{
U(): t(0), l(0), a(0) {}
int t,l,a;
operator int()
{
return t + l;
}
I& d(I& s)
{
char c,b;
s >> c;
if (c == '(')
{
s >> c >> b;
T(b != ')')
c = tolower(c);
}
if (s)
{
R::iterator f = D.find(c);
T(f == D.end())
if (P[c] == l)
{
l = f->S - l;
a = 0;
}
else
{
T(l&&(f->S > l))
a=l==f->S?a+1:1;
T(a>M[c])
t += l;
l = f->S;
}
}
return s;
}
};
I& operator>>(I& s,U& d)
{
return d.d(s);
}
int main()
{
D[' ']=-1;
X(73,1,32,3)
X(86,5,73,1)
X(88,10,73,3)
X(76,50,88,1)
X(67,100,88,3)
X(68,500,67,1)
X(77,1000,67,3)
X(118,5000,77,1)
X(120,10000,77,3)
X(108,50000,120,1)
X(99,100000,120,3)
X(100,500000,99,1)
X(109,1000000,99,3)
string w;
while(cin >> w)
{
try
{
stringstream s(w);
U c;
while(s >> c);
cout << c << "\n";
}
catch(int x)
{
cout << "0\n";
}
}
}
(C)(D)(L)MMI
serait 452 001. Comment avez-vous obtenu votre valeur? De plus, cela doit-il prendre en charge les formulaires "inappropriés" (par exempleIC
au lieu deXCIX
)?