
{
printf("存物请输入1,取物请输入2,退出请输入0\n")
scanf("%d",&i)
if(i==1)
{
for(k[y].flag==1y++)
printf("寄存柜%d已经打开,您的密码为%d\n",y,rand())
}
}
改成:
do
{
printf("存物请输入1,取物请输入2,退出请输入0\n")
scanf("%d",&i)
if(i==1)
{
for(k[y].flag==1y++)
printf("寄存柜%d已经打开,您的密码为%d\n",y,rand())
}
}while(i!=0)
#include<stdio.h>#include<stdlib.h>
#include<math.h>
#include<time.h>
typedef struct bag_infor
{
int id
int pass[6]
int lock_stat//0 为可用 1为不可用
struct bag_infor *next
} bag_infor, *bag_link
int get_length(bag_link *link)
{
bag_link temp_node
int i = 0
if(*link == NULL)
{
return i
}
temp_node = (*link)->next
while(temp_node)
{
i = i + 1
temp_node = temp_node->next
}
return i
}
void enter_bag(bag_link *link)
{
int count = get_length(link)
int i
int flag = 0
int choice
if(count == 0)
{
printf("\n\n超市存包系统没有进行初始化,不能使用!\n\n")
}
else
{
while(1)
{
printf("\n请放入一枚一元硬币:1->确定 2->取消\n")
printf("请 *** 作:>")
scanf("%d", &choice)
if(choice == 1)
{
bag_link temp_node
temp_node = (*link)->next
while(temp_node)
{
if(temp_node->lock_stat == 0)//该柜子是可用的
{
printf("\n存在可用的存包柜,您的存包柜是第%d个\n", temp_node->id)
printf("\n请记住您的密码信息:\n")
srand( (unsigned)time( NULL ) )
for(i = 0i <6i++)
{
//printf("%d", rand() % 10)
temp_node->pass[i] = (rand() % 10)
printf("%d",temp_node->pass[i])
}
printf("\n\n")
flag = 1
temp_node->lock_stat = 1
break
}
temp_node = temp_node->next
}
if(flag == 0)
{
printf("\n!存包柜已满,不能进行存包!\n")
}
break
}
else if(choice == 2)
{
printf("谢谢您的使用!再见")
break
}
else
{
printf("输入错误!!!")
}
}
}
}
void get_bag(bag_link *link)
{
char pass[6]
int temp[6]
int i
int flag = 0
bag_link temp_node
printf("\n欢迎使用取包功能\n")
printf("请输入你的密码:>")
scanf("%s", pass)
for(i = 0i <6i++)
{
temp[i] = (int)(pass[i] - '0')
}
temp_node = (*link)->next
while(temp_node)
{
if(temp_node->lock_stat == 1)
{
for(i = 0i <6i++)
{
if(temp_node->pass[i] != temp[i])
{
break
}
else
{
if(i == 5)//最后的数据都是相等的
{
temp_node->lock_stat = 0
flag = 1
}
}
}
}
if(flag == 1)
{
break
}
temp_node = temp_node->next
}
if(flag == 1)
{
printf("\n\n成功打开存包箱\n")
printf("你的存包箱是第%d个\n", temp_node->id)
printf("请妥善保管你的财务,谢谢你的使用\n\n\n")
}
else
{
printf("\n\n密码输入不正确,不能完成取包 *** 作!\n\n")
}
}
void init_bag(bag_link *link)
{
int num
int i
bag_link temp_link
printf("\n\n/**程序将会初始化超市存包柜系统信息**/\n")
printf("请输入超市存包柜个数:>")
scanf("%d", &num)
*link = (bag_link)malloc(sizeof(bag_infor))
(*link)->next = NULL
for(i = 0i <numi++)
{
temp_link = (bag_link)malloc(sizeof(bag_infor))
if(temp_link == NULL)
{
printf("\nError:内存分配失败,程序退出!")
return
}
temp_link->id = i + 1
temp_link->lock_stat = 0
temp_link->next = (*link)->next
(*link)->next = temp_link
}
}
void destory_bag(bag_link *link)
{
bag_link temp_node
temp_node = *link
while(temp_node)
{
temp_node = (*link)->next
free(link)
*link = temp_node
}
}
int main()
{
bag_link bags = NULL
int menu
printf("******C存包柜问题模拟程序******\n")
while(1)
{
printf("*******************MENU********************\n")
printf("* *\n")
printf("*1,初始化存包系统 *\n")
printf("*2,存包 *\n")
printf("*3,取包 *\n")
printf("*4,关闭系统 *\n")
printf("* *\n")
printf("* *\n")
printf("*******************************************\n")
printf("\n\n请选择 *** 作项目:>")
scanf("%d", &menu)
if(menu == 1)
{
init_bag(&bags)
printf("\n\n系统初始化成功.............\n\n")
}
else if(menu == 2)
{
enter_bag(&bags)
}
else if(menu == 3)
{
get_bag(&bags)
}
else if(menu == 4)
{
destory_bag(&bags)
printf("Bye-Bye")
return
}
else
{
printf("\n选择错误,请重新选择!\n\n")
}
}
return 0
}
经过我的测试,可以进行使用,使用的是链表的形式完成的,如果有需要加以家QQ:564777005希望对LZ所有帮助
1、链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。 相比于线性表顺序结构,链表比较方便插入和删除 *** 作。2、例程:/**对链表的综合 *** 作*功能有建立,排序,插入,删除,输出*/#include<stdio.h>#include<malloc.h>typedef int ElemTypetypedef struct NodeType{ElemType datastruct NodeType *next} NodeType,*LinkTypeLinkType create(){//建立链表,返回链表的首地址,头结点没有数据LinkType head,p1,p2head=(LinkType)malloc(sizeof(NodeType))p1=headwhile(p1->data!=0)//当data=0时链表结束{p2=p1p1=(LinkType)malloc(sizeof(NodeType))printf("Enter student's information:\ndata=")scanf("%d",&p1->data)p2->next=p1}p2->next=NULLfree(p1)return(head)}void output(LinkType head){//链表的输出,接收链表的首地址head=head->nextwhile(head!=NULL){printf("data=%d\n",head->data)head=head->next}}LinkType sort(LinkType head){//链表排序,接收链表首地址,返回链表首地址LinkType ph,p1ElemType tempph=head->nextp1=head->nextwhile(p1->next!=NULL)//冒泡法{ph=headwhile(ph->next!=NULL){if(ph->data>ph->next->data)//按data由小到大排序{temp=ph->dataph->data=ph->next->dataph->next->data=temp}ph=ph->next}p1=p1->next}return(head)}LinkType del(LinkType head){//删除结点,接收链表的首地址,返回链表的首地址ElemType DelDataLinkType ph,pph=head->nextprintf("Enter the data you want to del:\nDelData=")scanf("%d",&DelData)while(ph!=NULL &&ph->data!=DelData)//寻找要删除的结点{p=phph=ph->next}if(ph==NULL)//没有找到要删除的结点{printf("Enter error!\n")return(head)}else{if(ph==head->next)//删除头结点{head->next=ph->next}else//删除其它结点{p->next=ph->next}}free(ph)return(head)}LinkType insert(LinkType head){//插入结点,接收链表首地址,返回链表首地址LinkType ph,p,insert,tempinsert=(LinkType)malloc(sizeof(NodeType))printf("Enter the data you want to insert:\ndata=")scanf("%d",&insert->data)ph=head->nextwhile(ph!=NULL &&ph->data <insert->data)//寻找插入的位置{p=phph=ph->next}if(head->next->data >insert->data)//插入头部{temp=head->nexthead->next=insertinsert->next=temp}else//插入到其它地方{p->next=insertinsert->next=ph}return(head)}void main(){LinkType headhead=create()output(head)printf("\n\n")head=sort(head)output(head)printf("\n\n")head=del(head)output(head)printf("\n\n")head=insert(head)output(head)}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)