Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <cstdlib>
- #include <string>
- #include <fstream>
- #define file_replace_list "replace.ini"
- #define file_data "input.txt"
- #define file_output "output.txt"
- struct TReplace_Const
- {
- long num;
- std::string word;
- std::string word2;
- };
- struct alist
- {
- struct alist *next;
- struct TReplace_Const data;
- };
- //struct alist *test_list;
- /*struct alist
- {
- struct alist *next;
- struct TReplace_Const data;
- };*/
- //typedef struct alist *List;
- struct alist *small_list=new (struct alist);
- struct alist *big_list=new (struct alist);
- struct alist *def_list=new (struct alist);
- bool flag_replace;
- long StrToInt(std::string s)
- {
- long code,value;
- return atoi (s.c_str());
- }
- void read_line(FILE *f, std::string *line)
- {
- char c=' ';
- *line="";
- while ((f!=NULL)&&(c!=(char)(10))&&(c!=EOF))
- {
- c = (char) fgetc(f);
- //printf("\n%c",c);
- if (c!=(char)(10)) *line=*line+c;
- }
- }
- std::string ParseString(std::string *line, int *i)
- {
- std::string s="";
- while ((*i<(*line).size())&&((*line).at(*i)!=((char) 9))&&((*line).at(*i)!=' '))
- {
- //printf("parsestring");//,line[*i]);
- //printf("\n%s",s);
- //strcpy(s,(const std::string *)(line[*i]));
- s=s+(*line).at(*i);
- //strcat(s,'a');
- (*i)++;
- }
- (*i)++;
- if (*i==1) *i=0;
- return s;
- }
- long ParseInteger(std::string *line, int *i)
- {
- std::string s="";
- // std::string a[255];
- while (((*i)<(*line).size())&&((*line).at(*i)!=((char) 9))&&((*line).at(*i)!='.'))
- {
- //printf("parseinteger");//,line[*i]);
- //printf("\n%s",s);
- s=s+(*line).at(*i);
- (*i)++;
- }
- if (*i==1) *i=0;
- return atoi(s.c_str());
- }
- void pload_curr_list(FILE* f, std::string *line, struct alist *pList, bool type_load)
- {
- char fline[255];
- printf("pload_curr_list");
- struct alist *pointer=new (struct alist);
- int i,k;
- struct alist *a;
- pointer=pList;
- k=0;
- (*line).at(0)=' ';
- while ((!feof(f))&&((*line).at(0)!='['))
- {
- //fgets(line,sizeof(line),f);
- //fscanf(f,"%[^\n]",fline);
- read_line(f,line);
- printf("\n%s",(*line).c_str());
- //printf("\n%s",*line);
- //line=fline;
- i=0;
- pointer->data.word=ParseString(line,&i);
- if (type_load) pointer->data.num=ParseInteger(line,&i);
- else pointer->data.word2=ParseString(line,&i);
- pointer->next = new(struct alist);
- pointer=pointer->next;
- k++;
- }
- pointer=pList;
- for (i=1;i<k;i++) pointer=pointer->next;
- delete pointer->next;
- pointer->next=NULL;
- int at=0;
- //pointer->next=NULL;
- }
- /*procedure pload_lists;
- var
- line : string;
- f : text;
- begin
- new(small_list);
- new(big_list);
- new(def_list);
- assign(f,file_replace_list);
- reset(f);
- Readln(f,line);
- if line='[small]' then pLoad_curr_list(f, line, small_list,true);
- if line='[big]' then pLoad_curr_list(f, line, big_list,true);
- if line='[definitions]' then pLoad_curr_list(f, line, def_list,false);
- close(f);
- end;
- */
- void pload_lists()
- {
- std::string line="";
- // FILE* f;
- char fline[255];
- FILE* f = fopen(file_replace_list,"r");
- //fgets(line,sizeof(line),f);
- //fscanf(f,"%[^\n]",&fline);
- read_line(f,&line);
- printf("%s %d ",line,strcmp(line.c_str(),"[small]"));
- if (line=="[small]") pload_curr_list(f,&line,small_list,true);
- if (line=="[big]") pload_curr_list(f,&line,big_list,true);
- if (line=="[definitions]") pload_curr_list(f,&line,def_list,false);
- fclose(f);
- }
- /*function is_in_list(word:string; var pList:List):string;
- var
- pointer : List;
- i,k : integer;
- begin
- pointer:=pList;
- while (pointer<>nil) do begin
- if pointer^.data.word=word then begin
- is_in_list:=pointer^.data.word2;
- exit;
- end;
- pointer:=pointer^.next;
- end;
- is_in_list:=word;
- end;
- */
- std::string is_in_list(std::string word, struct alist *pList)
- {
- struct alist *pointer;
- int i,k;
- pointer=pList;
- k=0;
- while (pointer!=NULL)
- {
- k++;
- printf("\n%s",pointer->data.word.c_str());
- if (pointer->data.word==word) return pointer->data.word2;
- pointer=pointer->next;
- }
- return word;
- }
- /*function find_in_list(word:string; var pList:List):longint;
- var
- pointer : List;
- begin
- pointer:=pList;
- while pointer<>nil do begin
- if pointer^.data.word=word then begin
- find_in_list:=pointer^.data.num;
- exit;
- end;
- pointer:=pointer^.next;
- end;
- find_in_list:=-1;
- end;*/
- long find_in_list(std::string *word, struct alist *pList)
- {
- struct alist *pointer;
- int i,k;
- pointer=pList;
- while (pointer!=NULL)
- {
- printf("\n%s %s ",pointer->data.word.c_str(),(*word).c_str());
- if (pointer->data.word==*word) return pointer->data.num;
- pointer=pointer->next;
- }
- return -1;
- }
- /*procedure preplace_strings;
- var
- line : string;
- small_num, big_num : longint;
- f : text;
- sum, small_sum, res: longint;
- i : integer;
- temp : string;
- // flag : boolean;
- begin
- assign(f,file_data);
- reset(f);
- sum :=0;
- assign(output, 'output.txt');
- rewrite(output);
- while not eof(f) do
- begin
- readln(f,line);
- i:=1;
- //flag:=false;
- while i<length(line) do begin
- temp:=ParseString(line,i);
- if flag_replace then temp:=is_in_list(temp, def_list);
- res:=find_in_list(temp,small_list);
- if res=-1 then begin
- res:=find_in_list(temp,big_list);
- if res=-1 then begin
- if small_sum<>0 then inc(sum,small_sum);
- if sum<>0 then write(sum,' ');
- write(temp,' ');
- sum:=0;
- small_sum:=0;
- end
- else begin
- sum:=sum+small_sum*res;
- small_sum:=0;
- end
- end
- else begin
- inc(small_sum,res);
- end;
- end;
- writeln;
- end;
- close(f);
- close(output);
- end;*/
- void preplace_strings()
- {
- char fline[255];
- std::string line;
- long small_num,big_num;
- //FILE *f,*fo;
- long sum, small_sum, res;
- int i,b;
- std::string temp;
- FILE* f = fopen(file_data,"r");
- FILE* fo = fopen(file_output,"w");
- sum=0;
- small_sum=0;
- while (!feof(f))
- {
- //fgets(line,sizeof(line),f);
- read_line(f,&line);
- i=0;
- while (i<line.size())
- {
- temp=ParseString(&line, &i);
- b=1;
- //printf("\n%d",b);
- if (flag_replace) temp=is_in_list(temp, def_list);
- b=2;
- //printf("\n%d",b);
- res=find_in_list(&temp, small_list);
- b=3;
- //printf("\n%d",b);
- if (res==-1)
- {
- res=find_in_list(&temp, big_list);
- if (res==-1)
- {
- if (small_sum!=0) sum+=small_sum;
- if (sum!=0) fprintf(fo, "%d ", sum);
- fprintf(fo, "%s ", temp.c_str());
- printf("%s ", temp);
- sum=0;
- small_sum=0;
- }
- else
- {
- sum+=(small_sum*res);
- small_sum=0;
- }
- }
- else small_sum+=res;
- }
- fprintf(fo, "\n");
- }
- fclose(f);
- fclose(fo);
- }
- /*
- clrscr;
- pload_lists;
- writeln('Shall we replace valuta with short names?');
- readln(answer);
- if (answer[1]='y') or (answer[1]='Y') then flag_replace:=true else flag_replace:=false;
- preplace_strings;*/
- int _tmain()
- {
- //system("clear");
- char fline[255];
- std::string answer;
- pload_lists();
- printf("Shall we replace valuta with short names?");
- scanf("%s",&fline);
- answer=fline;
- if ((answer.at(0)='y')||(answer[0]='Y')) flag_replace=true;
- else flag_replace=false;
- preplace_strings();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement