1、堆栈应用题程序与算法综合设计课程设计报告设计题目:堆栈应用题学生姓名: 专业: 班级: 指导老师: 完成日期:2013.7.6合肥工业大学(一) 需求和规格说明堆栈应用题要求:一、 设计一个堆栈类,实现对于软件操作中常用的撤销/重做(Undo/Redo)的支持。二、 使用控制台或者图形界面,测试这个堆栈类的使用。三、 创建一个文件并进行编辑注:一、二为题目要求,三是自己附加的功能(二) 设计 系统类图f_stackint count; /统计数目node * top; /头指针 filestack(); filestack(); bool empty()const; error_code ge
2、t_top(char fileroad,char filename)const ; /取出栈顶元素 error_code push(char fileroad,char filename); /压栈 error_code pop(); /弹栈 error_code pop(char fileroad,char filename); /将指定元素出栈 bool search(char fileroad,char filename); /寻找指定元素类外的函数函数名称返回类型所含参数实现功能in_word()intchar fileroadchar filename对文件进行输入编辑copy_wo
3、rd()intchar fileorignlchar filenamechar fileroad 对文件中的内容进行拷贝creatfile()voidchar fileroadchar filenamefilestack &fstack1filestack &fstack3filestack &fstack4创建新文件并备份deletefile()voidfilestack & fstackchar fileroadchar filename删除文件操作recoverfile()voidfilestack & fstack,char fileroadchar filename恢复删除的文件re
4、coverfileroad()voidfilestack & fstackchar ofileroadchar fileroadchar filename复制文件路径undo_moreoperate()voidfilestack & fstack1filestack & fstack2filestack & fstack3filestack & fstack4char & opchar froado撤销之前的某步操作undo_operate()voidfilestack & fstack1filestack & fstack2filestack & fstack3filestack & fst
5、ack4char & opchar froado撤消操作searchroad()voidfilestack & fstack4char ofileroadchar filename寻找文件路径redo_file()voidfilestack & fstack1filestack & fstack3filestack & fstack4重做操作delete_copyoperate()voidfilestack & fstack删除副本(及删除“copy”文件夹里的文件)save()voidfilestack & fstack对所有操作进行保存,不可恢复f_operate()void无对文件进行操
6、作(三) 用户手册程序运行时,首先提示选择要进行的操作。对于创建文件时,提示输入文件路径以及文件名对于撤消操作时,提示进行撤销上一步还是撤销之前的操作;对于重做操作,提示进行恢复的路径;对于结束所有操作,提示是否保存所有操作。(四) 调试及测试运行实例:(1)创建文件的操作(2)撤销操作(3)重做操作(4)保存所有操作进一步改进(1)目前程序中,主要以建立文件,删除文件以及恢复文件为主要功能,辅以对文件进行编辑。以后可以加入打开文件等其他功能。(2)目前是通过控制台进行操作,并不是很直观,在以后可以借助图形界面(MFC)来实现对文件的操作。附录 源程序(1)f_stack.h#include
7、#include #include using namespace std;enum error_codesuccess,underflow;struct node char froad50; /存储文件路径 char fname30; /存储文件名 node * next;class filestackpublic: filestack(); filestack(); bool empty()const; error_code get_top(char fileroad,char filename)const ; /取出栈顶元素 error_code push(char fileroad,c
8、har filename); /压栈 error_code pop(); /弹栈 error_code pop(char fileroad,char filename); /将指定元素出栈 bool search(char fileroad,char filename) ; /寻找指定元素private: int count; node * top;filestack:filestack() count=0; top=NULL;bool filestack:empty()const return count=0;error_code filestack:get_top(char fileroa
9、d,char filename)const if(empty() return underflow; else strcpy(fileroad,top-froad); strcpy(filename,top-fname); return success; error_code filestack:push(char fileroad,char filename) node *s=new node; strcpy(s-froad,fileroad); strcpy(s-fname,filename); s-next=NULL; s-next=top; top=s; count+; return
10、success;error_code filestack:pop() if(empty() return underflow; else node *u=top; top=top-next; delete u; count-; return success; error_code filestack:pop(char fileroad,char filename) char croad80; strcpy(croad,fileroad); strcat(croad,filename); filestack f1; if(empty() return underflow; else char f
11、r50,frc50,fn30; get_top(fr,fn); strcpy(frc,fr); if(strcmp(strcat(frc,fn),croad)=0) pop(); else while(strcmp(strcat(frc,fn),croad)!=0) f1.push(fr,fn); pop(); get_top(fr,fn); strcpy(frc,fr); pop(); while(!f1.empty() f1.get_top(fr,fn); push(fr,fn); f1.pop(); return success;/寻找指定文件bool filestack:search(
12、char fileroad,char filename) char croad80; strcpy(croad,fileroad); strcat(croad,filename); filestack f1; if(empty() return false; else char fr50,frc50,fn30; get_top(fr,fn); strcpy(frc,fr); if(strcmp(strcat(frc,fn),croad)=0) return true; else while(strcmp(strcat(frc,fn),croad)!=0) f1.push(fr,fn); pop
13、(); get_top(fr,fn); strcpy(frc,fr); while(!f1.empty() f1.get_top(fr,fn); push(fr,fn); f1.pop(); return true; return false;/析构函数/对文件进行文字编辑int in_word(char fileroad,char filename) char c=A; char frc50; strcpy(frc,fileroad); /拷贝文件路径 char fname80; strcpy(fname,strcat(frc,filename); ofstream outfile(fnam
14、e,ios:out|ios:trunc); /打开相应文件 if(!outfile) cerropen error!endl; exit(1); coutenter the words:c; outfilec; outfile.close(); return 0;/对文件中的内容进行拷贝int copy_word(char fileorignl,char filename,char fileroad )/创建新文件并备份void creatfile(char fileroad,char filename,filestack & fstack1,filestack & fstack3,files
15、tack & fstack4) /指定备份路径 char fr80=copy/; char frc80=copy/; char croad80; char frs80=redosave/; char frsc180=redosave/; char frsc280=redosave/; char frsr80=redosaveroad/; char frscr180=redosaveroad/; char frscr280=redosaveroad/; strcpy(croad,fileroad); /创建文件 ofstream outfile1(strcat(croad,filename),i
16、os:out|ios:trunc); if(!outfile1) cerropen error!endl; exit(1); outfile1.close(); /备份到指定路径 ofstream outfile2(strcat(fr,filename),ios:out|ios:trunc); if(!outfile2) cerrcopy error!endl; exit(1); outfile2.close(); /对所有创建操作建立映像 ofstream outfile3(strcat(frs,filename),ios:out|ios:trunc); if(!outfile3) cerr
17、savecopy error!endl; exit(1); outfile3.close(); /对创建文件的路径进行保存 ofstream outfile4(strcat(frsr,filename),ios:out|ios:trunc); if(!outfile4) cerrsavecopy error!endl; exit(1); char c50; int i=0; while(fileroadi!=0) ci=fileroadi; i+; ci=0; i=0; while (ci!=0) outfile4ci; i+; outfile4.close(); coutendl; cout
18、成功建立(更新)fileroadfilename文件并备份成功n; coutendl; /对已创建的文件进行编辑 char choice; fstack1.push(fileroad,filename); fstack3.push(frsc2,filename); fstack4.push(frscr2,filename); cout是否需要对新创建的文件进行编辑?n需要,键入Y;不需要,键入N。n; coutchoice; switch(choice) case N: cout请选择下一步操作。endl; break; case Y: in_word(fileroad,filename);
19、copy_word(croad,filename,frc); copy_word(croad,filename,frsc1); cout结束编辑,请选择下一步操作。endl; break; default: cout输入错误!请确认!endl; break; while(choice!=Y&choice!=N);/删除文件操作/恢复删除的文件void recoverfile(filestack & fstack,char fileroad,char filename) fstack.push(fileroad,filename); char or80=copy/; strcat(or,file
20、name); char croad80; strcpy(croad,fileroad); ofstream outfile1(strcat(croad,filename),ios:out|ios:trunc); if(!outfile1) cerrredo error!endl; exit(1); copy_word(or,filename,fileroad); outfile1.close();/复制文件路径void recoverfileroad(filestack & fstack,char ofileroad,char fileroad,char filename) fstack.pu
21、sh(fileroad,filename); char frsr80; strcpy(frsr,fileroad); ofstream outfile(strcat(frsr,filename),ios:out|ios:trunc); if(!outfile) cerrsavecopy error!endl; exit(1); char c50; int i=0; while(ofileroadi!=0) ci=ofileroadi; i+; ci=0; i=0; while (ci!=0) outfileci; i+; outfile.close();/撤销之前的某步操作void undo_
22、moreoperate(filestack & fstack1,filestack & fstack2,filestack & fstack3,filestack & fstack4,char & op,char froado) char choice=C; char fileroad50; char filename30; char ofile50=copy/; char sfile50=redosave/; char srfile50=redosaveroad/; coutchoice; switch(choice) case U: coutfileroad; strcpy(froado,
23、fileroad); coutfilename; deletefile(fstack1,fileroad,filename); deletefile(fstack3,sfile,filename); deletefile(fstack4,srfile,filename); fstack2.push(ofile,filename); op=U; break; case C: coutfileroad; strcpy(froado,fileroad); coutfilename; recoverfile(fstack1,fileroad,filename); cout已成功恢复文件fileroad
24、filenameendl; recoverfile(fstack3,sfile,filename); recoverfileroad(fstack4,fileroad,srfile,filename); fstack2.pop(ofile,filename); op=C; break; default: cout请确认输入编号是否正确; break; while(choice!=C&choice!=U);/撤消操作void undo_operate(filestack & fstack1,filestack & fstack2,filestack & fstack3,filestack & f
25、stack4,char & op,char froado) char fileroad50; char filename30; char choice=O; char ofile50=copy/; char sfile50=redosave/; char srfile50=redosaveroad/; cout请确认撤消操作!撤销上一步键入O,其它请键入Mn; coutchoice; switch(choice) case O: if(op=C) fstack1.get_top(fileroad,filename); strcpy(froado,fileroad); deletefile(fs
26、tack1,fileroad,filename); deletefile(fstack3,sfile,filename); deletefile(fstack4,srfile,filename); fstack2.push(ofile,filename); cout成功删除文件fileroadfilename!n ; op=U; else fstack2.get_top(fileroad,filename); recoverfile(fstack1,froado,filename); recoverfile(fstack3,sfile,filename); recoverfileroad(fstack4,froado,srfile,filename); fstack2.pop();
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1