#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define FILENAME "comp.dat"
#define MAX 20
typedef struct _stuinfo{
char num[MAX];
char name[MAX];
char sex[MAX];//man female
char kind[MAX];
time_t time;
time_t starttime;
struct _stuinfo *next;
}StuInfo, *pStuInfo;
typedef struct _headstu{
int cnt;
StuInfo *node;
}HeadStu;
HeadStu UpqueueHead ;
#define IsStuEq(p, si) (!strcmp(p->num, si->num) && !strcmp(p->name , si->name) && !strcmp(p->sex , si->sex) \
&& !strcmp(p->kind , si->kind))
void GetStuInfo(StuInfo *si, int flag)
{
printf("input num: \t");
scanf("%s", si->num);
printf("input name: \t");
scanf("%s", si->name);
printf("input sex: \t");
scanf("%s", si->sex);
printf("input kind: \t");
scanf("%s", si->kind);
if (flag)
{
printf("input time: \t");
scanf("%d", &si->time);
}
}
void Input(void)
{
FILE *fp;
StuInfo si, sitmp, *p;
if ((fp = fopen(FILENAME, "a+")) == NULL)
{
printf("open dat error!\n");
return;
}
GetStuInfo(&si, 1);
fseek(fp, 0, SEEK_SET);
while (fread(&sitmp, sizeof(sitmp)-4-sizeof(time_t), 1, fp))
{
p = &sitmp;
if (IsStuEq(p, (&si)))
{
printf("%s already exist!\n", si.name);
fclose(fp);
return;
}
}
fseek(fp, 0, SEEK_END);
fwrite(&si, sizeof(si)-4-sizeof(time_t), 1, fp);
fclose(fp);
}
void FreeQueue(void)
{
StuInfo *p = UpqueueHead.node;
while(p)
{
UpqueueHead.node = p->next;
free(p);
p = UpqueueHead.node;
}
}
void FreeAll(void)
{
FILE *fp;
time_t curtime;
StuInfo *p, *q = UpqueueHead.node, sitmp;
if ((fp = fopen(FILENAME, "r+")) == NULL)
{
printf("userinfo data lost!\n");
FreeQueue();
return;
}
while (q)
{
fseek(fp, 0, SEEK_SET);
while (fread(&sitmp, sizeof(sitmp)-4-sizeof(time_t), 1, fp))
{
p = &sitmp;
if (IsStuEq(p, q))
{
curtime = time(NULL);
if (((q->time -= curtime - q->starttime)) <= 0)
{
printf("Not enough time to use!");
}
fseek(fp, -(sizeof(sitmp)-4-sizeof(time_t)), SEEK_CUR);
fwrite(q, sizeof(sitmp)-4-sizeof(time_t), 1, fp);
break;
}
}
q = q->next;
}
FreeQueue();
fclose(fp);
}
void AddQueue(StuInfo si)
{
StuInfo *tmpsi = (StuInfo *)malloc(sizeof(StuInfo));
memcpy(tmpsi, &si, sizeof(StuInfo));
tmpsi->next = UpqueueHead.node;
UpqueueHead.node = tmpsi;
}
int Up(StuInfo si)
{
FILE *fp;
StuInfo sitmp;
int ret;
StuInfo *p = UpqueueHead.node;
while(p)
{
if (IsStuEq(p, (&si)))
{
return 1;
}
p = p->next;
}
if ((fp = fopen(FILENAME, "a+")) == NULL)
{
printf("open dat error!\n");
return 4;
}
fseek(fp, 0, SEEK_SET);
while (fread(&sitmp, sizeof(sitmp)-4-sizeof(time_t), 1, fp))
{
p = &sitmp;
if (IsStuEq(p, (&si)))
{
if (p->time <= 0)
{
printf("Not enough time to use!\n");
fclose(fp);
return -2;
}
time(&sitmp.starttime);
AddQueue(sitmp);
fclose(fp);
return 0;
}
}
fclose(fp);
return -1;
}
int Down(StuInfo si)
{
FILE *fp;
time_t curtime;
StuInfo sitmp;
StuInfo *p = UpqueueHead.node, *prev = NULL;
while(p)
{
if (IsStuEq(p, (&si)))
{
curtime = time(NULL);
if (((p->time -= curtime - p->starttime)) <= 0)
{
printf("Not enough time!");
}
if ((fp = fopen(FILENAME, "r+")) == NULL)
{
printf("open dat error!\n");
return 4;
}
fseek(fp, 0, SEEK_SET);
while (fread(&sitmp, sizeof(sitmp)-4-sizeof(time_t), 1, fp))
{
if (IsStuEq(p, (&sitmp)))
{
fseek(fp, -(sizeof(sitmp)-4-sizeof(time_t)), SEEK_CUR);
fwrite(p, sizeof(sitmp)-4-sizeof(time_t), 1, fp);
fclose(fp);
if (prev == NULL)
{
UpqueueHead.node = p->next;
}else
{
prev->next = p->next;
}
free(p);
return 0;
}
}
fseek(fp, 0, SEEK_END);
fwrite(p, sizeof(sitmp)-4-sizeof(time_t), 1, fp);
fclose(fp);
if (prev == NULL)
{
UpqueueHead.node = p->next;
}else
{
prev->next = p->next;
}
free(p);
printf("Lose user data, rewrite!\n");
return -2;
}
prev = p;
p = p->next;
}
if ((fp = fopen(FILENAME, "r+")) == NULL)
{
printf("open dat error!\n");
return 4;
}
fseek(fp, 0, SEEK_SET);
while (fread(&sitmp, sizeof(sitmp)-4-sizeof(time_t), 1, fp))
{
p = &sitmp;
if (IsStuEq(p, (&si)))
{
printf("%s don't online!\n", p->name);
fclose(fp);
return -3;
}
}
fclose(fp);
return -1;
}
void AppendTime(StuInfo si)
{
FILE *fp;
StuInfo *p, sitmp;
if ((fp = fopen(FILENAME, "r+")) == NULL)
{
printf("open dat error!\n");
return;
}
fseek(fp, 0, SEEK_SET);
while (fread(&sitmp, sizeof(sitmp)-4-sizeof(time_t), 1, fp))
{
p = &sitmp;
if (IsStuEq(p, (&si)))
{
sitmp.time += si.time;
fseek(fp, -(sizeof(sitmp)-4-sizeof(time_t)), SEEK_CUR);
fwrite(&sitmp, sizeof(sitmp)-4-sizeof(time_t), 1, fp);
fclose(fp);
printf("Append time success!\n");
return;
}
}
printf("Append time fail!\n");
fclose(fp);
}
void PrintUpqueue()
{
StuInfo *p = UpqueueHead.node;
printf("num\t\tname\t\tsex\t\tname\t\tCur Used time\n");
while (p)
{
printf("%s\t\t%s\t\t%s\t\t%s\t\t%d\n", p->num, p->name, p->sex, p->kind, time(NULL)-p->starttime);
p = p->next;
}
}
void PrintAllUser()
{
FILE *fp;
StuInfo *p, sitmp;
if ((fp = fopen(FILENAME, "a+")) == NULL)
{
printf("open dat error!\n");
return;
}
fseek(fp, 0, SEEK_SET);
printf("num\t\tname\t\tsex\t\tname\t\ttime\n");
while (fread(&sitmp, sizeof(sitmp)-4-sizeof(time_t), 1, fp))
{
p = &sitmp;
printf("%s\t\t%s\t\t%s\t\t%s\t\t%d\n", p->num, p->name, p->sex, p->kind, p->time);
}
fclose(fp);
}
int main(void)
{
char c;
int flag;
StuInfo si;
= 0;
UpqueueHead.node = NULL;
printf(">>
");
while(1)
{
c = getchar();
switch (c)
{
case 'u'://上机
GetStuInfo(&si, 0);
if ((flag = Up(si)) == -1)
{
printf("%s is not exist!\n", si.name);
} else if (flag == 1)
{
printf("%s have Up !\n", si.name);
}else if (flag == 0)
{
printf("%s Up Success!\n", si.name);
}
break;
case 'd'://下机
GetStuInfo(&si, 0);
if ((flag = Down(si)) == -1)
{
printf("%s is not exist!\n", si.name);
}else if (flag == 1)
{
printf("%s Have down !\n", si.name);
}else if (flag == 0)
{
printf("%s Down Success!\n", si.name);
}
break;
case 'i'://录入
Input();
break;
case 'a'://充值
GetStuInfo(&si, 1);
AppendTime(si);
break;
case 'p'://打印在线用户
PrintUpqueue();
break;
case 'P'://打印录入了的用户
PrintAllUser();
break;
case 'q'://退出
FreeAll();
exit(0);
default:
printf("Undefine!\n");
}
while ((c = getchar()) != '\n')
continue;
printf(">> ");
}
}