📄 baggage.c
字号:
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<fcntl.h>#include<signal.h>#include<curses.h>#include<malloc.h>#include<time.h>#include<errno.h>#include<unistd.h>#include<pwd.h>#define ROW 7#define ROWS 14#define MAXSTA 10000#define INROW 22#define MSGROW 23#define BEGINYEAR 1970#define DSEC 86400#define HSEC 3600#define MSEC 60struct time{ int second; /* 0--59 */ int minute; /* 0--59 */ int hour; /* 0--23 */ int mday; /* 1--31 */ int month; /* 1--12 */ int year; /* 1970-- */ int wday; /* 0--6(0=Sunday)*/ int yday; /* 1--365(366) */ long tseconds; /* seconds from 1970.1.1,0:0:0 */};struct station{ int no; char code[5]; char name[10]; unsigned int howfar;}sta[MAXSTA];int nsta;struct ticket{ int bus; int sta; unsigned user; /* sale user */ char flag; /* 0-all 1-half */ int date; /* date saled */ int time; /* time saled */ char checkflag; /* checked or not */ unsigned number; /* number of ticket */ int seat; /* seat of ticket in bus */ int price; /* all price or half price */ int ruser; /* return user(normal -1) */ int rdate; /* date returned */ int rtime; /* time returned */ unsigned rprice; /* price returned */}tic; struct busstate{ int bus; int time; char regi; int regiuser; int regitime; char check; int gate; int checkuser; int checkbegintime; int checkendtime; char print; int printuser; int printtime; char settle; int owner; int rate; int setleuser; int setletime;}bs;struct baggage{ int bus; int seat; int sta; int user; int date; int time; char checkflag; char model; int npiece; int weight; unsigned price; unsigned lift; int ruser; int rdate; int rtime; unsigned rprice;}bag;struct userdata{ int user; int date; int bus; char flag; int time; int seat; int sta; unsigned price; unsigned fee; unsigned rprice; unsigned number;};enum Key{ NONE,Up,Down,Right,Left,Center,End,PgDn,Home, PgUp,Ins,F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12};char *title[]={"*** 行包系统 ***", "1--办理行包 2--退办行包 Q--退出", "----------------------------------------" "---------------------------------------" };char *RP,*PT;int uno;int pretime=0;char getkey();main(){ char key,quit=0; RP=getenv("ROOTPATH"); if(RP==NULL) { showmsg("环境变量'ROOTPATH'未初始化!"); return -1; } PT=getenv("PRETIME"); if(PT!=NULL) pretime=atoi(PT); uno=getuid(); nsta=readsta(); if(nsta==-1) return -1; signal(SIGINT,SIG_IGN); initscr(); raw(); nonl(); noecho(); clear(); mvaddstr(ROW-4,40-strlen(title[0])/2,title[0]); mvaddstr(ROW-2,40-strlen(title[1])/2,title[1]); mvaddstr(ROW-1,0,title[2]); mvaddstr(ROW+ROWS,0,title[2]); refresh(); while(quit==0) { move(ROW-2,41+strlen(title[1])/2); refresh(); key=getkey(); if(key!=-1) { clrscr(); clrin(); clrmsg(); } if(key>='A'&&key<='Z') key=key-'A'+'a'; switch(key) { case -1: break; case '1': baggage(); break; case '2': retbag(); break; case 'q': quit=1; break; default: break; } } erase(); clear(); refresh(); endwin();}baggage(){ long tn; struct time t; int date,tt,bus=0,seat=0,model=0,npiece=1,weight=100; int i,n; char s[80]; while(1) { clrscr(); showtitle(); tn=time(0); gettime(tn,&t); date=t.year*10000+t.month*100+t.mday; tt=t.hour*100+t.minute; bag.date=date; bag.time=t.hour*100+t.minute; while(1) { n=get_n(ROW+3,35,"",&date,10); clrin(), clrmsg(); if(n==-1) break; if(checkdate(date)==0) break; else showmsg("错误的日期, 请重新输入!"); } if(n==-1) return; if(get_n(ROW+4,35,"",&bus,20)==-1) continue; if(get_n(ROW+5,35,"",&seat,20)==-1) continue; if(readtic(date,bus,seat,&tic)==-1) continue; if(readbs(date,tic.bus,&bs)==-1) continue; if(tic.ruser!=-1) { showmsg("该票已办理过退票手续, 不能办理行包!"); continue; } if(tic.checkflag!=0) { showmsg("该票已经检过, 不能再办理行包!"); continue; } if((readbag(date,tic.bus,tic.seat,&bag))!=-1) { showmsg("该票已办理过行包, 不能重复办理!"); continue; } if(date==bag.date&&bs.time<=tt) { sprintf(s,"行包应于发车前办理, 现在时间已过, 不能受理!"); showmsg(s); continue; } /* if(date==bag.date&&bs.time<=tt+pretime) { sprintf(s,"行包应于发车%d分钟前办理, 现在时间已过, 不能受理!",pretime); showmsg(s); continue; }*/ /*mvprintw(ROW+5,35,"%d",tic.bus); mvprintw(ROW+6,35,"%d",tic.seat);*/ for(i=0;i<nsta&&tic.sta!=sta[i].no;i++) ; if(i<nsta) mvaddstr(ROW+6,35,sta[i].name); else mvaddstr(ROW+6,35,"(none)"); refresh(); if(get_n(ROW+7,35,"",&model,5)==-1) continue; if(get_n(ROW+8,35,"",&npiece,5)==-1) continue; if(get_n(ROW+9,35,"",&weight,5)==-1) continue; bag.bus=tic.bus; bag.seat=tic.seat; bag.sta=tic.sta; bag.user=uno; bag.checkflag=0; bag.model=model; bag.npiece=npiece; bag.weight=weight; bag.price=tic.price*bag.weight*12/1000; if(bag.price%10!=0) bag.price+=10-bag.price%10; if(bag.weight<30) bag.lift=240; else if(bag.weight%10==0) bag.lift=bag.weight*8; else bag.lift=8*(bag.weight+10-(bag.weight%10)); mvprintw(ROW+10,35,"%d.%02d(元)",(bag.price+bag.lift)/100, (bag.price+bag.lift)%100); refresh(); bag.ruser=-1; refresh(); s[0]='\0'; if(get_s(INROW,30,"上述数据正确吗(y/n)? ",s,10)==-1) { showmsg("你取消了此次行包!"); continue; } if(s[0]!='y'&&s[0]!='Y') { showmsg("你取消了此次行包!"); continue; } if(writebag(date,bag)==0&&writeud(date,bag,tic)==0) { print(bag); showmsg("行包办理成功!"); } }}showtitle(){ mvaddstr(ROW+3,30,"日期:"); /*mvaddstr(ROW+4,30,"票号:");*/ mvaddstr(ROW+4,30,"班次:"); mvaddstr(ROW+5,30,"座号:"); mvaddstr(ROW+6,30,"到站:"); mvaddstr(ROW+7,30,"货名:"); mvaddstr(ROW+8,30,"件数:"); mvaddstr(ROW+9,30,"重量:"); mvaddstr(ROW+10,30,"金额:"); refresh();}int checkdate(int date){ int year,month,day; short mdays[2][12]={31,28,31,30,31,30,31,31,30,31,30,31, 31,29,31,30,31,30,31,31,30,31,30,31}; long tn; struct time t; year=date/10000; month=date%10000/100; day=date%100; tn=time(0); gettime(tn,&t); if(year<t.year||year>t.year+1) return -1; if(month<1||month>12) return -1; if(year%4==0) { if(day<1||day>mdays[1][month-1]) return -1; } else if(day<1||day>mdays[0][month-1]) return -1; return 0;}int writebag(int date,struct baggage bag){ char fn[80]; int fd; long size,ss; sprintf(fn,"%s/data/baggage/%d",RP,date); fd=open(fn,O_RDWR|O_APPEND); if(fd==-1) { fd=open(fn,O_CREAT|O_RDWR,0666); if(fd==-1) { showmsg("行包文件无法打开, 数据不能写入!"); return -1; } else chmod(fn,0666); } while(lockf(fd,F_TLOCK,0L)!=0) ; size=write(fd,(char *)&bag,sizeof(struct baggage)); close(fd); if(size!=sizeof(struct baggage)) { showmsg("行包数据写入时有错误, 办理失败!"); return -1; } else return 0;}int writeud(int date,struct baggage bag,struct ticket tic){ struct userdata ud; char fn[80]; int fd; long size,ss; long tn; struct time t; int dd; tn=time(0); gettime(tn,&t); dd=t.year*10000+t.month*100+t.mday; ud.user=bag.user; ud.date=date; ud.bus=bag.bus; if(bag.ruser==-1) ud.flag=2; else ud.flag=-2; ud.time=t.hour*100+t.minute; ud.seat=bag.seat; ud.sta=bag.sta; ud.price=bag.price; ud.fee=bag.lift; if(ud.flag==2) ud.rprice=0; else ud.rprice=bag.rprice; ud.number=tic.number; sprintf(fn,"%s/data/user/%d",RP,dd); fd=open(fn,O_RDWR|O_APPEND); if(fd==-1) { fd=open(fn,O_CREAT|O_RDWR,0666); if(fd==-1) { showmsg("行包文件无法打开, 数据不能写入!"); return -1; } else chmod(fn,0666); } while(lockf(fd,F_TLOCK,0L)!=0) ; size=write(fd,(char *)&ud,sizeof(struct userdata)); close(fd); if(size!=sizeof(struct userdata)) { showmsg("数据写入行包员文件时有错误, 办理失败!"); return -1; } else return 0;}print(struct baggage bag){ printf("%c{",0x1b);printf("\n\n\n\n\n\n\n\n\n\n\n %d %d.%02d",bag.npiece,(bag.price+bag.lift)/100,(bag.lift+bag.price)%100);printf("\n\n\n"); printf("%c}",0x1b);return;}retbag(){ long tn; struct time t; int date,dd,tt,busno=0,seat=1; int i,n,locate; char s[10]="";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -