gcc编程

这里写自定义目录标题

1.所得税计算:

makefile文件
CC = gcc
CFLAGS = -Wall -Wextra -Werror

all: taxcalc

taxcalc: taxcalc.o
$(CC) $(CFLAGS) -o taxcalc taxcalc.o

taxcalc.o: taxcalc.c
$(CC) $(CFLAGS) -c taxcalc.c

clean:
rm -f taxcalc taxcalc.o

主文件:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

// 税率表
double taxTable[][2] = {
{0, 0},
{1500, 0.03},
{4500, 0.1},
{9000, 0.2},
{35000, 0.25},
{55000, 0.3},
{80000, 0.35},
{4294967295, 0.45}
};

double calculateTax(double income) {
double tax = 0;
double taxableIncome = income - 3500;

if (taxableIncome <= 0) {
return 0;
}

int length = sizeof(taxTable) / sizeof(taxTable[0]);

for (int i = 1; i < length; i++) {
if (taxableIncome <= taxTable[i][0]) {
tax = taxableIncome * taxTable[i][1] - taxTable[i-1][1];
break;
}
}

return tax;
}

int main(int argc, char *argv[]) {
if (argc != 2) {
printf(“error occured\n”);
return 1;
}

double income = atof(argv[1]);
if (income < 0 || income > 4294967295) {
printf(“error occured\n”);
return 1;
}

double tax = calculateTax(income);
double afterTaxIncome = income - tax;

printf(“%.0lf\n”, round(afterTaxIncome));

return 0;
}

2. 掩码

#include <stdio.h>

void cover(int w, int h, int x1, int y1, int x2, int y2) {
int size = ((w * h) + 7) / 8; // 计算需要的字节数,向上取整
unsigned char mask[size]; // 初始化掩码数组
for (int i = 0; i < size; i++) {
mask[i] = 0;
}

// 遍历所有的小区间
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
// 如果当前小区间在遮挡矩形内,设置对应的掩码位
if (i >= y1 && i < y2 && j >= x1 && j < x2) {
int index = i * w + j;
mask[index / 8] |= (1 << (index % 8));
}
}
}

// 输出掩码,每个字节用16进制表示,每两个字节间用空格隔开
for (int i = 0; i < size; ++i) {
if (i % 2 == 1) {
printf(" “);
}
printf(”%02x", mask[i]);
}
printf(“\n”);
}

int main() {
int w, h, x1, y1, x2, y2;
scanf(“%d %d %d %d %d %d”, &w, &h, &x1, &y1, &x2, &y2);
cover(w, h, x1, y1, x2, y2);
return 0;
}

3.消息队列

1)消息队列
#include <stdlib.h>
#include <string.h>

// 消息结构体
typedef struct Message {
char* data;
struct Message* next;
} Message;

// 消息队列结构体
typedef struct MessageQueue {
Message* front;
Message* rear;
} MessageQueue;

// 创建新的消息队列
MessageQueue* createQueue() {
MessageQueue* q = (MessageQueue*)malloc(sizeof(MessageQueue));
q->front = q->rear = NULL;
return q;
}

// 添加消息到队列
void enqueue(MessageQueue* q, char* data) {
Message* msg = (Message*)malloc(sizeof(Message));
msg->data = strdup(data);
msg->next = NULL;
if (q->rear == NULL) {
q->front = q->rear = msg;
return;
}
q->rear->next = msg;
q->rear = msg;
}

// 从队列中取出消息
char* dequeue(MessageQueue* q) {
if (q->front == NULL)
return NULL;

Message* msg = q->front;
char* data = strdup(msg->data);

q->front = q->front->next;
if (q->front == NULL)
	q->rear = NULL;

free(msg->data);
free(msg);

return data;

}
2)订阅列表
我们需要一个订阅列表,记录每个消费者订阅的队列和优先级。我们可以使用哈希表来实现,键是消费者的标识,值是一个队列列表。

#define MAX_SUBSCRIPTIONS 8

// 订阅结构体
typedef struct Subscription {
MessageQueue* queue;
int priority;
} Subscription;

// 消费者结构体
typedef struct Consumer {
Subscription subscriptions[MAX_SUBSCRIPTIONS];
int numSubscriptions;
} Consumer;

// 创建新的消费者
Consumer* createConsumer() {
Consumer* c = (Consumer*)malloc(sizeof(Consumer));
c->numSubscriptions = 0;
return c;
}

// 添加订阅
void subscribe(Consumer* c, MessageQueue* q, int priority) {
if (c->numSubscriptions < MAX_SUBSCRIPTIONS) {
c->subscriptions[c->numSubscriptions].queue = q;
c->subscriptions[c->numSubscriptions].priority = priority;
c->numSubscriptions++;
}
}

// 取消订阅
void unsubscribe(Consumer* c, MessageQueue* q) {
for (int i = 0; i < c->numSubscriptions; i++) {
if (c->subscriptions[i].queue == q) {
// 移动后面的订阅
for (int j = i; j < c->numSubscriptions - 1; j++) {
c->subscriptions[j] = c->subscriptions[j + 1];
}
c->numSubscriptions–;
break;
}
}
}
发布函数
一个发布函数,将消息添加到指定的队列,并通知所有订阅该队列的消费者。
#define MAX_CONSUMERS 8

// 全局消费者列表
Consumer* consumers[MAX_CONSUMERS];
int numConsumers = 0;

// 添加消费者
void addConsumer(Consumer* c) {
if (numConsumers < MAX_CONSUMERS) {
consumers[numConsumers] = c;
numConsumers++;
}
}

// 发布消息
void publish(MessageQueue* q, char* data) {
// 将消息添加到队列
enqueue(q, data);

// 通知所有订阅了该队列的消费者
for (int i = 0; i < numConsumers; i++) {
Consumer* c = consumers[i];
for (int j = 0; j < c->numSubscriptions; j++) {
if (c->subscriptions[j].queue == q) {
// 这里只是打印一条通知,实际的实现可能需要唤醒消费者线程或发送信号
printf(“Consumer %d: new message in queue %p\n”, i, q);
break;
}
}
}
}

读取函数
读取函数,消费者可以用它来从他们订阅的队列中读取消息。这个函数需要考虑优先级和订阅顺序。
// 读取函数
char* readMessage(Consumer* c) {
// 遍历所有订阅,找到优先级最高的队列
int highestPriority = -1;
MessageQueue* highestPriorityQueue = NULL;
for (int i = 0; i < c->numSubscriptions; i++) {
if (c->subscriptions[i].priority > highestPriority && c->subscriptions[i].queue->front != NULL) {
highestPriority = c->subscriptions[i].priority;
highestPriorityQueue = c->subscriptions[i].queue;
}
}

// 如果找到了队列,从中读取消息
if (highestPriorityQueue != NULL) {
return dequeue(highestPriorityQueue);
}

// 如果没有找到队列,返回NULL
return NULL;
}
** api**
最后,我们需要一个命令行接口,接收用户的命令并调用相应的函数。我们可以使用scanf函数来读取命令和参数。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char *argv[]) {
int opt;
char *subscriptionList = NULL;
char *message = NULL;

while ((opt = getopt(argc, argv, “sⓂ️”)) != -1) {
switch (opt) {
case ‘s’:
subscriptionList = optarg;
break;
case ‘m’:
message = optarg;
break;
default:
fprintf(stderr, “Usage: %s [-s subscriptionList] [-m message]\n”, argv[0]);
exit(EXIT_FAILURE);
}
}

printf(“Subscription List: %s\n”, subscriptionList);
printf(“Message: %s\n”, message);

// 这里可以添加你的代码来处理订阅列表和消息

return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值