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

📄 position.h

📁 杭州公交查询系统 可以查询公交线路
💻 H
字号:
#include<iostream.h>
#include<string.h>

int Index_KMP(char* s,char* t)
{
//转化 s 和 t 成为另一种类型的字符串,它的第一个字节存放串的长度,没有串的结束符如'\0'等
//且内容都为 unsigned char 型。此类型可用模式匹配算法。
	int count=1;
	unsigned char T[11];
	while(*t) T[count++]=(unsigned char)*t++;
	T[0]=count-1;

	count=1;
	unsigned char S[500];
	while(*s) S[count++]=(unsigned char)*s++;
	S[0]=count-1;
//---------
	int i=1,j=1;
    while(i<=S[0] && j<=T[0])
	{
		if( S[i] == T[j]){ ++i; ++j;}
        else {i=i-j+2;j = 1; } 
	}
    if(j>T[0]) return i-T[0];
    else  return 0;
}// Index_KMP

//---------------class position declare---------------------

class position
{
private:
	char* name;
	char* passBuses;
	int isExist(char*);
public:
	position(char* n);
	~position();
	char* getName(){return name;}
	char* getPassBuses(){return passBuses;}
	void printPassBuses(){cout<<passBuses<<endl;}
	void isThrough(position*);
	class Error{};//异常类
};//class position

//=============position members function==============

position::position(char* n)
{
	name=new char[40];
	strcpy(name,n);
	if(isExist(name))
	{		
		char* locator=new char[100];
		strcpy(locator,"source\\position\\");
		strcat(locator,name); strcat(locator,".txt");

		ifstream in_file;
		in_file.open(locator, ios::in);
		char* mystr=new char[600];
	    in_file.getline(mystr,1000);
	    in_file.close();
	
		passBuses=new char[600];
	    strcpy(passBuses,mystr);
	    delete []mystr;
	    delete []locator;
	}
	else
	{
		throw Error();
	}
}//position::position

position::~position()
{
	delete []name;
	delete []passBuses;
}//position::~position

void position::isThrough(position* destination)
{
	char* returnInfo=new char[300];
	strcpy(returnInfo,"");
	char* destinationTrack=new char[600];
	char* outBuses=new char[500];
	char* inBuses=new char[500];
	char subString[10];
	
	char* p=passBuses;
	while(*p!=' ') p++;
	p++;
	strcpy(outBuses,p);

	strcpy(destinationTrack,destination->getPassBuses());
	p=destinationTrack;
	while(*p!=' ') p++;
	p++;
	strcpy(inBuses,p);

	p=inBuses;
	while(*p)
	{
		int i=0;
		while(*p!=' '&&*p)
		{
			subString[i++]=*p++;
		}
		subString[i]='\0';
		if(Index_KMP(outBuses,subString))
		{
			strcat(returnInfo,subString); 
			strcat(returnInfo," "); 
		}//if
		if(*p) p+=2;
	}//while
	if(*returnInfo) strcat(returnInfo," 等线路两站都会经过。");
	else strcpy(returnInfo,"很遗憾,两站之间没有线路可通。");

    cout<<endl<<returnInfo<<endl;

	delete []destinationTrack;
	delete []outBuses;
	delete []inBuses;
	delete []returnInfo;
}//position::isThrough

int position::isExist(char* posName)
{
	ifstream in_file;
	in_file.open("source\\positionname.txt", ios::in);
	char* instring=new char[40];
	while(in_file)
	{
	    in_file>>instring;
		if(!strcmp(posName,instring)) {in_file.close();return 1;}
	}
	in_file.close();
	return 0;
}//position::isExist

⌨️ 快捷键说明

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