#include
#include
#include
#include
#include
#include
#include
ofstream out("信息保存文件.txt");
#define M 30
int flag=0;
int n=0;
struct car
{
char license_num[8]; //牌照号码
char car_mark[20]; //汽车商标
char colour[10]; //汽车颜色
char registration[11]; //注册时间
char name[20]; //车主姓名
}cars[M];
int legal_license(char *s)
{
int t;
char a[3];
if(strlen(s)==7)
{
if((s[0]>='0'&&s[0]<='9')&&(s[1]>='0'&&s[1]<='9')&&(s[2]>='A'&&s[2]<='Z')&&(s[3]>='0'&&s[3]<='9')&&(s[4]>='0'&&s[4]<='9')&&(s[5]>='0'&&s[5]<='9')&&(s[6]>='0'&&s[6]<='9'))
{
a[0]=s[0];a[1]=s[1];a[2]='\0';
if(atoi(a)>=1&&atoi(a)<=31)
t=1;
else
t=0;
}
else
t=0;
}
else
t=0;
return t;
}
int leap_year(int years)
{
if(((years%4==0)&&(years0!=0))||years@0==0)
return 1;
else
return 0;
}
int legal_year(char *s)
{
int i,t;
int a,b,c;
char aa[5],bb[3],cc[3];
if(strlen(s)==10)
{
for(i=0;i<4;i++)
aa[i]=s[i];
aa[4]='\0';
for(i=5;i<7;i++)
bb[i-5]=s[i];
bb[2]='\0';
for(i=8;i<10;i++)
cc[i-8]=s[i];
cc[2]='\0';
if((s[0]>'0'&&s[0]<='9')&&(s[1]>='0'&&s[1]<='9')&&(s[2]>='0'&&s[2]<='9')&&(s[3]>='0'&&s[3]<='9')&&(s[4]=='-')&&(s[5]>='0'&&s[5]<='9')&&(s[6]>='0'&&s[6]<='9')&&(s[7]=='-')&&(s[8]>='0'&&s[8]<='9')&&(s[9]>='0'&&s[9]<='9'))
{
a=atoi(aa);
b=atoi(bb);
c=atoi(cc);
if(leap_year(a)==1)
{
if(b<=12&&b>=1)
{
if(b==2)
{
if(c<=29&&c>=1)
t=1;
else
t=0;
}
else if(b==1||b==3||b==5||b==7||b==8||b==10||b==12)
{
if(c<=31&&c>=1)
t=1;
else
t=0;
}
else
{
if(c<=30&&c>=1)
t=1;
else
t=0;
}
}
else
t=0;
}
if(leap_year(a)==0)
{
if(b<=12&&b>=1)
{
if(b==2)
{
if(c<=28&&c>=1)
t=1;
else
t=0;
}
else if(b==1||b==3||b==5||b==7||b==8||b==10||b==12)
{
if(c<=31&&c>=1)
t=1;
else
t=0;
}
else
{
if(c<=30&&c>=1)
t=1;
else
t=0;
}
}
else
t=0;
}
}
else
t=0;
}
else
t=0;
return t;
}
int judge_repeat(int count,char *s)
{
int i;
for(i=0;i
if(strcmp(s,cars[i].license_num)==0)
return 1;
return 0;
}
int key_inform(int amount)
{
int i,count=0;
char num[7],dates[10];
for(i=0;i
{
printf("请输入第%d辆汽车的信息:\n",i+1);
while(1)
{
printf("牌照号码(如01A4526):");
scanf("%s",num);
if(legal_license(num)==1)
{
if(count>0)
{
if(judge_repeat(count,num)==1)
printf("牌照号码已存在!请重新输入!\n");
else
{
strcpy(cars[i].license_num,num);
count++;
break;
}
}
else
{
strcpy(cars[i].license_num,num);
count++;
break;
}
}
else
{
printf("牌照号码不合法,请重新输入!\n");
}
}
printf("汽车商标:");
scanf("%s",cars[i].car_mark);
printf("颜色:");
scanf("%s",cars[i].colour);
while(1)
{
printf("注册日期(格式为:YYYY-MM-DD):");
scanf("%s",dates);
if(legal_year(dates)==1)
{
strcpy(cars[i].registration,dates);
break;
}
else
{
printf("注册日期不合法,请重新输入!\n");
}
}
printf("车主姓名:");
scanf("%s",cars[i].name);
}
n=amount;
return 1;
}
int rand_inform()
{
int a,b;
int i;
int j;
int count=0;
char c1,c2;
char mark[][10]={"雪铁龙","奇瑞","奔驰","宝马","奥迪","标致","比亚迪","东风","一汽","大众","现代"};
char colours[][5]={"白色","黄色","黑色","蓝色","绿色","青色","红色","银色","橙色","灰色","褐色","棕色"};
int y1;
char y2[5],m2[3],d2[3];
char da[11];
int len;
srand(time(NULL));
for(i=0;i
{
while(1)
{
if(count==0)
{
c1=(char)(rand()%4+48);
if(c1=='0')
c2=(char)(rand()%9+49);
else if(c1=='1'||c1=='2')
c2=(char)(rand()+48);
else
c2=(char)(rand()%2+48);
cars[i].license_num[0]=c1;
cars[i].license_num[1]=c2;
cars[i].license_num[2]=(char)(rand()&+65);
for(j=3;j<7;j++)
cars[i].license_num[j]=(char)(rand()+48);
count++;
break;
}
else
{
c1=(char)(rand()%4+48);
if(c1=='0')
c2=(char)(rand()%9+49);
else if(c1=='1'||c1=='2')
c2=(char)(rand()+48);
else
c2=(char)(rand()%2+48);
cars[i].license_num[0]=c1;
cars[i].license_num[1]=c2;
cars[i].license_num[2]=(char)(rand()&+65);
for(j=3;j<7;j++)
cars[i].license_num[j]=(char)(rand()+48);
if(judge_repeat(count,cars[i].license_num)==1)
printf("牌照号码已存在!请重新输入!\n");
else
{
count++;
break;
}
}
}
strcpy(cars[i].car_mark,mark[rand()]);
strcpy(cars[i].colour,colours[rand()]);
while(1)
{
y1=rand()!+1990;
itoa(y1,y2,10);
m2[0]=(char)(rand()%2+48);
if(m2[0]=='0')
m2[1]=(char)(rand()%9+49);
else
m2[1]=(char)(rand()%3+48);
m2[2]='\0';
a=strlen(m2);
d2[0]=(char)(rand()%4+48);
if(d2[0]=='0')
d2[1]=(char)(rand()%9+49);
else if(d2[0]=='1'||d2[0]=='2')
d2[1]=(char)(rand()+48);
else
d2[1]=(char)(rand()%2+48);
d2[2]='\0';
b=strlen(d2);
for(j=0;j<4;j++)
{
da[j]=y2[j];
}
da[4]='-';
da[5]=m2[0];
da[6]=m2[1];
da[7]='-';
da[8]=d2[0];
da[9]=d2[1];
da[10]='\0';
if(legal_year(da)==1)
{
for(j=0;j<10;j++)
cars[i].registration[j]=da[j];
cars[i].registration[j]='\0';
break;
}
}
len=rand()%5+5;
for(j=0;j
{
cars[i].name[j]=(char)(rand()&+97);
}
cars[i].name[j]='\0';
}
n=M;
return 1;
}
int file_inform()
{
int i=0;
FILE *fp;
if((fp=fopen("T923-1-18.txt","r"))==NULL)
{
printf("文件打开失败!\n");
exit(0);
}
while((!feof(fp))&&i
{
fscanf(fp,"%s%s%s%s%s",cars[i].license_num,cars[i].car_mark,cars[i].colour,cars[i].registration,cars[i].name);
i++;
}
if(fclose(fp))
{
printf("不能关闭文件!\n");
exit(0);
}
n=i;
return 1;
}
void show_inform()
{
int i;
printf("车辆信息如下:\n");
printf("牌照号码\t汽车商标\t颜色\t注册日期\t车主姓名\n");
for(i=0;i
{
printf("%s\t\t",cars[i].license_num);
printf("%s\t\t",cars[i].car_mark);
printf("%s\t",cars[i].colour);
printf("%s\t",cars[i].registration);
printf("%s\n",cars[i].name);
}
}
void save_to_file(char *s)
{
for(int i=0;i
{
out<<" "<<cars[i].license_num<<" "<<cars[i].car_mark<<" "<<cars[i].colour<<" "<<cars[i].registration<<" "<<cars[i].name;
}
}
void radix_sort(int amount)
{
int i,k,m;
char j;
struct car cars1[M];
for(i=6;i>=3;i--)
{
m=0;
for(j='0';j<='9';j++)
{
for(k=0;k
{
if(cars[k].license_num[i]==j)
{
strcpy(cars1[m].license_num,cars[k].license_num);
strcpy(cars1[m].car_mark,cars[k].car_mark);
strcpy(cars1[m].colour,cars[k].colour);
strcpy(cars1[m].registration,cars[k].registration);
strcpy(cars1[m].name,cars[k].name);
m++;
}
}
}
for(k=0;k
{
strcpy(cars[k].license_num,cars1[k].license_num);
strcpy(cars[k].car_mark,cars1[k].car_mark);
strcpy(cars[k].colour,cars1[k].colour);
strcpy(cars[k].registration,cars1[k].registration);
strcpy(cars[k].name,cars1[k].name);
}
}
m=0;
for(j='A';j<='Z';j++)
{
for(k=0;k
{
if(cars[k].license_num[2]==j)
{
strcpy(cars1[m].license_num,cars[k].license_num);
strcpy(cars1[m].car_mark,cars[k].car_mark);
strcpy(cars1[m].colour,cars[k].colour);
strcpy(cars1[m].registration,cars[k].registration);
strcpy(cars1[m].name,cars[k].name);
m++;
}
}
}
for(k=0;k
{
strcpy(cars[k].license_num,cars1[k].license_num);
strcpy(cars[k].car_mark,cars1[k].car_mark);
strcpy(cars[k].colour,cars1[k].colour);
strcpy(cars[k].registration,cars1[k].registration);
strcpy(cars[k].name,cars1[k].name);
}
for(i=1;i>=0;i--)
{
m=0;
for(j='0';j<='9';j++)
{
for(k=0;k
{
if(cars[k].license_num[i]==j)
{
strcpy(cars1[m].license_num,cars[k].license_num);
strcpy(cars1[m].car_mark,cars[k].car_mark);
strcpy(cars1[m].colour,cars[k].colour);
strcpy(cars1[m].registration,cars[k].registration);
strcpy(cars1[m].name,cars[k].name);
m++;
}
}
}
for(k=0;k
{
strcpy(cars[k].license_num,cars1[k].license_num);
strcpy(cars[k].car_mark,cars1[k].car_mark);
strcpy(cars[k].colour,cars1[k].colour);
strcpy(cars[k].registration,cars1[k].registration);
strcpy(cars[k].name,cars1[k].name);
}
}
}
int binary_search(int low,int high,char *s)
{
int mid;
while(low<=high)
{
mid=(low+high)/2;
if(strcmp(s,cars[mid].license_num)==0)
{
return mid;
}
else if(strcmp(s,cars[mid].license_num)>0)
{
low=mid+1;
binary_search(low,high,s);
}
else
{
high=mid-1;
binary_search(low,high,s);
}
}
return -1;
}
void usinghelp()
{
system("cls");
printf("\n\n");
printf("\t=========================================================\n");
printf("\t 帮助菜单 \n");
printf("\t=========================================================\n");
printf("\t 一辆汽车的使用信息包括五项,分别为牌照号码、汽车商 \n");
printf("\t 标、颜色、注册日期和车主的姓名。牌照号码由大写字母和数 \n");
printf("\t 字组成共7位,前两位为01—31(代表地区),第三位为A—Z \n");
printf("\t (代表车的使用类型),后四位为0000—9999(代表车号), \n");
printf("\t 例如:01A1452。输入数据时,需输入合法的牌照号码,并且 \n");
printf("\t 不能输入重复的数据,否则会提示错误。注册日期也一样需输 \n");
printf("\t 入合法的日期,否则也会提示错误。 \n");
printf("\t=========================================================\n");
}
//主菜单显示
void choicemenu()
{
printf("\t ================================\n");
printf("\t 功 能 菜 单 \n");
printf("\t ================================\n");
printf("\t 1.键盘输入信息 \n");
printf("\t 2.随机产生信息 \n");
printf("\t 3.文件导入信息 \n");
printf("\t 4.显示信息 \n");
printf("\t 5.查找信息 \n");
printf("\t 6.使用帮助 \n");
printf("\t 0.退出 \n");
printf("\t ================================\n");
printf("请输入您的选择:");
}
void main(void)
{
char s[8];
int choose;
while(1)
{
system("cls");
choicemenu();
scanf("%d",&choose);
switch(choose)
{
case 1:
while(1)
{
printf("请输入车辆的数量(30辆以内):");
scanf("%d",&n);
if(n>=0&&n<=30)
break;
else
printf("您输入的车辆数量不在0-30内!请重新输入!\n");
}
flag=key_inform(n);
radix_sort(n);
printf("信息输入成功!请按任意键继续...\n");
getch();
Sleep(200);
break;
case 2:
flag=rand_inform();
radix_sort(n);
printf("信息产生成功!请按任意键继续...\n");
getch();
Sleep(200);
break;
case 3:
flag=file_inform();
radix_sort(n);
printf("信息导入成功!请按任意键继续...\n");
getch();
Sleep(200);
break;
case 4:
if(flag=1&&n!=0)
{
show_inform();
printf("请按任意键继续...\n");
getch();
Sleep(200);
break;
}
else
{
printf("信息为空!显示失败!请按任意键继续...\n");
getch();
Sleep(200);
break;
}
case 5:
if
(flag=1&&n!=0)
{
printf("请输入要查找的车牌号码:");
scanf("%s",s);
flag=binary_search(0,n-1,s);
if(flag>=0)
{
printf("查找成功!信息如下:\n");
printf("%s\t\t",cars[flag].license_num);
printf("%s\t\t",cars[flag].car_mark);
printf("%s\t",cars[flag].colour);
printf("%s\t",cars[flag].registration);
printf("%s\n",cars[flag].name);
}
else
printf("查找失败!");
printf("请按任意键继续...\n");
getch();
Sleep(200);
break;
}
else
{
printf("信息为空!查找失败!请按任意键继续...\n");
getch();
Sleep(200);
break;
}
case 6:
usinghelp();
printf("请按任意键继续...\n");
getch();
Sleep(200);
break;
case 0:
printf("谢谢使用!\n");
Sleep(200);
exit(0);
default:
printf("对不起,您输入的功能编号有错!请重新输入!!!\n");
printf("请按任意键返回...\n");
getch();
Sleep(200);
break;
}
}
}