⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 article.cpp

📁 对文章的字母个数,关键字个数进行统计,浏览文章,删除特定字符串,输入文章等进行管理,采用链表实现.
💻 CPP
字号:
#include"Article.h"
///////////////////////////////////////////////////////////////////
Article::Article() {  
	n=-1;
	head = NULL;
}
///////////////////////////////////////////////////////////////////
bool Article::empty()const {
	return size()==0;
}
///////////////////////////////////////////////////////////////////
int Article::size()const {                    
	if(head==NULL) return 0;                          
	Node *p=head;                               
	int count=0;                                      
	while(p!=NULL) {                                     
		count++;                                         
		p=p->next;                                   
	}                                  
	return count; 
}                    
////////////////////////////////////////////////////////////////////////////
void Article::clear() {     
	Node *p, *q;
	for (p=head;p!=NULL;p=q) {
		q=p->next;
		delete p;
	}
	head = NULL;
}   
////////////////////////////////////////////////////////////////////////////
Article::~Article() {
	clear();
}            
//////////////////////////////////////////////////////////////////
void Article::add(char data) {         
	if(head==NULL){    
		n++;
		head=new Node;                               
		head->data=data;
		head->num=n;
		head->next=NULL;                             
		return;
	}                                     
	
	Node *temp;                                                          
	Node *p=head;                               
	while(p->next!=NULL) p=p->next;            
	temp=new Node; 
	n++;
	temp->data=data;
	temp->num=n;
	temp->next=NULL;                                
	p->next=temp;                                   
	return;
}                                     
/////////////////////////////////////////////////////////////////////////////////
void Article::remove(int num) {          
	Node *temp=find(num);               
	if(temp==NULL)  return;                 
	Node *p=head;                          
	if(head==temp) {                      
		head=head->next;                 
		delete p;   
		return;
	}                                    
	while(p->next!=temp) p=p->next;                   
	Node *t=p->next;                                 
	p->next=t->next;                                
	delete t;                                      
	return;                                        
}
/////////////////////////////////////////////////////////////////////////////////
void Article::print(Node *p) {              
	cout<<p->data;
	return;                                         
}
/////////////////////////////////////////////////////////////////////////////////
void Article::show() {                              
	if (empty()) {                           
		cout <<"当前的列表为空!\n";             
		return;
	}                                      
	Node *p=head;
	while(p!=NULL) {                                       
		print(p);                                          
		p=p->next;
	}                                                           
    return;
} 
////////////////////////////////////////////////////////////////////////////////
Node *Article::find(int num) {                    
	if (empty()) {                         
		cout <<"当前的列表为空!\n";           
		return NULL;                                
	}
	Node *p=head;                                 
	while(p!=NULL) {                            
		if(p->num==num) break;                      
		p=p->next;                            
	}
	if(p==NULL) {                             
		cout <<"找不到该记录!\n";      
		return NULL;                        
	}
	return p;                            
}
////////////////////////////////////////////////////////////////////////////////
int Article::countLetter() {
	int count=0;
	Node *p=head;
	while(p!=NULL) {
		if(p->data>='a'&&p->data<='z'||p->data>='A'&&p->data<='Z')
			count++;
		p=p->next;
	}
	return count;
}
///////////////////////////////////////////////////////////////////////////////
int Article::countDigit() {
	int count=0;
	Node *p=head;
	while(p!=NULL) {
		if(p->data>='0'&&p->data<='9') 
			count++;
		p=p->next;
	}
	return count;
}
////////////////////////////////////////////////////////////////////////////////
int Article::countSpace() {
	int count=0;
	Node *p=head;
	while(p!=NULL) {
		if(p->data==' ')
			count++;
		p=p->next;
	}
	return count;
}
/////////////////////////////////////////////////////////////////////////////////
int Article::countAll() {
	int count=0;
	Node *p=head;
	while(p!=NULL) {                                       
		if(p->data!='\n') 
			count++;
		p=p->next;
	}     
	return count;
}
//////////////////////////////////////////////////////////////////////////////
void Article::printCount() {
	cout<<endl<<"文章的统计结果如下:"<<endl;
	cout<<"全部字母数:"<<countLetter()<<endl;
	cout<<"数字个数:"<<countDigit()<<endl;
	cout<<"空格个数:"<<countSpace()<<endl;
	cout<<"文章总字数:"<<countAll()<<endl;
}
/////////////////////////////////////////////////////////////////////////////
int Article::countWord(string str) {
	Node *p=head;
	int count=0; 
	int len=str.size(); //待统计字符串的长度  
	if(p!=NULL)	{ 	
		for(int i=0;i<size();i++) {
			if(find(i)->data==str[0]) { 
				int k=0; 
				for(int j=0;j<=len-1;j++) 
					if(find(i+j)->data==str[j])
						k=k+1; //如果字符串首字符相同,依次往后比较 
					if(k==len) {
						count++;
						i=i+k-1;
					}//字符串完全相同则计数 
			} 
		} 
	} 
	return count;
}
///////////////////////////////////////////////////////////////////////////
void Article::removeWord(string str) {
	Node *p=head;	 
	int len=size();
	if(p!=NULL)	{ 	
		for(int i=0;i<len;i++) {
			if(find(i)->data==str[0]) { 
				int k=0; 
				for(int j=0;j<=str.size()-1;j++) 
					if(find(i+j)->data==str[j])
						k=k+1; //如果字符串首字符相同,依次往后比较 
					if(k==str.size()) {
						for(int m=i;m<i+str.size();m++) {
							remove(m);
						}
						i=i+k-1;
					}//字符串完全相同则计数 
			} 
		} 	
	cout<<"删除字符串 "<<str<<" 后的文章如下:"<<endl;
	show();           //显示删除后的文章
	adjustArticle();  //删除特定字符后调整文章
	} 
}
//////////////////////////////////////////////////////////////////////////
void Article::adjustArticle() {
	if (empty()) {                           
		cout <<"当前的列表为空!\n";             
		return;
	}      
	int count=0;
	Node *p=head;
	while(p!=NULL) {                                       
		p->num=count++;
		p=p->next;
	}                                                           
    return;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -