#include<iostream.h>
#include <string.h>
#include <stdio.h> /*文本每行以字串形式存盤,行與行之間以鏈表存盤*/
typedef struct line
{
char *data;
struct line *next;
}LINE; /*創建一鏈表,同時向里面輸入文本資料*/
void Create(LINE * head)
{
printf ("請輸入一頁文章,以Ctrl+E(^E)為結尾(每行最多輸入80字符!):\n");
LINE *p=new LINE; /*首先為鏈表 建立一個附加表頭結點*/
head=p; /*將p付給 表頭指標*/
char tmp[100];
while(1)
{
gets(tmp); /*輸入字串!*/
if(strlen(tmp)>80)
{
printf("每行最多輸入80字符");
break;
}
if(tmp[0]==5)break; /*如果發現輸入 ^E,則退出輸入*/
p=p->next=new LINE;
p->data=https://bbs.csdn.net/topics/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 CountLetter(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]>='a'&&p->data[i]<='z')||(p->data[i]>='A'&&p->data[i]<='Z')) /*計算字母數*/
count++;
}
while((p=p->next)!=NULL); /*遍歷 鏈表*/
return count; /*回傳文章的字母總數*/
}
/*統計數字數*/
int CountNumber(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++;
/*計算數字數,ASCII碼*/
}
while((p=p->next)!=NULL); /*遍歷 鏈表*/
return count;
}
/*統計空格數*/
int CountSpace(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]==32)count++; /*計算空格數,空格ASCII碼為32*/
}
while((p=p->next)!=NULL); /*遍歷 鏈表*/
return count;
}
/*統計文章的總字數*/
int CountAll(LINE * &head)
{
LINE *p=head; /*保存鏈表的首地址*/
int count=0;
do /*計算總字符數*/
{
count+=strlen(p->data);
}
while((p=p->next)!=NULL); /*遍歷 鏈表*/
return count;
}
/*統計str在文章中出現的次數*/
int FindString(LINE * &head,char *str)
{
LINE *p=head;
int count=0;
int h=0;
int len1=0; /*保存當前行的總字符數*/
int len2=strlen(str); /*待統計字串的長度*/
int i,j,k;
do
{
len1=strlen(p->data); /*當前行的字符數*/
for(i=0;i<len1;i++) /*字符匹配*/
{
if(p->data[i]==str[0])
{
k=0;
for(j=0;j<len2;j++)
if(p->data[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)
/* *s為輸入的字串,*str為將要洗掉的字符*/
{
char *p=strstr(s,str); /*從字串s中尋找str第一次出現的位置*/
char tmp[80];
int len=strlen(s);
int i=len-strlen(p);
int j=i+strlen(str);
int count=0;
for(int m=0;m<i;m++)tmp[count++]=s[m];
for(int n=j;n<len;n++)tmp[count++]=s[n];
tmp[count]='\0';
strcpy(s,tmp); /*回傳新的字串*/
}
void DelString(LINE * &head,char *str)
{
LINE *p=head;
do
{
if(strstr(p->data,str)!=NULL)delstringword(p->data,str);
}
while((p=p->next)!=NULL); /*遍歷 鏈表*/
}
/*向螢屏輸出文章*/
void OutPut(LINE * &head)
{
LINE *p=head;
do
{
printf("%s\n",p->data);
}
while((p=p->next)!=NULL); /*遍歷 鏈表*/
}
void main()
{
LINE *head;
Create(head);
printf("輸入的文章為:\n");
OutPut(head);
printf("\n");
printf("全部字母數:%d \n",CountLetter(head));
printf("數字個數:%d \n",CountNumber(head));
printf("空格個數: %d \n",CountSpace(head));
printf("文章總字數: %d \n",CountAll(head));
char str1[20],str2[20];
printf("\n");
printf("請輸入要統計的字串:");
scanf("%s",str1);
printf("%s出現的次數為:%d \n",str1,FindString(head,str1));
printf("\n");
printf("請輸入要洗掉的某一字串:");
scanf("%s",str2);
DelString(head,str2);
printf("洗掉%s后的文章為:\n",str2);
OutPut(head);
}
uj5u.com熱心網友回復:
建議你先掌握除錯的方法吧,F9設斷點,F10 逐步跟蹤你會發現你上面的程式之所以報錯在于 head地址為空,因為 你的 void Create(LINE * head) 沒有回傳值,出了這個函式head生命期也結束了。解決方法:重新定義 LINE * Create(LINE * head) 函式內 return head.呼叫時將回傳值賦值給你自己定義的LINE *head; 就可以了:
LINE * Create(LINE * head)
{
.......
return head;
}
void main()
{
LINE *head = Create(head);
......
}
uj5u.com熱心網友回復:
最好把錯誤貼出來,方便別人幫你查錯轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/118583.html
標籤:基礎類
