最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

交通罚单管理系统

IT培训 admin 0浏览 0评论

交通罚单管理系统

交通罚单管理系统

大一时的C语言程序设计的课设,当时上课没怎么听,在网络和同学的帮助下完成的,现在看来代码不足很多,贴在这留个纪念吧。主要实现功能就是对交通罚单(一个txt文件)的增删查改,交通罚单的txt文件需要自己准备放在工程目录下。

#define NUM 100
#define TEST 28
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
#include<stdlib.h>typedef struct
{char driver[10];int car;char police[10];int num;int year;int month;int date;
}Traffic;void Output(Traffic trafList[],int i);
void Testcreat(Traffic trafList[],int n);
int menu_select();
void Display_Main_Menu();
int Input(Traffic trafList[],int n);
void Display(Traffic trafList[],int n);
int Delete_a_record(Traffic trafList[],int n);
int Delete(Traffic trafList[],int n,char *name);
void Sort_by_Name(Traffic trafList[],int n);
int Insert(Traffic trafList[],int n,Traffic *s);
int Insert_a_record(Traffic trafList[],int n);
int Query(Traffic trafList[],int n,char *name);
void Query_a_record(Traffic trafList[],int n);
void WritetoText(Traffic trafList[],int n,char *filename);
int AddfromText(Traffic trafList[],int n,char *filename);main()
{char filename[30];int n=0;Traffic trafList[NUM];for(;;){switch(menu_select()){case 1:n=Input(trafList,n);system("pause");break;case 2:Display(trafList,n);system("pause");break;case 3:n=Delete_a_record(trafList,n);system("pause");break;case 4:Sort_by_Name(trafList,n);system("pause");break;case 5:n=Insert_a_record(trafList,n);system("pause");break;case 6:Query_a_record(trafList,n);system("pause");break;case 7:printf("输入数据文件名(含拓展名):");getchar();gets(filename);n=AddfromText(trafList,n,filename);system("pause");break;case 8:printf("输出数据文件名(含拓展名):");getchar();gets(filename);WritetoText(trafList,n,filename);system("pause");break;case 0:exit(0);system("pause");break;}}
}void Output(Traffic trafList[],int i)
{printf("text-%03d\n",i+1);printf("Driver's name:\t%s\n",trafList[i].driver);printf("Car number  :\t%d\n",trafList[i].car);printf("Police's name:\t%s\n",trafList[i].police);printf("Note number :\t%04d\n",trafList[i].num);printf("Date        :\t%d-",trafList[i].year);printf("%02d-",trafList[i].month);printf("%02d",trafList[i].date);printf("\n\n");}int menu_select()
{
char c;
do
{system("cls");Display_Main_Menu();c=getchar();
}
while(c>'8'||c<'0');
return(c-'0');
}void Display_Main_Menu()				
{printf("1.Input Records\n");
printf("2.Display all records\n");
printf("3.Delete a record\n");
printf("4.Sort\n");
printf("5.Insert a record\n");
printf("6.Query\n");
printf("7.Add records from a TextFile\n");
printf("8.Write to a Text File\n");
printf("0.Quit\n");
printf("Give Your Choice(0-8):");
}int Input(Traffic trafList[],int n)
{
int i;for(;i!=2;){printf("Input drivers's name:");scanf("%s",trafList[n].driver);printf("Input car number:");scanf("%d",&trafList[n].car);printf("Input police's name:");scanf("%s",trafList[n].police);printf("Input text number:");scanf("%d",&trafList[n].num);printf("Input Year:");scanf("%d",&trafList[n].year);printf("Input Month:");scanf("%d",&trafList[n].month);printf("Input Date:");scanf("%d",&trafList[n].date);printf("成功写入一条信息!\n");n++;printf("继续添加信息? \n");printf("1 - 是\t\t2 - 否\n");scanf("%d",&i);}return n;
}void Display(Traffic trafList[],int n)		
{
int i,r,d;for(i=0,r=1;i<n;i++,r++){Output(trafList,i);if (r%10==0&&i>1) {printf("输入 1 显示下一页\n输入 2 返回主菜单:\t");scanf("%d",&d);if(d==2) break;}};
}/*void Testcreat(Traffic trafList[],int n)	
{getchar();gets(trafList[0].driver);trafList[0].car=10086;trafList[0].police[0]='p';trafList[0].police[1]='\0';trafList[0].num=0001;trafList[0].year=2014;trafList[0].month=4;trafList[0].date=18;gets(trafList[1].driver);trafList[1].car=10010;trafList[1].num=0002;trafList[1].police[0]='p';trafList[1].police[1]='\0';trafList[1].year=2014;trafList[1].month=5;trafList[1].date=1;gets(trafList[2].driver);trafList[2].car=10000;trafList[2].police[0]='p';trafList[2].police[1]='\0';	trafList[2].num=0003;trafList[2].year=2014;trafList[2].month=4;trafList[2].date=1;*/
/*int i,j;for(i=n;i<TEST;i++){for(j=0;j<3;j++) trafList[i].driver[j]='d'+j;trafList[i].driver[j]='\0';trafList[i].car=10000+i;for(j=0;j<3;j++)  trafList[i].police[j]='P'+j;trafList[i].police[j]='\0';trafList[i].num=i+1;trafList[i].year=2014;trafList[i].month=4;trafList[i].date=17;}printf("写入信息成功!\n");
}*/int Delete_a_record(Traffic trafList[],int n)
{int i;char name[10];printf("输入驾驶员姓名:");getchar();gets(name);printf("确认删除?");printf("1 - 是\t\t2 - 否\n");scanf("%d",&i);if(i==1) n=Delete(trafList,n,name);return n;}int Delete(Traffic trafList[],int n,char *name)
{
int i,j;for(i=0;i<n;i++){if(strcmp(trafList[i].driver,name)==0) break; //查找到输入的驾驶员姓名对应的下标}if(i==n) {printf("查无此人!");return n;}for(j=i;j<n;j++){trafList[j]=trafList[j+1];       //后一条数据覆盖前一条数据}printf("删除成功!");return n-1;
}void Sort_by_Name(Traffic trafList[],int n)
{int p,q,i;
Traffic temp;for(p=0;p<n-1;p++){i=p;for(q=p+1;q<n;q++){if(strcmp(trafList[i].driver,trafList[q].driver)>0)    /*选择法按姓名排序*/{temp=trafList[i];trafList[i]=trafList[q];trafList[q]=temp;}}}/*for(i=0;i<n-1;i++)for(j=0;j<n-1-i;j++)if(strcmp(trafList[j].driver,trafList[j+1].driver)<0){t=trafList[j];trafList[j]=trafList[j+1];trafList[j]=t;}
*//*
for(i=0;i<n-1;i++)
{p=i;for(j=i+1;j<n;j++)if(strcmp(trafList[j].driver,trafList[p].driver)<0)p=j;if(p!=i){t=trafList[p];trafList[p]=trafList[j];trafList[j]=t;}}
*/
printf("排序完成!\n");}int Insert_a_record(Traffic trafList[],int n)
{
Traffic s[1];printf("Input drivers's name:");scanf("%s",trafList[n].driver);printf("Input car number:");scanf("%d",&trafList[n].car);printf("Input police's name:");scanf("%s",trafList[n].police);printf("Input text number:");scanf("%d",&trafList[n].num);printf("Input Year:");scanf("%d",&trafList[n].year);printf("Input Month:");scanf("%d",&trafList[n].month);printf("Input Date:");scanf("%d",&trafList[n].date);printf("成功写入一条信息!\n");
s[0]=trafList[n];n++;
Insert(trafList,n,s);
return n;
}int Insert(Traffic trafList[],int n,Traffic *s)
{
int i,j;Traffic t;for(i=1;i<n;i++){t=trafList[i];for(j=i-1;j>=0&&strcmp(t.driver,trafList[j].driver)<=0;j--)trafList[j+1]=trafList[j];trafList[j+1]=t;}/*	
int i,j,k;
Traffic t;for(i=1;i<n;i++){t=trafList[i];for(j=0;j<i&&(strcmp(t.driver,trafList[i].driver)>=0);j++);for(k=i;k>j;k--)trafList[k]=trafList[k-1];trafList[j]=t;}
*/
return n;
}void Query_a_record(Traffic trafList[],int n)
{int k;char name[10];getchar();printf("输入待查找驾驶员姓名:");gets(name);k=Query(trafList,n,name);//printf("返回下标:%d\n",k);if(k!=-1) Output(trafList,k);
}int Query(Traffic trafList[],int n,char *name)
{int i;for(i=0;i<n;i++){if(strcmp(trafList[i].driver,name)==0){printf("成功找到一条信息!\n");return i;}}printf("查无此人!\n");return -1;}void WritetoText(Traffic trafList[],int n,char *filename)
{
FILE *fp;
int i;
fp=fopen(filename,"w");
if(fp==NULL)
{printf("Can not open file %s!\n",filename);exit(1);}for(i=0;i<n;i++)
{/*fprintf(fp,"%s\n",trafList[i].driver);fprintf(fp,"%d\n",trafList[i].car);fprintf(fp,"%s\n",trafList[i].police);fprintf(fp,"%d\n",trafList[i].num);fprintf(fp,"%d\n",trafList[i].year);fprintf(fp,"%d\n",trafList[i].month);fprintf(fp,"%d\n",trafList[i].date);*/fprintf(fp,"Text-%d\n",trafList[i].num);fprintf(fp,"%s\n",trafList[i].driver);fprintf(fp,"Car number  :\t%d\n",trafList[i].car);fprintf(fp,"Police's name:\t%s\n",trafList[i].police);fprintf(fp,"Note number :\t%04d\n",trafList[i].num);fprintf(fp,"Date        :\t%d-",trafList[i].year);fprintf(fp,"%02d-",trafList[i].month);fprintf(fp,"%02d",trafList[i].date);fprintf(fp,"\n\n");
}
fclose(fp);
}int AddfromText(Traffic trafList[],int n,char *filename)
{
FILE *fp;
int i=0;
fp=fopen(filename,"r");
if(fp==NULL)
{printf("该文件不存在! %s!\n",filename);exit(1);}fscanf(fp,"%d",&n);
for(i=0;feof(fp)==0;i++)
{	fscanf(fp,"%s",trafList[i].driver);fscanf(fp,"%d",&trafList[i].car);fscanf(fp,"%s",trafList[i].police);fscanf(fp,"%d",&trafList[i].num);fscanf(fp,"%d",&trafList[i].year);fscanf(fp,"%d",&trafList[i].month);fscanf(fp,"%d",&trafList[i].date);
}return n;}

交通罚单管理系统

交通罚单管理系统

大一时的C语言程序设计的课设,当时上课没怎么听,在网络和同学的帮助下完成的,现在看来代码不足很多,贴在这留个纪念吧。主要实现功能就是对交通罚单(一个txt文件)的增删查改,交通罚单的txt文件需要自己准备放在工程目录下。

#define NUM 100
#define TEST 28
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
#include<stdlib.h>typedef struct
{char driver[10];int car;char police[10];int num;int year;int month;int date;
}Traffic;void Output(Traffic trafList[],int i);
void Testcreat(Traffic trafList[],int n);
int menu_select();
void Display_Main_Menu();
int Input(Traffic trafList[],int n);
void Display(Traffic trafList[],int n);
int Delete_a_record(Traffic trafList[],int n);
int Delete(Traffic trafList[],int n,char *name);
void Sort_by_Name(Traffic trafList[],int n);
int Insert(Traffic trafList[],int n,Traffic *s);
int Insert_a_record(Traffic trafList[],int n);
int Query(Traffic trafList[],int n,char *name);
void Query_a_record(Traffic trafList[],int n);
void WritetoText(Traffic trafList[],int n,char *filename);
int AddfromText(Traffic trafList[],int n,char *filename);main()
{char filename[30];int n=0;Traffic trafList[NUM];for(;;){switch(menu_select()){case 1:n=Input(trafList,n);system("pause");break;case 2:Display(trafList,n);system("pause");break;case 3:n=Delete_a_record(trafList,n);system("pause");break;case 4:Sort_by_Name(trafList,n);system("pause");break;case 5:n=Insert_a_record(trafList,n);system("pause");break;case 6:Query_a_record(trafList,n);system("pause");break;case 7:printf("输入数据文件名(含拓展名):");getchar();gets(filename);n=AddfromText(trafList,n,filename);system("pause");break;case 8:printf("输出数据文件名(含拓展名):");getchar();gets(filename);WritetoText(trafList,n,filename);system("pause");break;case 0:exit(0);system("pause");break;}}
}void Output(Traffic trafList[],int i)
{printf("text-%03d\n",i+1);printf("Driver's name:\t%s\n",trafList[i].driver);printf("Car number  :\t%d\n",trafList[i].car);printf("Police's name:\t%s\n",trafList[i].police);printf("Note number :\t%04d\n",trafList[i].num);printf("Date        :\t%d-",trafList[i].year);printf("%02d-",trafList[i].month);printf("%02d",trafList[i].date);printf("\n\n");}int menu_select()
{
char c;
do
{system("cls");Display_Main_Menu();c=getchar();
}
while(c>'8'||c<'0');
return(c-'0');
}void Display_Main_Menu()				
{printf("1.Input Records\n");
printf("2.Display all records\n");
printf("3.Delete a record\n");
printf("4.Sort\n");
printf("5.Insert a record\n");
printf("6.Query\n");
printf("7.Add records from a TextFile\n");
printf("8.Write to a Text File\n");
printf("0.Quit\n");
printf("Give Your Choice(0-8):");
}int Input(Traffic trafList[],int n)
{
int i;for(;i!=2;){printf("Input drivers's name:");scanf("%s",trafList[n].driver);printf("Input car number:");scanf("%d",&trafList[n].car);printf("Input police's name:");scanf("%s",trafList[n].police);printf("Input text number:");scanf("%d",&trafList[n].num);printf("Input Year:");scanf("%d",&trafList[n].year);printf("Input Month:");scanf("%d",&trafList[n].month);printf("Input Date:");scanf("%d",&trafList[n].date);printf("成功写入一条信息!\n");n++;printf("继续添加信息? \n");printf("1 - 是\t\t2 - 否\n");scanf("%d",&i);}return n;
}void Display(Traffic trafList[],int n)		
{
int i,r,d;for(i=0,r=1;i<n;i++,r++){Output(trafList,i);if (r%10==0&&i>1) {printf("输入 1 显示下一页\n输入 2 返回主菜单:\t");scanf("%d",&d);if(d==2) break;}};
}/*void Testcreat(Traffic trafList[],int n)	
{getchar();gets(trafList[0].driver);trafList[0].car=10086;trafList[0].police[0]='p';trafList[0].police[1]='\0';trafList[0].num=0001;trafList[0].year=2014;trafList[0].month=4;trafList[0].date=18;gets(trafList[1].driver);trafList[1].car=10010;trafList[1].num=0002;trafList[1].police[0]='p';trafList[1].police[1]='\0';trafList[1].year=2014;trafList[1].month=5;trafList[1].date=1;gets(trafList[2].driver);trafList[2].car=10000;trafList[2].police[0]='p';trafList[2].police[1]='\0';	trafList[2].num=0003;trafList[2].year=2014;trafList[2].month=4;trafList[2].date=1;*/
/*int i,j;for(i=n;i<TEST;i++){for(j=0;j<3;j++) trafList[i].driver[j]='d'+j;trafList[i].driver[j]='\0';trafList[i].car=10000+i;for(j=0;j<3;j++)  trafList[i].police[j]='P'+j;trafList[i].police[j]='\0';trafList[i].num=i+1;trafList[i].year=2014;trafList[i].month=4;trafList[i].date=17;}printf("写入信息成功!\n");
}*/int Delete_a_record(Traffic trafList[],int n)
{int i;char name[10];printf("输入驾驶员姓名:");getchar();gets(name);printf("确认删除?");printf("1 - 是\t\t2 - 否\n");scanf("%d",&i);if(i==1) n=Delete(trafList,n,name);return n;}int Delete(Traffic trafList[],int n,char *name)
{
int i,j;for(i=0;i<n;i++){if(strcmp(trafList[i].driver,name)==0) break; //查找到输入的驾驶员姓名对应的下标}if(i==n) {printf("查无此人!");return n;}for(j=i;j<n;j++){trafList[j]=trafList[j+1];       //后一条数据覆盖前一条数据}printf("删除成功!");return n-1;
}void Sort_by_Name(Traffic trafList[],int n)
{int p,q,i;
Traffic temp;for(p=0;p<n-1;p++){i=p;for(q=p+1;q<n;q++){if(strcmp(trafList[i].driver,trafList[q].driver)>0)    /*选择法按姓名排序*/{temp=trafList[i];trafList[i]=trafList[q];trafList[q]=temp;}}}/*for(i=0;i<n-1;i++)for(j=0;j<n-1-i;j++)if(strcmp(trafList[j].driver,trafList[j+1].driver)<0){t=trafList[j];trafList[j]=trafList[j+1];trafList[j]=t;}
*//*
for(i=0;i<n-1;i++)
{p=i;for(j=i+1;j<n;j++)if(strcmp(trafList[j].driver,trafList[p].driver)<0)p=j;if(p!=i){t=trafList[p];trafList[p]=trafList[j];trafList[j]=t;}}
*/
printf("排序完成!\n");}int Insert_a_record(Traffic trafList[],int n)
{
Traffic s[1];printf("Input drivers's name:");scanf("%s",trafList[n].driver);printf("Input car number:");scanf("%d",&trafList[n].car);printf("Input police's name:");scanf("%s",trafList[n].police);printf("Input text number:");scanf("%d",&trafList[n].num);printf("Input Year:");scanf("%d",&trafList[n].year);printf("Input Month:");scanf("%d",&trafList[n].month);printf("Input Date:");scanf("%d",&trafList[n].date);printf("成功写入一条信息!\n");
s[0]=trafList[n];n++;
Insert(trafList,n,s);
return n;
}int Insert(Traffic trafList[],int n,Traffic *s)
{
int i,j;Traffic t;for(i=1;i<n;i++){t=trafList[i];for(j=i-1;j>=0&&strcmp(t.driver,trafList[j].driver)<=0;j--)trafList[j+1]=trafList[j];trafList[j+1]=t;}/*	
int i,j,k;
Traffic t;for(i=1;i<n;i++){t=trafList[i];for(j=0;j<i&&(strcmp(t.driver,trafList[i].driver)>=0);j++);for(k=i;k>j;k--)trafList[k]=trafList[k-1];trafList[j]=t;}
*/
return n;
}void Query_a_record(Traffic trafList[],int n)
{int k;char name[10];getchar();printf("输入待查找驾驶员姓名:");gets(name);k=Query(trafList,n,name);//printf("返回下标:%d\n",k);if(k!=-1) Output(trafList,k);
}int Query(Traffic trafList[],int n,char *name)
{int i;for(i=0;i<n;i++){if(strcmp(trafList[i].driver,name)==0){printf("成功找到一条信息!\n");return i;}}printf("查无此人!\n");return -1;}void WritetoText(Traffic trafList[],int n,char *filename)
{
FILE *fp;
int i;
fp=fopen(filename,"w");
if(fp==NULL)
{printf("Can not open file %s!\n",filename);exit(1);}for(i=0;i<n;i++)
{/*fprintf(fp,"%s\n",trafList[i].driver);fprintf(fp,"%d\n",trafList[i].car);fprintf(fp,"%s\n",trafList[i].police);fprintf(fp,"%d\n",trafList[i].num);fprintf(fp,"%d\n",trafList[i].year);fprintf(fp,"%d\n",trafList[i].month);fprintf(fp,"%d\n",trafList[i].date);*/fprintf(fp,"Text-%d\n",trafList[i].num);fprintf(fp,"%s\n",trafList[i].driver);fprintf(fp,"Car number  :\t%d\n",trafList[i].car);fprintf(fp,"Police's name:\t%s\n",trafList[i].police);fprintf(fp,"Note number :\t%04d\n",trafList[i].num);fprintf(fp,"Date        :\t%d-",trafList[i].year);fprintf(fp,"%02d-",trafList[i].month);fprintf(fp,"%02d",trafList[i].date);fprintf(fp,"\n\n");
}
fclose(fp);
}int AddfromText(Traffic trafList[],int n,char *filename)
{
FILE *fp;
int i=0;
fp=fopen(filename,"r");
if(fp==NULL)
{printf("该文件不存在! %s!\n",filename);exit(1);}fscanf(fp,"%d",&n);
for(i=0;feof(fp)==0;i++)
{	fscanf(fp,"%s",trafList[i].driver);fscanf(fp,"%d",&trafList[i].car);fscanf(fp,"%s",trafList[i].police);fscanf(fp,"%d",&trafList[i].num);fscanf(fp,"%d",&trafList[i].year);fscanf(fp,"%d",&trafList[i].month);fscanf(fp,"%d",&trafList[i].date);
}return n;}

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论