淘先锋技术网

首页 1 2 3 4 5 6 7
#include<stdio.h>
#include<stdlib.h> // rand
#include<assert.h>
#include<string.h>
#include<iostream>
using namespace std; // vs 2019 

#include"MyStack.h"
typedef struct
{
	int row;
	int col;
}PosType;

typedef struct
{
	int ord;
	PosType seat;
	int di;	// 1 2 3 4 
}SElemType;
#define ROWSIZE 10
#define COLSIZE 10
#define FOOT 8
#define MARK 4
typedef int MazeType[ROWSIZE][COLSIZE];

void Print_Maze(MazeType maze)
{
	for (int i = 0; i < ROWSIZE; ++i)
	{
		for (int j = 0; j < COLSIZE; ++j)
		{
			printf("%2d", maze[i][j]);
		}
		printf("\n");
	}
	printf("\n");
}

void PathMaze(MazeType maze, PosType begin, PosType end)
{
	PosType curpos = begin;
	int curstep = 1;
	SeqStack<SElemType> st;
	do
	{
		if (Pass(maze, curpos))
		{
			FootPrint(maze, curpos);
			SElemType e = { curstep,curpos,1 };
			st.Push(e);
			if (curpos.row == end.row && curpos.col == end.col)
			{
				return;
			}
			curpos = NextPos(curpos, 1);
			curstep += 1;
		}
		else
		{
			if (!st.Is_Empty())
			{
				SElemType e = st.GetTop();
				st.Pop();
				while (e.di == 4 && !st.Is_Empty())
				{
					MarkPrint(maze, e.seat);
					e = st.GetTop();
					st.Pop();
				}
				if (e.di < 4)
				{
					e.di += 1;
					st.Push(e);
					curpos = NextPos(e.seat, e.di);
				}
			}
		}
	} while (!st.Is_Empty());
}
int main()						  
{
	MazeType maze = {
		{1,1,1,1,1,1,1,1,1,1},
		{1,0,1,1,1,0,0,0,0,1},
		{1,0,0,0,0,0,1,1,0,1},
		{1,0,1,1,1,1,1,1,0,1},
		{1,0,1,1,0,0,0,0,0,1},
		{1,0,1,1,1,1,0,1,1,1},
		{1,0,0,0,0,0,1,1,1,1},
		{1,0,1,1,1,0,1,1,1,1},
		{1,0,1,0,0,0,0,0,0,1},
		{1,1,1,1,1,1,1,1,1,1},
	};
	Print_Maze(maze);
	PosType begin = { 1,1 }, end = { 8,8 };
	PathMaze(maze, begin, end);
	Print_Maze(maze);

}

#if 0

class Int
{
	int value;
public:
	Int(int x = 0 ) :value(x)
	{
		cout << "Create Int: " << this << endl;
	}
	Int(const Int& it) :value(it.value)
	{
		cout << "Copy  Create Int: " << this << endl;
	}
	Int& operator=(const Int& it)
	{
		if (this != &it)
		{
			value = it.value;
		}
		return *this;
	}
	~Int()
	{
		cout << "Destroy Int" << this << endl;
	}
	int GetValue() { return value; }
	void SetValue(int x) { value = x; }
};

template<class T>
class MyAuto_Ptr
{
	T* ptr;
public:
	MyAuto_Ptr(T* p) :ptr(p)
	{}
	T& operator*()
	{
		return *ptr;
	}
	const T& operator*() const
	{
		return *ptr;
	}
	T* operator->()
	{
		return &*ptr;
	}
	const T* operator->() const
	{
		return &*ptr;
	}
	~MyAuto_Ptr()
	{
		delete ptr;
	}
};
template<class T>
class MyAuto_Ptr<T[]>
{
	T* ptr;
public:
	MyAuto_Ptr(T* p) :ptr(p)
	{}
	T& operator*()
	{
		return *ptr;
	}
	const T& operator*() const
	{
		return *ptr;
	}
	T* operator->()
	{
		return &*ptr;
	}
	const T* operator->() const
	{
		return &*ptr;
	}
	~MyAuto_Ptr()
	{
		delete []ptr;
	}
};
int main()
{
	MyAuto_Ptr<Int[]> ap(new Int[10]);
	

	return 0;

}

class Empty
{
};
int main()
{
	Empty x;
	int size = sizeof(x);
	cout << size << endl;

}

int main()
{
	char data[10];
	//strcpy(data, "yhping");
	strcpy_s(data, 10, "yhpinghellotulun");
	return 0;
}

//1
//2
class BigInt
{
private:
	char* str;
	int cursize;
public:
	BigInt() :str(NULL), cursize(0)
	{}
	
	//int GetLen(const BigInt * const this)
	int GetLen() const 
	{
		//this->cursize = 100;
		return this->cursize;
	}
};
int main()
{
	BigInt bt1;
	BigInt bt2;
	bt1.GetLen();
	// GetLen(&bt1);
	bt2.GetLen();
	// GetLen(&bt2);


	return 0;
}

