Pagini recente » Cod sursa (job #1302980) | Cod sursa (job #23979) | Cod sursa (job #347836) | Cod sursa (job #2016310) | Cod sursa (job #1361212)
#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 &endHeader ){
while( (*posInStr) != '"' ){
header[endHeader++] = (*posInStr);
posInStr++;
}
header[endHeader++] = ',';
}
void goToValue(){
while( (*posInStr) != '"' )
posInStr++;
posInStr++;
while( (*posInStr) != '"' && (!isNumber()) )
posInStr++;
}
void readValue( char *output , int &endOutput ){
if( isNumber() ){
while( isNumber() ){
output[endOutput++] = (*posInStr);
posInStr++;
}
}
if( (*posInStr) == '"' ){
posInStr++;
while( (*posInStr) != '"' ){
output[endOutput++] = (*posInStr);
posInStr++;
}
}
output[endOutput++] = ',';
}
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 endHeader = 0 , endOutput = 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 , endHeader );
}
print( output , endOutput );
break;
}
if( (*posInStr) == '}' ){
if( !readFirstElement ){
print( header , endHeader );
}
readFirstElement = true;
print( output , endOutput );
}
if( (*posInStr) == '"' ){
posInStr++;
if( !readFirstElement ){
readKey( header , endHeader );
}
goToValue();
readValue( output , endOutput );
}
posInStr++;
}
return 0;
}