?? 文章編輯10.c
字號(hào):
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include<malloc.h>
/*創(chuàng)建每行的一個(gè)存儲(chǔ)結(jié)構(gòu)*/
typedef struct line{
char *data; //字符串指針需要時(shí) 動(dòng)態(tài)分配內(nèi)存
struct line *next; //指向下一個(gè)字符串的地址
}LINE;
LINE *CreateTXT(LINE *head){
LINE *p;
char tmp[1000];
printf("\n請(qǐng)輸入文本,每行最多輸入80字符!\n");
printf("輸入 Ctrl + E (^E) 則結(jié)束輸入\n");
p=(LINE *)malloc(sizeof(LINE)); //首先為鏈表 建立一個(gè)附加表頭結(jié)點(diǎn)
head=p; //將p付給 表頭指針
while(1)
{
gets(tmp); //輸入字符串!使用C的輸入函數(shù)
if(tmp[0]==5)break; //如果發(fā)現(xiàn)輸入 ^E,則退出輸入???
p= p->next=(LINE *)malloc(sizeof(LINE));
p->data=(char *)malloc(strlen(tmp)+1); //為結(jié)點(diǎn)分配空間
strcpy(p->data,tmp);
if(tmp[strlen(tmp)-1]==5){ //除去最后一個(gè)控制符 ^E
p->data[strlen(tmp)-1]='\0';
break;
}
}
p->next=NULL; //是最后的一個(gè)指針為空。
head=head->next;
return head;
}
int Count_Space(LINE *head){ //統(tǒng)計(jì)空格數(shù)
LINE *p=head;
int asc_space=32; //空格的ASCIC 碼值
int count=0;
int Len,i;
do
{
Len=strlen(p->data); //計(jì)算當(dāng)前 data 里的數(shù)據(jù)元素的個(gè)數(shù)
for(i=0;i<Len;i++)
if(p->data[i]==asc_space)count++; //計(jì)算空格數(shù)
}
while((p=p->next)!=NULL); //遍歷 鏈表
return count;
}
int Count_Num(LINE * head){ //統(tǒng)計(jì)數(shù)字個(gè)數(shù)
LINE *p=head;
int count=0;
int Len,i;
do
{
Len=strlen(p->data); //計(jì)算當(dāng)前 data 里的數(shù)據(jù)元素的個(gè)數(shù)
for(i=0;i<Len;i++)
if(p->data[i]>=48 && p->data[i]<=57)count++; //計(jì)算數(shù)字個(gè)數(shù)
}
while((p=p->next)!=NULL); //遍歷 鏈表
return count;
}
int Count_All_Word(LINE *head){ //統(tǒng)計(jì)文章的總字?jǐn)?shù)
LINE *p=head; //保存鏈表的首地址
int count=0; //總字母數(shù)
do
{
count+=strlen(p->data);
} //計(jì)算當(dāng)前行內(nèi)的字符數(shù)!除'\0'結(jié)束符外!注意,該統(tǒng)計(jì)包含“空格的長(zhǎng)度!”
while((p=p->next)!=NULL); //遍歷 鏈表
return count;
}
int Count_ZM(LINE *head){ // 統(tǒng)計(jì)字母數(shù)
int count,space_count;
count=Count_All_Word(head); //總的字符數(shù),包含空格
space_count=Count_Space(head); //空格數(shù)
return count-space_count; //返回文章的字母總數(shù)
}
int Find_Word(LINE *head,char *sch)
{ //統(tǒng)計(jì) sch 在文章中出現(xiàn)的次數(shù)
LINE *p=head;
int count=0;
int len1=0; //保存當(dāng)前行的總字符數(shù)
int len2=strlen(sch); //待統(tǒng)計(jì)字符串的長(zhǎng)度
int i,j,k;
while(p!=NULL) //遍歷 鏈表
{
len1=strlen(p->data);//當(dāng)前行的字符數(shù)
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;}//字符串完全相同則計(jì)數(shù)
}
}
p=p->next;
}return count;
}
void del_string_word(char *s,char *sch)
{ // *s為輸入的字符串
int k,kk,len,i,j; // *sch 為將要?jiǎng)h除的字符
char *p=strstr(s,sch); //查詢(xún)結(jié)果 函數(shù)strstr()用來(lái)在一個(gè)字符串中查找另一個(gè)字符串
char tmp[80];
int count=0; //返回第二個(gè)字符串在第一個(gè)字符串中第一次出現(xiàn)的位置
len=strlen(s);
i=len-strlen(p);
j=i+strlen(sch);
for(k=0;k<i;k++) tmp[count++]=s[k];
for(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 *phead;
do
{
printf("%s",p->data);
}while((p=p->next)!=NULL); //遍歷 鏈表
}
void Stats(LINE *head){ //統(tǒng)計(jì)
printf("文章統(tǒng)計(jì)信息結(jié)果如下:\n");
printf("英文字母數(shù):%d\n",Count_ZM(head));
printf("空格數(shù): %d \n",Count_Space(head));
printf("文章中共出現(xiàn)數(shù)字:%d\n",Count_Num(head));
printf("統(tǒng)計(jì)文章的總字?jǐn)?shù): %d\n",Count_All_Word(head));
printf("\n");
}
void main(){
char ID;
char ch;
LINE *head; //文章的首結(jié)點(diǎn)
printf(" ***********************************************\n");
printf(" * 文章編輯系統(tǒng) *\n");
printf(" ***********************************************\n");
printf("\n");
printf("主菜單如下:\n");
while(1){
printf(" *************************************************************\n");
printf(" | 1.新建文本鏈表 2.瀏覽輸入文本 3.文本字?jǐn)?shù)統(tǒng)計(jì) |\n");
printf(" | 4.特定字符串統(tǒng)計(jì) 5.字符串的刪除 6.返回 |\n");
printf(" *************************************************************\n");
printf("請(qǐng)您按關(guān)鍵字代號(hào)選擇菜單操作!\n");
scanf("%c",&ID);
getchar();
while(2)
{ if(ID=='1') {
head=CreateTXT(head);
printf("你確定要退出此菜單嗎?(Y/N)\n");
scanf("%c",&ch);
getchar();
if(ch=='n') continue;
else if(ch=='y') break;
}
else if(ID=='2') {
OutPutTxt(head);
printf("你確定要退出此菜單嗎?(Y/N)\n");
scanf("%c",&ch);
getchar();
if(ch=='n') continue;
else if(ch=='y') break;
}
else if(ID=='3') {
Stats(head);
printf("你確定要退出此菜單嗎?(Y/N)\n");
scanf("%c",&ch);
getchar();
if(ch=='n') continue;
else if(ch=='y') break;
}
else if(ID=='4') {
char sch[20];int m;
printf("\n請(qǐng)輸入要統(tǒng)計(jì)的 字符串\n");
gets(sch);
m=Find_Word(head,sch);
printf("出現(xiàn)的次數(shù)為:%d\n",m);
printf("你確定要退出此菜單嗎?(Y/N)\n");
scanf("%c",&ch);
getchar();
if(ch=='n') continue;
else if(ch=='y') break;
}
else if(ID=='5') {
char tmp_sch[20];
printf("\n請(qǐng)輸入要?jiǎng)h除的某一字符串:\n");
gets(tmp_sch);
Del_String(head,tmp_sch);
OutPutTxt(head);
printf("你確定要退出此菜單嗎?(Y/N)\n");
scanf("%c",&ch);
getchar();
if(ch=='n') continue;
else if(ch=='y') break;
}
else if(ID=='6') {
printf("你確定要退出系統(tǒng)嗎?(Y/N)\n");
scanf("%c",&ch); getchar();
if(ch=='n') break;
else if(ch=='y') exit(0);
}
else {
printf("您輸入字母有錯(cuò),請(qǐng)重新輸入!\n\n");
break;
}
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -