www.pudn.com > wordchanger.zip > Sentence.cpp, change:2007-06-18,size:7432b


// Sentence.cpp: implementation of the Sentence class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "Sentence.h" 
#include <iostream> 
#include <cstring> 
using namespace std; 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
Sentence::Sentence() 
{ 
	head = NULL; 
	link = NULL; 
	replace_flag = false; 
} 
 
Sentence::~Sentence() 
{ 
	Word *p = head, *q; 
	while(p) 
	{ 
		q = p; 
		p = p->getlink(); 
		delete q; 
	} 
 
	 
} 
 
void Sentence::print_sentence() 
{ 
	Word *p; 
	cout<<sentence<<endl; 
	int count = 0, i; 
	if(replace_flag == true) 
	{ 
		for(i=0;i<total_word_count+1;i++) 
		{ 
			p = head; 
			while(p) 
			{ 
				if(p->getorder() == i) 
					cout<<p->getword()<<" "; 
				p = p->getlink(); 
			} 
		} 
		cout<<endl; 
	} 
	p = head; 
	 
	while(p) 
	{ 
		cout<<p->getword()<<"->"; 
		p = p->getlink(); 
	} 
	cout<<"NULL"<<endl; 
} 
 
void Sentence::setsentence(char *sen, int count) 
{ 
	strcpy(sentence, sen); 
	char_count = count; 
} 
 
void Sentence::setlink(Sentence *ptr) 
{ 
	link = ptr; 
} 
 
Sentence* Sentence::getlink() 
{ 
	return link; 
} 
 
void Sentence::make_word_link() 
{ 
	Word *p, *q, *r; 
	char *a; 
	char temp[20]; 
	int word_count = 0; 
	a = strtok(sentence, " "); 
	 
	p = new Word; 
	strcpy(temp, a); 
	p->setword(temp); 
	p->setorder(word_count); 
	word_count++; 
	head = p; 
	 
	while( ( a = strtok(NULL, " ") ) != NULL) 
	{ 
		q = head; r = NULL; 
		while(q) 
		{ 
			r = q; 
			q = q->getlink(); 
		} 
		p = new Word; 
		strcpy(temp, a); 
		p->setword(temp); 
		p->setorder(word_count); 
		word_count++; 
		r->setlink(p); 
	} 
	total_word_count = word_count - 1; 
	sort(); 
 
} 
 
void Sentence::sort() 
{ 
	Word *p = head, *stalking_p = NULL, *q, *stalking_q, *p_next, *temp; 
	while(p) 
	{ 
		q = p; 
		stalking_q = NULL; 
		while(q) 
		{ 
			 
			if(strcmp(p->getword(),  q->getword()) > 0 ) 
			{ 
				if( p == stalking_q )  
				{ 
					p->setlink(q->getlink()); 
					q->setlink(p); 
					if(stalking_p) 
						stalking_p->setlink(q); 
					else 
						head = q; 
				} 
				else if( p->getlink() == stalking_q ) 
				{ 
					 
					p->setlink(q->getlink()); 
					q->setlink(stalking_q); 
					stalking_q->setlink(p); 
					if(stalking_p) 
						stalking_p->setlink(q); 
					else 
						head = q; 
				} 
				else 
				{ 
					p_next = p->getlink(); 
					p->setlink(q->getlink()); 
					q->setlink(p_next); 
					stalking_q->setlink(p); 
					if(stalking_p) 
						stalking_p->setlink(q); 
					else 
						head = q; 
				} 
				temp = p; 
				p = q; 
				q = temp; 
				 
			} 
			 
			stalking_q = q; 
			q = q->getlink(); 
 
		} 
		stalking_p = p; 
		p = p->getlink(); 
	} 
} 
 
void Sentence::set_num(int num) 
{ 
	number = num; 
} 
 
int Sentence::get_num() 
{ 
	return number; 
} 
 
char* Sentence::getsentence() 
{ 
	return sentence; 
} 
 
void Sentence::fix_sentence() 
{ 
	int i; 
	for(i=0;i<char_count;i++) 
		if(sentence[i] == 0) 
			sentence[i] = 32; 
} 
 
int Sentence::get_charcount() 
{ 
	return char_count; 
} 
 
void Sentence::del_word(char *word) 
{ 
	Word *p = head , *q = NULL; 
		 
	while(p) 
	{ 
 
		if(strcmp(p->getword(), word) == 0) 
		{ 
				replace_flag = true; 
				if(!q) 
				{ 
					q = head; 
					head = head->getlink(); 
					delete q; 
					p = head; 
					q = NULL; 
				} 
				else 
				{ 
					q->setlink(p->getlink()); 
					delete p; 
					p = q->getlink(); 
				} 
			 
			 
		} 
		else 
		{ 
			q = p; 
			p = p->getlink(); 
		} 
	} 
	 
	 
} 
 
