模拟记事本程序(C++实践)


/*输出的类*/
#include <iostream>
using namespace std;
class display{
 public:
  void displayzhumian();
};
 
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
/*编辑与保持的类*/
#include <iostream>
using namespace std;
class edit_save{
 public:
  int edit_save_display();
  void edit();
  int save();
  char str[3000];
  char path[50];
};
 
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
 /*打开文件与查看的类*/
#include <iostream>
using namespace std;
class openfile_watchfile{
 public:
  int openfile_watch_display();
  int openfile();
  void watchfile();
 private:
  char path2[20];
  char details[3000];   //用于把文件中提取的内容放进该数组中
};
 
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
/*复制与粘贴的类*/
#include <iostream>
#include "openfile_watchfile.h"
using namespace std;
class copy_stick:public openfile_watchfile{      //继承了类openfile_watchfile
 public:
  int display();
  void openfile();
 private:
  char a[3000];
  char c[3000];
  char b;
  int i;
  int k;
};
 
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
/*查找的类*/
#include <iostream>
#include "copy_stick.h"
using namespace std;
class find:public copy_stick{      //继承了类copy_stick   
 public:
  void Find();
  int display();
};
 
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
/*替换的类*/
#include "find.h"
class replace:public find{    //继承了类find
 public:
  int display();
};
 
 
======================================================================================
/*主函数*/
#include "displayclass.h"
#include "edit_save.h"
#include "replace.h"
#include <stdio.h>
#include <iostream>
#include <windows.h>
#include <conio.h>
using namespace std;
int main()
{
 while(1){
  int gongnengxuanze;
  int out=0;
  display a;
  edit_save b;
  openfile_watchfile c;
  copy_stick d;
  find e;
  replace f;
  a.displayzhumian();
  printf("请输入你要使用的功能:");
  cin>>gongnengxuanze;
  switch(gongnengxuanze){
  case 1:b.edit_save_display();break;
  case 2:c.openfile_watch_display();break;
  case 3:d.display();break;
  case 4:e.display();break;
  case 5:f.display();break;
  case 6:out=1;break;
  default:system("CLS");break;
  }
  if(out==1) break;
 }
 return 0;
}
 
======================================================================================
/*主界面的函数*/
#include "displayclass.h"
#include "stdio.h"

void display::displayzhumian()
{
 printf("\n--------------------------  记事本  -------------------------\n\n");
 printf("   1.编辑与保存\n\n");
 printf("   2.打开与查看\n\n");
 printf("   3.复制与粘贴\n\n");
 printf("   4.查找\n\n");
 printf("   5.替换\n\n");
 printf("                                               按ESC退出程序\n");
 printf("------------------------------------------------------------\n");
}
 
======================================================================================
/*编辑与保存的函数*/
#include "edit_save.h"
#include <windows.h>
#include  <fstream>
#include <stdio.h>
#include <string.h>
int edit_save::edit_save_display()
{
 int i,j;
 system("CLS");                          //清屏
 printf("--------------------------  编辑与保存  -------------------------\n");
 printf("\n按“回车”结束操作,请输入:\n");
 printf("-------------------------------------------------------------\n");
 edit_save::edit();
 printf("\n\n\n-------------------------------------------------------------\n");
 printf("文件还没保存,是否要保存,按“1”保存,按“2”退出返回主界面\n");
 printf("--->");
 cin>>i;
 switch(i){
 case 1:j=edit_save::save();break;
 default:system("CLS");return 1;
 }
 if(j==0){
  printf("保存成功\n");
 }
 printf("按“回车”返回主界面");
 getchar();
 getchar();
 system("CLS");
 return 0;
}

void edit_save::edit()
{
 scanf("%s",str);
}

int edit_save::save()
{
 printf("\n请输入你要保存的文件名称,例如“文件.txt”\n");
 printf("--->");
 cin>>path;
 ofstream fout(path);    //先是声明fstream头文件中的ofstream类中的fout函数,再进行调用,输入地址
 if(!fout){
  cout<<"打开文件失败";
  return 1;
 }
 fout<<str<<endl;        //把str字符串的内容输入到该文件中
 //fout.close();           //把文件关闭
 return 0;
}
 
