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

📄 cpp1.cpp

📁 TSP货郎担过河问题
💻 CPP
字号:
#include <iostream>
#include<string>
#include<fstream>
#include <windows.h>

using namespace std;

static string SymbolTable[100];
static string IntRealTable[100];
static string StringTable[100];

//==================Table1()============================
//=======catch the table1 word of SIC
//=======then compare which position that the word is
//======================================================
int Table1(string tmp_word)
{
	int count=1;  // record the word position
	string word;  // register the word of SIC one by one
	ifstream file;
	file.open("table1.txt",ios::in);
	if(!file)
	{
		//cout<<"no table1.txt file"<<endl;
	}
	else
	{
		//cout<<"read table1.txt in"<<endl;
		while(!file.eof())
		{
			int compare_value;  //if compare_value is 0,it means the two words the same
			file>>word;
			compare_value=tmp_word.compare(word);
			if(compare_value==0)
				return count;
			count++;
		}
	}
	return 0;
	file.close();
}


//==================Table2()============================
//=======catch the table2 word of SIC
//=======then compare which position that the word is
//======================================================
int Table2(string tmp_word)
{
	int count=1;  // record the word position
	string word;  // register the word of SIC one by one
	ifstream file;
	file.open("table2.txt",ios::in);
	if(!file)
	{
		//cout<<"no table2.txt file"<<endl;
	}
	else
	{
		//cout<<"read table2.txt in"<<endl;
		while(!file.eof())
		{
			int compare_value;  //if compare_value is 0,it means the two words the same
			file>>word;
			compare_value=tmp_word.compare(word);
			if(compare_value==0)
				return count;
			count++;
		}
	}
	return 0;
	file.close();
}


//==================Table3()============================
//=======catch the table3 word of SIC
//=======then compare which position that the word is
//======================================================
int Table3(string tmp_word)
{
	int count=1;  // record the word position
	string word;  // register the word of SIC one by one
	ifstream file;
	file.open("table3.txt",ios::in);
	if(!file)
	{
		//cout<<"no table3.txt file"<<endl;
	}
	else
	{
		//cout<<"read table3.txt in"<<endl;
		while(!file.eof())
		{
			int compare_value;  //if compare_value is 0,it means the two words the same
			file>>word;
			compare_value=tmp_word.compare(word);
			if(compare_value==0)
				return count;
			count++;
		}
	}
	return 0;
	file.close();
}


//==================Table4()============================
//=======catch the table4 word of SIC
//=======then compare which position that the word is
//======================================================
int Table4(string tmp_word)
{
	int count=1;  // record the word position
	string word;  // register the word of SIC one by one
	ifstream file;
	file.open("table4.txt",ios::in);
	if(!file)
	{
		//cout<<"no table4.txt file"<<endl;
	}
	else
	{
		//cout<<"read table4.txt in"<<endl;
		while(!file.eof())
		{
			int compare_value;  //if compare_value is 0,it means the two words the same
			file>>word;
			compare_value=tmp_word.compare(word);
			if(compare_value==0)
				return count;
			count++;
		}
	}
	return 0;
	file.close();
}

/*
int Table5(string tmp_word)
{
	int count=0;
	string word[100];
	while(!word[count].empty())
	{
		int compare_value;
		compare_value=tmp_word.compare(word[count]);
		if(compare_value==0)
			return count+1;
		count++;
	}
	word[count]=tmp_word;
	return ;
}*/


//==================Symbol Table======================
//======== Create a Symbol Table with hash function
//======== and record the position information
//====================================================
int Table5(string tmp_word)
{
	int count=0;
	for(int i=0;i<tmp_word.length();i++)
	{
		count=count+tmp_word.at(i);
	}
	//cout<<count<<endl;
	count=count%100;
	//compare_value=tmp_word.compare(SymbolTable[count]);
	//cout<<compare_value<<endl;
	while(!SymbolTable[count].empty())
	{
		int compare_value;
		compare_value=tmp_word.compare(SymbolTable[count]);
		//cout<<compare_value;
		if(compare_value!=0)
			count++;
		else
			break;
	}
	SymbolTable[count].assign(tmp_word);
	return count;
}


//==================Integer/Real Table======================
//======== Create a Integer/Real Table with hash function
//======== and record the position information
//==========================================================
int Table6(string tmp_word)
{
	int count=0;
	for(int i=0;i<tmp_word.length();i++)
	{
		count=count+tmp_word.at(i);
	}
	count=count%100;
	while(!IntRealTable[count].empty())
	{
		int compare_value;
		compare_value=tmp_word.compare(IntRealTable[count]);
		if(compare_value!=0)
			count++;
		else
			break;
	}
	IntRealTable[count].assign(tmp_word);
	return count;
}


//==================String Table======================
//======== Create a String Table with hash function
//======== and record the position information
//==========================================================
int Table7(string tmp_word)
{
	int count=0;
	for(int i=0;i<tmp_word.length();i++)
	{
		count=count+tmp_word.at(i);
	}
	count=count%100;
	while(!StringTable[count].empty())
	{
		int compare_value;
		compare_value=tmp_word.compare(StringTable[count]);
		if(compare_value!=0)
			count++;
		else
			break;
	}
	StringTable[count].assign(tmp_word);
	return count;
}

//==============get()=========================
//========= Make the useless char to be NULL
//========= and record in a
//============================================
string get(char tmp[])
{
	int ptr=0;
	string a;
	a.erase();
	while(tmp[ptr]!=NULL)
	{
		a=a+tmp[ptr];
		ptr++;
	}
	return a;
}


//============ReadFile()===========================
//===== 1st. Read the gived file in
//===== 2ed. Judge the word and table one by one
//===== 3rd. output the file
//=================================================
void ReadFile()
{
	char word[50];
	for(int i=0;i<50;i++)
		word[i]=NULL;
	string string_tmp;
	char filename[50];
	ifstream file;
	cout<<"Enter a file name"<<endl;
	cin>>filename;
	file.open(filename,ios::in);

	fstream cleanfile,outputfile;

	//===clean the file===
	cleanfile.open("9027110.txt",ios::out | ios::trunc);
	cleanfile.close();
	
	//===write the file===
	outputfile.open("9027110.txt",ios::out | ios::app);
    //cout << "write file OK" << endl;

	if(!file)
	{
		cout<<"no this file"<<endl;
	}
	else
	{
		cout<<"read file in";
		for(int x=0;x<5;x++)
		{
			Sleep(300);
			cout << "."; 
		}
		cout<<"OK"<<endl;
		while(!file.eof())
		{
			
			int length=0;
			//string_tmp.erase();
			file.getline(word,50);
			//string_tmp=word;
			//cout<<endl;
			//cout<<word<<endl;
			outputfile<<endl;
			outputfile<<word<<endl;
			
			//cout<<string_tmp<<endl;

			while(length<=50 && word[length]!=NULL)
			{
				//Maybe Space or Tab at the first
				if(word[length]==0 || word[length]==32)
					length++;
				else
				{
					// Judge Table1 or Table2 or Table3 or Table5
					if((word[length]>=65 && word[length]<=90) || (word[length]>=97 && word[length]<=122))
					{
						int length_tmp=0;
						char short_word[50];
						for(int i=0;i<50;i++)
							short_word[i]=NULL;
						while((word[length]>=65 && word[length]<=90) || (word[length]>=97 && word[length]<=122))
						{
							short_word[length_tmp]=word[length];
							length_tmp++;
							length++;
						}
						// if not the table , then return zero
						if(Table1(short_word)==0)
						{
							if(Table2(short_word)==0)
							{
								if(Table3(short_word)==0)
								{
									//cout<<"("<<5<<" , "<<Table5(get(short_word))<<")";
									outputfile<<"("<<5<<" , "<<Table5(get(short_word))<<")";
								}
								else
								{
									//cout<<"("<<3<<" , "<<Table3(get(short_word))<<")";
									outputfile<<"("<<3<<" , "<<Table3(get(short_word))<<")";
								}
							}
							else
							{
								//cout<<"("<<2<<" , "<<Table2(get(short_word))<<")";
								outputfile<<"("<<2<<" , "<<Table2(get(short_word))<<")";
							}

						}
						else
						{
							//cout<<"("<<1<<" , "<<Table1(get(short_word))<<")";
							outputfile<<"("<<1<<" , "<<Table1(get(short_word))<<")";
						}
					}
					else
					{
						// Judge Table4 or Table 6 or Table7
						int length_tmp3=0;
						char short_word[50];
						for(int i=0;i<50;i++)
							short_word[i]=NULL;
						if(word[length]>=48 && word[length]<=57)
						{
							while(word[length]>=48 && word[length]<=57)
							{
								short_word[length_tmp3]=word[length];
								length_tmp3++;
								length++;
							}
							//cout<<"("<<6<<" , "<<Table6(get(short_word))<<")";
							outputfile<<"("<<6<<" , "<<Table6(get(short_word))<<")";
						}
						else
						{
							//cout<<"("<<4<<" , "<<Table4(get(short_word))<<")";
							//cout<<word[length]<<endl;
							string table4_tmp;
							table4_tmp=word[length];
							outputfile<<"("<<4<<" , "<<Table4(table4_tmp)<<")";
							length++;
							// The table7 is String_table,so it must be between two (')
							if(word[length-1]==39)
							{
								int length_tmp2=0,flag=0;
								char short_word2[50];
								for(int i=0;i<50;i++)
									short_word2[i]=NULL;
								while(flag==0)
								{
									if(word[length]!=39)
									{
										// record string
										short_word2[length_tmp2]=word[length];
										length_tmp2++;
										length++;
									}
									else
										flag=1;
								}
								//cout<<"("<<7<<" , "<<Table7(get(short_word2))<<")";
								//cout<<"("<<4<<" , "<<Table4(get(short_word))<<")";
								outputfile<<"("<<7<<" , "<<Table7(get(short_word2))<<")";
								string table4_tmp2;
								table4_tmp2=word[length];
								outputfile<<"("<<4<<" , "<<Table4(table4_tmp2)<<")";
								length++;
							}
						}
					}
				}
			}
		}
	}
	cout << "write file out";
	for(int x=0;x<5;x++)
	{
		Sleep(300);
		cout << "."; 
	}
	cout<<"OK" << endl;
	file.close();
}

void main()
{
	ReadFile();
	//string a("'");
	//cout<<Table4(a)<<endl;

	/*
	char a={'a'};
	string b;
	b.erase();
	b=b+a;
	cout<<b<<endl;
	cout<<b.length()<<endl;*/
	/*char a[2];
	a[0]=;
	string b;
	b.erase();
	b=a;
	cout<<a<<endl;
	cout<<b<<endl;*/
	/*char *b;
	b="AB";
	//cout<<b.at(1)<<endl;
	int c;
	c=atoi(b);
	cout<<c<<endl;*/
	//string b("Hello,World");
	//string c("check it out");
	//string d("Hello,Worle");
	//Table5(b);
	//Table5(c);
	//cout<<Table7(b)<<endl;
	//cout<<Table7(c)<<endl;
	//cout<<Table7(d)<<endl;
	//cout<<SymbolTable[31]<<endl;
	//cout<<SymbolTable[32]<<endl;
}

⌨️ 快捷键说明

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