汽车行驶记录仪C语言编程.

上传人:我** 文档编号:115358192 上传时间:2019-11-13 格式:DOC 页数:20 大小:41.50KB
返回 下载 相关 举报
汽车行驶记录仪C语言编程._第1页
第1页 / 共20页
汽车行驶记录仪C语言编程._第2页
第2页 / 共20页
汽车行驶记录仪C语言编程._第3页
第3页 / 共20页
汽车行驶记录仪C语言编程._第4页
第4页 / 共20页
汽车行驶记录仪C语言编程._第5页
第5页 / 共20页
点击查看更多>>
资源描述

《汽车行驶记录仪C语言编程.》由会员分享,可在线阅读,更多相关《汽车行驶记录仪C语言编程.(20页珍藏版)》请在金锄头文库上搜索。

1、汽车黑匣子项目说明1.汽车黑匣子简介汽车黑匣子,又称汽车工作信息记录仪,汽车安全信息记录仪,也有人将其形象地称为汽车电子警察。它能够完整、准确地记录汽车行驶状态下的有关情况,并通过专用软件在电脑上再现。本项目只是实现的是它的软件部分。2.项目要求1.记录汽车运行时的日期,时间和速度;2.经可能的多存一些信息:使用位段;3.每满10次(可根据用户要求更改)记录向文件中存一次;4.文件大小只有1.2K(可根据用户要求更改);3.分析建模本次项目主要的关键点有不断的采集数据直到汽车停止,将采集到了数据存入到一个大小固定的文件中(此文件大小可以根据用户要求改变),将文件里的数据读出查看汽车行驶时采集到

2、的数据。3.1采集数据集存储通过调用time 和localtime函数来采集日期和时间,(由于目前没有采集速度的设备所以速度采用手动从键盘输入);程序如下;void input(PPER new)int n=0;printf(请输入卡车行驶的速度n);scanf(%d,&n);time_t timep;struct tm *p;time(&timep);p=localtime(&timep);new-year=(1900+p-tm_year);new-mouth=(1+p-tm_mon);new-day=p-tm_mday;new-hour=p-tm_hour;new-fen=p-tm_min

3、;new-second=p-tm_sec;new-speed=n;new-next=NULL;由于每条数据有比较多的信息所以我们定义一个结构体来存储采集到的每条数据,数据采用位段存储省空间;程序如下:typedef struct carunsigned int year:12;unsigned int mouth:4;unsigned int day:5;unsigned int hour:5;unsigned int fen:6;unsigned int second:6;unsigned int speed:9;struct car * next;PER,*PPER;由于存储的数据条数比较

