文章目录
- 员工管理系统
- 约瑟夫环
- 四阶斐波那契数列
- 八皇后
- 停车场
- 银行业务
员工管理系统
#include
#include
struct staff{
char name[20];
int id;
char duty[20];
};
struct list{
struct staff sta[40];
int count;
};
int search(struct list*l,int id){
int i;
for(i=0;i<l->count;i++){
if(l->sta[i].id==id)
return i;
}
return -1;
}
void add(struct list* l,struct staff s){
l->count++;
l->sta[l->count]=s;
printf("添加成功!\n");
}
void del(struct list* l,int id){
int loc=search(l,id); int i;
if(loc==-1)
printf("不存在该职工\n");
else {
for(i=loc;i<l->count;i++){
l->sta[i]=l->sta[i+1];
}
l->count--;
printf("删除成功!\n");
}
}
void print(struct list* l){
int i;
for(i=0;i<=l->count;i++){
printf("姓名:%s 工号:%d 职务:%s\n",l->sta[i].name,l->sta[i].id,l->sta[i].duty);
}
}
int main(){
struct staff s;
struct list li;
struct list* l=&li;
int id;
l->count=-1;
printf("-----------------员工管理系统------------------\n");
printf("*****菜单*****\n");
printf("1.入职\n2.离职\n3.打印\n");
printf("请输入您选择的操作:\n");
int choose;
while(scanf("%d",&choose)==1)
{
switch (choose){
case 1:
printf("请输入要添加职工的姓名,工号,职务:\n");
scanf("%s%d%s",s.name,&s.id,s.duty);
add(l,s);
break;
case 2:
printf("请输入要删除职工的工号\n");
scanf("%d",&id);
del(l,id);
break;
case 3:
print(l);
break;
}
}
return 0;
}
约瑟夫环
四阶斐波那契数列
#include
#include
#define MAXSIZE 4
int f[100];
typedef struct {
int *base;
int front;
int rear;
int length;
}SqQueue;
SqQueue* CreatQueue(){
SqQueue *Q = (SqQueue*)malloc(sizeof(SqQueue));
Q->base = (int*)malloc(sizeof(int)*MAXSIZE);
if(!Q->base)
exit(0);
Q->front = 0;
Q->rear = 0;
Q->length = 0;
return Q;
}
int fb(int k,int max)
{
SqQueue *Q=CreatQueue();
SqQueue cq=*Q;
int i;
for(i=0;i<=k-2;i++){
f[i]=0;cq.base[i]=0;
}
f[k-1]=1;cq.base[k-1]=1;
cq.rear=k-1;
int n=k;
while(cq.base[cq.rear]<max)
{
f[n]=0;
int j;
for(j=0;j<k;j++){
f[n]=f[n]+cq.base[j];
}
cq.rear=(cq.rear+1)%k;
cq.base[cq.rear]=f[n];
n++;
}
if(cq.base[cq.rear]>max)
n=n-2;
else n=n-1;
if(max==1){
n=k;
f[k=1];
}
return n;
}
int main()
{
int i;
int n=fb(4, 200);
for (i = 0; i <= n; i++)
printf(" %d ", f[i]);
}
八皇后
#include
#define n 8
int m = 0, a[n];
int ok(int i, int j);
void queen(int j);
int main()
{
queen(1);
return 0;
}
int ok(int i, int j)
{
int j1, i1, ok1;
j1 = j; i1 = i; ok1 = 1;
while ((j1 > 1) && ok1)
{
j1--;
ok1 = a[j1] != i;
}
j1 = j; i1 = i;
while ((j1 > 1) && (i1 > 1) && ok1)
{
j1--; i1--;
ok1 = a[j1] != i1;
}
j1 = j; i1 = i;
while ((j1 > 1) && (i1 < n) && ok1)
{
j1--; i1++;
ok1 = a[j1] != i1;
}
return ok1;
}
void queen(int j) {
int i;
if (j > n)
{
m++; printf("第%2d种情况: ", m);
for (i = 1;i <= n;i++) printf(" %d", a[i]);
printf("\n");
}
else for (i = 1; i <= n;i++)
if (ok(i, j)) {
a[j] = i;
queen(j + 1);
}
}
停车场
#include
#include
#define MAXSIZE 100
#define QUEUESIZE 10
int A[MAXSIZE];
int num_a;
int num_b;
int cnt;
typedef struct TIME {
long long t;
int y;
int m;
int d;
int h;
int f;
int s;
}TIME;
TIME getTime() {
time_t tt;
tt = time(&tt);
time_t timep;
struct tm* p;
time(&timep);
p = localtime(&timep);
TIME T;
T.t = tt;
T.y = 1900 + p->tm_year;
T.m = 1 + p->tm_mon;
T.d = p->tm_mday;
T.h = p->tm_hour;
T.f = p->tm_min;
T.s = p->tm_sec;
return T;
}
void printTime(TIME T) {
printf("%d-%d-%d %d时%d分%d秒\n", T.y, T.m, T.d, T.h, T.f, T.s);
}
int cFee(TIME iTime, TIME oTime) {
int Fee;
int tHour;
tHour = (iTime.t - iTime.t) / 3600;
Fee = tHour * 1;
return Fee;
}
typedef struct car {
TIME iTime;
TIME oTime;
int id[20];
int fee;
int flag;
int loc;
}car;
car Ifo[200];
typedef int QElemType;
typedef struct {
QElemType data[QUEUESIZE];
int f;
int r;
}SqQueue;
void initQueue(SqQueue* Q) {
Q->f = Q->r = 0;
}
int getQueueLength(SqQueue* Q) {
return (Q->r - Q->f + MAXSIZE) % MAXSIZE;
}
void enQueue(SqQueue* Q, QElemType cnt) {
if (Q->f == (Q->r + 1) % MAXSIZE) {
printf("\n");
return;
}
Q->data[Q->r] = cnt;
Q->r = (Q->r + 1) % MAXSIZE;
}
QElemType deQueue(SqQueue* Q) {
QElemType e = Q->data[Q->f];
Ifo[e].flag = 1;
Ifo[e].iTime = getTime();
Q->f = (Q->f + 1) % MAXSIZE;
return e;
}
int SearchQueue(SqQueue* Q, int cnt)
{
int p;
p = Q->f;
int w = 0;
while (Q->data[p] != cnt)
{
w++;
p = (p + 1) % QUEUESIZE;
}
return w;
}
void printIfo(SqQueue* Q)
{
printf("*************当前的所有停车记录**************\n");
int i;
printf("当前停车场内的车辆数:%d", num_a);
if (num_b > 0)
printf("当前便道上等待的车辆数:%d", num_b);
for (i = 0; i <= cnt; i++)
{
printf("\n-------No#%d--------\n", i);
printf("车牌号:%s\n", Ifo[i].id);
if (Ifo[i].flag == 0)
{
printf("状态:已经离开停车场\n");
printf("进入时间:");
printTime(Ifo[i].iTime);
printf("离开时间:");
printTime(Ifo[i].oTime);
printf("收费:%d", Ifo[i].fee);
}
else if (Ifo[i].flag == 1)
{
printf("状态:在停车场中\n");
printf("进入时间:");
printTime(Ifo[i].iTime);
printf("车位:%d", Ifo[i].loc);
}
else if (Ifo[i].flag == 2)
{
printf("状态:在便道上等待\n");
printf("还需等待的车辆数:%d\n", SearchQueue(Q, i));
}
}
printf("\n************************************\n");
}
void park()
{
printf("\n*******停车场管理系统*******\n");
printf("1.车辆进入i\n");
printf("2.车辆离开o\n");
printf("3.显示信息p\n");
printf("4.退出系统e\n");
SqQueue Q;
initQueue(&Q);
cnt = -1;
num_a = 0;
num_b = 0;
int ii;
for (ii = 0; ii < MAXSIZE; ii++)
{
A[ii] = -1;
}
char c;
while (scanf_s("%c", &c) != EOF)
{
switch (c) {
case 'i':
cnt++;
printf("请输入车牌号:\n");
scanf_s("%s", Ifo[cnt].id);
if (num_a < MAXSIZE)
{
num_a++;
Ifo[cnt].flag = 1;
Ifo[cnt].iTime = getTime();
int jj = 0;
while (A[jj] >= 0)
{
jj++;
}
A[jj]=cnt;
Ifo[cnt].loc = jj;
printf("车位号为:%d",jj);
}
else
{
num_b++;
Ifo[cnt].flag = 2;
enQueue(&Q, cnt);
}
break;
case 'o':
printf("请输入离开车辆的编号:\n");
int bh;
scanf("%d",&bh);
if (Ifo[bh].flag == 1)
{
num_a--;
A[Ifo[bh].loc] = -1;
Ifo[bh].oTime = getTime();
Ifo[bh].flag = 0;
Ifo[bh].fee = cFee(Ifo[bh].iTime, Ifo[bh].oTime);
if (num_b > 0)
{
int ee = deQueue(&Q);
Ifo[ee].loc = Ifo[bh].loc;
A[Ifo[ee].loc] = ee;
num_a++;
num_b--;
}
}
else {
printf("编号输入有误,该车不在停车场内\n");
}
break;
case 'p':
printIfo(&Q);
break;
case 'e':
return;
}
}
}
int main()
{
park();
return 0;
}
银行业务
#include
#define MAXSIZE 3
typedef struct client {
int status;
int win;
int issue;
}client;
client Ifo[100];
int cnt;
typedef int QElemType;
typedef struct {
QElemType data[MAXSIZE];
int f;
int r;
}SqQueue;
typedef struct {
int num;
SqQueue* Q;
}win;
win wins[7];
void initQueue(SqQueue* Q) {
Q->f = Q->r = 0;
}
int getLen(SqQueue* Q) {
return (Q->r - Q->f + MAXSIZE) % MAXSIZE;
}
int enQueue(SqQueue* Q, int bh) {
if (Q->f == (Q->r + 1) % MAXSIZE) {
return -1;
}
Q->data[Q->r] = bh;
Q->r = (Q->r + 1) % MAXSIZE;
return 1;
}
int deQueue(SqQueue* Q) {
if (Q->f == Q->r) {
return -1;
}
QElemType e = Q->data[Q->f];
printf("编号为%d的客户业务办理完成\n", e);
Q->f = (Q->f + 1) % MAXSIZE;
return e;
}
int best2()
{
int min;
if (wins[2].num == MAXSIZE-1 && wins[3].num == MAXSIZE-1 && wins[4].num == MAXSIZE-1)
min = 0;
if (wins[2].num < wins[3].num && wins[2].num < wins[4].num)
min = 2;
else if (wins[3].num < wins[4].num)
min = 3;
else min = 4;
return min;
}
int best3()
{
int min;
if (wins[5].num == MAXSIZE-1 && wins[6].num == MAXSIZE-1)
{
int mm;
mm = best2();
if (mm == 0)
{
min = 0;
}
else
{
min = mm;
}
}
else
{
if (wins[5].num < wins[6].num)
min = 5;
else
min = 6;
}
return min;
}
int SearchQueue(SqQueue* Q, int bh)
{
int i = Q->f;
int j = 0;
while (Q->data[i] != bh)
{
i = (i + 1) % MAXSIZE;
j++;
}
return j;
}
void printIfo(int bh)
{
printf("业务:");
if (Ifo[bh].issue == 1)
printf("公积金\n");
if (Ifo[bh].issue == 2)
printf("银行卡\n");
if (Ifo[bh].issue == 3)
printf("理财\n");
if (Ifo[bh].status == 2)
printf("状态:办理中\n");
else if (Ifo[bh].status == 1)
{
int dd;
int ww = Ifo[bh].win;
dd = SearchQueue(wins[ww].Q, bh);
printf("状态:还需等待%d人\n", dd);
}
}
void printWin(SqQueue* Q)
{
int i = Q->f;
int bh;
int j = 1;
while (i != Q->r)
{
bh = Q->data[i];
i = (i + 1) % MAXSIZE;
printf("@ %d\n", j);
printIfo(bh);
j++;
}
}
void printQueue() {
int j;
printf("\n------------窗口信息--------------\n");
for (j = 1; j <= 6; j++) {
printf("\n*****No#%d*****:\n", j);
if (wins[j].num > 0)
printWin(wins[j].Q);
else
printf("该窗口为空!\n");
}
printf("\n-----------------------------------\n");
}
void get(int bh) {
printf("\n~~~~~~取号~~~~~~\n");
printf("公积金办理-> 1\n");
printf("银行卡办理-> 2\n");
printf("理财办理-> 3\n");
printf("请选择您要办理的业务:\n");
int yewu; scanf_s("%d", &yewu);
Ifo[bh].issue = yewu;
int bw;
switch (yewu)
{
case 1:
bw = 1;
break;
case 2:
bw = best2();
break;
case 3:
bw = best3();
break;
}
if (bw == 0)
{
Ifo[bh].status = 0;
printf("当前窗口排队人数已满,取号失败!\n");
}
else {
Ifo[bh].win = bw;
enQueue(wins[bw].Q, bh);
if (wins[bw].num == 0)
{
Ifo[bh].status = 2;
printf("即刻办理!\n");
}
else
{
Ifo[bh].status = 1;
printf("取号成功!\n");
}
wins[bw].num++;
}
}
void done()
{
printf("请输入完成当前业务的窗口:");
int w; scanf_s("%d", &w);
int bh;
bh = deQueue(wins[w].Q);
if (bh < 0)
{
printf("当前窗口为空,输入错误!\n");
}
else
{
Ifo[bh].status = 3;
}
}
void business()
{
printf("****************银行业务系统**************\n");
printf("1.取号请输入g\n");
printf("2.业务完成请输入d\n");
printf("3.显示窗口业务信息请输入p\n");
printf("4.银行关门结束当前所有业务请输入e\n");
char c;
cnt = -1;
int i = 0;
SqQueue Q[7];
for (i = 1; i <= 6; i++)
{
wins[i].num = 0;
wins[i].Q = &Q[i];
initQueue(wins[i].Q);
}
while (scanf_s("%c", &c) != EOF)
{
switch (c) {
case 'g':
cnt++;
get(cnt);
break;
case 'd':
done();
break;
case 'e':
printf("银行关门,系统结束!\n");
break;
case 'p':
printQueue();
break;
}
}
}
int main() {
business();
return 0;
}