用户日程管理
工作大致流程图
源文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <pthread.h>
#include <unistd.h>
#define LENGTH 100
char filename[LENGTH];
typedef struct Schedule {
int ID;
int year;
int month;
int day;
int hour;
int minute;
int second;
char content[LENGTH];
char priority[LENGTH];
struct Schedule* next;
}Schedule;
typedef struct User{
char name[LENGTH];
char password[LENGTH];
char HASH_Password[LENGTH];
struct Schedule *Schedule;
struct User *next;
}User;
User* Init(){
User *eHead = (User*)malloc(sizeof(User));
eHead->next=NULL;
if(eHead!=NULL){
return eHead;
}
else{
printf("空间分配失败!");
exit(1);
}
}
int Load(User *head,int n){
FILE *data;
int i=0;
for (; head->next != NULL; head = head->next);
User* test = (User*)malloc(sizeof(User));
if((data=fopen("userInfo","r"))==NULL){
printf("Can not open file\n");
return 0;
}
while(i<=n){
fscanf(data,"%s %s\n ",test->name,test->HASH_Password);
head->next=test;
i++;
}
fclose(data);
return 0;
}
int check_New(User *eHead,char *name){
int flag=1;
eHead=eHead->next;
while(eHead!=NULL){
if(strcmp(eHead->name,name)==0){
flag=0;
break;
}
eHead=eHead->next;
}
return flag;
}
int check_name(User *eHead,char *name){
int flag=0;
eHead=eHead->next;
while(eHead!=NULL){
if(strcmp(eHead->name,name)==0){
flag=1;
break;
}
eHead=eHead->next;
}
return flag;
}
int check_key(User *eHead,char *name,char *key,char *Hkey){
int flag=0;
eHead=eHead->next;
while(eHead!=NULL){
if(strcmp(eHead->name,name)==0){
strcpy(Hkey,eHead->HASH_Password);
int i=0,len=strlen(Hkey);
while (i<len)
{
Hkey[i]=Hkey[i]-i-5;
i++;
}
if (strcmp(key,Hkey)==0)
{
flag=1;
break;
}
}
eHead=eHead->next;
}
return flag;
}
Schedule *init(){
Schedule *head = (Schedule*)malloc(sizeof(Schedule));
head->next=NULL;
if(head!=NULL){
return head;
}
else{
printf("空间分配失败!");
exit(1);
}
}
int check_time(int year, int month, int day,int hour,int min,int sec,Schedule *head)
{
int monthDays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int judg = 0;
time_t timep;
time(&timep);
struct tm *p;
p = gmtime(&timep);
if ((year % 400 == 0) ||(year % 100 != 0 && year % 4 == 0))
{
monthDays[1] = 29;
}
if(year<1900 + p->tm_year||year < 0){
printf(" Are you sure the entered year[%d] is correct?\n", year);
}
else if(year>=(1900 + p->tm_year+1)){
judg=1;
}
else if(year>=1900 + p->tm_year){
if(month<1 + p->tm_mon||month < 1 || month > 12) {
printf("Are you sure the entered month[%d] is correct?\n", month);
}
else if(month>=(1 + p->tm_mon+1)){
judg=1;
}
else if(month>=1 + p->tm_mon){
if(day < p->tm_mday || day < 1 || day > monthDays[month - 1]){
printf("Are you sure the entered day[%d] is correct?\n", day);
}
else if(day>=p->tm_mday+1){
judg=1;
}
else if( monthDays[month - 1] >= day && day >= p->tm_mday){
if(hour < p->tm_hour+8){
printf("Are you sure the entered hour[%d] is correct?\n", hour);
}
else if(hour>= p->tm_hour+9){
judg=1;
}
else if(24>=hour && hour>=(p->tm_hour+8)){
if(min<p->tm_min||min>60){
printf("Are you sure the entered min[%d] is correct?\n", min);
}
else if(min>p->tm_min+1){
judg=1;
}
else if(p->tm_min<=min && min<=60){
if (p->tm_sec>sec||sec>60){
printf("Are you sure the entered sec[%d] is correct?\n", sec);
}
else{
judg=1;
}
}
}
}
}
}
head=head->next;
while(head!=NULL){
if (head->year==year&&head->month==month&&head->day==day&&head->hour==hour&&head->minute==min)
{
printf("There is already a task at the current time!\n");
judg=0;
break;
}
else{
judg=1;
}
head=head->next;
}
return judg;
}
int load(Schedule *head,int n){
FILE *data;
int i=0;
for (; head->next != NULL; head = head->next);
Schedule* test = (Schedule*)malloc(sizeof(Schedule));
if((data=fopen(filename,"a+"))==NULL){
printf("Can not open file\n");
return 0;
}
while(i<=n){
fscanf(data,"%d %d %d %d %d %d %s %s\n ",
&test->year,&test->month, &test->day,&test->hour, &test->minute, &test->second,test->content,test->priority);
head->next=test;
i++;
}
fclose(data);
return 0;
}
void insert(Schedule* eHead,Schedule *head) {
int x=0;
time_t timep;
time(&timep);
struct tm *p;
p = gmtime(&timep);
for (; eHead->next != NULL; eHead = eHead->next);
Schedule* test = (Schedule*)malloc(sizeof(Schedule));
test->next = NULL;
do{
printf("请输入日期和时间:yyyy-mm-dd hh:mm:ss(例如:2022-7-7 00:00:00)\n");
printf("当前时间是:");
printf("%d-%02d-%02d %02d:%02d:%02d\n", 1900 + p->tm_year, 1 + p->tm_mon, p->tm_mday, p->tm_hour+8, p->tm_min, p->tm_sec);
scanf("%d %*c %d %*c %d %d %*c %d %*c %d", &test->year, &test->month, &test->day,&test->hour, &test->minute, &test->second);
x=check_time(test->year,test->month, test->day,test->hour, test->minute, test->second,head);
}while(x==0);
if(x)
printf("Correct!\n");
char c_tmp;
while ((c_tmp = getchar() != '\n') && c_tmp != EOF);
printf("请输入事件内容(最长为50个字符):\n");
scanf("%s", test->content);
while ((c_tmp = getchar() != '\n') && c_tmp != EOF);
printf("请输入事件优先级(高级、中级、低级):\n");
scanf("%s", test->priority);
while ((c_tmp = getchar() != '\n') && c_tmp != EOF);
FILE *fp;
fp=fopen(filename, "a+");
fprintf(fp, " %04d %02d %02d %02d %02d %02d %s %s \n ",
test->year,test->month, test->day,test->hour, test->minute, test->second,test->content,test->priority);
fclose(fp);
printf("事件已成功保存!\n");
eHead->next = test;
}
void save(Schedule *head){
FILE *fp;
fp=fopen(filename, "w");
head=head->next;
while(head!=NULL){
fprintf(fp, " %04d %02d %02d %02d %02d %02d %s %s \n ",
head->year,head->month, head->day,head->hour, head->minute, head->second,head->content,head->priority);
head=head->next;
}
fclose(fp);
}
int compare(Schedule *a,Schedule *b){
char str_a[100]={0};
sprintf(str_a,"%d04%02d%02d%02d%02d%02d",a->year,a->month,a->day,a->hour,a->minute,a->second);
char str_b[100]={0};
sprintf(str_b,"%d04%02d%02d%02d%02d%02d",b->year,b->month,b->day,b->hour,b->minute,b->second);
int n=strcmp(str_a,str_b);
if(n>0){
return 1;
}
else{
return 0;
}
}
Schedule* sort(Schedule *L){
Schedule *p, *q, *next;
int temp;
p = L->next;
for( ; p != NULL; p = p->next )
{
next = p;
for( q = p->next; q; q = q->next )
{
if( compare(next,q)==1 )
next = q;
}
if( next != p )
{
temp = p->year;
p->year = next->year;
next->year = temp;
temp = p->month;
p->month = next->month;
next->month = temp;
temp = p->day;
p->day = next->day;
next->day = temp;
temp = p->hour;
p->hour = next->hour;
next->hour = temp;
temp = p->minute;
p->minute = next->minute;
next->minute = temp;
temp = p->second;
p->second = next->second;
next->second = temp;
char tem[LENGTH];
memcpy(tem,p->content,100);
memcpy(p->content,next->content,100);
memcpy(next->content,tem,100);
}
}
return L;
}
void Assign_ID(Schedule *head){
int temp=1;
for (head = head->next; head != NULL; head = head->next) {
head->ID=temp;
temp++;
}
}
void deleteByID(Schedule *head){
Schedule *before,*cur;
int temp;
printf("请输入事件ID:\n");
scanf("%d",&temp);
for(before=head,cur=head->next;cur!=NULL;before=before->next,cur=cur->next){
if(cur->ID==temp){
before->next=cur->next;
printf("删除成功\n");
break;
}
}
if(cur==NULL){
printf("未找到该事件\n");
}
}
void deleteByDate(Schedule *head){
Schedule *before,*cur;
int year;
int month;
int day;
int hour;
int minute;
int second;
printf("请输入日期:yyyy/mm/dd(例如:2018/01/09)\n");
scanf("%d %*c %d %*c %d",&year,&month,&day);
printf("请输入具体时间:hh:MM:ss(例如:23:01:00)\n");
scanf("%d %*c %d %*c %d",&hour,&minute,&second);
for(before=head,cur=head->next;cur!=NULL;cur=cur->next,before=before->next){
if(cur->day==day && cur->hour==hour && cur->minute==minute && cur->month==month && cur->second==second && cur->year==year){
before->next=cur->next;
printf("删除成功\n");
break;
}
}
if(cur==NULL){
printf("未找到该事件\n");
}
}
void deleteByContent(Schedule *head){
Schedule *before,*cur;
char temp[LENGTH];
printf("请输入事件内容:\n");
scanf("%s",temp);
for(before=head,cur=head->next;cur!=NULL;before=before->next,cur=cur->next){
if(strcmp(temp,cur->content)==0){
before->next=cur->next;
printf("删除成功\n");
break;
}
}
if(cur==NULL){
printf("未找到该事件\n");
}
}
void printInfo(Schedule *Sch){
printf("ID: %03d 日期: %04d/%02d/%02d 时间: %02d:%02d:%02d 事件:%s 优先级:%s\n",Sch->ID,Sch->year,Sch->month,Sch->day,Sch->hour,Sch->minute,Sch->second,Sch->content,Sch->priority);
}
void modifyDate(Schedule *head){
char temp[LENGTH];
printf("请输入您要查找事件内容:\n");
scanf("%s",temp);
printf("查找中...");
for(head=head->next;head!=NULL;head=head->next){
if(head==NULL){
printf("未找到该事件\n");
return;
}
if(strcmp(temp,head->content)==0){
printf("已找到:\n");
printInfo(head);
int year;
int month;
int day;
int hour;
int minute;
int second;
printf("进行修改:");
printf("请输入日期:yyyy/mm/dd(例如:2018/01/09)\n");
scanf("%d %*c %d %*c %d",&year,&month,&day);
printf("请输入具体时间:hh:MM:ss(例如:23:01:00)\n");
scanf("%d %*c %d %*c %d",&hour,&minute,&second);
head->year=year;
head->month=month;
head->day=day;
head->hour=hour;
head->minute=minute;
head->second=second;
}
}
printf("修改完成\n");
}
void travel(Schedule *head){
head=head->next;
while(head!=NULL){
printInfo(head);
head=head->next;
}
}
void findByDate(Schedule *head){
int year;
int month;
int day;
int hour;
int minute;
int second;
printf("请输入日期:yyyy/mm/dd(例如:2018/01/09)\n");
scanf("%d %*c %d %*c %d",&year,&month,&day);
printf("请输入具体时间:hh:MM:ss(例如:23:01:00)\n");
scanf("%d %*c %d %*c %d",&hour,&minute,&second);
printf("查找中...");
for(head=head->next;head!=NULL;head=head->next){
if(head->day==day && head->hour==hour && head->minute==minute && head->month==month && head->second==second && head->year==year){
printf("已找到:\n");
printInfo(head);
break;
}
}
if(head==NULL){
printf("未找到该事件\n");
}
}
void findByScheduleContent(Schedule *head){
char temp[LENGTH];
printf("请输入您要查找事件内容:\n");
scanf("%s",temp);
printf("查找中...");
for(head=head->next;head!=NULL;head=head->next){
if(strcmp(temp,head->content)==0){
printf("已找到:\n");
printInfo(head);
break;
}
}
if(head==NULL){
printf("未找到该事件\n");
}
}
void find(Schedule *head){
char decide = 'y';
int num;
while (decide != 'n')
{
printf(" ***************************************************\n");
printf(" **** 1 遍历 2 时间查找 3 事件查找 4 退出 ****\n");
printf(" ***************************************************\n");
scanf("%d", &num);
fflush(stdin);
switch (num)
{
case 1:
travel(head);
break;
case 2:
findByDate(head);
break;
case 3:
findByScheduleContent(head);
break;
default:
decide = 'n';
break;
}
}
}
void ScheduleDelete(Schedule *head){
char decide = 'y';
int num;
while (decide != 'n')
{
printf(" ***************************************************\n");
printf(" **** 1 时间删除 2 事件删除 3 ID删除 4 退出 ****\n");
printf(" ***************************************************\n");
scanf("%d", &num);
fflush(stdin);
switch (num)
{
case 1:
deleteByDate(head);
break;
case 2:
deleteByContent(head);
break;
case 3:
deleteByID(head);
break;
default:
decide = 'n';
break;
}
}
}
long long int difpsec(){
int monthDays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int judg = 0;
time_t t;
struct tm* llt;
time(&t);
llt = localtime(&t);
int flag = 0,sumday=0,year,md,difyear;
int pyear = llt->tm_year + 1900 - 2022;
int pmon = llt->tm_mon + 1-1;
int pday = llt->tm_mday-1;
int phour = llt->tm_hour;
int pmin = llt->tm_min;
for (difyear = 0; difyear <= pyear; difyear++) {
if (difyear == 0) {
if (((llt->tm_year+1900) % 400 == 0) || ((llt->tm_year + 1900) % 100 != 0 && (llt->tm_year + 1900) % 4 == 0))
{
monthDays[1] = 29;
}
for (md = 0; md <= pmon; md++) {
sumday = sumday + monthDays[md];
}
sumday = sumday + pday;
}
else if (difyear >= 1) {
year = llt->tm_year + 1900 - difyear;
if ((year % 400 == 0) || (year % 100 != 0 && year % 4 == 0))
{
sumday = sumday + 366;
}
else {
sumday = sumday + 365;
}
}
}
long long int sec = 0;
sec = ((sumday * 24 + phour) * 60 + pmin) * 60+llt->tm_sec;
return sec;
}
void *alert(void *arg) {
while(1){
Schedule *Head;
Head = init();
Head->next = NULL;
FILE *fp;
int Flag = 0, count = 0;
if((fp = fopen(filename, "a+")) == NULL){
printf("Can not open file\n");
}
while(!feof(fp))
{
Flag = fgetc(fp);
if(Flag == '\n'){
count++;
}
}
fclose(fp);
for (int i = 0; i < count; i++)
{
load(Head,i);
}
Head=sort(Head);
Assign_ID(Head);
time_t t;
struct tm* p;
time(&t);
p = localtime(&t);
int flag=0;
long long int sec = 0,psec=0;
int monthDays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
Head=Head->next;
while(Head != NULL){
int sumday=0,year=0,md=0,difyear=0;
int pyear = Head->year - 2022;
int pmon = Head->month -1;
int pday = Head->day-1;
int phour = Head->hour;
int pmin = Head->minute;
psec=difpsec();
for (difyear = 0; difyear <= pyear; difyear++) {
if (difyear == 0) {
if ((Head->year % 400 == 0) || (Head->year % 100 != 0 && Head->year % 4 == 0))
{
monthDays[1] = 29;
}
for (md = 0; md <= pmon; md++) {
sumday = sumday + monthDays[md];
}
sumday = sumday + pday;
}
else if (difyear >= 1) {
year = Head->year - difyear;
if ((year % 400 == 0) || (year % 100 != 0 && year % 4 == 0))
{
sumday = sumday + 366;
}
else {
sumday = sumday + 365;
}
}
}
sec = ((sumday * 24 + phour) * 60 + pmin) * 60+Head->second;
if (flag==0 && (sec-psec)<=1800 && (sec-psec)>=0) {
printf("\n\n ****************************************************\n");
printf("************* 你有如下事件将要完成: *************\n");
printf("***** 日期: %04d/%02d/%02d 时间: %02d:%02d:%02d 事件:%s *****\n ",Head->year, Head->month, Head->day,
Head->hour,Head->minute, Head->second, Head->content);
printf(" ****************************************************\n");
flag = 1;
}
while (flag==1 && difpsec()<=sec)
{
}
Head = Head->next;
}
while (flag==0)
{
sleep(60);
break;
}
}
}
void menu(){
pthread_t th2;
char decide = 'y';
int num = 0;
Schedule *head;
head = init();
head->next = NULL;
FILE *fp;
int flag = 0, count = 0;
if((fp = fopen(filename, "a+")) == NULL){
printf("Can not open file\n");
}
while(!feof(fp))
{
flag = fgetc(fp);
if(flag == '\n'){
count++;
}
}
fclose(fp);
for (int i = 0; i < count; i++)
{
load(head,i);
}
head=sort(head);
Assign_ID(head);
pthread_create(&th2,NULL,alert,NULL);
while (decide != 'n')
{
printf(" ****************************************************\n");
printf(" ********** 日程管理系统 ********\n");
printf(" ****************************************************\n");
printf(" ********** 1 输入 2 查找 3 修改 ********\n");
printf(" ********** 4 删除 5 退出 ********\n");
printf(" ****************************************************\n");
sleep(3);
scanf("%d", &num);
switch (num)
{
case 1:
insert(head,head);
head=sort(head);
Assign_ID(head);
break;
case 2:
find(head);
break;
case 3:
modifyDate(head);
save(head);
break;
case 4:
ScheduleDelete(head);
head=sort(head);
Assign_ID(head);
save(head);
break;
default:
decide = 'n';
break;
}
}
pthread_detach(th2);
}
void Insert(User* eHead,User *head) {
for (; eHead->next != NULL; eHead = eHead->next);
User* test = (User*)malloc(sizeof(User));
test->next = NULL;
int judg=0;
char choice[1];
extern char filename[LENGTH];
printf("是新用户么?(输入Y/N,不区分大小写)\n");
scanf("%s",choice);
if (strcmp("Y",choice)==0||strcmp(choice,"y")==0)
{
while(judg==0){
printf("请输入用户名:\n");
scanf("%s",test->name);
judg=check_New(head,test->name);
if (judg==1)
{
strcpy(filename,test->name);
break;
}
printf("当前用户名已被使用,请重新输入\n");
}
char c_tmp;
while ((c_tmp = getchar() != '\n') && c_tmp != EOF);
if(judg){
printf("请输入密码:\n");
char key[LENGTH]={'\0'};
scanf("%s",key);
printf("%s",key);
strcpy(test->password,key);
int i=0,len=strlen(key);
while (i<len)
{
key[i]=key[i]+i+5;
i++;
}
strcpy(test->HASH_Password,key);
}
FILE *fp;
fp=fopen("userInfo", "a+");
fprintf(fp,"%s %s\n",test->name,test->HASH_Password);
fclose(fp);
printf("用户已成功注册!\n");
menu();
}
else if(strcmp("N",choice)==0||strcmp(choice,"n")==0){
printf("请输入用户名:\n");
while(judg==0){
scanf("%s",test->name);
judg=check_name(head,test->name);
if(judg==1){
strcpy(filename,test->name);
break;
}
printf("用户名不正确,请重新输入:\n");
}
judg=0;
char c_tmp;
while ((c_tmp = getchar() != '\n') && c_tmp != EOF);
printf("请输入密码:\n");
while(judg==0){
char KEY[LENGTH];
scanf("%s",KEY);
judg=check_key(head,test->name,KEY,test->HASH_Password);
printf("%d\n",judg);
if(judg==1){
break;
}
printf("用户密码不正确,请重新输入:\n");
}
if(judg==1){
printf("用户成功登录!\n");
menu();
}
}
eHead->next = test;
}
int main()
{
time_t timep;
time(&timep);
struct tm *p;
p = gmtime(&timep);
printf("当前时间是:");
printf("%d-%02d-%02d %02d:%02d:%02d\n", 1900 + p->tm_year, 1 + p->tm_mon, p->tm_mday, p->tm_hour+8, p->tm_min, p->tm_sec);
char Decide = 'y';
User *eHead;
eHead = Init();
eHead->next = NULL;
FILE *fp;
int flag = 0, count = 0;
if((fp = fopen("userInfo", "a+")) == NULL){
printf("Can not open file\n");
}
while(!feof(fp))
{
flag = fgetc(fp);
if(flag == '\n')
count++;
}
fclose(fp);
for (int i = 0; i < count; i++)
{
Load(eHead,i);
}
while (Decide != 'n'){
printf("\n\n");
printf("\t*******************************\n");
printf("\t********* 用户登录 *********\n");
printf("\t*******************************\n");
printf("\n\n");
Insert(eHead,eHead);
return 0;
}
}
Cmake文件
cmake_minimum_required(VERSION 3.10.2)
# set the project name
project(Schedule_Management)
set(CMAKE_BUILD_TYPE "Debug")
# add the executable
add_executable(Schedule_Management Schedule.cc)
set(CMAKE_CXX_FLAGS -lunistd)
set(CMAKE_CXX_FLAGS -pthread)
message(STATUS "CMAKE_CXX_FLAGS = ${CMAKE_CXX_FLAGS}")