Pagini recente » Monitorul de evaluare | Cod sursa (job #2823364) | Istoria paginii utilizator/tudorfinaru | Profil gabipurcaru | Cod sursa (job #1361218)
#include <fstream>
#include<string>
#include<iostream>
#define MAX_LINE_LENGTH 1100
#define MAX_OUTPUT_LINE_LENGTH 1000000
using namespace std;
ifstream f("convertor.in");
ofstream g("convertor.out");
char line[MAX_LINE_LENGTH];
char *posInStr = line;
bool isNumber(){
if( '0' <= (*posInStr) && (*posInStr) <= '9' )
return true;
return false;
}
void readKey( char *header , int &last ){
while( (*posInStr) != '"' ){
header[last++] = (*posInStr);
posInStr++;
}
header[last++] = ',';
}
void goToValue(){
while( (*posInStr) != '"' )
posInStr++;
posInStr++;
while( (*posInStr) != '"' && (!isNumber()) )
posInStr++;
}
void readValue( char *output , int &last ){
if( isNumber() ){
while( isNumber() ){
output[last++] = (*posInStr);
posInStr++;
}
}
if( (*posInStr) == '"' ){
posInStr++;
while( (*posInStr) != '"' ){
output[last++] = (*posInStr);
posInStr++;
}
}
output[last++] = ',';
}
void print( char *str , int &last ){
str[last] = '\0';
g<<str<<"\n";
last = 0;
}
int main()
{
char output[MAX_OUTPUT_LINE_LENGTH] , header[MAX_OUTPUT_LINE_LENGTH];
int lastPosInHeader = 0 , lastPosInOutput = 0;
bool readFirstElement = false;
f.getline( line , MAX_LINE_LENGTH );
posInStr = line;
while( true ){
if( (*posInStr) == '\0' ){
f.getline( line , MAX_LINE_LENGTH );
posInStr = line;
}
if( (*posInStr) == ']' ){
if( !readFirstElement ){
print( header , lastPosInHeader );
}
print( output , lastPosInOutput );
break;
}
if( (*posInStr) == '}' ){
if( !readFirstElement ){
print( header , lastPosInHeader );
}
readFirstElement = true;
print( output , lastPosInOutput );
}
if( (*posInStr) == '"' ){
posInStr++;
if( !readFirstElement ){
readKey( header , lastPosInHeader );
}
goToValue();
readValue( output , lastPosInOutput );
}
posInStr++;
}
return 0;
}