4、多且条数不固定所以采用链表将这些结构体链接起来,来的数据往链表末尾一挂程序如下:PPER addlink(PPER head)int i=0;PPER ps=head;while(1)PPER new=calloc(1,sizeof(PER);input(new);if(NULL=head)head=new;ps=head;i+;elsehead-next=new;if(0=new-speed)return ps;head=head-next;i+;if(i=N)return ps;3.2将数据存入文件中每一次存储数据在文件的位置都要从上一次结束的位置开始,我们定义一个变量n来记录每次文件存储

5、结束的位置,并存入文件的开头,第二次要存储数据的时候先读取n的数据就知道上一次存在哪里,然后接着往后面存;文件的大小我们宏定义一个FILEMAX来控制每一次剩余文件的大小不能存储一条 数据时将文件跳到n后面开始存储程序如下:void inputfile(PPER head)int n=sizeof(int);FILE *fp=fopen(xiangmu.txt,r+);if(NULL=fp)fp=fopen(xiangmu.txt,w);fwrite(&n,sizeof(int),1,fp);elsefread(&n,sizeof(int),1,fp);fseek(fp,n,SEEK_SET)

6、;while(1)if(NULL=head)break;if(FILEMAX-ftell(fp)sizeof(PER)fwrite(head,sizeof(PER),1,fp);head=head-next;elsefseek(fp,sizeof(int),SEEK_SET);n=ftell(fp);rewind(fp);fwrite(&n,sizeof(int),1,fp);fclose(fp);3.3显示文件由于文件是以二进制代码方式存储,人类无法直接查看所以要先将文件的内容调入PC机内存中显示在显示器上才好查看,文件到内存放在链表中存储程序如下:PPER outputfile(PPER

7、head)FILE *fp=fopen(xiangmu.txt,r);if(NULL=fp)printf(文件打开失败n);return NULL;fseek(fp,sizeof(int),SEEK_SET);while(1)PPER new=calloc(1,sizeof(PER);fread(new,sizeof(PER),1,fp);new-next=NULL;if(0!=feof(fp)break;head=addlink1(head,new);fclose(fp);return head;PPER output(PPER head)PPER ps=head;if(NULL=head)

8、return head;while(1)show(ps);ps=ps-next;if(NULL=ps)return head;4.画流程图由于时间有限在此就不画电子版的流程图;5.编写程序程序附带在同一个文件夹里,再此就不做编写6.显示程序运行结果主菜单页请输入功能号0-退出1-将采集来的数据输入链表中2-显示链表3-将链表里的数据存入文件中4-释放链表5-把文件里面的数据导入链表中6-求最大速度7求平均速度显示文件记录的所有数据时间2015年8月5日9时36分48秒speed93km/h时间2015年8月5日9时36分49秒speed94km/h时间2015年8月5日9时36分50秒spee

9、d95km/h时间2015年8月5日9时36分50秒speed96km/h时间2015年8月5日9时36分52秒speed97km/h时间2015年8月5日9时36分53秒speed98km/h时间2015年8月5日9时36分54秒speed99km/h时间2015年8月5日9时36分56秒speed100km/h时间2015年8月5日9时37分1秒speed101km/h时间2015年8月5日9时37分22秒speed102km/h时间2015年8月5日9时37分24秒speed103km/h时间2015年8月5日9时37分25秒speed104km/h时间2015年8月5日9时37分27秒

10、speed105km/h时间2015年8月5日9时37分28秒speed106km/h时间2015年8月5日9时37分29秒speed107km/h时间2015年8月5日9时37分31秒speed108km/h时间2015年8月5日9时37分33秒speed109km/h时间2015年8月5日9时37分36秒speed110km/h时间2015年8月5日9时37分38秒speed111km/h时间2015年8月5日9时32分15秒speed12km/h时间2015年8月5日9时32分17秒speed13km/h时间2015年8月5日9时32分18秒speed14km/h时间2015年8月5日9

11、时32分19秒speed15km/h时间2015年8月5日9时32分20秒speed16km/h时间2015年8月5日9时32分21秒speed17km/h时间2015年8月5日9时32分22秒speed18km/h时间2015年8月5日9时32分23秒speed19km/h时间2015年8月5日9时32分25秒speed20km/h时间2015年8月5日9时32分27秒speed21km/h时间2015年8月5日9时32分57秒speed22km/h时间2015年8月5日9时32分58秒speed23km/h时间2015年8月5日9时32分59秒speed24km/h时间2015年8月5日9

12、时33分0秒speed25km/h时间2015年8月5日9时33分1秒speed26km/h时间2015年8月5日9时33分2秒speed27km/h时间2015年8月5日9时33分3秒speed28km/h时间2015年8月5日9时33分4秒speed29km/h时间2015年8月5日9时33分6秒speed30km/h时间2015年8月5日9时33分7秒speed31km/h时间2015年8月5日9时33分26秒speed32km/h时间2015年8月5日9时33分28秒speed33km/h时间2015年8月5日9时33分29秒speed34km/h时间2015年8月5日9时33分30秒speed35km/h时间2015年8月5日9时33分31秒speed36km/h时间2015年8月5日9时33分32秒speed37km/h时间2015年8月5日9时33分34秒speed38km/h时间2015年8月5日9时33分35秒speed39km/h时间2015年8月5日9时33分37秒speed40km/h时间2015年8月5日9时3

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号