中工18级数据结构

单位员工通讯录管理系统(线性表的使用)



#include<iostream>
#include <string>
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std;


typedef struct
{                      //员工通讯信息的结构类型定义
	string num;       //员工编号
	string name;     //员工姓名
	string tel;     //手机号码
	string email;     //邮箱
}DataType;


typedef struct node
{
	DataType data;      //结点的数据域
	struct node *next;  //结点的指针域
}ListNode, *LinkList;



void CreateList_L(LinkList &L)
{
	int i, n;
	L = new ListNode;
	L->next = NULL;
	LinkList s = L;
	LinkList p;

	cin >> n;
	for (i = 1; i <= n; ++i)
	{
		p = new ListNode;
		cin >> p->data.num;
		cin >> p->data.name;
		cin >> p->data.tel;
		cin >> p->data.email;
		p->next = NULL;
		s->next = p;
		s = p;
	}
}

void GetdataList_L(LinkList &L)
{//查询员工信息
	ListNode *p;
	int j = 0;
	p = L->next;
	string numm;
	cin >> numm;
	while (p)
	{
		if (p->data.name == numm)
		{
			j = 1; break;
		}
		p = p->next;
	}
	if (j)
	{
		//cout << "===编号===" << "===姓名===" << "===手机号码===" << "===邮箱===" << endl;
		cout << p->data.num << " " << p->data.name << " " << p->data.tel << " " << p->data.email << endl;
	}
	else
		cout << "NOT FOUND!" << endl;
}

void ModifydataList_L(LinkList &L)
{//修改员工信息
	ListNode *p;
	int j = 0;
	p = L->next; string numm;
	//cout << "请输入修改员工的姓名:" << endl;
	cin >> numm;
	while (p)
	{
		if (p->data.name == numm)
		{
			j = 1; break;
		}
		p = p->next;
	}
	if (j)
	{
		string shuxin;
		string xgxx;
		cin >> shuxin >> xgxx;

		if (shuxin == "name")
			p->data.name = xgxx;
		else if (shuxin == "tel")
			p->data.tel = xgxx;
		else if (shuxin == "email")
			p->data.email = xgxx;
	}
	else
		cout << "NOT FOUND!" << endl;
}

void InsretdataList_L(LinkList &L)
{//插入员工信息
	ListNode *p;
	LinkList s = L;
	p = new ListNode;
	cin >> p->data.num >> p->data.name >> p->data.tel >> p->data.email;
	p->next = NULL;
	while (s->next != NULL)
	{
		s = s->next;
	}
	s->next = p;
}

void DeletedataList_L(LinkList &L)
{//指定位置删除元素
	ListNode *p;
	int j = 0;
	p = L;
	string numm;
	//cout << "请输入删除员工的姓名:" << endl;
	cin >> numm;
	while (p->next)
	{
		if (p->next->data.name == numm)
		{
			j = 1; break;
		}
		p = p->next;
	}
	if (j)
	{
		p->next = p->next->next;
		//cout << "删除成功" << endl;
	}
	else
		cout << "NOT FOUND!" << endl;
}

void PrintfList_L(LinkList &L)
{
	ListNode *p;
	int j = 0;
	p = L->next;
	//cout << "===编号===" << "===姓名===" << "===手机号码===" << "===邮箱===" << endl;
	while (p)
	{
		cout << p->data.num
			<< " " << p->data.name
			<< " " << p->data.tel
			<< " " << p->data.email << endl;
		p = p->next;
	}
}


int main()
{
	LinkList L;
	CreateList_L(L);
	int  m;
	string n;
	cin >> m;
	while (m != 0)
	{
		cin >> n;
		if (n == "query")
			GetdataList_L(L);
		else if (n == "modify")
			ModifydataList_L(L);
		else if (n == "add")
			InsretdataList_L(L);
		else if (n == "del")
			DeletedataList_L(L);
		else if (n == "print")
			PrintfList_L(L);
		m--;
	}
	return 0;
}

停车场管理(栈和队列的应用)


#include <stdio.h>
#include<iostream>
#include<string>
using namespace std;

typedef struct
{
	string CarNo[5000];           //车牌号
	int front, rear;         //队首和队尾指针
} SqQueue;
typedef struct
{
	string CarNo[5000];           //车牌号
	int CarTime[5000];         //进场时间
	int top;                //栈指针
} SqStack;                  //定义顺序栈类型,用于描述停车场

void InitStack(SqStack *&s)                     //构造一个空栈
{
	s = new SqStack;										//构造一个空栈
	s->top = -1;											//栈顶指针指向-1
}
int Push(SqStack *&s, string e1, int e2)	             //顺序栈入栈操作
{
	s->top++;
	s->CarNo[s->top] = e1;
	s->CarTime[s->top] = e2;								
	return 1;
}
int Pop(SqStack *&s, string &e1, int &e2)	                //顺序栈出栈操作
{
	if (s->top == -1)										//判断栈是否是空栈
		return 0;
	e1 = s->CarNo[s->top];									//将栈顶元素出栈
	e2 = s->CarTime[s->top];								//将栈顶元素出栈
	s->top--;												//栈顶指针减一
	return 1;
}
void InitQueue(SqQueue *&q)		                         //初始化一个循环队列(候车处)
{
	q = new SqQueue;
	q->front = q->rear = 0;									//队头和队尾的指针置为0,表示队列为空
}
int enQueue(SqQueue *&q, string e)	                  //进队
{

	q->rear = q->rear + 1;									//入队就是在队尾插入一个新元素。队尾指针加一
	q->CarNo[q->rear] = e;									//元素入队
	return 1;
}
int deQueue(SqQueue *&q, string &e, string b)           //出队
{
	if (q->front == q->rear)                         //队空的情况
		return 0;
	for (int i = q->front; i <= q->rear; i++)
	{
		if (q->CarNo[i] == b)							//表示找到了
		{
			if (i == q->front)								//如果找到的元素是在队头
			{
				q->front = q->front + 1;					//对头指针向后加一
				e = q->CarNo[q->front];						//元素出队
				return 1;
			}
			else if (i == q->rear)							//如果找到的元素是在队尾
			{
				e = q->CarNo[q->rear];						//队尾元素出队
				q->rear--;									//队尾指针向前减一
				return 1;
			}
			else                                          //如果不是在头也不是在尾
			{
				e = q->CarNo[i];                          //元素出队
				for (int j = i; j < q->rear; j++)
					q->CarNo[j] = q->CarNo[j + 1];       //将出队元素后面的元素向前移动一位
				    q->rear--;								//队尾指针向前移动一位
				return 1;
			}
			break;
		}
	}
}

void carsystem(SqStack *St,SqStack *St1,SqQueue *Qu)
{
	int e2;        //出车时间 
	int i, j, t, n, q;		
	int time;				
	string a;				
	string b;				
	string e1;      //车牌 
	cin >> n >> q;			
	int x = 0, y = 0, f = 0;		
	for (int k = 0; k < q; k++)
	{

		cin >> a >> b >> time;
		if (a == "arrive")
		{

			if (x < n)
			{
				x++;
				Push(St, b, time);
				cout << "Stack " << x << endl;
			}
			else
			{
				y++;
				enQueue(Qu, b);
				cout << "Queue " << y << endl;
			}
		}
		else if (a == "leave")
		{
			for (i = 0; i < x; i++)
			{
				if (St->CarNo[i] == b)
					break;
			}
			if (i == x)
			{
				deQueue(Qu, e1, b);
				cout << f;
				if (k < (q - 1))
					cout << endl;
				y--;
			}
			else
			{
				x--;
				t = St->top - i;  
				for (j = 0; j < t; j++)
				{
					Pop(St, e1, e2);
					Push(St1, e1, e2);    //把后面的车弄进进辅助栈
					cout << e1 << ":out ";
				}
				Pop(St, e1, e2);             //该汽车离开
				cout << e1 << ":out ";
				int p = time - e2;
				while (St1->top != -1)    //将临时栈St1重新回到St中
				{
					Pop(St1, e1, e2);
					Push(St, e1, e2);
					cout << e1 << ":in ";
				}if (Qu->front != Qu->rear)
				{
					deQueue(Qu, e1, Qu->CarNo[Qu->front]);
					Push(St, e1, time);
					y--; x++;
					cout << e1 << ":in ";
				}
				cout << p;
				if (k < (q - 1))
					cout << endl;   
			}
		}
	}
}


int main()
{
	SqStack *St, *St1;
	SqQueue *Qu;
	InitStack(St);
	InitStack(St1);
	InitQueue(Qu);
	carsystem( St, St1,Qu);
	
}


哈夫曼编码/译码系统(树应用)


 
#include<iostream>
#include<map>
#include<cstring>
#include<algorithm>
#include<string>

using namespace std;
#define MAXSIZE 1111                                       /*可传递信息的最大长度*/
//结构
int kind;
string jiema = "";
typedef struct {
	int weight;                                           /*结点的权值*/
	int parent, lchild, rchild;                             /*结点的双亲、左孩子、右孩子的下标*/
	char ch;                                              /*结点的字符*/
	string HuffmanCode;                                   /*结点的哈夫曼编码*/
}HTNode, *HuffmanTree;                                     /*动态分配数组储存哈夫曼树*/
map<char, int> Map;                                        /*map容器储存字符及对应次数*/
//子函数
void GetInformation(int &kind);                           /*统计字符种类数和各字符出现的次数*/
void SelectMinNode(HuffmanTree &HT, int k, int &s1, int &s2);/*选择两个权值最小结点*/
bool CreatHuffmanTree(HuffmanTree &HT, int n);             /*哈夫曼树的创建*/
void CreatHuffmanCode(HuffmanTree &HT, int n);             /*哈夫曼编码的创建*/
void Translate(HuffmanTree HT, int n);                     /*译码*/
//具体内容
char str1[MAXSIZE];