template<class Type>
class SeqStack	   // 栈 先进后出
{
private:
	enum { INC = 2 };
	Type* data;
	int maxsize;  // 容量
	int top;	  // 
public:
	SeqStack(int sz = 10) :maxsize(sz), top(-1)
	{
		data = (Type*)malloc(sizeof(Type) * maxsize);
		if (data == NULL) exit(1);
	}
	SeqStack(const SeqStack& seq)
	{
		maxsize = seq.maxsize;
		top = seq.top;
		data = (Type*)malloc(sizeof(Type) * maxsize);
		memcpy(data, seq.data, sizeof(Type) * maxsize);
	}
	SeqStack& operator=(const SeqStack& seq)
	{
		if (this != &seq)
		{
			data = seq.data;
			maxsize = seq.maxsize;
			top = seq.top;
		}
		return *this;
	}
	~SeqStack()
	{
		free(data);
		data = NULL;
		maxsize = 0;
		top = -1;
	}
	int GetSize() const { return top + 1; }
	bool Is_Empty() const { return GetSize() == 0; }
	bool Is_Full() const { return GetSize() == maxsize; }
	bool Push(const Type& x)
	{
		if (Is_Full())
		{
			Type* newdata = (Type*)realloc(data, sizeof(Type) * maxsize * INC);
			if (newdata == NULL) return false;
			data = newdata;
			maxsize = maxsize * INC;
		}
		data[++top] = x;
		return true;
	}
	Type& GetTop()
	{
		return data[top];
	}
	const Type& GetTop() const
	{
		return data[top];
	}
	void Pop()
	{
		--top;
	}
	void Clear()
	{
		top = -1;
	}

};

int main()
{
	SeqStack<int> ista;
	for (int i = 0; i < 20; ++i)
	{
		ista.Push(i);
	}
	SeqStack<int> istb(ista);

	while (!ista.Is_Empty())
	{
		int x = ista.GetTop();
		ista.Pop();
		cout << x << endl;
	}


	return 0;
}

int main()
{
	SeqStack<int> ist = SeqStack<int>();
	ist.Push(12);
	ist.Push(23);
	ist.Push(34);
	ist.Push(45);

	while (!ist.Is_Empty())
	{
		int x = ist.GetTop();
		ist.Pop();
		cout << x << endl;
	}
	return 0;
}


template<class Type>
class SeqStack
{
	Type* data;
	int maxsize;
	int top;
public:
	SeqStack(int sz = 100)
	{
		data = new Type[sz];
		maxsize = sz;
		top = -1;
	}
};


//1
//2
class CGoods
{
private:
	char Name[20];
	int Amount;
	float Price;
	float Total_value;
public:
	//void RegisterGoods(CGoods *this,const char*, int, float); //输入数据
	void RegisterGoods(const char*, int, float); //输入数据
	//void CountTotal(CGoods *this)
	void CountTotal()                  //计算商品总价值
	{
		Total_value = Amount * Price;
	}
	//void GetName(CGoods *this,char name[])
	void GetName(char name[])             //读取商品名 
	{
		strcpy_s(name,20, Name);
	}
	//int GetAmount(CGoods *this)
	int GetAmount()                    //读取商品数量 
	{
		return Amount;
	}
	//float GetPrint(CGoods *this)
	float GetPrice()                //读取商品单价
	{
		return Price;
	}
	//float GetTotal_value(CGoods *this)
	float GetTotal_value()             //读取商品总价值 
	{
		return Total_value;
	}
};

//void CGoods::RegisterGoods(CGoods *this,const char* name, int amount, float price)
void CGoods::RegisterGoods(const char *name, int amount, float price)
{
	strcpy_s(this->Name,20, name);
	this->Amount = amount;
	this->Price = price;
}
int main()
{
	CGoods c1, c2;
	c1.RegisterGoods("iphone", 10, 6800);
	// RegisterGoods(&c1,"iphone",10,6800);
	c1.CountTotal();
	c2.RegisterGoods("huawei", 12, 7800);
	// RegisterGoods(&c2,"huawei",12,7800);
	c2.CountTotal();

}




#define INITSHOW 0x80 // 1000 0000	// @
#define MAKEMINE 0x40 // 0100 0000
#define MINESHOW 0x20 // 0010 0000
#define MAKESUM  0x10 // 0001 0000
#define PRINTSUM 0x0F // 0000 1111
#define CLSINIT  0x7f // 0111 1111
#define MINENUM  25 
#define ROWSIZE  10
#define COLSIZE  10

typedef unsigned char Grid[ROWSIZE + 2][COLSIZE + 2];

