Pagini recente » Cod sursa (job #1177650) | Cod sursa (job #1532483) | Cod sursa (job #1170340) | Clasament fminostressrecap | Cod sursa (job #1348883)
#include<fstream>
#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
class Convertor{
ifstream *in;
ofstream *out;
public:
Convertor(ifstream &in, ofstream &out);
void solve();
};
Convertor::Convertor(ifstream &in, ofstream &out){
this->in = ∈ this->out = &out;
}
void Convertor::solve(){
ifstream &f = *in; //I used new references to avoid using writing (*in)>>something
ofstream &g = *out;
vector<string> firstValues;
f>>noskipws; //not to skip whitespaces
char c;
f>>c;
while(true){ //solving for the first object
while(c!='"'){
f>>c;
}
f>>c; //we're jumping over the '"'
while(c!='"'){
g<<c; //printing the mutual keys
f>>c;
}
g<<',';
f>>c;
while((c<'0' || c>'9') && c!='"'){ //going to the next string or number
f>>c;
}
if(c=='"'){ //if we have a string
f>>c;
string firstElementString;
while(c!='"'){
firstElementString.append(1, c);
f>>c;
}
firstValues.push_back(firstElementString);
}
else{ //if we have a number
string firstElementString;
while(c>='0' && c<='9'){
firstElementString.append(1, c);
f>>c;
}
firstValues.push_back(firstElementString);
}
while(c!=',' && c!='}'){
f>>c;
}
if(c=='}'){ //then we finished the first object
g<<'\n';
break;
}
}
///<>------------------------------------------------------------------------------------------------
for(int i=0; i<firstValues.size(); ++i){ //printing first values
g<<firstValues[i]<<',';
}
g<<'\n';
///<>------------------------------------------------------------------------------------------------
while(true){ //solving for the rest of the file
f>>c;
while (c!= '{' && c != ']'){
f>>c;
}
if(c==']'){ //if we meet the ']' character then the Jason file is over
break;
}
while(true){
f>>c;
while(c!=':'){ //going to the first value, step 1: to ":"
f>>c;
}
f>>c;
while((c<'0' || c>'9') && c!='"'){ //step 2: to the number or string which is after the ":"
f>>c;
}
if(c=='"'){
f>>c;
while(c!='"'){
g<<c;
f>>c;
}
g<<',';
}
else{
while(c>='0'&&c<='9'){
g<<c;
f>>c;
}
g<<',';
}
while(c!=',' && c!='}'){
f>>c;
}
if(c=='}'){
g<<'\n';
break;
}
}
}
}
int main(){
ifstream f("convertor.in");
ofstream g("convertor.out");
Convertor *newConvertor = new Convertor(f,g);
newConvertor->solve();
return 0;
}