#include <iostream.h>
#include <string.h>
#include <stdio.h>
#include <iomanip>
#include <stdlib.h>
/*创建每行的一个存储结构*/
typedef struct line{
char *data; //字符串指针需要时 动态分配内存
struct line *next; //指向下一个字符串的地址
}LINE;
void CreateTXT(LINE * &head){
printf("\n请输入文本,每行最多输入80字符!\n");
printf("输入 Ctrl + E (^E) 则结束输入\n");
LINE *p=new LINE; //首先为链表 建立一个附加表头结点
head=p; //将p付给 表头指针
char tmp[1000];
while(1)
{
gets(tmp); //输入字符串!使用C的输入函数
if(tmp[0]==5)break; //如果发现输入 ^E,则退出输入???
p=p->next=new LINE;
p->data=new char[strlen(tmp)+1];//为结点分配空间
strcpy(p->data,tmp);
if(tmp[strlen(tmp)-1]==5){ //除去最后一个控制符 ^E
p->data[strlen(tmp)-1]='\0';
break;
}
}
p->next=NULL; //是最后的一个指针为空。
head=head->next;
}
int Count_Space(LINE * &head){ //统计空格数
LINE *p=head;
int asc_space=32; //空格的ASCIC 码值
int count=0;
do
{
int Len=strlen(p->data); //计算当前 data 里的数据元素的个数
for(int i=0;i<Len;i++)
if(p->data[i]==asc_space)count++; //计算空格数
}
while((p=p->next)!=NULL); //遍历 链表
return count;
}
int Count_Num(LINE * &head){ //统计数字个数
LINE *p=head;
int count=0;
do
{
int Len=strlen(p->data); //计算当前 data 里的数据元素的个数
for(int i=0;i<Len;i++)
if(p->data[i]>=48 && p->data[i]<=57)count++; //计算数字个数
}
while((p=p->next)!=NULL); //遍历 链表
return count;
}
int Count_All_Word(LINE * &head){ //统计文章的总字数
LINE *p=head; //保存链表的首地址
int count=0; //总字母数
do
{count+=strlen(p->data);} //计算当前行内的字符数!除'\0'结束符外!注意,该统计包含“空格的长度!”
while((p=p->next)!=NULL); //遍历 链表
return count;
}
int Count_ZM(LINE * &head){ // 统计字母数
int count=Count_All_Word(head); //总的字符数,包含空格
int space_count=Count_Space(head); //空格数
return count-space_count; //返回文章的字母总数
}
int Find_Word(LINE * &head,char *sch){ //统计 sch 在文章中出现的次数
LINE *p=head;
int count=0;
int h=0;
int len1=0; //保存当前行的总字符数
int len2=strlen(sch); //待统计字符串的长度
int i,j,k;
while((p=p->next)!=NULL) //遍历 链表
{
len1=strlen(p->data);//当前行的字符数
for(i=0;i<len1;i++)
{if(p->data[i]==sch[0])
{
k=0;
for(j=0;j<=len2-1;j++)
if(p->data[i+j]==sch[j])k=k+1; //如果字符串首字符相同,依次往后比较
if(k==len2) {count++;i=i+k-1;}//字符串完全相同则计数
}
}break;
}
return count ;
}
void del_string_word(char *s,char *sch)
{
// *s为输入的字符串
// *sch 为将要删除的字符
char *p=strstr(s,sch); //查询结果 函数strstr()用来在一个字符串中查找另一个字符串
char tmp[80]; //返回第二个字符串在第一个字符串中第一次出现的位置
int len=strlen(s);
int i=len-strlen(p);
int j=i+strlen(sch);
int count=0;
for(int k=0;k<i;k++) tmp[count++]=s[k];
for(int kk=j;kk<len;kk++) tmp[count++]=s[kk];
tmp[count]='\0';
strcpy(s,tmp); //返回新的字符串
}
void Del_String(LINE * &head,char *sch){ //删除指定的字符串
LINE *p=head;
do
{
if(strstr(p->data,sch)!=NULL)del_string_word(p->data,sch);
} while((p=p->next)!=NULL); //遍历 链表
}
void OutPutTxt(LINE * &head){ //向屏幕输出 文章
LINE *p=head;
do
{
cout<<p->data<<endl;
} while((p=p->next)!=NULL); //遍历 链表
}
void Tj(LINE * &head){ //统计
printf("文章统计信息结果如下:\n");
printf("全部字母数:%d\n",Count_ZM(head));
printf("空格数: %d \n",Count_Space(head));
printf("文章中共出现数字:%d\n",Count_Num(head));
printf("统计文章的总字数: %d\n",Count_All_Word(head));
printf("\n");
}
void main(){
LINE *head; //文章的首结点
printf(" \n");
printf(" 文章编辑系统 \n");
printf(" \n");
printf("\n");
printf("主菜单如下:\n");
while(1){
printf(" \n");
printf(" 1.新建文本链表 2.浏览输入文本 3.文本字数统计 \n");
printf(" 4.特定字符串统计 5.字符串的删除 6.返回 \n");
printf(" \n");
printf("请您按关键字代号选择菜单操作!\n");
char ID;
char ch;
cin>>ID;
while(2)
{ if(ID=='1') {CreateTXT(head); printf("你确定要退出此菜单吗?(Y/N)\n");
scanf("%s",&ch);
if(ch=='n') continue;
else if(ch=='y') break;}
else if(ID=='2') {OutPutTxt(head); printf("你确定要退出此菜单吗?(Y/N)\n");
scanf("%s",&ch);
if(ch=='n') continue;
else if(ch=='y') break;}
else if(ID=='3') { Tj(head); printf("你确定要退出此菜单吗?(Y/N)\n");
scanf("%s",&ch);
if(ch=='n') continue;
else if(ch=='y') break;}
else if(ID=='4') {char sch[20];int m;
printf("\n请输入要统计的 字符串\n");
gets(sch);
m=Find_Word(head,sch);
printf("出现的次数为:%d\n",m);
printf("你确定要退出此菜单吗?(Y/N)\n");
scanf("%c",&ch);
if(ch=='n') continue;
else if(ch=='y') break;}
else if(ID=='5') {char tmp_sch[20];printf("\n请输入要删除的某一字符串:\n");
scanf("%s",&tmp_sch); Del_String(head,tmp_sch);OutPutTxt(head); printf("你确定要退出此菜单吗?(Y/N)\n");
scanf("%s",&ch);
if(ch=='n') continue;
else if(ch=='y') break;}
else if(ID=='6') { printf("你确定要退出系统吗?(Y/N)\n");
scanf("%s",&ch);
if(ch=='n') break;
else if(ch=='y') exit(0);}
else {printf("您输入字母有错,请重新输入!\n");break;}
}
}
}
kechengsheji.rar_文章编辑_文章编辑系统
版权申诉
5星 · 超过95%的资源 175 浏览量
2022-09-19
15:58:55
上传
评论 1
收藏 2KB RAR 举报
御道御小黑
- 粉丝: 62
- 资源: 1万+