void GetInformation(int &kind)
{
	kind = 0;
	cin>>str1;                                            /*可读入含空格道德字符串*/
	for (int i = 0; i < strlen(str1); ++i)
		Map[str1[i]]++;                                    /*记录字符出现次数*/
	map<char, int> ::iterator it = Map.begin();
	while (it != Map.end())
	{
		++kind;
		it++;
	}                                                     /*迭代器遍历*/
}
void SelectMinNode(HuffmanTree &HT, int k, int &s1, int &s2)
{
	int Min = 1e9;                                          /*初始化当前最小权值*/
	for (int i = 1; i <= k; ++i)
		if (!HT[i].parent)
			if (HT[i].weight <= Min)
			{
				Min = HT[i].weight;                         /*不断更新Min*/
				s1 = i;
			}                                             /*从前k个结点找出最小结点*/
	Min = 1e9;                                              /*重置权值*/
	for (int i = 1; i <= k; ++i)
		if (!HT[i].parent&&i != s1)
			if (HT[i].weight <= Min)
			{
				Min = HT[i].weight;                         /*不断更新Min*/
				s2 = i;
			}                                             /*从前k个结点除s1外找出最小结点*/
}
bool CreatHuffmanTree(HuffmanTree &HT, int n)
{
	if (n <= 1)
	{
		return false;
	}
	else
	{
		int m = 2 * n - 1;
		HT = new HTNode[m + 1];                                 /*分配空间,0号单元未使用*/
		map<char, int> ::iterator it = Map.begin();
		int j = 0;
		while (it != Map.end())
		{
			++j;
			HT[j].weight = (*it).second;                      /*初始化字符出现次数*/
			HT[j].ch = (*it).first;                           /*初始化字符*/
			HT[j].HuffmanCode = "";                           /*初始化哈夫曼编码为空*/
			HT[j].lchild = HT[j].rchild = HT[j].parent = 0;       /*左右孩子及双亲均初始化为0*/
			++it;
		}
		for (int i = n + 1; i <= m; ++i)
			HT[i].weight = HT[i].parent = HT[i].rchild = HT[i].lchild = 0;
		/*初始化从n+1到m的结点*/
		for (int i = n + 1; i <= m; ++i)                             /*n-1次的选择删除合并来创建哈夫曼树*/
		{
			int s1, s2;
			SelectMinNode(HT, i - 1, s1, s2);                    /*将最小两个结点的下标赋予s1,s2;*/
			HT[s1].parent = i;                                /*s1双亲亲赋予i第i编号*/
			HT[s2].parent = i;                                /*s2双亲亲赋予i第i编号*/
			HT[i].lchild = s1;                                /*s1成为编号为i的左孩子*/
			HT[i].rchild = s2;                                /*s2成为编号为i的右孩子*/
			HT[i].weight = HT[s1].weight + HT[s2].weight;       /*合并左右孩子权值*/
		}
		return true;
	}
}
void CreatHuffmanCode(HuffmanTree &HT, int n)
{
	int c, f;
	int chang=0;
	int sum = 0;
	
	for (int i = 1; i <= n; i++)
	{
		c = i;                                                /*c:当前结点序号*/
		f = HT[i].parent;                                     /*f:当前节点的双亲*/
		while (f != 0)                                         /*f为根节点终止*/
		{
			if (HT[f].lchild == c)HT[i].HuffmanCode += "0";      /*左边加0*/
			else HT[i].HuffmanCode += "1";                    /*右边加1*/
			c = f;
			f = HT[f].parent;
			/*不断向上层查找*/
		}
		reverse(HT[i].HuffmanCode.begin(), HT[i].HuffmanCode.end());
		/*翻转,因为上步操作得到的是逆序*/
		//cout << HT[i].ch << ":" << HT[i].HuffmanCode << endl;
		chang = HT[i].weight*(HT[i].HuffmanCode.length());
		sum += chang;
	}
	cout << sum;
	cout << endl;
	for (int i = 0; i < strlen(str1); i++)
	{
		for(int j=0;j<= kind;j++)
		if (HT[j].ch == str1[i]) 
		{ 
			jiema += HT[j].HuffmanCode;
		}

		
	}
	//cout << jiema << endl;
}
void Translate(HuffmanTree HT, int n)
{
	
	int len = jiema.size();
	int flag = -1;                                            /*判断译码是否成功的标志*/
	string ans="", temp="";                                        /*ans:最终译码答案*/													/*temp:匹配临时字符串*/
	for (int i = 0; i < len; ++i)
	{
		temp += jiema[i];
		for (int j = 1; j <= n; ++j)
		 	if (HT[j].HuffmanCode == temp)
			{
				temp = "";                                  /*匹配成功将临时字符串置空*/
				ans += HT[j].ch;
				flag = i;
			} 
		
	}
	cout << ans.size();
	
	
}
int main()
{
	HuffmanTree HT;
	
	GetInformation(kind);
	if (CreatHuffmanTree(HT, kind))
	{
		CreatHuffmanCode(HT, kind);
		Translate(HT, kind);
	}

	return 0;
}



校园导游系统(图的应用)


#include<cstdio>
#include<iostream>
#include<cstring>
#define inf 5000
using namespace std;

const int Max_n = 1000;

typedef struct {//景点相关信息 
	string name;//景点名称 
	string number;//景点代号
	string Introduce;
}DataType;

typedef struct {//景点 
	int num;//编号
	DataType data;//相关信息 
}ScSpot;

typedef struct {//
	ScSpot Scspot[21];//十一个景点,0号位置不使用
	int D[21][21];//构建矩阵,景点间的距离 
}UDG;//这个类型包含了各个景点的关系,各个景点的基本信息 

UDG G;
int Path[21][21] = { 0 };
int D[21][21] = { 0 };
int n = 0;
//初始化景点信息/
	void InitMes() {
		int m = 0;
		int u = 0, v = 0, w = 0;
		int j=0;
		cin >> n >> m;
		for (int i = 1; i <= n; i++)
		{
			cin >> G.Scspot[i].data.number >> G.Scspot[i].data.name >> G.Scspot[i].data.Introduce;

		}

		for (int i = 1; i <= n; i++) 
		{
			for (int j = 1; j <= n; j++)
			{
				if (i == j)
				{
					G.D[i][j] = 0;
					D[i][j] = 0;
				}
				else
				{
					G.D[i][j] = inf;
					D[i][j] = inf;
				}
			}
		}
		for (int j = 1; j <= m; j++)
		{
			cin >> u >> v >> w;
			G.D[u][v] = w;
			G.D[v][u] = w;

		}

}
//修改景点信息

//修改景点信息
void  ChangeMes()
{
	string chose;
	cin >> chose;
	for (int i = 1; i <= n; i++)
	{
		if (G.Scspot[i].data.name == chose)
		{
			cin >> G.Scspot[i].data.number >> G.Scspot[i].data.name >> G.Scspot[i].data.Introduce;

		}
	}
}
//查看校园平面图/
 
//查看景点信息/

//查询景点信息/
	void ViewMes()
	{

		string chose;
		cin >> chose;
		for (int i = 1; i <= n; i++)
		{
			if (G.Scspot[i].data.name == chose)
			{
				cout << G.Scspot[i].data.number << " " << G.Scspot[i].data.name << " " << G.Scspot[i].data.Introduce << endl;
				break;			
			}
		}
}
//求出任意两点之间的最短距离/
void ShortestPath_Floyd() {
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			D[i][j] = G.D[i][j];
			if (D[i][j] < inf&&i != j) Path[i][j] = j;//两顶点有路,后继置为j 
			else Path[i][j] = -1;
		}
	}
	for (int k = 1; k <= n; k++) {
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= n; j++) {
				if (D[i][k] + D[k][j] < D[i][j]&&i != j) {//存在更短的路径 
					D[i][j] = D[i][k] + D[k][j];//更新值
					Path[i][j] = Path[i][k];//更改i的后继为k 
				}
			}
		}
	}
}

