欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

顺序表存取小练习

程序员文章站 2022-05-12 10:40:49
...
#include <iostream>
#include <conio.h>

struct kk 
{
	int type;
	int value;
};
#define  DATA kk
class SXB
{

public:
	int len;
	int size;
	DATA* p;
	SXB()
	{
		len=0;
		size=1;
		p=new DATA[size];
	}
	bool InsertSXB(int xb, DATA data)
	{
		if (xb < 0 || xb > len)
			return false;
		if (len == size)
		{
			DATA* np = new DATA[size*2];
			for (int i = 0;i < len; ++i)
				np[i] = p[i];
			delete []p;
			p = np;
			size *= 2;
		}
		//ÒÆλ
		for (int i = len-1; i >= xb; --i)
			p[i+1] = p[i];
		//²åÈëÊý¾Ý
		p[xb] = data;
		len++;
		return true;
	}
	void PushbackSXB(DATA data)
	{
		InsertSXB(len,data);
	}
	bool EraseSXB(int xb)
	{
		if(xb<0 || xb > len-1)
			return false;
		for(int i=xb+1;i<len;++i)
			p[i-1]=p[i];
		len--;
		return true;
	}
	int FindDataSXB(DATA data,bool (*bj)(DATA&, DATA&),
		int startxb = 0 )
	{
		for(int i= startxb;i<len;++i)
		{
			if(bj(p[i],data))
				return i;
		}
		return -1;
	}
	DATA* GetDataSXB(int xb)
	{
		if (xb < 0 || xb > len-1)
			return 0;
		return &p[xb];
	}
	void ClearSXB()
	{
		len = 0;
	}
	int GetLenSXB()
	{
		return len;
	}
	~SXB()
	{
		if (p)
		{
			delete[]p;
			p = 0;
		}
	}
};
void main()
{
	SXB a;
	kk k;
	int p[10]={1,2,3,4,5,6,7,8,9,11};
	int px=0;
	int py=0;
	const int w=10;
	const int h= 4;
	bool shuju = true ;
	int map[w*h]={};
	bool shuchu = false ;
	while(1)
	{

		system("cls");
		for(int i=0;i<10;++i)
			std::cout<<p[i]<<" ";
		std::cout<<"\n";
		for(int y=0;y<h;++y)
		{
			for(int x=0;x<w;++x)
			{
				if(px==x && py==y)
					std::cout<<"1";
				else if(x==0 && y==2)
					std::cout<<"+ ";
				else if(x==1 && y==2)
					std::cout<<"- ";
				else if(x==4 && y==2)
					std::cout<<"结果";
				else
					std::cout<<"  ";
			}
			std::cout<<"\n";
		}

		for(int i=0;i<a.GetLenSXB();++i)
		{
			if(a.GetDataSXB(i)->type==0)
				std::cout<<a.GetDataSXB(i)->value<<" ";
			if(a.GetDataSXB(i)->type == 1)
			{
				if(a.GetDataSXB(i)-> value == 1 )
					std::cout<<"+ " ;
				else if(a.GetDataSXB(i)-> value == 2 )
					std::cout<<"- " ;
			}
		}
		int b=_getch();
		if(b=='a'||b=='A')
		{
			if(px>0)
				px--;
		}		
		else if(b=='d'||b=='D')
		{
			if(px<w-1)
				px++;
		}
		else if(b==' ')
		{
			if(shuju)
			{
				shuju = false ;
				if(py==0)
				{
					kk k ;
					k.type=0;
					k.value=p[px];
					a.PushbackSXB(k);
					py = 3 ;
					px = 0 ;
				}
			}
			else
			{
				if(py==3)
				{
					if(px == 4 || px == 5)
					{
						shuju = true ;
						shuchu =true ;
					}
					if(shuchu == false)
					{
						kk k ;
						k.type=1;
						if(px == 0)
							k.value=1;
						else if(px == 1)
							k.value = 2 ;
						a.PushbackSXB(k);
						shuju = true ;
					}
					if(px == 0 || px == 1 || px == 4)
					{
						shuju = true ;
						py = 0 ;
						px = 0 ;
					}
				}
			}

		}
		if(shuchu)
		{
			int zonghe = a.GetDataSXB(0)->value ;
			for(int i = 1 ; i < a.GetLenSXB() - 1 ; ++ i)
			{
				if(a.GetDataSXB(i)->type == 1 )
				{
					if( a.GetDataSXB(i)-> value == 1 )
						zonghe += a.GetDataSXB(i+1)->value ;
					else if( a.GetDataSXB(i)-> value == 2 )
						zonghe -= a.GetDataSXB(i+1)->value ;
					++i ;
				}

			}
			std::cout<<"= "<<zonghe<<std::endl;
			system("pause");

		}
	}
	
	system("pause");
}

 

相关标签: 练习