当前位置:文档之家› 数据结构课程设计 文章编辑 源代码

数据结构课程设计 文章编辑 源代码

#include <stdio.h>#include<stdlib.h>#include <string.h>#include<malloc.h>#define MAXSIZE 1000typedef char DataType;typedef struct node{DataType ch[MAXSIZE];struct node *next;}Lstring;/*****输入文章*****/Lstring *input(){Lstring *p,*head;int i=0;printf ("请输入一页文章,若要换行,请直接敲回车键,若想结束请按@:\n");p=(Lstring *)malloc(sizeof(Lstring));head=p;p->ch[i]=NULL;p->next=NULL;char str[200];while(1){gets(str);if(strlen(str)>100){printf("每行最多输入100字符");break;}if(str[0]==64){str[0]='\0';p->ch[0]=str[0];break;}p->next=(Lstring *)malloc(sizeof(Lstring));strcpy(p->ch,str);if(str[strlen(str)-1]==64){p->ch[strlen(str)-1]='\0';break;}p=p->next;}p->next=NULL;return head;}/****输出文章*****/Lstring *OutPut(Lstring *head){Lstring *p=head;do{printf("%s\n",p->ch);}while((p=p->next)!=NULL);return head;}/****统计字母的个数*****/int Alphabet(Lstring *head){Lstring *p=head;int count=0;do{int Len;Len=strlen(p->ch);for(int i=0;i<Len;i++)if((p->ch[i]>='a'&&p->ch[i]<='z')||(p->ch[i]>='A'&&p->ch[i]<='Z')) count++;}while((p=p->next)!=NULL);return count;}/****统计数字的字数*****/int Num(Lstring *head){Lstring *p=head;int count=0;do{int Len;Len=strlen(p->ch);for(int i=0;i<Len;i++)if(p->ch[i]>='0' && p->ch[i]<='9')count++;}while((p=p->next)!=NULL);return count;}/****统计空格的字数*****/int Space(Lstring *head){Lstring *p=head;int count=0;do{int Len;Len=strlen(p->ch);for(int i=0;i<Len;i++)if(p->ch[i]==32)count++;}while((p=p->next)!=NULL);return count;}/*统计文章的总字数*/int All(Lstring *head){Lstring *p=head;int count=0;do{count+=strlen(p->ch);}while((p=p->next)!=NULL);return count;}/****串的简单模式匹配*****/int FindString(Lstring *head,char *str){Lstring *p=head;int count=0;int h=0;int len1=0;int len2=strlen(str);int i,j,k;do{len1=strlen(p->ch);for(i=0;i<len1;i++){if(p->ch[i]==str[0]){k=0;for(j=0;j<len2;j++)if(p->ch[i+j]==str[j]) k++;if(k==len2){count++;i=i+k-1;}}}}while((p=p->next)!=NULL);return count;}void delstringword(char *s,char *str){char *p;int count,len,i,j;char s1[80];p=strstr(s,str);len=strlen(s);i=len-strlen(p);j=i+strlen(str);count=0;for(int m=0;m<i;m++)s1[count++]=s[m];for(int n=j;n<len;n++)s1[count++]=s[n];s1[count]='\0';strcpy(s,s1);}Lstring *DelString(Lstring *head,char *str){Lstring *p=head;do{while(strstr(p->ch,str)!=NULL)delstringword(p->ch,str);}while((p=p->next)!=NULL);return head;}void main(){int i=0;int m;Lstring *head;char s1[20],s2[20];head=input();printf("输入的文章为:\n");head=OutPut(head);printf("\n");printf("全部字母数:%d \n",Alphabet(head));printf("数字个数:%d \n",Num(head));printf("空格个数: %d \n",Space(head));printf("文章总字数: %d \n",All(head));printf("\n");printf("**********************\n");printf("* 菜单 *\n");printf("**********************\n");printf("* 1---统计字符串 *\n");printf("* 2---删除字符串 *\n");printf("* 0---退出 *\n");printf("**********************\n");do{printf("请输入你要选择的操作(0~2):");scanf("%d",&m);switch(m){case 0:exit(0);break;case 1:printf("请输入要统计的字符串:");scanf("%s",&s1);printf("%s在文章中出现的次数为:%d \n",s1,FindString(head,s1));printf("\n");break;case 2:printf("请输入要删除的某一字符串:");scanf("%s",&s2);head=DelString(head,s2);printf("删除%s后的文章为:\n",s2);OutPut(head);break;}}while(m!=0);}。

相关主题