======================================================================================
/*打开与查看的函数*/
#include "openfile_watchfile.h"
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <windows.h>
#include <string.h>
using namespace std;
int openfile_watchfile::openfile_watch_display()
{
 int i,j;
 system("CLS");
 printf("--------------------------  打开与查看  -------------------------\n");
 i=openfile_watchfile::openfile();
 if(i==0){
  printf("\n-------------------------------------------------------------\n");
  printf("打开文件成功\n\n");
 }
 else{
  cout<<"按“回车”返回主界面"<<endl;
  getchar();
  getchar();                           //用于等待用户按一个键,然后返回
  system("CLS");      //清屏
  return 1;
 }
 printf("是否要查看文件内容,按“1”查看,按“2”返回主界面\n");
 printf("--->");
 cin>>j;
 if(j==1){
  openfile_watchfile::watchfile();
 }
 else{
  return 1;
 }
 printf("按“回车”返回主界面\n");
 getchar();
 getchar();                   //用于等待用户按一个键,然后返回
 system("CLS");               //清屏
 return 0;
}

int openfile_watchfile::openfile()
{
 printf("\n请输入你要打开的文件名称,例如“文件.txt”\n");
 printf("--->");
 scanf("%s",path2);
 ifstream fin(path2);                     //调用fstream中,派生类ifstream中的file函数
 if(fin.fail()){                           //如果打开文件失败则,fin.fail()会返回0,否则为返回非0
  cout<<endl<<"打开文件失败"<<endl;
  return 1;
 }
 for(int i=0;!fin.eof();i++){             //fin.eof函数判断是否为文件内容读取结束,结束就返回0,否则非0
  details[i]=fin.get();                      //fin.get函数是读书文件内容
 }
 details[i]='\0';                 
 fin.close();                             //关闭文件                  
 return 0;
}

void openfile_watchfile::watchfile()
{                
 system("CLS");               //清屏
 printf("--------------------------  打开与查看  -------------------------\n");
 Len=strlen(details);
 printf("\n\n文件的内容如下:\n");
 printf("=================================================================\n");
 for(int i=0;i<Len;i++){
  cout<<details[i];
 }
 cout<<endl;
 printf("\n=================================================================\n");
}
 
======================================================================================
/*复制与粘贴的函数*/
#include "copy_stick.h"
#include <stdio.h>
#include <iostream>
#include <windows.h>
#include <string.h>
#include <fstream>
using namespace std;
int copy_stick::display()
{
 int len;
 system("CLS");
 printf("--------------------------  复制与粘贴  -------------------------\n");
 openfile_watchfile::openfile();                                               //引用了公继承的openfile()函数
 len=strlen(details);
 int num1,num2;
 int place;
 printf("\n文件的内容如下:\n");
 printf("=================================================================\n");
 for(int j=0;j<len;j++){
  cout<<details[j];
 }
 cout<<endl;
 printf("=================================================================\n"); 
 printf("\n请选择你要复制的内容\n");
 printf("例如:原文件内容:abcdefgh,需要复制b到f,就输入2-6\n");
 printf("--->");
 cin>>i;
 cin>>b;
 cin>>k;
 for(num1=0,j=i-1;j<k;j++,num1++){
  a[num1]=details[j];
 }
 a[num1]='\0';
 while(1){
  printf("请输入你要粘贴的位置(后面)\n");
  printf("--->");
  cin>>place;
  if(place<=strlen(details)&&place>=0){
   break;
  }
  printf("\n请输入正确的位置!!\n");
 }
 for(num2=0,j=place;j<len;j++,num2++){
  c[num2]=details[j];
  details[j]='\0';
 }
 c[num2]='\0';
 strcat(details,a);
 strcat(details,c);
 ofstream fout(path2);                                                         //储存进记事本里
 fout<<details;
 printf("\n复制粘贴成功,粘贴后的内容如下:\n");
 printf("=============================================================\n");
 puts(details);
 printf("=============================================================\n");
 printf("按“回车”返回主界面\n");
 getchar();
 getchar();
 system("CLS");
 return 0;
}
 
