用c语言写:去银行取钱,输密码取钱
时间: 2023-12-08 15:22:06 浏览: 21
#include <stdio.h>
#include <stdlib.h>
int main()
{
int balance = 1000; // 初始余额为1000元
int password = 123456; // 银行卡密码
int input_password; // 输入的密码
int withdraw_amount; // 取款金额
printf("欢迎来到银行!\n");
printf("请输入银行卡密码:");
scanf("%d", &input_password);
if (input_password == password) {
printf("密码正确!\n");
printf("请输入取款金额:");
scanf("%d", &withdraw_amount);
if (withdraw_amount > balance) {
printf("余额不足!\n");
} else {
balance -= withdraw_amount;
printf("取款成功,余额为%d元。\n", balance);
}
} else {
printf("密码错误!\n");
}
return 0;
}
相关问题
用C语言写一个队列实现银行取钱模拟
好的,这个问题我可以回答。以下是用C语言实现队列的代码:
```
#include <stdio.h>
#include <stdlib.h>
#define MAX_QUEUE_SIZE 100
typedef struct {
int data[MAX_QUEUE_SIZE];
int front;
int rear;
} Queue;
void initQueue(Queue *q) {
q->front = 0;
q->rear = 0;
}
int isQueueEmpty(Queue *q) {
return q->front == q->rear;
}
int isQueueFull(Queue *q) {
return (q->rear + 1) % MAX_QUEUE_SIZE == q->front;
}
void enqueue(Queue *q, int x) {
if (isQueueFull(q)) {
printf("Queue is full.\n");
exit(1);
}
q->data[q->rear] = x;
q->rear = (q->rear + 1) % MAX_QUEUE_SIZE;
}
int dequeue(Queue *q) {
if (isQueueEmpty(q)) {
printf("Queue is empty.\n");
exit(1);
}
int x = q->data[q->front];
q->front = (q->front + 1) % MAX_QUEUE_SIZE;
return x;
}
int main() {
Queue q;
initQueue(&q);
// 模拟银行取钱
int money[] = {100, 200, 300, 400, 500};
int n = sizeof(money) / sizeof(int);
int i, x;
for (i = 0; i < n; i++) {
enqueue(&q, money[i]);
}
while (!isQueueEmpty(&q)) {
x = dequeue(&q);
printf("取出%d元\n", x);
}
return 0;
}
```
这个代码实现了一个队列,并且用队列模拟了银行取钱的过程。每个人的取钱金额存储在一个数组中,然后依次加入队列中。每次取钱时,从队列中取出一个元素即可。
用C语言写一个链队列实现银行取钱模拟
好的,我可以回答这个问题。以下是用C语言写一个链队列实现银行取钱模拟的代码:
```
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *next;
} Node;
typedef struct queue {
Node *front;
Node *rear;
} Queue;
void initQueue(Queue *q) {
q->front = q->rear = (Node *)malloc(sizeof(Node));
q->front->next = NULL;
}
int isEmpty(Queue *q) {
return q->front == q->rear;
}
void enQueue(Queue *q, int data) {
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
q->rear->next = newNode;
q->rear = newNode;
}
int deQueue(Queue *q) {
if (isEmpty(q)) {
printf("Queue is empty!\n");
return -1;
}
Node *temp = q->front->next;
int data = temp->data;
q->front->next = temp->next;
if (q->rear == temp) {
q->rear = q->front;
}
free(temp);
return data;
}
int main() {
Queue q;
initQueue(&q);
int choice, amount;
do {
printf("1. Deposit\n");
printf("2. Withdraw\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter amount to deposit: ");
scanf("%d", &amount);
enQueue(&q, amount);
printf("Amount deposited: %d\n", amount);
break;
case 2:
printf("Enter amount to withdraw: ");
scanf("%d", &amount);
if (isEmpty(&q)) {
printf("No money in the bank!\n");
} else if (amount > q.front->next->data) {
printf("Not enough money in the bank!\n");
} else {
int remaining = deQueue(&q) - amount;
if (remaining > 0) {
enQueue(&q, remaining);
}
printf("Amount withdrawn: %d\n", amount);
}
break;
case 3:
printf("Exiting...\n");
break;
default:
printf("Invalid choice!\n");
}
} while (choice != 3);
return 0;
}
```