//打印路径的函数
void OutPath(int s, int e) {
	int next = s;
	//printf(">>路径为:");
	while (next != e) {
		cout << G.Scspot[next].data.name << "->";
		next = Path[next][e];									//当前的后继点的编号 
	}
	cout << G.Scspot[e].data.name << endl;

}
//打印最短路径及最短距离
void PrintPath() 
{
	ShortestPath_Floyd();//预处理所有的最短路径
	int s, e;
	string name1, name2;
	cin >> name1 >> name2;
	for (int i = 1; i <= n; i++)
	{
		if (G.Scspot[i].data.name == name1)
		{
			s = i;
			break;
		}
	}
	for(int i = 1; i <= n; i++)
	{
		if (G.Scspot[i].data.name == name2)
		{
			e = i;
			break;
		}
	}
	
	if (s <= 20 && s >= 0 && e <= 20 && e >= 0) {
		cout << D[s][e] << endl;
		OutPath(s, e);
	}

}
void Menu() {
	int n = 0;
	int m = 0;
	string x;
	cin >> n;
	for(int ji=1;ji<=n;ji++)
	{
		cin >> x ;
		if (x == "Query")
		{
			cin >> m;
			if (m == 1)
			{
				ViewMes();
			}
		
			if (m == 2)
			{

				PrintPath();
				  
			}

		}
		else if (x == "Modify")
		{
			ChangeMes();
		}

	}
	
}

int main() {
	InitMes();
	Menu();
	return 0;
}





药店的药品销售统计系统(排序应用)


#include<cstdio>
#include<iostream>
#define MaxSize 100
#define OVERFLOW -2
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;

typedef struct node {
	char num[10];   药品编号
	char name[10]; 药品名称
	double price;   药品单价
	int count;     销售数量
	double sale;    本药品销售额
}DataType;

//存储药品信息的顺序表的定义:
typedef struct {
	DataType r[MaxSize];
	int length;
}SequenList;

SequenList L;

/初始化一个空表
void InitList() {
	//	L.r=new DataType[MaxSize];//为顺序表分配一个大小为MaxSize的空间 
	if (!L.r) exit(OVERFLOW);
	L.length = 0;//空表长度为0 
}

/打印药品信息
void PrintMes() {
	for (int i = 1; i <= L.length; i++) {
		printf("%s\t%s\t%.1lf\t%d\t%.1lf\n", L.r[i].num, L.r[i].name, L.r[i].price, L.r[i].count, L.r[i].sale);
	}
}

交换两个药品的信息
void Swap(int i, int j) {
	//临时变量 
	char num[10];    药品编号
	char name[20];  药品名称
	double price;   药品单价
	int count;      销售数量
	double sale;     药品销售额
	//交换编号,名字 
	strcpy(num, L.r[i].num); strcpy(L.r[i].num, L.r[j].num); strcpy(L.r[j].num, num);
	strcpy(name, L.r[i].name); strcpy(L.r[i].name, L.r[j].name); strcpy(L.r[j].name, name);
	//交换单价,数量,销售额 
	price = L.r[i].price; L.r[i].price = L.r[j].price; L.r[j].price = price;
	count = L.r[i].count; L.r[i].count = L.r[j].count; L.r[j].count = count;
	sale = L.r[i].sale; L.r[i].sale = L.r[j].sale; L.r[j].sale = sale;
}