void Init_InitShow(Grid ar, int row, int col)
{  
	for (int i = 1; i <= row; ++i)
	{
		for (int j = 1; j <= col; ++j)
		{
			ar[i][j] = INITSHOW;
		}
	}
}
void Init_Mine(Grid ar, int row, int col)
{
	int i = 0;
	while (i < MINENUM)
	{
		int r = rand() % ROWSIZE + 1;
		int c = rand() % COLSIZE + 1;
		if (!(ar[r][c] & MAKEMINE)) //
		{
			ar[r][c] = (ar[r][c] | MAKEMINE);
			++i;
		}
	}
}
void Init_Num(Grid ar, int row, int col)
{
	for (int r = 1; r <= row; ++r)
	{
		for (int c = 1; c <= col; ++c)
		{
			if (!(ar[r][c] & MAKEMINE))
			{
				int num = 0;
				for (int i = r - 1; i <= r + 1; ++i)
				{
					for (int j = c - 1; j <= c + 1; ++j)
					{
						if (ar[i][j] & MAKEMINE)
						{
							num += 1;
						}
					}
				}
				ar[r][c] = (ar[r][c] | MAKESUM);
				ar[r][c] = ar[r][c] | (num & PRINTSUM);
			}
		}
	}
}

void Init_Ar(Grid ar, int row, int col)
{
	//
	Init_InitShow(ar, row, col);
	//
	Init_Mine(ar, row, col);
	//
	Init_Num(ar, row, col);
}
void Print_Ar(Grid ar, int row, int col)
{
	printf("  ");
	for (int i = 1; i <= col; ++i)
	{
		printf("%2d", i);
	}
	printf("\n");
	for (int i = 1; i <= row; ++i)
	{ 
		printf("%2d", i);
		for (int j = 1; j <= col; ++j)
		{
			
			if (ar[i][j] & MINESHOW)
			{
				printf("%2c", '#');
			}
			else if (ar[i][j] & INITSHOW)
			{
				printf("%2c", '@');
			}else
			{
				printf("%2d", ar[i][j] & PRINTSUM);
				//                     0x0F
			}
		}
		printf("\n");
	}
	printf("\n");
}
int main()
{
	Grid ar = {};
	Init_Ar(ar, ROWSIZE, COLSIZE);
	int num = 0;
	int r, c; 
	char ch;
	while (num < MINENUM)
	{
		system("cls");
		Print_Ar(ar, ROWSIZE, COLSIZE);
		printf("input row col select(#,0,@) \n");
		scanf_s("%d %d %c", &r, &c, &ch);
		if (r >= 1 && r <= ROWSIZE && c >= 1 && COLSIZE)
		{
			if (ch == '0')
			{
				if (ar[r][c] & MAKEMINE)
				{
					printf("炸死了");
					getchar();
					break;
				}
				ar[r][c] = (ar[r][c] & CLSINIT);
			}
			else if (ch == '#')
			{
				if (ar[r][c] & MAKEMINE)
				{
					num += 1;
					ar[r][c] = (ar[r][c] & CLSINIT);
					ar[r][c] = (ar[r][c] | MINESHOW);
				}
			}
			else if (ch == '@')
			{
			}
			else
			{
				printf("select error \n");
			}

		}
		else
		{
			printf("row col error \n");
		}
	}
	return 0;
}


//&  |  ^ // ~
// >>  <<
int main()
{
	char a = 0;

	return 0;
}

int Get1Bit(int x)
{
	int sum = 0;
	while (x != 0)
	{
		x = x & (x - 1);
		sum += 1;
	}
	return sum;
}
int main()
{
	int x = 23;

	int sum = Get1Bit(x);

	printf("%d \n", sum);
	return 0;
}

int Get1Bit(int x)
{
	int sum = 0;
	int a = 1;
	for (int i = 0; i < 32; ++i)
	{
		if (x & a)
		{
			sum += 1;
		}
		a = a << 1;
	}
	return sum;
}
int Get1Bit(int x)
{
	int sum = 0;
	while (x != 0)
	{
		if (x & 0x01)
		{
			sum += 1;
		}
		x = x >> 1;
	}
	return sum;
}
int main()
{
	char a = 67;
	char b = 0x54;

	char c = 0;
	c = a & b;
	printf("%x \n", c);

	c = a | b;
	printf("%x \n", c);

	c = a ^ b;
	printf("%x \n", c);
	return 0;
}



int main()
{
	int a = 10;	    //10
	int b = 010;    // 8
	int c = 0x10;   //16 
	int d = 0b1010;	// 2

	printf("%d %d %d %d \n", a, b, c, d);
	printf("%o %o %o %o \n", a, b, c, d);
	printf("%x %x %x %x \n", a, b, c, d);

	return 0;
}


void Swap_Int(int x, int y)
{
	int tmp = x;
	x = y;
	y = tmp;
}
int main()
{
	int a = 10, b = 20;
	Swap_Int(a, b);
	printf("a = %d b = %d \n", a, b);
	return 0;
}

#endif