Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Курсовик2

.CPP
Скачиваний:
2
Добавлен:
01.05.2014
Размер:
2.6 Кб
Скачать
#include <stdio.h>
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
FILE *mfclose(FILE *fl)
{
  fclose(fl);
  return 0;
};
int main(void)
{
  FILE *fl1=NULL,*fl2=NULL;
  char *name,buf1,buf2,*name1;
  int result,sign;
  for(char r='y';r=='y';)
  {
    clrscr();
    while(fl1==NULL)
    {
      cout << "Enter name of source file: \n >";
      gets(name);
      if((fl1=fopen(name,"r"))==NULL)
	{
	  perror("While file was opening, there were some errors");
	  cout<<"\n Restart program (y/n) \n >";
	  cin>>r;
	  if(r=='n') return 0;
	};
    };
    cout<<"\n\n***Sourse file :: "<<name<<"***\n\n";
    buf1=' ';
    while(buf1!=EOF)
    {
      buf1=getc(fl1);
      cout<<buf1;
    };
    fl1=mfclose(fl1);
    fl1=fopen(name,"r");
    buf1=' ';
    cout<<"\n>>EOF\n\n***Modified text***\n\n";
    while(buf1!=EOF)
    {
      buf2=buf1;
      buf1=getc(fl1);
      if(buf1>47&&buf1<58||buf1=='-')
      {
	if(buf2==' ')
	{
	  sign=1;
	  if(buf1>47&&buf1<58)
	    result=buf1-48;
	  else
	    sign=-1;
	}
	else
	  if(buf2>47&&buf2<58||buf2=='-'||buf2=='\n')
	    (result*=10)+=(buf1-48);
      }
      else
      {
	if(buf2>47&&buf2<58&&(buf1==' '||buf1=='\n'))
	{
	  cout<<result*sign<<"\n";
	  result=0;
	}
      };
    };
    fl1=mfclose(fl1);
    fl2=mfclose(fl2);
    cout<<"\n>>EOF\n\n***Save this modified text? (y/n) \n >";
    cin>>r;
    buf1=' ';
    if(r=='y')
      {
	while(fl2==NULL&&r=='y')
	  {
	    cout<<"Enter name of new file to save modified text: \n >";
	    gets(name1);
	    if((fl2=fopen(name1,"w"))==NULL)
	      {
		perror("While file was rewriting, there were some errors");
		cout<<"\n Repeat enter file name (y/n) \n >";
		cin>>r;
	      };
	  };
	buf1=' ';
	if(r=='y')
	  {
	    fl1=fopen(name,"r");
	    cout<<"\n\n***Saved file :: "<<name1<<"***\n\n";
	    while(buf1!=EOF)
	    {
	      buf2=buf1;
	      buf1=getc(fl1);
	      if(buf1>47&&buf1<58||buf1=='-')
	      {
		if(buf2==' ')
		{
		  sign=1;
		  if(buf1>47&&buf1<58)
		    result=buf1-48;
		  else
		    sign=-1;
		}
		else
		  if(buf2>47&&buf2<58||buf2=='-'||buf2=='\n')
		    (result*=10)+=(buf1-48);
	      }
	      else
	      {
		if(buf2>47&&buf2<58&&(buf1==' '||buf1=='\n'))
		{
		  fprintf(fl2,"%d \n",result*sign);
		  cout << result*sign << "\n";
		  result=0;
		};
	      };
	    };
	  };
      };
    cout<<"\n>>EOF \n\nRepeat start of program (y/n) \n >";
    cin>>r;
    if(r!='y') return 0;
    fl1=mfclose(fl1);
    fl2=mfclose(fl2);
  };
  return 0;
};
Соседние файлы в предмете Программирование