/**从文件中读取药品信息并输出
void ScanMes() {
	FILE *fp;
	char num[10];  /*文件中编号
	char name[20]; /*文件中名称
	double price;  /*文件中单价
    int count;    /*文件中数量
	double sale;  /*文件中销售额
	cin >> L.length;
	for (int i = 1; i <= L.length; i++)
	{
		cin >> L.r[i].num >> L.r[i].name >> L.r[i].price >> L.r[i].count;
		L.r[i].sale = (L.r[i].count)*(L.r[i].price);
	}
	
}

/**按照药品编号排序(直接插入排序)(排序qwq)
void Distribute() {
	int j = 0;
	for (int i = 2; i <= L.length; i++)
	{
		if (strcmp(L.r[i - 1].num, L.r[i].num) > 0)
		{
			L.r[0] = L.r[i];
			L.r[i] = L.r[i - 1];
			for (j = i - 2; strcmp(L.r[j].num, L.r[0].num) > 0; j--)
			{
				L.r[j + 1] = L.r[j];
			}
			L.r[j + 1] = L.r[0];
		}
	}
	

	PrintMes();
	
}

/**按照药品单价排序(冒泡排序)
void BubbleSort() {
	for (int i = 1; i <= L.length - 1; i++) {
		for (int j = 1; j <= L.length - i; j++)
			if (L.r[j].price > L.r[j + 1].price)
				Swap(j, j + 1);
	}
	
	PrintMes();
	
}

void SimpleSort()
{
	DataType t;
	int k;
	for (int i = 1; i < L.length; i++)
	{

		k = i;
		for (int j = i+1; j <= L.length; j++)
			if (strcmp(L.r[k].name,L.r[j].name)>0)
			  k = j;
			if(k!=i)
			{
				t = L.r[k];
				L.r[k] = L.r[i];
				L.r[i] = t;
			}
		
	}
	PrintMes();
	
	
}


/**进行一次排序,返回枢轴位置
int Partition(int left, int right) {//对顺序表进行一次排序,返回枢轴位置 
	L.r[0] = L.r[left];//将枢轴信息放在0号位置
	int pivotkey = L.r[left].count;//记录枢轴的销售量 
	while (left < right) {
		while (left < right&&L.r[right].count >= pivotkey) right--;//右移,直到小于枢轴的销售量 
		L.r[left] = L.r[right];//覆盖 
		while (left < right&&L.r[left].count <= pivotkey) left++;//左移,直到大于枢轴的销售量 
		L.r[right] = L.r[left];//覆盖 
	}
	L.r[left] = L.r[0];
	return left;
}

/**对顺序表进行排序
void QSort(int left, int right) {
	if (left < right) {//长度大于1 
		int pivotloc = Partition(left, right);//记录返回的枢轴的位置 
		QSort(left, pivotloc - 1); //左子表排序
		QSort(pivotloc + 1, right);//右子表排序 
	}
}

/**按照销售量排序(快速排序)
void QuickSort() {//对L进行快速排序 
	QSort(1, L.length);
	
	PrintMes();
	
}

/**筛选法调整堆
void HeapAdjust(int s, int m) {
	//s根结点,m序列长度 
	L.r[0] = L.r[s];//保存根结点信息 
	for (int i = 2 * s; i <= m; i *= 2) {//沿着销售额较大的孩子结点向下筛选 
		if (i < m&&L.r[i].sale < L.r[i + 1].sale) i++;
		if (L.r[0].sale >= L.r[i].sale) break;//根结点的销售额大于两个孩子,0号位置的信息应该在s位置上 
		L.r[s] = L.r[i]; s = i;//否则0号位的信息应该在i,更新根结点,继续找他的左右堆,知道找到叶子结点,一次调整完毕 
	}
	L.r[s] = L.r[0];
}

/**建立大根堆
void CreatHeap() {
	int n = L.length;
	//所有大于n/2的位置上都是叶子结点,我们从叶子最后一个非叶子结点筛选 
	for (int i = n / 2; i > 0; i--) {
		HeapAdjust(i, n);//不断的调整堆 
		//PrintMes(); 
	}

}

/**按照销售额排序(堆排序)
void HeapSort() {
	CreatHeap();//把无序序列建成大根堆 
	//打印大根堆 
	//PrintMes();
	for (int i = L.length; i > 1; i--) {
		//1:堆顶元素的位置,i:未排序的最后一个 
		Swap(1, i);//将堆顶元素与没有排序(1-i)的的最后一个记录交换 
		HeapAdjust(1, i - 1);//除了堆顶信息,剩下的继续调整为大根堆 
	}
	
	PrintMes();
	
}
/**主菜单
void Menu() {

		Distribute();//直接插入
		cout << endl;
		SimpleSort();//选择排序
		cout << endl;
		BubbleSort();//冒泡排序
		cout << endl;
		QuickSort(); //快速排序
		cout << endl;
		HeapSort();  //根堆排序
		cout << endl;
}

int main() {
	InitList();
	ScanMes();
	Menu();
	return 0; 
}


敢死队问题(*)


//顺序表约瑟夫环
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 100
typedef struct List
{
    int data[MaxSize];
    int length;

}*Sqlist;
void InitList(Sqlist &L)
{
    L=(Sqlist)malloc(sizeof(Sqlist));
    L->length=0;
}
void CreateList(Sqlist &L,int n)
{
    printf("最初的顺序表为:");
    for(int i=0;i<n;i++)
    {
        L->data[i]=i+1;          //对数组赋值
        printf("%d ",L->data[i]);
    }
    L->length=n;
    printf("\n");
}
int ListDelete(int a,Sqlist &L,int m)
{
    int i,j,t;
    t=a-1;
    for(i=L->length;i>1;i--)
    {
        t=(t+m-1)%i;
        if(L->data[t]==1&&i>1)
            return 0;
        for(j=t;j<i-1;j++)
        {
            L->data[j]=L->data[j+1];
        //删除下标为j的数
        }


    }
    return 1;
    printf("\n");
}
int main()
{
    int n,m=5;
    printf("请总人数:");
    scanf("%d",&n);
    Sqlist L;
    InitList(L);
    CreateList(L,n);
    for(int a=1;a<=n;a++)
    {
   if( ListDelete(a,L,m)==1)
    printf("从%d号开始",a);
    }
    return 0;
}


//链式约瑟夫环问题
#include<stdio.h>
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
#include<stdbool.h>

struct List {
    int data;
    struct List *next;
};

struct List * CreatList ()//建立空链表
{
    struct List *L;
    L=(struct List *)malloc(sizeof(struct List));
    if (L==NULL)
    {
        printf("内存分配失败\n");
        exit(0);
    }
    L->next=NULL;
    return L;

}

 void InitList (struct List *L,int n)//将L初始化为无 头结点的链表
{
    struct List *P,*T ;
    int i;
    T=L;
    T->data=1;
    T->next=NULL;
    for ( i=2;i<=n;i++)
    {
        P=(struct List *)malloc(sizeof(struct List ));//申请空间
        if (P==NULL)// 如果不成功,则退出程序
        {
            printf("内存分配失败\n");
            exit(0);
        }
        P->data=i;
        P->next=NULL;
        T->next=P;
        T=P;
    }
    P->next=L;
}

bool deleteNode(int m,int n)
{

    struct List *L,*p,*t;
    L=CreatList();//由于在删除节点的时候,会破坏数据个原本的结构,所以每一次在函数里初始化
    InitList(L,m);
    p=L;
    int i,j;
    for(int i=1;i<n;i++)//将指针p移动到开始数的地方
    {
          p=p->next;
    }
    for (j=0;j<m-1;j++)//最多需要移除m-1个人
    {
        for (i=1;i<4;i++)//将p移动到待移除人的前一个位置
        {
            p=p->next;
        }
        if (p->next->data==1)//如果要一处的人是排长,则返回假
        {
            return false;
        }
        else//否则,将该人移除
        {
            t= p->next;
            p->next=t->next;
            p=p->next;//并从下一个人开始数
            free(t);
        }
    }
    return true;//如果只剩排长一人 ,返回真
}
void  answer()
{
    int m,n;
    printf("输入总人数\n");
    scanf("%d",&m);
    for (n=1;n<=m;n++)
    {
        if (deleteNode(m,n)==true)
        printf("从第%d个数\n ",n);
    }
}
int main()
{
    answer();
    return 0;
}


商品存货管理系统(**)

#include<iostream>
using namespace std;
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct data {
	int amount; // 数量
	char name[20]; // 名称
	char num[20]; // 编号
	char scost[20];//价格
	char overtime[20];//出厂时间

}food[100];

int S = 0; //货物数

//入库函数

void input()
{
	int i = 0;
	char c;
	while (1)
	{

		cout<<" 物品"<<++S<<endl;
		cout<<"请输入物品编号";
		cin >> food[i].num;

		cout<<"请输入物品名称:";
		cin >> food[i].name;

		cout<<"请输入物品数量:";
		cin >> food[i].amount;

		cout << "请输入物品价格:";
		cin >> food[i].scost; 
		
		cout << "请输入物品出厂时间:";
		cin >> food[i].overtime;
		cout << "是否继续入库(Y / N) :";
		cin >> c;

		i++;
		if (c == 'N' || c == 'n')
		{
			int j = 0;
			for (int i = 1; i < S; i++)
			{
				
				if (strcmp(food[i - 1].num, food[i].num) > 0)
				{
					food[99] = food[i];
					food[i] = food[i - 1];
					for (j = i - 2; strcmp(food[j].num, food[99].num) > 0; j--)
					{
						food[j + 1] = food[j];
					}
					food[j + 1] = food[99];
				}
			}
			break;
		}
	}
}

//出库函数

void output()
{
	int a, c, i, j;
	char b[20];
	char d;
	char _name[20];
	while (1)
	{
		system("cls");
		cout << "\t\t1、按编号出库" << endl;
		cout<<"\t\t2、按名称出库"<<endl;
		cout<<"\t\t0、返回"<<endl;
		cout<<"\t\t请选择:";
		cin >> a;
		if (a == 1)
		{
			cout<<"请输入出货货物的编号:";
			cin >> b;
			cout<<endl;
		for (i = 0; i < S; i++)
			{
				if (b == food[i].num) 
					j = i;
			}
			if (food[j].amount > 0)
			{
				cout << "请输入出货量";
				cin >> c;


				if (c > food[j].amount)
					cout << "货物不足" << endl;

				else
				{
					food[j].amount -= c;
					cout << "编号" << food[j].num << "货物出货成功" << endl;
					cout << "货物剩余" << food[j].amount << endl;

				}
			}
			else
			{
				printf("此货物为零\n");
			}
		}
		if (a == 2)
		{
			printf("请输入出货货物的名称:");
			scanf("%s", _name);
			printf("\n");
			for (i = 0; i < S; i++)
			{
				if (strcmp(food[i].name, _name) == 0) j = i;
			}
			if (food[j].amount > 0)
			{
				printf("请输入出货量:");
				scanf("%d", &c);
				if (c > food[j].amount)
					printf("货物不足\n");
				else
				{
					food[j].amount -= c;
					printf("编号%s货物成功出货!\n货物剩余%d\n", food[j].num, food[j].amount);
				}
			}
			else
			{
				printf("此货物为零\n");
			}
		}
		else if (a == 0) break;
		printf("是否继续出库(Y / N) :");
		scanf(" %c", &d);
		if (d == 'N' || d == 'n') break;
	}
}
//货物出库和入库

void current()
{
	int a;

	printf("\t\t1、货物入库\n");
	printf("\t\t2、货物出库\n");
	printf("\t\t0、返回\n");
	printf("\t\t请选择:");
	scanf("%d", &a);
	if (a == 1)
	{
		input();
	}
	else if (a == 2)
	{
		output();
	}
}
//查找函数

void search()
{
	int i, a;
	char b[20];
	char c;
	char _name[20];
	while (1)
	{

		printf("\t\t1、按编号查找\n");
		printf("\t\t2、按名称查找\n");
		printf("\t\t0、返回\n");
		printf("\t\t请选择:");
		scanf("%d", &a);
		if (a == 1)
		{
			printf("请输入需要查询货物的编号:");
			scanf("%d", &b);
			printf("\n");
			for (i = 0; i < S; i++)
			{
				if (b == food[i].num) break;
			}
			if (i < S)
				printf("编号:%s\n名称:%s\n数量:%d\n价格:%s\n出厂时间:%s\n", food[i].num, food[i].name, food[i].amount,food[i].scost,food[i].overtime);
			else
				printf("未找到此编号\n");
			printf("是否继续查找(Y / N) :");
			scanf("%c", &c);
			if (c == 'N' || c == 'n') break;
		}
		else if (a == 2)
		{
			printf("请输入需要查询货物的名称:");
			scanf("%s", &_name);
			printf("\n");
			for (i = 0; i < S; i++)
			{
				if (strcmp(_name, food[i].name) == 0) break;
			}
			if (i < S)
				printf("编号:%s\n名称:%s\n数量:%d\n价格:%s\n出厂时间:%s\n", food[i].num, food[i].name, food[i].amount, food[i].scost, food[i].overtime);
			else
				printf("未找到此名称\n");
			printf("是否继续查找(Y / N) :");
			scanf(" %c", &c);
			if (c == 'N' || c == 'n') break;
		}
		else if (a == 0) break;

	}



}
//显示函数

void show()
{
	int i;
	char c;
	printf("\t编号\t名称\t数量\t价格\t出厂时间\n");
	for (i = 0; i < S; i++)
	{
		printf("\t%s\t%s\t%d\t%s\t%s\t\n", food[i].num, food[i].name, food[i].amount, food[i].scost, food[i].overtime);
	}
	printf("\n");
	printf("按回车键返回!");
	c = getchar();
	c = getchar();
}

char file[30]; //文件名,全局变量

//打开文件

void open()
{
	FILE *fp;
	int i;
	char c;
	system("cls");
	
	
	if ((fp = fopen("D:\\abc.txt", "rb")) == NULL)
	{
		printf("cannot open infile\n");
	}
	if (fread(&S, sizeof(int), 1, fp) != 1)
	{
		if (feof(fp))
		{
			fclose(fp);
			return;
		}
		printf("文件读取错误!\n");
	}
	for (i = 0; i < S; i++)
	{
		if (fread(&food[i], sizeof(struct data), 1, fp) != 1)
		{
			if (feof(fp))
			{
				fclose(fp);
				return;
			}
			printf("文件读取错误!\n");
		}
	}
	fclose(fp);
	printf("文件打开成功!\n\n");
	printf("按回车键返回!");
	c = getchar();
	
}

//输出到文件

void save()
{
	FILE *fp;
	int i, a;
	char c;

	printf("1、储存\n");
	printf("2、另存为\n");
	printf("0、返回\n");
	printf("请选择:");
	scanf("%d", &a);
	if (a == 1)
	{
		if ((fp = fopen("D:\\abc.txt", "wb")) == NULL)
		{
			printf("cannot open file\n");
		}
		fprintf(fp, "%d\n", S);//循环写入数据
		int i=0;
		while (i < S)
		{
			fprintf(fp, "%s  %s  %d  %s  %s\n", food[i].num, food[i].name, food[i].amount, food[i].scost, food[i].overtime);
			i++;
		}
		fclose(fp);//关闭文件
		printf("\t\t\tSuccessed!\n");//返回成功
	/*
		if (fwrite(&S, sizeof(int), 1, fp) != 1)
		{
			printf("写入文件错误!\n");
		}
		for (i = 0; i < S; i++)
		{
			if (fwrite(&food[i], sizeof(struct data), 1, fp) != 1)
			{
				printf("写入文件错误!\n");
			}
		}
		*/