======================================================================================
/*查找的函数*/
#include "find.h"
#include <iostream>
#include <stdio.h>
#include <windows.h>
#include <string.h>
using namespace std;
int len;
int find::display()
{
 int i;
 system("CLS");
 printf("--------------------------  查找  -------------------------\n");
 i=openfile_watchfile::openfile();                                          //引用openfile_watch中的openfile函数
 if(i==0) printf("打开文件成功\n");
 else{
  printf("按“回车”返回主界面\n");
  getchar();
  getchar();
  system("CLS");
  return 0;
 }
 len=strlen(details);
 /*printf("\n文件的内容如下:\n");
 printf("=================================================================\n");
 for(int j=0;j<len;j++){
  cout<<details[j];
 }
 cout<<endl;
 printf("=================================================================\n");*/
 find::Find();
 system("CLS");
 return 0;
}

void find::Find()
{
 while(1){
  int u,j,i,place,y,l;
  int changdu;                                       //用来计算str1里面有多少个需要查找的内容
  char str1[3000],*pa,*pb;
  printf("==========================================================\n");
  printf("请输入要寻找的内容\n");
  printf("--->");
  cin>>str1;
  //p=strstr(details,str1);用不了strstr函数
  changdu=strlen(str1);                       
  pa=details;                                        //使用指针
  pb=str1;
  /*进行查找*/
  for(i=0,u=0,j=0,y=0;i<len;i++){
   if(*(pa+i)==*(pb+y)){
    if(y==0) place=i+1;
    u++;
    y++;
   }
   else{
    u=0;
    y=0;
   }
   if(u==changdu){
    if(j==0){
     printf("出现的位置(第几位): ");
     j++;
    }
    printf("%d ",place);
   }
  }
  cout<<endl;
  if(j==0) printf("\n没有搜索到你要找的内容!!\n");
  printf("\n继续查找请按“1”,按“2”返回主界面\n");
  printf("--->");
  scanf("%d",&l);
  if(l==2) break;
 }
}
 
======================================================================================
/*替换的函数*/
#include "replace.h"
#include <iostream>
#include <stdio.h>
#include <windows.h>
#include <string.h>
#include <fstream>
using namespace std;
int replace::display()
{
 char set1[3000],set2[3000];
 char str2[3000];
 int i;
 int len;
 int a1,a2,a3;
 char a0;
 system("CLS");
 printf("--------------------------  替换  -------------------------\n");
 i=openfile_watchfile::openfile();
 len=strlen(details);
 if(i==0){
  printf("--->>打开文件成功\n\n");
 }
 else{
  cout<<"按“回车”返回主界面"<<endl;
  getchar();
  getchar();                                 //用于等待用户按一个键,然后返回
  system("CLS");              //清屏
  return 1;
 }
 printf("\n文件的内容如下:\n");
 printf("=================================================================\n");
 for(int j=0;j<len;j++){
  cout<<details[j];
 }
 cout<<endl;
 printf("=================================================================\n");
 printf("请输入要替换的内容\n");
 printf("--->");
 cin>>str2;
 printf("请输入要替换的位置,例如:原文件内容:abcdefgh,需要复制b到f,就输入2-6\n");
 printf("--->");
 cin>>a1;
 cin>>a0;
 cin>>a2;
 /*替换操作*/
 for(i=0;i<a1-1;i++){
  set1[i]=details[i];
 }
 set1[i]='\0';
 for(i=a2,a3=0;i<len;i++,a3++){
  set2[a3]=details[i];
 }
 set2[a3]='\0';
 strcpy(details,set1);
 strcat(details,str2);
 strcat(details,set2);
 ofstream fout(path2);                           //输出规定的文件
 fout<<details<<endl;             //把内容输出
 fout.close();             //文件关闭
 len=strlen(details);
 printf("\n\n替换后的内容为:\n");
 printf("=================================================================\n");
  for(j=0;j<len;j++){
  cout<<details[j];
 }
 cout<<endl;
 printf("=================================================================\n");
 printf("按“回车”返回主界面\n");
 getchar();
 getchar();
 system("CLS");
 return 0;
}
 
 ===============================================================================











展开阅读全文

没有更多推荐了,返回首页