数据结构(c语言版)
课程设计报告
文章编辑程序的设计
学 院: 信息科学技术学院 班 级: 信息工程09级 1 班 学 号: ************ * 名: ** 指导教师:
***
完成日期: 2010年12月
目录
1.需求分析——文章编辑.......................................... - 3 - 2.程序设计...................................................... - 3 -
结构体的设计: .............................................. - 3 - 函数的设计: ................................................ - 3 - 主函数的设计: .............................................. - 8 - 3.测试分析...................................................... - 9 - 4.用户说明..................................................... - 10 - 5.经验和体会................................................... - 10 - 6.附录——程序完整原代码....................................... - 11 -
- 2 -
1.需求分析——文章编辑
题目:22题,文章编辑。
功能:从键盘输入一页文字,静态存储在一个文件中 要求:(1)分别统计出其中英文字母数和空格数及整篇文章总字数;
(2)统计某一字符串在文章中出现的次数,并输出该次数; (3)删除某一子串,并将后面的字符前移。
存储结构使用线性表,分别用几个子函数实现相应的功能;
输入数据的形式和范围:可以输入大写、小写的英文字母、任何数字及标点符号。
输出形式:(1)分行输出用户输入的各行字符;
(2)分4行输出\"全部字母数\"、\"数字个数\"、\"空格个数\"、\"文章总字数\" (3)输出删除某一字符串后的文章;
2.程序设计
结构体的设计:
typedef struct essay {
char *line;//动态分配的字符数组 struct essay *next;//结构体指针 }ESS;
//创建结构体,行与行之间用链表的形式
函数的设计:
#include void create(ESS * &h)//创造链表 { - 3 - char m[1000]; ESS *p; p=(ESS *)malloc(sizeof(ESS)); h=p; int i,k; FILE *fp; fp=fopen(\"d:\\\ext.txt\所输入的文本将保存在d盘的rr文本文档中 printf (\"输入文章, 用Ctrl+B结束输入:\\n\");//按Ctrl+B(^B)将结束输入 while(1) { gets(m); //输入字符串 if(strlen(m)>1000) { printf(\"超过1000字符\\n\"); break; } if(m[0]==2)break; //如果发现输入 ^B,则退出输入 p=p->next=(ESS *)malloc(sizeof(ESS)); p->line=(char *)malloc(sizeof(char)*strlen(m+1)); //为结点分配空间 strcpy(p->line,m);//将字符串m,复制到p所指向的字符串中 for(i=0;i<(k=strlen(m))&&(p->line[i]!=2);i++) fwrite(&p->line[i],sizeof(char),1,fp);//写入文本文档中 fputc('\\n',fp); if(m[strlen(m)-1]==2) //除去最后一个控制符 ^B { p->line[strlen(m)-1]='\\0'; break; } } p->next=NULL; /*最后的一个指针为空 */ h=h->next; //将下一个节点赋值到头结点中 } int Count(ESS * h) //统计数字 { ESS *p=h; int co=0; int i; int Lenght; do { Lenght=strlen(p->line); /*计算当前 line 里的数据元素的个数*/ for(i=0;i - 4 - 数字 co++; } while((p=p->next)!=NULL); //遍历 链表不为空 return co; } int Letter(ESS * h)//统计字母 { ESS *p=h; int co=0; int Lenght; do { Lenght=strlen(p->line); /*计算当前 line 里的数据元素的个数*/ for(int i=0;i while((p=p->next)!=NULL); //遍历 链表不为空 return co; /*返回文章的字母总数*/ } int chinese(ESS * h)//统计符号 { ESS *p=h; int co=0; int Lenght; do { Lenght=strlen(p->line); /*计算当前 line 里的数据元素的个数*/ for(int i=0;i while((p=p->next)!=NULL); //遍历 链表不为空 return co; /*返回*/ } int Space(ESS * h)//统计空格 { /*计算- 5 - ESS *p=h; int co=0; int Lenght; do { Lenght=strlen(p->line); //计算当前 line 里的数据元素的个数 for(int i=0;i int CountAll(ESS * h)//统计文章字数 { ESS *p=h; //保存链表的首地址 int co=0; do //计算总字符数 { co+=strlen(p->line); } while((p=p->next)!=NULL); //遍历 链表 return co; } int Find(ESS * h,char *s)//统计s出现次数 { ESS *p=h; int co=0; int len1=0; /*保存当前行的总字符数*/ int len2=strlen(s); /*待统计字符串的长度*/ int i,j,q; do { len1=strlen(p->line); /*当前行的字符数*/ for(i=0;i for(j=0;j - 6 - } } } while((p=p->next)!=NULL); /*遍历 链表*/ return co; } void delstring(char *s,char *str) /* *s为输入的字符串,*str为将要删除的字符*/ { char *p=strstr(s,str); /*从字符串s中寻找str第一次出现的位置*/ char m[80]; int len=strlen(s); int i=len-strlen(p);//比较两串字符串之间的字符个数 int j=i+strlen(str); int co=0; for(int k=0;kstrcpy(s,m); /*返回新的字符串*/ } void DelString(ESS * h,char *str) { ESS *p=h; do { if(strstr(p->line,str)!=NULL) delstring(p->line,str);//调用删除每个字符串,只删一次。 } while((p=p->next)!=NULL); } void Out(ESS * &h)// { ESS *p=h; do { printf(\"%s\\n\显示字符串 } while((p=p->next)!=NULL); /*遍历 链表*/ } - 7 - void display() { printf(\"\\n\"); printf(\"\\n\"); printf(\"***********************************************\\n\"); printf(\"******************** 统计数字******************\\n\"); printf(\"******************** 统计字母******************\\n\"); printf(\"******************** 统计空格******************\\n\"); printf(\"******************** 统计文章总字数************\\n\"); printf(\"******************** 统计重复字符串************\\n\"); printf(\"******************** 统计重复出现字符串********\\n\"); printf(\"******************** 删除功能******************\\n\"); printf(\"***********************************************\\n\"); printf(\"\\n\"); }//构造一个良好的界面良好的界面 主函数的设计: int main() { ESS *h; create(h); display(); Out(h); printf(\"\\n\"); printf(\"数字个数:%d \\n\ printf(\"全部字母数:%d \\n\ printf(\"中文字数:%d \\n\ printf(\"空格个数: %d \\n\ printf(\"文章总字数(含空格): %d \\n\ printf(\"文章总字数(不含空格): %d \\n\ char s1[20],s2[20]; printf(\"\\n\"); printf(\"请输入要统计的字符串:\"); scanf(\"%s\ printf(\"%s出现的次数为:%d \\n\ printf(\"\\n\"); printf(\"请输入要删除的某一字符串:\"); scanf(\"%s\ DelString(h,s2); printf(\"删除%s后的文章为:\\n\ Out(h); //调用删除函数后的显示 Return 0; } - 8 - 3.测试分析 输入: 输出及其他操作: - 9 - 4.用户说明 根据界面提示即可完成所有操作。 5.经验和体会 在这次课程设计中,我有将书本重新过了一边,不但巩固了以前就会的知识,还弥补了从前的空缺,学到了许多从前没有注意的知识。 - 10 - 在实践的过程中,遇到了许多问题,我查阅了许多资料,请教了一些朋友对于编程中应注意的问题又有了新的认识,我知道了许多书上没有的经验和教训。 通过进行这次课程设计,我对数据结构又有了更加深刻的认识,使我以后学习更加轻松,对于编程更又信心!! 6.附录——程序完整原代码 #include //创建结构体,行与行之间用链表的形式 void create(ESS * &h)//创造链表 { char m[1000]; ESS *p; p=(ESS *)malloc(sizeof(ESS)); h=p; int i,k; FILE *fp; fp=fopen(\"d:\\\ext.txt\所输入的文本将保存在d盘的rr文本文档中 printf (\"输入文章, 用Ctrl+B结束输入:\\n\");//按Ctrl+B(^B)将结束输入 while(1) { gets(m); //输入字符串 if(strlen(m)>1000) { printf(\"超过1000字符\\n\"); break; } if(m[0]==2)break; //如果发现输入 ^B,则退出输入 p=p->next=(ESS *)malloc(sizeof(ESS)); p->line=(char *)malloc(sizeof(char)*strlen(m+1)); //为结点分配空间 strcpy(p->line,m);//将字符串m,复制到p所指向的字符串中 for(i=0;i<(k=strlen(m))&&(p->line[i]!=2);i++) fwrite(&p->line[i],sizeof(char),1,fp);//写入文本文档中 - 11 - fputc('\\n',fp); if(m[strlen(m)-1]==2) //除去最后一个控制符 ^B { p->line[strlen(m)-1]='\\0'; break; } } p->next=NULL; /*最后的一个指针为空 */ h=h->next; //将下一个节点赋值到头结点中 } int Count(ESS * h) //统计数字 { ESS *p=h; int co=0; int i; int Lenght; do { Lenght=strlen(p->line); /*计算当前 line 里的数据元素的个数*/ for(i=0;i while((p=p->next)!=NULL); //遍历 链表不为空 return co; } int Letter(ESS * h)//统计字母 { ESS *p=h; int co=0; int Lenght; do { Lenght=strlen(p->line); /*计算当前 line 里的数据元素的个数*/ for(int i=0;i while((p=p->next)!=NULL); //遍历 链表不为空 - 12 - return co; /*返回文章的字母总数*/ } int chinese(ESS * h)//统计符号 { ESS *p=h; int co=0; int Lenght; do { Lenght=strlen(p->line); /*计算当前 line 里的数据元素的个数*/ for(int i=0;i while((p=p->next)!=NULL); //遍历 链表不为空 return co; /*返回*/ } int Space(ESS * h)//统计空格 { ESS *p=h; int co=0; int Lenght; do { Lenght=strlen(p->line); //计算当前 line 里的数据元素的个数 for(int i=0;i int CountAll(ESS * h)//统计文章字数 { ESS *p=h; //保存链表的首地址 int co=0; do //计算总字符数 { co+=strlen(p->line); } while((p=p->next)!=NULL); //遍历 链表 /*计算- 13 - return co; } int Find(ESS * h,char *s)//统计s出现次数 { ESS *p=h; int co=0; int len1=0; /*保存当前行的总字符数*/ int len2=strlen(s); /*待统计字符串的长度*/ int i,j,q; do { len1=strlen(p->line); /*当前行的字符数*/ for(i=0;i for(j=0;j while((p=p->next)!=NULL); /*遍历 链表*/ return co; } void delstring(char *s,char *str) /* *s为输入的字符串,*str为将要删除的字符*/ { char *p=strstr(s,str); /*从字符串s中寻找str第一次出现的位置*/ char m[80]; int len=strlen(s); int i=len-strlen(p);//比较两串字符串之间的字符个数 int j=i+strlen(str); int co=0; for(int k=0;kstrcpy(s,m); /*返回新的字符串*/ } - 14 - void DelString(ESS * h,char *str) { ESS *p=h; do { if(strstr(p->line,str)!=NULL) delstring(p->line,str);//调用删除每个字符串,只删一次。 } while((p=p->next)!=NULL); } void Out(ESS * &h)// { ESS *p=h; do { printf(\"%s\\n\显示字符串 } while((p=p->next)!=NULL); /*遍历 链表*/ } void display() { printf(\"\\n\"); printf(\"\\n\"); printf(\"***********************************************\\n\"); printf(\"******************** 统计数字******************\\n\"); printf(\"******************** 统计字母******************\\n\"); printf(\"******************** 统计空格******************\\n\"); printf(\"******************** 统计文章总字数************\\n\"); printf(\"******************** 统计重复字符串************\\n\"); printf(\"******************** 统计重复出现字符串********\\n\"); printf(\"******************** 删除功能******************\\n\"); printf(\"***********************************************\\n\"); printf(\"\\n\"); }//构造一个良好的界面良好的界面 void main() { ESS *h; create(h); display(); - 15 - Out(h); printf(\"\\n\"); printf(\"数字个数:%d \\n\ printf(\"全部字母数:%d \\n\ printf(\"中文字数:%d \\n\ printf(\"空格个数: %d \\n\ printf(\"文章总字数(含空格): %d \\n\ printf(\"文章总字数(不含空格): %d \\n\ char s1[20],s2[20]; printf(\"\\n\"); printf(\"请输入要统计的字符串:\"); scanf(\"%s\ printf(\"%s出现的次数为:%d \\n\ printf(\"\\n\"); printf(\"请输入要删除的某一字符串:\"); scanf(\"%s\ DelString(h,s2); printf(\"删除%s后的文章为:\\n\ Out(h); //调用删除函数后的显示 } - 16 - 因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- efsc.cn 版权所有 赣ICP备2024042792号-1
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务