node.h
====================================
#include<iostream>
template<class T>
class node
{
private:
node<T> *next;
public:
T data;
node(void);
node(T data1,node<T> *p=NULL);
void insertafter(node<T> *p);
void deleteafter(void);
node<T>* nextnode(void);
};
template<class T>
node<T>::node(void)
{
next=NULL;
}
template<class T>
node<T>::node(T data1,node<T> *p)
{
data=data1;
next=p;
}
template<class T>
void node<T>::insertafter(node<T> *p)
{
p->next=next;
next=p;
}
template<class T>
void node<T>::deleteafter(void)
{
node<T> *temp;
temp=next;
next=temp->next;
delete temp;
}
template<class T>
node<T>* node<T>::nextnode(void)
{
return next;
}
list.h
=====================================
#include<iostream>
#include<stdlib.h>
#include"node.h"
template<class T>
class list
{
private:
node<T> *head,*newnode,*last,*currnode,*prenode;
public:
list(void);
node<T>* setnode(T data2,node<T>* p=NULL);
void createlist(T data2,node<T>* p=NULL);
void Insertlast(T p);
void deletedata(T d);
void print(void);
void deletelist(void);
void athead(void);
T Data(void);
node<T>* Next(void);
node<T>* Currnode(void);
};
template<class T>
list<T>::list(void)
{
head=NULL;
newnode=NULL;
last=NULL;
currnode=NULL;
prenode=NULL;
}
template<class T>
node<T>* list<T>::setnode(T data2,node<T>* p)
{
newnode=new node<T>(data2);
return newnode;
}
template<class T>
void list<T>::createlist(T data2,node<T>* p=NULL)
{
newnode=new node<T>(data2);
if(head==NULL)
{
head=newnode;
}
else
{
currnode=head;
while(currnode->nextnode()!=NULL)
{
currnode=currnode->nextnode();
}
currnode->insertafter(newnode);
currnode=newnode;
}
last=newnode;
}
template<class T>
void list<T>::Insertlast(T p)
{
newnode=new node<T>(p);
if(last->nextnode()==NULL)
{
last->insertafter(newnode);
}
else
{
cout<<"there are some error in last"<<endl;
exit(0);
}
}
template<class T>
void list<T>::deletedata(T d)
{
if(head==NULL)
{
cout<<"no data in the list"<<endl;
exit(0);
}
if(head->data==d)
{
head=head->nextnode();
}
else
{
currnode=head;
while(currnode!=NULL)
{
if(currnode->data!=d)
{
prenode=currnode;
currnode=currnode->nextnode();
}
else
{
prenode->deleteafter();
currnode=prenode->nextnode();
}
}
}
}
template<class T>
void list<T>::print(void)
{
currnode=head;
if(head==NULL)
{
cout<<"no data in list"<<endl;
exit(0);
}
while(currnode!=NULL)
{
cout<<currnode->data<<" ";
currnode=currnode->nextnode();
}
cout<<endl;
}
template<class T>
void list<T>::deletelist(void)
{
node<T> *temp;
if(head==NULL)
{
cout<<"no data in list"<<endl;
exit(0);
}
while(head!=NULL)
{
temp=head;
head=head->nextnode();
delete temp;
}
}
template<class T>
void list<T>::athead(void)
{
if(head!=NULL)
{
currnode=head;
}
else
{
cout<<"no data at head"<<endl;
exit(0);
}
}
template<class T>
T list<T>::Data(void)
{
if(currnode!=NULL)
{
return currnode->data;
}
}
template<class T>
node<T>* list<T>::Next(void)
{
if(currnode!=NULL)
{
currnode=currnode->nextnode();
return currnode;
}
else
{
return NULL;
}
}
template<class T>
node<T>* list<T>::Currnode(void)
{
return currnode;
}
guanli.cpp
============================================
#include<iostream>
#include<string>
#include<fstream>
#include"list.h"
struct file // file struct;
{
string filename;
string aboutfile;
string filepath;
};
struct getfile
{
string filename;
string aboutfile;
string filepath;
};
void main(void)
{
int select;
file myfile;
string space,findvalue;
ofstream writefile;
list<getfile> mylist;
char ch,text;
int stringsize,i;
for(;;)
{
string linevalue; // jing zhi zai for(;;){} xuan huan zhong ding yi(1-3);
string openname("/home/c++/zuoping2/jiru");// 2
ifstream readfile(openname.c_str(),ios::in);//3
string::size_type pos=0,pre=0;
getfile getmyfile;
cout<<"1:add file"<<endl;
cout<<"2:find file"<<endl;
cout<<"3:watch file"<<endl;
cout<<"4:delete file"<<endl;
cout<<"5:quite"<<endl;
cin>>select;
switch(select)
{
case 1:
writefile.open("/home/c++/zuoping2/jiru",ios::out | ios::app);
if(!writefile)
{
cout<<"Don't open jiru file"<<endl;
exit(1);
}
getline(cin,space,'\n');
cout<<"please input file name"<<endl;
getline(cin,myfile.filename,'\n');
cout<<"please input file description"<<endl;
getline(cin,myfile.aboutfile,'\n');
cout<<"please input file path"<<endl;
getline(cin,myfile.filepath,'\n');
if(myfile.filepath[myfile.filepath.size()-1]=='/')
{
writefile<<myfile.filename<<";"<<myfile.aboutfile<<";"<<myfile.filepath<<";"<<'\n';
}
else
{
myfile.filepath=myfile.filepath+"/";
writefile<<myfile.filename<<";"<<myfile.aboutfile<<";"<<myfile.filepath<<";"<<'\n';
}
writefile.close();
break;
case 2:
if(!readfile)
{
cout<<"Don't open jiru file"<<endl;
exit(-1);
}
cout<<"please input file name for finding file"<<endl;
cin>>findvalue;
while(getline(readfile,linevalue,'\n'))
{
pre=0;
pos=0;
//get file name;
pos=linevalue.find_first_of(';',pos);
getmyfile.filename=linevalue.substr(pre,pos-pre); ++pos;
pre=pos;
//get file description;
pos=linevalue.find_first_of(';',pos);
getmyfile.aboutfile=linevalue.substr(pre,pos-pre);
++pos;
pre=pos;
//get file path;
pos=linevalue.find_first_of(';',pos);
getmyfile.filepath=linevalue.substr(pre,pos-pre);
++pos;
pre=pos;
//get getmylist data for create list;
mylist.createlist(getmyfile);
}
mylist.athead();
while(mylist.Currnode()!=NULL)
{
if(mylist.Data().filename==findvalue) //when find file;
{
cout<<"find file"<<endl;
cout<<"###########################################"<<endl;
cout<<"<"<<mylist.Data().aboutfile<<"<"<<endl;
cout<<"###########################################"<<endl;
cout<<"do you want to like source"<<endl;
cin>>ch;
if(ch=='y')
{
string filepath;
filepath=mylist.Data().filepath+mylist.Data().filename;
//erase char '\n';
for(i=0;i<=filepath.size();i++)
{
if(filepath[i]=='\n')
{
filepath.erase(i,1);
}
}
//open file;
ifstream openfiles;
openfiles.open(filepath.c_str(),ios::in);
if(!openfiles)
{
cerr<<"don't open file"<<endl;
exit(-1);
}
//read file context;
while(!openfiles.eof())
{
openfiles.get(text);
cout.put(text);
}
}
}
mylist.Next();
}
mylist.deletelist();
break;
case 3:
if(!readfile)
{
cerr<<"don't open the file"<<endl;
exit(-1);
}
while(getline(readfile,linevalue,'\n'))
{
//get file name;
pos=linevalue.find_first_of(';',pos);
getmyfile.filename=linevalue.substr(pre,pos-pre); ++pos;
pre=pos;
//get file description;
pos=linevalue.find_first_of(';',pos);
getmyfile.aboutfile=linevalue.substr(pre,pos-pre);
++pos;
pre=pos;
//get file path;
pos=linevalue.find_first_of(';',pos);
getmyfile.filepath=linevalue.substr(pre,pos-pre);
++pos;
pre=pos;
//get getmylist data for create list;
mylist.createlist(getmyfile);
pos=0;
pre=0;
}
mylist.athead();
while(mylist.Currnode()!=NULL)
{
string filepath,descp;
cout<<"the file description"<<endl;
cout<<mylist.Data().aboutfile<<endl;
cout<<"*****************************************************************"<<endl;
cout<<"do you want to look source"<<endl;
cin>>ch;
if(ch=='y')
{
filepath=mylist.Data().filepath+mylist.Data().filename;
pos=0;
for(i=0;i<=filepath.size();i++)
{
if(filepath[i]=='\n')
{
filepath.erase(i,1);
}
}
cout<<filepath;
cout<<endl;
ifstream openfiles;
openfiles.open(filepath.c_str(),ios::in);
if(!openfiles)
{
cerr<<"don't open file"<<endl;
exit(-1);
}
while(!openfiles.eof())
{
openfiles.get(text);
cout.put(text);
}
}
else if(ch=='q')
{
break;
}
mylist.Next();
}
mylist.deletelist();
break;
case 5:
exit(0);
}
}
}
makefile
========================================================
guanli:guanli.o list.h node.h
g++ -g -I/home/c++/zuoping2/ guanli.cpp -o guanli
all:guanli
.PHONY:clean
clean:
rm guanli.o
本文地址:http://com.8s8s.com/it/it29055.htm