Cod sursa(job #1360482)

Utilizator MmihaiMihai Mihailescu Mmihai Data 25 februarie 2015 15:27:31
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;
  
  

    size_t pos=0;
    string new_string;
    int counter=0;


      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;
        

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

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

            }
            str="";
            }

return 0;
}