优化了命名

This commit is contained in:
lzy
2024-04-28 13:10:56 +08:00
parent d75a518c0f
commit ba9f2e37a7
155 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
# 方便起见一般都会先定义编译器链接器
CC = gcc
LD = gcc
# 正则表达式表示目录下所有.c文件相当于SRCS = main.c a.c b.c
SRCS = $(wildcard *.c)
# OBJS表示SRCS中把列表中的.c全部替换为.o相当于OBJS = main.o a.o b.o
OBJS = $(patsubst %c, %o, $(SRCS))
# 可执行文件的名字
TARGET = main
# .PHONE伪目标具体含义百度一下一大堆介绍
.PHONY:all clean
# 要生成的目标文件
all: $(TARGET)
# 第一行依赖关系冒号后面为依赖的文件相当于Hello: main.o a.o b.o
# 第二行规则:$@表示目标文件,$^表示所有依赖文件,$<表示第一个依赖文件
$(TARGET): $(OBJS)
$(LD) -o $@ $^
# 上一句目标文件依赖一大堆.o文件这句表示所有.o都由相应名字的.c文件自动生成
%.o:%.c
$(CC) -c $^
# make clean删除所有.o和目标文件
clean:
rm -f $(OBJS) $(TARGET)

View File

@@ -0,0 +1,113 @@
/**
* @file main.c
* @brief 球钟算法
*
* @author lzy (lllzzzyyy@buaa.edu.cn)
* @url https://lzyyyyyy.fun
*
* @date 2024-04-18
*
*/
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
#include "sqstack.h"
#define NR_BALL 27
int check(queue *qu)
{
int i = (qu->head + 1) % MAXSIZE;
do
{
if (qu->data[i] > qu->data[(i + 1) / MAXSIZE])
return 0;
i = (i + 1) % MAXSIZE;
} while (i != qu->tail);
return 1;
}
int main()
{
int i;
queue *qu;
int t, value;
int time;
sqstack *st_min, *st_fivemin, *st_hour;
qu = qu_create();
if (NULL == qu)
exit(1);
st_min = st_create();
if (NULL == st_min)
exit(1);
st_fivemin = st_create();
if (NULL == st_fivemin)
exit(1);
st_hour = st_create();
if (NULL == st_hour)
exit(1);
for (i = 1; i <= NR_BALL; i++)
qu_enqueue(qu, &i);
qu_travel(qu);
while (1)
{
qu_dequeue(qu, &t);
time++;
if (st_min->top != 3)
{
st_push(st_min, &t);
}
else
{
while (!st_isempty(st_min))
{
st_pop(st_min, &value);
qu_enqueue(qu, &value);
}
if (st_fivemin->top != 10)
st_push(st_fivemin, &t);
else
{
while (!st_isempty(st_fivemin))
{
st_pop(st_fivemin, &value);
qu_enqueue(qu, &value);
}
if (st_hour->top != 10)
st_push(st_hour, &t);
else
{
while (!st_isempty(st_hour))
{
st_pop(st_hour, &value);
qu_enqueue(qu, &value);
}
qu_enqueue(qu, &t);
if (check(qu))
break;
}
}
}
}
printf("time = %d\n", time);
qu_destroy(qu);
st_destroy(st_min);
st_destroy(st_fivemin);
st_destroy(st_hour);
exit(0);
}

View File

@@ -0,0 +1,72 @@
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
queue *qu_create()
{
queue *sq;
sq = malloc(sizeof(*sq));
if (NULL == sq)
return NULL;
sq->head = 0;
sq->tail = 0;
return sq;
}
int qu_isempty(queue *sq)
{
return (sq->head == sq->tail);
}
int qu_enqueue(queue *sq, datatype *x)
{
if ((sq->tail + 1) % MAXSIZE == sq->head)
return -1;
sq->tail = (sq->tail + 1) % MAXSIZE;
sq->data[sq->tail] = *x;
return 0;
}
int qu_dequeue(queue *sq, datatype *x)
{
if (qu_isempty(sq))
return -1;
sq->head = (sq->head + 1) % MAXSIZE;
*x = sq->data[sq->head];
return 0;
}
void qu_travel(queue *sq)
{
if (sq->head == sq->tail)
return;
int i;
i = (sq->head + 1) % MAXSIZE;
while (i != sq->tail)
{
printf("%d ", sq->data[i]);
i = (i + 1) % MAXSIZE;
}
printf("%d \n", sq->data[i]);
}
void qu_clear(queue *sq)
{
sq->head = sq->tail;
}
void qu_destroy(queue *sq)
{
free(sq);
}

View File

@@ -0,0 +1,29 @@
#ifndef QUEUE_H__
#define QUEUE_H__
#define MAXSIZE 32
typedef int datatype;
typedef struct
{
datatype data[MAXSIZE];
int head, tail;
} queue;
queue *qu_create();
int qu_isempty();
int qu_enqueue(queue *, datatype *);
int qu_dequeue(queue *, datatype *);
void qu_travel(queue *);
void qu_clear(queue *);
void qu_destroy(queue *);
#endif

View File

@@ -0,0 +1,72 @@
#include <stdio.h>
#include <stdlib.h>
#include "sqstack.h"
sqstack *st_create(void)
{
sqstack *st;
st = malloc(sizeof(*st));
if (NULL == st)
return NULL;
st->top = -1;
return st;
}
int st_isempty(sqstack *st)
{
return (-1 == st->top);
}
int st_push(sqstack *st, type *data)
{
if (SIZE - 1 == st->top)
return -1;
st->data[++st->top] = *data;
return 0;
}
/* 取出栈顶元素 */
int st_pop(sqstack *st, type *data)
{
if (st_isempty(st))
return -1;
*data = st->data[st->top--];
return 0;
}
/* 查看栈顶元素 */
int st_top(sqstack *st, type *data)
{
if (st_isempty(st))
return -1;
*data = st->data[st->top];
return 0;
}
void st_travel(sqstack *st)
{
if (st_isempty(st))
return;
int i;
for (i = 0; i <= st->top; i++)
printf("%d ", st->data[i]);
printf("\n");
}
void st_destroy(sqstack *st)
{
free(st);
}

View File

@@ -0,0 +1,30 @@
#ifndef SQSTACK_H__
#define SQSTACK_H__
#define SIZE 32
typedef int type;
typedef struct node_st
{
type data[SIZE];
int top;
} sqstack;
sqstack *st_create(void);
int st_isempty(sqstack *);
int st_push(sqstack *, type *);
/* 取出栈顶元素 */
int st_pop(sqstack *, type *);
/* 查看栈顶元素 */
int st_top(sqstack *, type *);
void st_travel(sqstack *);
void st_destroy(sqstack *);
#endif