📄 c++feiji.c
字号:
#include <stdio.h>//标准输入、输出头文件
#include <string.h> //包含字符串函数处理头文件
#include <conio.h>
#include <stdlib.h>//包含动态存储与释放函数头文件
struct FeiJiDingPiao//定义结构体数组
{
char AirNo[4];//定义航班号
char StartPos[6];//定义始发地
char EndPos[6];//定义目的地
char StartTime[14]; //yymmddwwhhmmss
char Mount[4];//定义机票数量
char Price[5];//定义票价
unsigned char a;//定义无符号变量a
};
struct FeiJiDingPiao s,ss[50];
long filesize(FILE *stream)
{
long curpos, length;
curpos = ftell(stream);
fseek(stream, 0L, SEEK_END);
length = ftell(stream);
fseek(stream, curpos, SEEK_SET);
return length;
}
void disp()//浏览信息
{ char i;
printf("\n-");for(i=0;i<4;i++) printf("%c",s.AirNo[i]);
printf("-"); for(i=0;i<6;i++) printf("%c",s.StartPos[i]);
printf("-"); for(i=0;i<6;i++) printf("%c",s.EndPos[i]);
printf("-"); for(i=0;i<14;i++) printf("%c",s.StartTime[i]);
printf("-"); for(i=0;i<4;i++) printf("%c",s.Mount[i]);
printf("-"); for(i=0;i<5;i++) printf("%c",s.Price[i]);
// printf("\n");
}
void Pstrcpy( char i)//
{ char j;
for(j=0;j<4;j++) s.AirNo[j]=ss[i].AirNo[j];
for(j=0;j<6;j++) s.StartPos[j]=ss[i].StartPos[j];
for(j=0;j<6;j++) s.EndPos[j]=ss[i].StartPos[j];
for(j=0;j<14;j++) s.StartTime[j]=ss[i].StartPos[j];
for(j=0;j<4;j++) s.Mount[j]=ss[i].StartPos[j];
for(j=0;j<5;j++) s.Price[j]=ss[i].StartPos[j];
s.a=0x0a;
}
int write_ssToFile (char count)//添加记录
{
FILE *stream;
char i;
if ((stream = fopen("record.txt", "w+")) == NULL) /* open file TEST.$$$ */
{
fprintf(stderr, "Cannot open output file.\n");
return 1;
}
for(i=0;i<count;i++)
fwrite(&ss[i], 40, 1, stream); /* write struct s to file */
fclose(stream); /* close file */
printf("\n Write Record Successed!");
getch();
return 0;
}
char Func31(char n)
{ char i;
printf("\nAppend Record!\n");
printf("\nAirNo[4]"); for(i=0;i<4;i++) ss[n].AirNo[i]=getche();
printf("\nStartPos[6]"); for(i=0;i<6;i++) ss[n].StartPos[i]=getche();
printf("\nEndPos[6]"); for(i=0;i<6;i++) ss[n].EndPos[i]=getche();
printf("\nStartTime[14]");for(i=0;i<14;i++)ss[n].StartTime[i]=getche();
printf("\nMount[4]"); for(i=0;i<4;i++) ss[n].Mount[i]=getche();
printf("\nPrice[5]"); for(i=0;i<5;i++) ss[n].Price[i]=getche();
ss[n].a=0x0a;
do{printf("\nConfirm Y/N ?");
i=getche();
}while((i!='Y')&(i!='y')&(i!='N')&(i!='n'));
if((i=='Y')|(i=='y')) return 1;
else return 0;
}
int Insert_i(char no_to_delete)//插入信息
{
FILE *stream;
// struct mystruct s;
char i,j,k=0;
long curpos,Length;
if ((stream = fopen("Record.txt", "r"))
== NULL)
{
fprintf(stderr, "Cannot open output file.\n");
return 1;
}
Length=filesize(stream);
Length/=41;
printf("\nthere are %d Records\n",Length);
if(Length>=no_to_delete)
{ for(j=0,i=0;j<Length;j++) {
curpos=41*j; //*0 *1 *2
if(j==no_to_delete){
if(Func31(i)==1) {k=1; Pstrcpy(i); disp(); i++;}
}
fseek(stream, curpos, SEEK_SET);
fread(&ss[i], 40, 1, stream);
//i++;
Pstrcpy(i); disp(); i++;
}
if(k==1) write_ssToFile(Length+1);
}
fclose(stream);
return 0;
}
int delet_i(char no_to_delete)//删除记录
{
FILE *stream;
// struct mystruct s;
char i,j;
long curpos,Length;
if ((stream = fopen("Record.txt", "r"))
== NULL)
{
fprintf(stderr, "Cannot open output file.\n");
return 1;
}
Length=filesize(stream);
Length/=41;
printf("\nthere are %d Records\n",Length);
if(Length>=no_to_delete)
{ for(j=0,i=0;j<Length;j++) {
curpos=41*j; //*0 *1 *2
if(j!=no_to_delete)
{fseek(stream, curpos, SEEK_SET);
fread(&ss[i], 40, 1, stream);
//i++;
Pstrcpy(i); disp(); i++;
}
}
write_ssToFile(Length-1);
}
fclose(stream);
return 0;
}
int disp_all(void)//浏览所有信息
{
FILE *stream;
// struct mystruct s;
char j;
long curpos,Length;
if ((stream = fopen("Record.txt", "r"))
== NULL)
{
fprintf(stderr, "Cannot open output file.\n");
return 1;
}
Length=filesize(stream);
Length/=41;
printf("\nthere are %d Records\n",Length);
for(j=0;j<Length;j++) {
curpos=41*j; //*0 *1 *2
fseek(stream, curpos, SEEK_SET);
fread(&s, 40, 1, stream);
disp();
}
fclose(stream);
return 0;
}
int Read_all(void)//读入所有信息
{
FILE *stream;
// struct mystruct s;
char j;
long curpos,Length;
if ((stream = fopen("Record.txt", "r"))
== NULL)
{
fprintf(stderr, "Cannot open output file.\n");
return 1;
}
Length=filesize(stream);
Length/=41;
printf("\nthere are %d Records\n",Length);
for(j=0;j<Length;j++) {
curpos=41*j; //*0 *1 *2
fseek(stream, curpos, SEEK_SET);
fread(&ss[j], 41, 1, stream);
//disp();
}
fclose(stream);
return Length;
}
int write_all (unsigned char n)//写入信息
{
FILE *stream;
char i;
if ((stream = fopen("record2.txt", "w")) == NULL)
{
fprintf(stderr, "Cannot open output file.\n");
return 1;
}
for(i=0;i<n;i++)
{//fseek(stream, curposT, SEEK_SET);
ss[i].a=0x0a;
fwrite(&ss[i], 40, 1, stream); /* write struct s to file */
}
fclose(stream); /* close file */
printf("\n Write All Record Successed!");
getch();
return 0;
}
long disp_i(long No)
{ //No=0/1/2/3...
FILE *stream;
// struct mystruct s;
// char i;
long curpos,Length;
if ((stream = fopen("Record.txt", "r")) //对打开文件进行测试
== NULL)
{
fprintf(stderr, "Cannot open output file.\n");
return 1;
}
Length=filesize(stream);
Length/=41;
if(Length>=No){
printf("\nNo.%d is in Records",No);
curpos=41*No; //*0 *1 *2
fseek(stream, curpos, SEEK_SET);//寻找文件
fread(&s, 40, 1, stream);//读入文件
disp();}
else printf("\nNo.%d is not in Records",No);
fclose(stream); //关闭文件
return curpos;
}
int write_rewrite (long curposT)//重新写入信息
{
FILE *stream;
// char i;
if ((stream = fopen("record.txt", "r+")) == NULL) /* 对打开文件进行测试 */
{
fprintf(stderr, "Cannot open output file.\n");
return 1;
}
fseek(stream, curposT, SEEK_SET);
fwrite(&s, 40, 1, stream); /* 将共用体s写入文件 */
fclose(stream); /* 关闭文件 */
printf("\n Append Record Successed!");
getch();
return 0;
}
int write_app(void)
{
FILE *stream;
// char i;
if ((stream = fopen("record.txt", "a+")) == NULL) /* 对打开文件进行测试 */
{
fprintf(stderr, "Cannot open output file.\n");
return 1;
}
fseek(stream, 0L, SEEK_END);
fwrite(&s, 40, 1, stream); /*将共用体s写入文件 */
fclose(stream); /* 关闭文件 */
printf("\n Append Record Successed!");
getch();
return 0;
}
char Func11(void)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -