程序设计与实践 自动寄存柜 C语言

实验内容:

    输入数据时,可先输入寄存箱总数n, 再由用户选择是“投硬币”还是“输密码”。

    如果选择“投硬币”,则只有硬币值是1时才开箱。如果有空闲的箱子,则输出箱子编号及密码(4位数字); 如果无空闲的箱子,则提示“本柜已满”。

如果选择“输密码”,若输入的密码与某一箱子密码相符,则显示打开的箱子编号,否则输出提示“密码错误”。

万能密码假定有一个密码000086该密码用于营业结束后整理寄存柜。先在菜单中输入隐藏的选项5,然后提示请输入万能密码,密码输入后,打开所有的箱子,如有箱子中客户没有取走东西,则提示:请重点检查某编号的箱子是否有物留存。

    请编写开箱控制程序实现上述过程。

    输入输出示例:


    寄存箱总数:10

    1.投硬币  2.输密码  0.退出   请选择:1

    投币值:1

    寄存箱编号:1  密码:9342

    1.投硬币  2.输密码  0.退出   请选择:2

    输入密码:9342

1号寄存箱已打开

1.投硬币  2.输密码  0.退出   请选择:5

请输入万能密码:000086

箱子已经全部打开,请重点检查编号1,编号7的箱子是否有物留存

    1.投硬币  2.输密码  0.退出   请选择:0


#define _CRT_SECURE_NO_WARNINGS 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>

typedef struct {
	int number;            //box的编号
	int password;          //四位密码
	int status;            //box的状态  空为 0  有留存 1
}BOX;

void friuse(BOX* box, int btot)                                //初始化
{
	int i = 0;
	while (btot--) {
		box[i].number = i + 1;
		box[i].status = 0;
		box[i].password = 0;
		i++;
	}
}
void Menu()
{
	printf("1.投硬币(支持币值:壹)\n");
	printf("2.------输密码--------\n");
	printf("0.-------退出---------\n");
	printf("--------请选择:");
}

int YNempty(BOX*box,int btot)                            //box是否为空
{
	for (int i = 0; i < btot; i++) {
		if (box[i].status == 0)
			return i;                                 //返回下标
	}
	return 1399;     //为空
}

int backbox(BOX* box,int password,int btot)                //打开一个box
{
	for (int i = 0; i < btot; i++) {
		if (box[i].password == password) {
			printf("%d号寄存箱已打开\n", box[i].number);
			box[i].password = 0;
			box[i].status = 0;
			return 0;
		}
	}
	return printf("密码错误!\n");
}

int  Password(BOX*box ,int btot)                        //返回一个不重复的四位密码
{
	srand((unsigned)time(NULL));
	int x = 0, jude = 1;
	while (jude) {
		x = rand() % 9000 + 1000;
	    for (int i = 0; i < btot; i++) {
			if (x != box[i].password)
				jude = 0;
	    }
	}
	return x;
}

void Allbox(BOX* box,int btot)                  //列出所有box,并指出重点检查的box
{
	int impt[100];
	int j = 0;
	printf("---寄存箱编号-----状态----\n");
	for (int i = 0; i < btot; i++) {
		if (box[i].status == 0) {
			printf("-----%d------------空-----\n", box[i].number);
		}
		else {
			printf("-----%d-----------有留存---\n", box[i].number);
			impt[j] = box[i].number;
			j++;
		}
	}
	if (j == 0) {
        printf("箱子已经全部打开,无重点检查的箱子\n");
	}
	else {
		printf("箱子已经全部打开,请重点检查");
		for (int i = 0; i < j; i++) {
			printf("编号%d ", impt[i]);
		}
		printf("的箱子是否有物留存。\n");
	}
}
int main()
{
	int btot = 0;
	int input = 122;
	int pword = 0, coin = 0;
	char arr[10]="000086";
	printf(" 寄存箱总数:");
	scanf("%d", &btot);
	BOX box[100];
	friuse(box, btot);
	while (input) {
		Menu();
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			printf("投币值:");
			scanf("%d", &coin);
			if (coin == 1) {
				pword = Password(box, btot);
				int f = YNempty(box, btot);
				if (f != 1399) {
					box[f].password = pword;
					box[f].status = 1;
					printf("寄存箱编号:%d  密码:%d\n", box[f].number, box[f].password);
				}
				else {
					printf("寄存箱已满!\n");
				}
			}
			else {
				printf("不支持此类硬币\n");
				printf("已退还:%d\n", coin);
			}
			Sleep(2000);
			system("cls");
			break;
		case 2:
			printf("输入密码:");
			scanf("%d", &pword);
			backbox(box, pword, btot);
			Sleep(2000);
			system("cls");
			break;
		case 0:
			break;
		case 5:
			printf("请输入万能密码:");
			scanf("%s", arr);
			if (!strcmp(arr, "000086")) {
				system("cls");
				Allbox(box,btot);
			}
			else {
                printf("密码错误!");
			}
			Sleep(10000);
			system("cls");
			break;
		default:
			printf("输入错误!");
			Sleep(2000);
			system("cls");
			break;
		}
	}
	return 0;
}

  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值