/*
		fclose(fp);
		printf("保存成功!\n\n");
		printf("按回车键返回!");
		c = getchar();
		c = getchar();
	}
	else if (a == 2)
	{
	
		
		if ((fp = fopen("D:\\abc.txt", "wb")) == NULL)
		{
			printf("cannot open file\n");
		}
		if (fwrite(&S, sizeof(int), 1, fp) != 1)
		{
			printf("写入文件错误!\n");
		}
		for (i = 0; i < S; i++)
		{
			if (fwrite(&food[i], sizeof(struct data), 1, fp) != 1)
			{
				printf("写入文件错误!\n");
			}
		}
		fclose(fp);
		printf("保存成功!\n\n");
		printf("按回车键返回!");
		c = getchar();
		c = getchar();
	}
}

int main()
{
	int a;
	///主菜单
	while (1)
	{
		system("cls");
		printf("\t\t\t\t* * * * * * * * * * * * * * * * \n");
		printf("\t\t\t\t * 1、货物出库和入库 * \n");
		printf("\t\t\t\t * 2、查找货物表 * \n");
		printf("\t\t\t\t * 3、显示仓库货物表 * \n");
		printf("\t\t\t\t * 4、输出到文件 * \n");
		printf("\t\t\t\t * 5、打开文件 * \n");
		printf("\t\t\t\t * 0、退出 * \n");
		printf("\t\t\t\t* * * * * * * * * * * * * * * * \n");
		printf("\t\t\t\t请选择:");
		scanf("%d", &a);
		switch (a)
		{
		case 1: current(); break;
		case 2: search(); break;
		case 3: show(); break;
		case 4: save(); break;
		case 5: open(); break;
		case 0: exit(0);
		}
	}
	return 0;
}


二叉排序树的基本操作(**)


#include <iostream>
using namespace std;

typedef struct BSTnode {
	int num;
	struct  BSTnode *lchild, *rchild;
}BSTnode, *Btree;
Btree SearchBST(Btree T, int key) {
	if (!T || key == T->num) {
		return T;
	}
	else if (key < T->num) {
		return SearchBST(T->lchild, key);
	}
	else 
	{
		return SearchBST(T->rchild, key);
	}
	if (T)
		return 0;

}
//插入
void InsertBSF(Btree &T, int e) {
	if (!T) {
		BSTnode *s;
		s = new BSTnode;
		s->num = e;
		s->lchild = s->rchild = NULL;
		T = s;
	}
	else if (e < T->num) {
		InsertBSF(T->lchild, e);
	}
	else {
		InsertBSF(T->rchild, e);
	}
}
//创建
void CreatBST(Btree &T) {
	int num;
	T = NULL;
	cin >> num;
	while (num != 0) {
		InsertBSF(T, num);
		cin >> num;
	}
}
void DeleteBSF(Btree &T, int key) {
	BSTnode *s, *p, *f, *q;
	s = new BSTnode;
	p = new BSTnode;
	p = T;
	f = NULL;
	while (p)
	{
		if (p->num == key) {     //找到关键字等于key的节点*p,结束循环
			break;
		}
		//
		f = p;
		if (p->num > key) {
			p = p->lchild;
		}
		else p = p->rchild;
	}
	if (!p) return;
	q = p;  //被删节点*p有三种情况,1.只有左子树2.只有右子树3.左右都有
	if ((p->lchild) && p->rchild) {   //左右都有
		s = p->lchild;
		while (s->rchild) {
			q = s;
			s = s->rchild;
		}
		p->num = s->num;
		if (q != p) {
			q->rchild = s->lchild;
		}
		else {
			q->lchild = s->rchild;
		}
		delete s;
		return;
	}
	else if (!p->rchild) {  //被删节点*p无右子树,只需重接左子树
		p = p->lchild;
	}
	else if (!p->lchild) {  //被删节点*p无左子树,只需重接右子树
		p = p->rchild;
	}
	//将删除节点p所指的子树接到双亲节点*f的位置
	if (!f) T = p;
	else if (q == f->lchild) f->rchild = p;
	else f->rchild = p;
	delete q;
}
int main()
{

	cout << "建立二叉排序树,以0为结束符号" << endl;
	Btree T;
	CreatBST(T);
	cout << "请输入指令完成功能" << endl << "查找元素:1" << endl << "插入元素:2" << endl << "删除元素:3"<<endl<<"退出程序:0"<<endl;
	while (1) {
		int n=0;
		
		cin >> n;
		switch (n) {
		case 1:
			cout << "二叉排序树查找" << endl;
			int key;
			cin >> key;
			cout << "查到的结果是:" << endl;
			if (SearchBST(T, key) == NULL) {
				cout << "未查到" << endl;
			}
			else {
				cout << SearchBST(T, key)->num << endl;
			}
			break;
		case 2:
			cout << "请输入要插入的数:" << endl;
			int insert;
			cin >> insert;
			InsertBSF(T, insert);
			break;
		case 3:
			cout << "请输入要删除的数:" << endl;
			int del;
			cin >> del;
			DeleteBSF(T, del);
			break;
		case 0:
			exit(0);
			
		}
	}
	
}




最小生成树问题(***)



#include <iostream>
using namespace std;

typedef struct {
	int begin;    //边的起点
	int end;      //边的终点
	int  quan;    //边的权值
}Bian;
typedef struct {
	int dian[30];
	Bian bian[30];
	int diannum, biannum;
	int arcs[100][100];                       //邻接矩阵
}AMGraph;
int Locatedian(AMGraph G, int u) {    //找到在图中点的位置
	for (int i = 0; i < G.diannum; i++) {
		if (G.dian[i] == u) {
			return i;
		}
	}
	return -1;
}
void CreatAMP(AMGraph &G) {
	int m, n;
	cin >> G.diannum >> G.biannum;
	for (int i = 0; i < G.diannum; i++) {
		//	cout << "输入点信息";
		cin >> G.dian[i];
	}
	for (int i = 0; i < G.diannum; i++)//初始化邻接矩阵,边的权值
		for (int j = 0; j < G.diannum; j++)
		{
			G.arcs[i][j] = 9999;//均置为极大值
			if (i == j)
				G.arcs[i][j] = 0;//i与j相等初始化为0
		}
	for (int i = 0; i < G.biannum; i++) {
		//	cout << "输入边信息";
		cin >> G.bian[i].begin >> G.bian[i].end >> G.bian[i].quan;
		m = Locatedian(G, G.bian[i].begin);
		n = Locatedian(G, G.bian[i].end);
		G.arcs[m][n] = G.bian[i].quan;
		G.arcs[n][m] = G.bian[i].quan;
	}
}
void sort(AMGraph &G) {  //冒泡排序法
	int m;
	m = G.biannum - 1;
	Bian t;
	while (m > 0) {
		for (int j = 0; j < m; j++) {
			if (G.bian[j].quan > G.bian[j + 1].quan) {
				t = G.bian[j];
				G.bian[j] = G.bian[j + 1];
				G.bian[j + 1] = t;
			}
		}
		m--;
	}
}
int liantong[100];
void Mintree_kruskal(AMGraph G) {
	int v1, v2;
	int vs1, vs2;
	sort(G);              //将G中边数组按权值从小到大排列
	for (int i = 0; i < G.diannum; i++) {
		liantong[i] = G.dian[i];
	}
	for (int i = 0; i < G.biannum; i++) {
		v1 = G.bian[i].begin;
		v2 = G.bian[i].end;
		vs1 = liantong[v1 - 1];         //点v1所对应的连通分量
		vs2 = liantong[v2 - 1];         //点v2所对应的连通分量
		if (vs1 != vs2) {       //不在同一个连通分量里
			cout << G.bian[i].begin << "    " << G.bian[i].end << endl;
			for (int j = 0; j < G.diannum; j++) {     //合并连同分量
				if (liantong[j] == vs2) {
					liantong[j] = vs1;   //把vs2的连通分量改成vs1
				}

			}
		}

	}
}

void Prim(AMGraph g)
{
	int v;
	printf("请输入你想从第几个顶点开始生成最小生成树--prim算法\n");
	cin >> v;
	int lowcost[100];
	int min;
	int closest[100], i, j, k;
	for (i = 0; i < g.diannum; i++)
	{
		lowcost[i] = g.arcs[v][i];//将v顶点周围边的权值赋给lowcost,lowcoist[i]表示的是到这个顶点权值最小的边
		closest[i] = v;//存储开始顶点
	}
	for (int i = 1; i < g.diannum; i++)//n-1条边
	{
		min = 99999;
		for (j = 0; j < g.diannum; j++)
		{
			if (lowcost[j] != 0 && lowcost[j] < min)//从顶点周围边中找到一个最小权值的边,这是prim算法的核心思想
			{
				min = lowcost[j];
				k = j;
			}
		}
		//	printf("边(%d,%d)权值为:%d\n", closest[k]+1, k+1, min);//closest[k]是开始顶点,k为结束顶点
		cout << closest[k] + 1 << "    " << k + 1 << "    权值为" << min << endl;
		lowcost[k] = 0;
		for (j = 0; j < g.diannum; j++)
		{
			if (g.arcs[k][j] != 0 && g.arcs[k][j] < lowcost[j])//k顶点此时变为开始顶点,以它为中心去寻找周围权值最小的边
			{
				lowcost[j] = g.arcs[k][j];
				closest[j] = k;//将原本的结束顶点变为开始顶点
			}
		}
	}
}
int main()
{
	int u;
	AMGraph G;
	CreatAMP(G);
	Mintree_kruskal(G);
	//cout << "输入prim算法中的起点";
	//cin >> u;
	Prim(G);
	//Mintree_prim(G, u);
}



  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值