Cod sursa(job #1360464)

Utilizator MmihaiMihai Mihailescu Mmihai Data 25 februarie 2015 15:08:55
Problema Convertor Scor 30
Compilator cpp Status done
Runda rosedu_cdl_2015 Marime 2.71 kb
#include <fstream>
#include <iostream>
#include <string>
#include <string.h>
#include <vector>
#include <algorithm>

using namespace std;
int main() 
{ 
    std::ifstream file("convertor.in");
    std::string str; 
    ofstream out("convertor.out");
  	string line;
  	vector<string> keys;
  	vector<string> values;
  	int number_of_values=0;

    size_t pos=0;
    string new_string;
    

      while(file>>new_string)
      {
   
        line=line+" "+new_string;
        if(new_string.compare("},")==0)
        {
          break;
        }
        
      }

        
        std::size_t found;
        found=line.find("\"");
        size_t temp;
        temp=line.find("\"",found+1);
      
        size_t virgula=line.find(",");
        string token=line.substr(found+1,temp-found-1);
        token=token+",";
        keys.push_back(token);
        while(found!=string::npos)
        {
         
          found=line.find("\"",virgula+1);
          if(found==string::npos)
            break;
          temp=line.find("\"",found+1);
          token=line.substr(found+1,temp-found-1);
          token=token+",";
          keys.push_back(token);
          virgula=line.find(",",virgula+1);
     
        }
        for(int i=0;i<keys.size();i++)
        {
          out<<keys[i];
        }
        out<<endl;
        number_of_values=keys.size();

        ifstream myfile("convertor.in");
        size_t two_points;

         while (std::getline(myfile, str))
           {
            string s=str;
            two_points=s.find(":");
           
            
            while(two_points!=std::string::npos)
            {
              if(s.substr(two_points+2,1).compare("\"")==0)//if string
              {            
             
               temp=s.find("\"",two_points+3);
               token=s.substr(two_points+3,temp-two_points-3);
               token=token+",";
               values.push_back(token);
               two_points=s.find(":",two_points+3);
               
              } 
              else
              {
                virgula=s.find(",",two_points+2);
                token=s.substr(two_points+2,virgula-two_points-2);
                while(!isdigit(token[0]))
                {
                    token.erase(token.begin(),token.begin()+1);
                }
                token=token+",";
                values.push_back(token);
                two_points=s.find(":",virgula+1);
              }      

            }
            s="";
            }

int counter;
            for(int i=0;i<values.size();i++)
            {
              out<<values[i];
              counter++;
              if(counter==keys.size())
              {
                out<<endl;
                counter=0;
              }
            }

return 0;
}