void Sentence::del_word_step(char *word) 
{ 
	Word *p = head , *q = NULL, *pdel; 
	char select; bool cur_check = true; 
	int i, total_same_word_count = 0, current_word_count = 0; 
	 
	while(p) 
	{ 
		if( strcmp(p->getword(), word) == 0) 
			total_same_word_count++; 
		p = p->getlink(); 
	} 
	 
	while(cur_check != false) 
	{ 
		cur_check = false; 
		if(total_same_word_count == current_word_count) 
			break; 
	for(i=0;i=total_word_count;i++) 
	{ 
		p = head; 
		while(p) 
		{ 
			if(p->getorder() == i) 
			{ 
				if( (strcmp(p->getword(), word) == 0) && (!p->isChecked()) && (!cur_check) ) 
				{ 
					p->setcheck(true); 
					cur_check = true; 
					cout<<"[["<<p->getword()<<"]]"<<" "; 
					pdel = p; 
					current_word_count++; 
				} 
				else 
					cout<<p->getword()<<" "; 
			} 
			p = p->getlink(); 
		} 
	} 
	if(cur_check == true) 
	do 
	{ 
		cout<<endl<<"»èÁ¦ÇϽðڽÀ´Ï±î?(y/n) "; 
		cin>>select; 
		switch(select) 
		{ 
		case 'y': 
		replace_flag = true; 
		p = head; q = NULL;  
		while(p) 
		{ 
			if(p == pdel) 
			{ 
					if(!q) 
					{ 
						q = head; 
						head = head->getlink(); 
						delete q; 
						p = head; 
						q = NULL; 
					} 
					else 
					{ 
						q->setlink(p->getlink()); 
						delete p; 
						p = q->getlink(); 
					} 
			} 
			else 
			{ 
				q = p; 
				p = p->getlink(); 
			} 
		} 
		break; 
		case 'n': 
			break; 
		default: 
			cout<<"À߸øµÈ ÀÔ·ÂÀÔ´Ï´Ù."<<endl; 
		} 
	}while(select != 'y' && select != 'n'); 
	 
	} 
	p = head; 
	while(p) 
	{ 
		p->setcheck(false); 
		p = p->getlink(); 
	} 
	 
} 
 
 
void Sentence::replace_word(char *word1, char *word2) 
{ 
	Word *p = head; 
	while(p) 
	{ 
		if( strcmp( p->getword() , word1) == 0 ) 
		{ 
			p->setword(word2); 
			replace_flag = true; 
		} 
		p = p->getlink(); 
	} 
	 
	sort(); 
} 
 
void Sentence::replace_word_step(char *word, char *word2) 
{ 
	Word *p = head , *q = NULL, *pedit; 
	char select; bool cur_check = true; 
	int i, total_same_word_count = 0, current_word_count = 0; 
	 
	while(p) 
	{ 
		if( strcmp(p->getword(), word) == 0) 
			total_same_word_count++; 
		p = p->getlink(); 
	} 
	 
	while(cur_check != false) 
	{ 
		cur_check = false; 
		if(total_same_word_count == current_word_count) 
			break; 
	for(i=0;i=total_word_count;i++) 
	{ 
		p = head; 
		while(p) 
		{ 
			if(p->getorder() == i) 
			{ 
				if( (strcmp(p->getword(), word) == 0) && (!p->isChecked()) && (!cur_check) ) 
				{ 
					p->setcheck(true); 
					cur_check = true; 
					cout<<"[["<<p->getword()<<"]]"<<" "; 
					pedit = p; 
					current_word_count++; 
				} 
				else 
					cout<<p->getword()<<" "; 
			} 
			p = p->getlink(); 
		} 
	} 
	if(cur_check == true) 
	do 
	{ 
		cout<<endl<<"¼öÁ¤ÇϽðڽÀ´Ï±î?(y/n) "; 
		cin>>select; 
		switch(select) 
		{ 
		case 'y': 
		replace_flag = true; 
		p = head; q = NULL;  
		while(p) 
		{ 
			if(p == pedit) 
				p->setword(word2); 
			p = p->getlink(); 
		} 
		break; 
		case 'n': 
			break; 
		default: 
			cout<<"À߸øµÈ ÀÔ·ÂÀÔ´Ï´Ù."<<endl; 
		} 
	}while(select != 'y' && select != 'n'); 
	 
	} 
	p = head; 
	while(p) 
	{ 
		p->setcheck(false); 
		p = p->getlink(); 
	} 
	sort(); 
} 
 
void Sentence::print_word_to_file(FileIO &f) 
{ 
	Word *p; 
	f.outfile<<sentence<<endl; 
	int i; 
	if(replace_flag == true) 
	{ 
		for(i=0;i<total_word_count+1;i++) 
		{ 
			p = head; 
			while(p) 
			{ 
				if(p->getorder() == i) 
					f.outfile<<p->getword()<<" "; 
				p = p->getlink(); 
			} 
		} 
		f.outfile<<endl; 
	} 
	p = head; 
	while(p) 
	{ 
		f.outfile<<p->getword()<<"->"; 
		p = p->getlink(); 
	} 
	f.outfile<<"NULL"<<endl; 
} 
 
void Sentence::print_sentence_to_file(FileIO &f) 
{ 
	int i; 
	Word *p = head; 
	 
	for(i=0;i=total_word_count;i++) 
	{ 
		p = head; 
		while(p) 
		{ 
			if(p->getorder() == i) 
				f.outfile<<p->getword()<<" "; 
			p = p->getlink(); 
		} 
	} 
	 
}