Cod sursa(job #1348975)
Utilizator | Data | 19 februarie 2015 22:04:37 | |
---|---|---|---|
Problema | Convertor | Scor | 90 |
Compilator | cpp | Status | done |
Runda | rosedu_cdl_2015 | Marime | 3.04 kb |
#include <iostream>
#include <fstream>
using namespace std;
#define isNum(x) (x >= '0' && x <= '9')
int main ()
{
ifstream inFile ("convertor.in");
ofstream outFile ("convertor.out");
int state = 0;
char ch;
while (state != -1) {
ch = inFile.get();
switch (state) {
// Waiting for name to start
case 0:
if (ch == '"')
state = 1;
break;
// Reading the name/waiting for the name to end
case 1:
if (ch != '"')
outFile << ch;
else {
outFile << ',';
state = 2;
}
break;
// Waiting for the next end to start/waiting to end name reading
case 2:
if (ch == ',')
state = 0;
else if (ch == '}') {
// Rewinding the file, to read the values this time
inFile.seekg (0);
outFile << '\n';
state = 3;
}
break;
// Waiting for the start of name/end of file
case 3:
if (ch == '"')
state = 4;
else if (ch == ']')
state = -1;
else if (ch == '}')
outFile << '\n';
break;
// Waiting for the end of the name
case 4:
if (ch == ':')
state = 5;
break;
// Waiting for value to start
case 5:
if (isNum(ch)) {
outFile << ch;
state = 6;
}
else if (ch == '"')
state = 7;
break;
// Reading number value/waiting for value to end
case 6:
if (isNum(ch))
outFile << ch;
else {
outFile << ',';
if (ch == '}')
outFile << '\n';
state = 3;
}
break;
// Reading string value/waiting for value to end
case 7:
if (ch == '"') {
outFile << ',';
if (ch == '}')
outFile << '\n';
state = 3;
}
else
outFile << ch;
break;
// End
default:
state = -1;
break;
}
}
inFile.close();
outFile.close();
return 0;
}