C语言客房管理&酒店管理

#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<iomanip>
#include<fstream>
using namespace std;
string head_1, head_2, head_3,head_4,head_5;
char a[10],b[10],c[10],d[10],e[10];
int length=0;
typedef struct HNode
{
	char roomN[7];	
	float Price;	
	float PriceL;	
	int Beds;		
	char State[5];	
	struct HNode *next;
}Hotel, *HLink;
Hotel *createlink()
{
	HLink head=(Hotel*)malloc(sizeof(Hotel));
	HLink p,q;
	float Price,PriceL;
	int Beds;
	char roomN[10];
	char state[10];
	p=q=head;
	FILE *r= fopen("hotel.txt","r");
	if(r==NULL)
	{
		printf("打开文件失败!");
		return NULL;
	}
	fscanf(r,"%s%s%s%s%s",a,b,c,d,e);
	while(fscanf(r,"%s%f%f%d%s",roomN,&Price,&PriceL,&Beds,state)!=EOF)
	{
		q=(Hotel*)malloc(sizeof(Hotel));
		strcpy(q->roomN,roomN);
		q->Price=Price;
		q->PriceL=PriceL;
		q->Beds=Beds;
		strcpy(q->State,state);
		p->next=q;
		p=q;
	}
	p->next=NULL;
	return head;
}
void outlink(HLink &H)
{
	HLink p=H->next;
	FILE *w =fopen("hotel.txt","w");
	if(w==NULL)
	{
		printf("打开文件失败!");
		return;
	}
	fprintf(w,"%-16s %-16s %-16s %-16s %-16s\n\n",a,b,c,d,e);
	while(p)
	{
		if(p->Price)
			fprintf(w,"%-18s %-13.2f %8.2f %12d %20s\n\n",p->roomN,p->Price,p->PriceL,p->Beds,p->State);
		p=p->next;
	}
	fclose(w);
	return;
}
void Build(HLink &L)
{ 
	HLink p, r, link, alink;
	L = new Hotel;
	alink=createlink();
	p=alink->next;
	FILE *w =fopen("hotel.txt","w");
	if(w==NULL)
	{
		printf("打开文件失败!");
		return;
	}
	fprintf(w,"%-16s %-16s %-16s %-16s %-16s\n\n",a,b,c,d,e);
	while(p)
	{
		if(p->Price)
			fprintf(w,"%-18s %-13.2f %8.2f %12d %20s\n\n",p->roomN,p->Price,0.8*p->Price,p->Beds,"free");
		p=p->next;
	}
	fclose(w);
	link=L;
	L->next = NULL; //先建立一个带头结点的空链表
	r = L; //尾指针r指向头结点
	fstream file; //打开文件进行读写操作
	file.open("hotel.txt");
	if (!file) 
	{
		cout << "未找到相关文件,无法打开!" << endl;
		exit(0);
	}
	file >> head_1 >> head_2 >> head_3 >> head_4 >> head_5;
	while (!file.eof()) 
	{ 
		//将文件中的信息运用后插法插入到链表中
		p = new Hotel;//生成新结点
		file >> p->roomN >> p->Price >> p->PriceL >> p->Beds >> p->State;//输入元素值赋给新结点*p的数据域
		p->next = NULL;
		r->next = p;//将新结点*p插入尾结点*r之后 		
		r = p;//r指向新的尾结点*p
		length++;
	}
	file.close();
} 
void Exp(HLink &H)
{
	HLink p = H->next;
	//printf("roomN   Price   PriceL  Beds    State\n");
	printf("%-16s %-16s %-16s %-16s %-16s\n\n",a,b,c,d,e);
	while(p)
	{
		if(p->Price==0) 
			{
				p=p->next;
				continue;
			}
		else
			//printf("%-8s%-8.0f%-8.0f%-8d%-8s\n",p->roomN,p->Price,p->PriceL,p->Beds,p->State);
			printf("%-16s %-14.2f %8.2f %12d %18s\n\n",p->roomN,p->Price,p->PriceL,p->Beds,p->State);
		p=p->next;
	}
}
int Find(HLink &H, char *roomN)
{
	HLink a=H;
	int i=0;
	while(a&&strcmp(a->roomN,roomN))
	{
		a=a->next;
		i++;
	}
	if (i>length) 
		i=0;
	return i;
}
void updateH(HLink &H, int beds, char *state)
{
	HLink p=H->next,q=H->next;
	FILE *w =fopen("hotel.txt","w");
	if(w==NULL)
	{
		printf("打开文件失败!");
		return;
	}
	fprintf(w,"%-16s %-16s %-16s %-16s %-16s\n\n",a,b,c,d,e);
	while(p)
	{//修改文本 
		if(p->Price==0) 
			{
				p=p->next;
				continue;
			}
		if (p->Beds==beds)
			fprintf(w,"%-18s %-13.2f %8.2f %12d %20s\n\n",p->roomN,p->Price,p->Price,p->Beds,state);
		else
			fprintf(w,"%-18s %-13.2f %8.2f %12d %20s\n\n",p->roomN,p->Price,p->Price,p->Beds,p->State);
		p=p->next;
	}
	fclose(w);
	while(q)
	{//修改链表

		if(q->Beds==beds)
		 	strcpy(q->State,state);
		q=q->next;
	}
}
void Add(HLink &H)
{
	HLink p=H->next;
	p=H->next;
	FILE *w =fopen("hotel.txt","w");
	if(w==NULL)
	{
		printf("打开文件失败!");
		return;
	}
	fprintf(w,"%-16s %-16s %-16s %-16s %-16s\n\n",a,b,c,d,e);
	while(p)
	{
		if(p->Price==0)
			break;
		if(strcmp(p->State,"free")==0)
			{
				fprintf(w,"%-18s %-13.2f %8.2f %12d %20s\n\n",p->roomN,p->Price,1.2*p->PriceL,p->Beds,p->State);
				p->PriceL=1.2*p->PriceL;	
			}
		else
			fprintf(w,"%-18s %-13.2f %8.2f %12d %20s\n\n",p->roomN,p->Price,p->PriceL,p->Beds,p->State);
		p=p->next;
	}
	fclose(w);
}
HLink FirstH(HLink &H)
{
	HLink p=H->next,max,q=H->next,tmp;
	max->PriceL=0;//p->PriceL;
	while(p)
	{
		if(p->PriceL>max->PriceL)
			max=p;
		p=p->next;
	}
	printf("%-16s %-16s %-16s %-16s %-16s\n\n",a,b,c,d,e);
	printf("%-16s %-14.2f %8.2f %12d %18s\n\n",max->roomN,max->Price,max->PriceL,max->Beds,max->State);
	while(q)
	{
		q=q->next;
		if(q->next==max) break;
	}//找到了max的直接前驱 
	tmp=max->next;
	max->next=NULL;
	q->next=tmp;
	FILE *w =fopen("hotel.txt","w");
	fprintf(w,"%-16s %-16s %-16s %-16s %-16s\n\n",a,b,c,d,e);
	p=H->next;
	while(p)
	{//修改文本 
		if(p->Price==0) 
			{
				p=p->next;
				continue;
			}
		fprintf(w,"%-18s %-13.2f %8.2f %12d %20s\n\n",p->roomN,p->Price,p->Price,p->Beds,p->State);
		p=p->next;
	}
	fclose(w);
	return max;
	free(max);
}
void MoveK1(HLink &H, int k)
{
	//将单链表中倒数第k个结点移到第一个结点位置,
	//注意:严禁采用先计算链表长度n再减k(即n-k)的方法
	HLink p = H->next, q = H, node;
	for (int i=0; i<=k; i++) 
		p = p->next;
	//此时p是正数第k+1个结点
	while (p)
	{
		p = p->next;
		q = q->next;
	}//q为倒数第k+1个
	node = q->next;//node是倒数第k个结点 
	q->next=node->next;
	node->next=H->next;
	H->next=node;//头结点H指向node
	outlink(H);
}
void ReverseN2(HLink &H)
{
	//将单链表的正中间位置结点之后的全部结点倒置,
	//注意:严禁采用先计算链表长度n再除以2(即n/2)的方法
	HLink p=H,q=H;
	while (p->next&&p->next->next)
	{//利用快慢指针,快指针一次走两步,慢指针一次走一步
		p = p->next->next;
		q = q->next;
	}//q即为正中间的结点
	Hotel *pre,*temp,*L=q;
    if(L->next)
    {//将后半部分链表结点倒置
        pre = L->next;
        if(pre ->next)
        {
            p = pre->next;
            while(p)
            {
                temp = p->next;//改变指针指向 
                p->next = pre;
                pre = p;
                p = temp;
            }
            L->next->next = NULL;
            L->next = pre;
        }
    }
	outlink(H);
}
void SortPriceL(HLink &H)
{
	HLink p, p1, q, pre;
	if (H->next)
	{
		p = H->next->next;
		H->next->next = NULL;
		while (p)
		{
			pre = H;  //pre指向q的前驱
			q = pre->next;
			while (q && q->PriceL < p->PriceL)//从链表第二个结点开始找比当前插入值大的结点
			{
				pre = q;
				q = q->next;
			}
			p1 = p->next;//将p插入到结点pre和p之间
			p->next = pre->next;
			pre->next = p;
			p = p1;
		}
	}
	outlink(H); 
}
void upBed(HLink &H,int beds)
{
	HLink newnode=(Hotel*)malloc(sizeof(Hotel));
	HLink p=H->next;
	newnode->Beds=beds;
	printf("请输入客房名称和标准价格:\n"); 
	scanf("%s%f",newnode->roomN,&newnode->Price);
	newnode->PriceL=newnode->Price;
	strcpy(newnode->State,"free");
	while(p)
	{
		if(p->Beds>newnode->Beds&&p->next->Beds<=newnode->Beds)
			break;
		p=p->next;
	}
	if(p)
	{ 
		newnode->next=p->next;
		p->next=newnode;
		printf("创建成功!\n"); 
	}
	else
		printf("输入的床位数不合适,创建失败!\n");
	outlink(H); 
}
int main()
{
	printf("1.读取数据并初始化\n\n2.输出所有函数信息\n\n3.查找房间序号\n\n4.修改入住状态\n\n");
	printf("5.空房加价\n\n6.查找最高价房间\n\n7.移动倒数第k个结点为首元结点\n\n"); 
	printf("8.链表后半部分倒置\n\n9.升序排序\n\n10.创建新结点\n\n0.退出\n\n");
	HLink H=NULL,p;
	int h,j=-1;
	char s[10];
	while(j!=0)
	{
		printf("请输入:");
		scanf("%d",&j);
		printf("\n");
		switch(j)
		{
			case 0:
				if(H)
					free(H);
				break;	
			case 1:
				Build(H);
				p=H->next;
				printf("链表建立成功!\n"); 
				printf("\n"); 
				break;
			case 2:
				Exp(H);
				printf("\n"); 	
				break;
			case 3:
				printf("请输入想查询的房间名称:");
				scanf("%s",s);
				h=Find(H,s);
				printf("房间序号为%d",h);
				if(!h)
					printf(",找不到此房间!\n\n");
				else
					printf("\n");
				printf("\n"); 
				break;
			case 4: 
				int m;
				char q[10];
				printf("请输入房间床位数:");
				scanf("%d",&m);
				printf("请输入入住状态:");
				scanf("%s",q);
				updateH(H,m,q);
				printf("修改成功!\n");
				printf("\n"); 
				break;
			case 5:
				Add(H);
				printf("加价成功!\n");
				printf("\n"); 
				break;
			case 6:
				printf("指针值为:%d\n",FirstH(H)); 
				printf("\n"); 
				break;
			case 7:
				int k;
				printf("请输入k的值:");
				scanf("%d",&k);
				MoveK1(H,k);
				printf("移动成功!\n");
				printf("\n"); 
				break;
			case 8:
				ReverseN2(H);
				printf("倒置成功!\n");
				printf("\n"); 
				break;
			case 9:
				SortPriceL(H);
				printf("排序成功!\n"); 
				printf("\n"); 
				break;
			case 10:
				int upbed;
				printf("请输入床位数:"); 
				scanf("%d",&upbed);
				upBed(H,upbed);
				printf("\n"); 
				break;
			default:
				printf("输入错误!!!\n"); 
		}
	} 
	return 0;
}
posted @ 2022-06-08 19:43  CJK'sBLOG  阅读(72)  评论(0编辑  收藏  举报