银行业务队列简单模拟 c语言 程序
时间: 2024-08-13 17:07:22 浏览: 54
在C语言中,我们可以使用结构体和队列数据结构来简单模拟银行的业务处理系统。这里是一个简单的示例,展示了如何创建一个队列来存储客户请求(比如取款、存款和转账),以及处理这些请求:
```c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
// 定义客户请求结构体
typedef struct {
char* operation; // 请求类型(取款、存款、转账)
int amount; // 请求金额
} ClientRequest;
// 队列结构体
typedef struct Queue {
ClientRequest* requests;
int front, rear, size;
} Queue;
// 初始化队列
void initQueue(Queue* q) {
q->requests = (ClientRequest*)malloc(sizeof(ClientRequest));
q->front = q->rear = -1;
q->size = 0;
}
// 添加请求到队列
bool enqueue(Queue* q, ClientRequest request) {
if (q->size == sizeof(q->requests)/sizeof(ClientRequest)) {
printf("Queue is full, cannot add more requests.\n");
return false;
}
q->requests[++q->rear] = request;
q->size++;
return true;
}
// 从队列中取出并处理请求
void dequeue(Queue* q, void (*process_request)(ClientRequest*)) {
if (q->front == -1) {
printf("Queue is empty, no requests to process.\n");
return;
}
ClientRequest request = q->requests[q->front];
q->requests[q->front++] = q->requests[--q->rear]; // 将队首元素移到队尾
q->size--;
process_request(request);
}
// 处理取款、存款和转账操作
void handle_operations(ClientRequest request) {
switch (request.operation) {
case 'T': // 取款
printf("Processing withdrawal of %.2f\n", request.amount);
break;
case 'D': // 存款
printf("Processing deposit of %.2f\n", request.amount);
break;
case 'R': // 转账
printf("Processing transfer of %.2f from account %s to account %s\n", request.amount, request.operation + 2, request.operation + 4);
break;
default:
printf("Invalid operation.\n");
}
}
int main() {
Queue queue;
initQueue(&queue);
// 添加一些请求
enqueue(&queue, (ClientRequest){"T", 500});
enqueue(&queue, (ClientRequest){"D", 1000});
enqueue(&queue, (ClientRequest){"R", "100", "200", 200});
// 处理队列中的请求
while (queue.size > 0) {
dequeue(&queue, handle_operations);
}
return 0;
}
```
阅读全文