Advertisement
Guest User

programforum

a guest
Jun 3rd, 2014
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.82 KB | None | 0 0
  1. #include "stdafx.h"
  2.  
  3. #include <cstdlib>
  4. #include <string>
  5. #include <fstream>
  6.  
  7. #define file_replace_list "replace.ini"
  8. #define file_data         "input.txt"
  9. #define file_output       "output.txt"
  10. struct TReplace_Const
  11. {
  12.         long num;
  13.         std::string word;
  14.         std::string word2;
  15. };
  16.  
  17. struct alist
  18. {
  19.     struct alist *next;
  20.     struct TReplace_Const data;
  21. };
  22. //struct alist *test_list;
  23. /*struct alist
  24. {
  25.     struct alist *next;
  26.     struct TReplace_Const data;
  27. };*/
  28. //typedef struct alist *List;
  29.  
  30. struct alist *small_list=new (struct alist);
  31. struct alist *big_list=new (struct alist);
  32. struct alist *def_list=new (struct alist);
  33. bool flag_replace;
  34.  
  35. long StrToInt(std::string s)
  36. {
  37.     long code,value;
  38.     return atoi (s.c_str());
  39. }
  40. void read_line(FILE *f, std::string *line)
  41. {
  42.     char c=' ';
  43.     *line="";
  44.     while ((f!=NULL)&&(c!=(char)(10))&&(c!=EOF))
  45.     {
  46.         c = (char) fgetc(f);
  47.         //printf("\n%c",c);
  48.         if (c!=(char)(10)) *line=*line+c;
  49.     }
  50. }
  51.  
  52. std::string ParseString(std::string *line, int *i)
  53. {
  54.     std::string s="";
  55.     while ((*i<(*line).size())&&((*line).at(*i)!=((char) 9))&&((*line).at(*i)!=' '))
  56.     {
  57.         //printf("parsestring");//,line[*i]);
  58.         //printf("\n%s",s);
  59.         //strcpy(s,(const std::string *)(line[*i]));
  60.         s=s+(*line).at(*i);
  61.         //strcat(s,'a');
  62.         (*i)++;
  63.     }
  64.     (*i)++;
  65.     if (*i==1) *i=0;
  66.  
  67.     return s;
  68. }
  69.  
  70. long ParseInteger(std::string *line, int *i)
  71. {
  72.     std::string s="";
  73. //  std::string a[255];
  74.     while (((*i)<(*line).size())&&((*line).at(*i)!=((char) 9))&&((*line).at(*i)!='.'))
  75.     {
  76.         //printf("parseinteger");//,line[*i]);
  77.         //printf("\n%s",s);
  78.         s=s+(*line).at(*i);
  79.         (*i)++;
  80.     }
  81.     if (*i==1) *i=0;
  82.     return atoi(s.c_str());
  83. }
  84.  
  85. void pload_curr_list(FILE* f, std::string *line, struct alist *pList, bool type_load)
  86. {
  87.     char fline[255];
  88.     printf("pload_curr_list");
  89.     struct alist *pointer=new (struct alist);
  90.     int i,k;
  91.     struct alist *a;
  92.     pointer=pList;
  93.     k=0;
  94.     (*line).at(0)=' ';
  95.     while ((!feof(f))&&((*line).at(0)!='['))
  96.     {
  97.         //fgets(line,sizeof(line),f);
  98.         //fscanf(f,"%[^\n]",fline);
  99.         read_line(f,line);
  100.         printf("\n%s",(*line).c_str());
  101.         //printf("\n%s",*line);
  102.         //line=fline;
  103.         i=0;
  104.         pointer->data.word=ParseString(line,&i);
  105.         if (type_load) pointer->data.num=ParseInteger(line,&i);
  106.         else pointer->data.word2=ParseString(line,&i);
  107.         pointer->next = new(struct alist);
  108.         pointer=pointer->next;
  109.         k++;
  110.     }
  111.     pointer=pList;
  112.     for (i=1;i<k;i++) pointer=pointer->next;
  113.     delete pointer->next;
  114.     pointer->next=NULL;
  115.     int at=0;
  116.     //pointer->next=NULL;
  117. }
  118. /*procedure pload_lists;
  119.     var
  120.         line : string;
  121.         f    : text;
  122.     begin
  123.         new(small_list);
  124.         new(big_list);
  125.         new(def_list);
  126.         assign(f,file_replace_list);
  127.         reset(f);
  128.         Readln(f,line);
  129.         if line='[small]' then pLoad_curr_list(f, line, small_list,true);
  130.         if line='[big]'   then pLoad_curr_list(f, line, big_list,true);
  131.         if line='[definitions]' then pLoad_curr_list(f, line, def_list,false);
  132.         close(f);
  133.     end;
  134.     */
  135. void pload_lists()
  136. {
  137.     std::string line="";
  138. //  FILE* f;
  139.     char fline[255];
  140.     FILE* f = fopen(file_replace_list,"r");
  141.     //fgets(line,sizeof(line),f);
  142.     //fscanf(f,"%[^\n]",&fline);
  143.     read_line(f,&line);
  144.     printf("%s %d ",line,strcmp(line.c_str(),"[small]"));
  145.     if (line=="[small]") pload_curr_list(f,&line,small_list,true);
  146.     if (line=="[big]") pload_curr_list(f,&line,big_list,true);
  147.     if (line=="[definitions]") pload_curr_list(f,&line,def_list,false);
  148.     fclose(f);
  149. }
  150.  
  151. /*function is_in_list(word:string; var pList:List):string;
  152.     var
  153.         pointer : List;
  154.         i,k     : integer;
  155.     begin
  156.         pointer:=pList;
  157.         while (pointer<>nil) do begin
  158.             if pointer^.data.word=word then begin
  159.                 is_in_list:=pointer^.data.word2;
  160.                 exit;
  161.             end;
  162.             pointer:=pointer^.next;
  163.         end;
  164.         is_in_list:=word;
  165.     end;
  166.     */
  167. std::string is_in_list(std::string word, struct alist *pList)
  168. {
  169.     struct alist *pointer;
  170.     int i,k;
  171.  
  172.     pointer=pList;
  173.     k=0;
  174.     while (pointer!=NULL)
  175.     {
  176.         k++;
  177.         printf("\n%s",pointer->data.word.c_str());
  178.         if (pointer->data.word==word) return pointer->data.word2;
  179.         pointer=pointer->next;
  180.     }
  181.     return word;
  182. }
  183.  
  184. /*function find_in_list(word:string; var pList:List):longint;
  185.     var
  186.         pointer : List;
  187.     begin
  188.         pointer:=pList;
  189.         while pointer<>nil do begin
  190.             if pointer^.data.word=word then begin
  191.                 find_in_list:=pointer^.data.num;
  192.                 exit;
  193.             end;
  194.             pointer:=pointer^.next;
  195.         end;
  196.         find_in_list:=-1;
  197.     end;*/
  198. long find_in_list(std::string *word, struct alist *pList)
  199. {
  200.     struct alist *pointer;
  201.     int i,k;
  202.  
  203.     pointer=pList;
  204.     while (pointer!=NULL)
  205.     {
  206.         printf("\n%s %s ",pointer->data.word.c_str(),(*word).c_str());
  207.         if (pointer->data.word==*word) return pointer->data.num;
  208.         pointer=pointer->next;
  209.     }
  210.     return -1;
  211. }
  212.  
  213. /*procedure preplace_strings;
  214.     var
  215.         line               : string;
  216.         small_num, big_num : longint;
  217.         f                  : text;
  218.         sum, small_sum, res: longint;
  219.         i                  : integer;
  220.         temp               : string;
  221.        // flag               : boolean;
  222.     begin
  223.         assign(f,file_data);
  224.         reset(f);
  225.         sum :=0;
  226.         assign(output, 'output.txt');
  227.         rewrite(output);
  228.         while not eof(f) do
  229.         begin
  230.             readln(f,line);
  231.             i:=1;
  232.             //flag:=false;
  233.             while i<length(line) do begin
  234.                 temp:=ParseString(line,i);
  235.                 if flag_replace then temp:=is_in_list(temp, def_list);
  236.                 res:=find_in_list(temp,small_list);
  237.                 if res=-1 then begin
  238.                     res:=find_in_list(temp,big_list);
  239.                     if res=-1 then begin
  240.                         if small_sum<>0 then inc(sum,small_sum);
  241.                         if sum<>0 then write(sum,' ');
  242.                         write(temp,' ');
  243.                         sum:=0;
  244.                         small_sum:=0;
  245.                     end
  246.                     else begin
  247.                         sum:=sum+small_sum*res;
  248.                         small_sum:=0;
  249.                     end
  250.                 end
  251.                 else begin
  252.                     inc(small_sum,res);
  253.                 end;
  254.  
  255.             end;
  256.             writeln;
  257.         end;
  258.         close(f);
  259.         close(output);
  260.     end;*/
  261. void preplace_strings()
  262. {
  263.     char fline[255];
  264.     std::string line;
  265.     long small_num,big_num;
  266.     //FILE *f,*fo;
  267.     long sum, small_sum, res;
  268.     int i,b;
  269.     std::string temp;
  270.  
  271.     FILE* f = fopen(file_data,"r");
  272.     FILE* fo = fopen(file_output,"w");
  273.     sum=0;
  274.     small_sum=0;
  275.     while (!feof(f))
  276.     {
  277.         //fgets(line,sizeof(line),f);
  278.         read_line(f,&line);
  279.  
  280.         i=0;
  281.         while (i<line.size())
  282.         {
  283.             temp=ParseString(&line, &i);
  284.             b=1;
  285.             //printf("\n%d",b);
  286.             if (flag_replace) temp=is_in_list(temp, def_list);
  287.             b=2;
  288.             //printf("\n%d",b);
  289.             res=find_in_list(&temp, small_list);
  290.             b=3;
  291.             //printf("\n%d",b);
  292.             if (res==-1)
  293.             {
  294.                 res=find_in_list(&temp, big_list);
  295.                 if (res==-1)
  296.                 {
  297.                     if (small_sum!=0) sum+=small_sum;
  298.                     if (sum!=0) fprintf(fo, "%d ", sum);
  299.                     fprintf(fo, "%s ", temp.c_str());
  300.                     printf("%s ", temp);
  301.                     sum=0;
  302.                     small_sum=0;
  303.                 }
  304.                 else
  305.                 {
  306.                     sum+=(small_sum*res);
  307.                     small_sum=0;
  308.                 }
  309.             }
  310.             else small_sum+=res;
  311.         }
  312.         fprintf(fo, "\n");
  313.     }
  314.     fclose(f);
  315.     fclose(fo);
  316. }
  317.  
  318. /*
  319.     clrscr;
  320.     pload_lists;
  321.     writeln('Shall we replace valuta with short names?');
  322.     readln(answer);
  323.     if (answer[1]='y') or (answer[1]='Y') then flag_replace:=true else flag_replace:=false;
  324.     preplace_strings;*/
  325. int _tmain()
  326. {
  327.     //system("clear");
  328.  
  329.     char fline[255];
  330.     std::string answer;
  331.    
  332.     pload_lists();
  333.     printf("Shall we replace valuta with short names?");
  334.     scanf("%s",&fline);
  335.     answer=fline;
  336.     if ((answer.at(0)='y')||(answer[0]='Y')) flag_replace=true;
  337.     else flag_replace=false;
  338.     preplace_strings();
  339.     return 0;
  340. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement