int main()
{
int x = 0, z = 0
scanf("%d %d", &x, &z)
// 问:为什么(x - z) * 100 / z前面宏扰要加个double强乱凳制转换?
// 答:因为x,z,100都是整数(int型),他们三的+-*/得到的结果也是整形,
// 如果不转换会丢失精度,导致计算结果不准。
double b = (double)(x - z) * 100 / z
if (b > 50)
{
printf("Exceed %.0f%%. License Revoked", b)
}
else if (b >= 10 && b <= 50)
{
printf("Exceed %.0f%%. Ticket 200", b)
}
else if (b < 10)
{
printf("OK")
哗绝旅 }
return 0
}
//VC++6.0下调试通过#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <time.h>
typedef enum {s_park=1, s_speed, s_signal, s_other} SheetType
typedef enum {f_id, f_car, f_driver, f_police} FindType
typedef struct Sheet
{
signed long id//罚单号
char carNo[20]//车牌号
char driver[20]//司机名
char police[20]//警察编号
SheetType type//罚单类型
tm time//出单日期
}Sheet
typedef struct Node
{
Sheet data
Node* next
}Node
static Node* begin=NULL//哨兵结点
static signed long len=0//链表长度
void printMenu()//打印菜单
void printBye()//打印退出界面
void printList(Node* bg)//打印所有罚单信息
void printNode(Sheet sheet)//打印单条罚单
Node* lookupSheet(void* pInfo, signed short* n,
FindType type=f_id)//根据类型查询罚单。成功返回结点。侍指岁失败NULL
int addNode(Node* bg, Sheet sheet)//添加罚单。成功返回链表长度。内存分配失败0
int deleteNode(signed short stid)//根据罚单号删除罚单。
int total(char* driver, tm date1, tm date2, signed short* icar,
signed short* ispeed, signed short* isignal, signed short* iother)//统计
int getListlen(Node* bg)
void freeList(Node* bg)//释放链表
void main()
{
//初始化链表。
begin=(Node*)malloc(sizeof(Node))
begin->next=NULL
printf("----------------------------------------------------------------------------\n")
printf("--- 《交通罚单管理系统》---\n")
printf("---沙支夸依 2007-12-24 ---\n")
printf("----------------------------------------------------------------------------\n")
printf("||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\n")
printMenu()
while (1)
{
fflush(stdin)
char cmd=getchar()
Sheet sheet
memset(&sheet, 0, sizeof(Sheet))
switch (cmd)
{
case '1':
printf("---添加交通处罚单信息。(车牌号为 0 结束)\n")
while (1)
{
printf("请输入违章车辆号: ")
fflush(stdin)
gets(sheet.carNo)
if (!strcmp(sheet.carNo,"0"))
break
printf("请输老睁入逗磨违章司机姓名: ")
fflush(stdin)
gets(sheet.driver)
printf("请输入开单交警编号: ")
fflush(stdin)
gets(sheet.police)
printf("请选择违章类型(1-违章停车 2-超速 3-不按信号灯行使 4-其它):")
scanf("%d", &sheet.type)
printf("请输入开单日期(例 2007-12-24): ")
scanf("%d-%d-%d", &sheet.time.tm_year, &sheet.time.tm_mon, &sheet.time.tm_mday)
int succ=addNode(begin, sheet)
if (!succ)
{
printf("内存错误!请重新输入!\n")
}
else
printf("添加成功!当前记录数: %d\n", len)
printf("\n")
}
printMenu()
break
case '2':
printf("---删除交通处罚单信息。(罚单号为 0 结束)\n")
while (1)
{
signed short stid=0
printf("请输入要删除的罚单号:")
scanf("%d", &stid)
if (!stid)
break
Node* delNode=lookupSheet(&stid, NULL)
if (!delNode)
printf("对不起!你要删除的记录不存在。\n")
else
{
printNode(delNode->data)
printf("您确定要删除这条记录吗?(任意键删除/ n 取消)\n")
fflush(stdin)
char cDelete=getchar()
if ('n'==cDelete)
continue
printf("删除成功!剩余记录数: %d\n",deleteNode(stid))
}
printf("\n")
}
printMenu()
break
case '3':
printf("---查询交通处罚单信息。(输入 0 结束)\n")
while (1)
{
printf("请选择查询类型: 1-车辆号 2-司机 3-交警 4-退出\n>>>")
Node* findNode=NULL
int findtype=0
scanf("%d", &findtype)
if (4==findtype)
break
char findInfo[20]
signed short findcout=0
switch (findtype)
{
case 1:
printf("请输入车辆号: ")
fflush(stdin)
gets(findInfo)
findNode=lookupSheet(findInfo, &findcout, f_car)
if (getListlen(findNode)>=5)
{
printf("---该车辆违章超过5次。建议重罚。\n")
}
break
case 2:
printf("请输入司机姓名: ")
fflush(stdin)
gets(findInfo)
findNode=lookupSheet(findInfo, &findcout, f_driver)
if (getListlen(findNode)>=5)
{
printf("---该司机违章超过5次。建议重罚。\n")
}
break
case 3:
default:
printf("请输入交警编号: ")
fflush(stdin)
gets(findInfo)
findNode=lookupSheet(findInfo, &findcout, f_police)
}
if (!findNode)
printf("对不起!你要查询的记录不存在。\n")
else
{
printList(findNode)
freeList(findNode)
}
printf("\n")
}
printMenu()
break
case '4':
printf("---统计司机处罚单信息。(输入 0 结束)\n")
while (1)
{
printf("请输入司机姓名: ")
char driver[20]
fflush(stdin)
gets(driver)
if (!strcmp(driver, "0"))
break
printf("请输入起始日期(如 2007-12-24): ")
tm date1
memset(&date1, 0, sizeof(tm))
scanf("%d-%d-%d", &date1.tm_year, &date1.tm_mon, &date1.tm_mday)
printf("请输入终止日期(如 2007-12-24): ")
tm date2
memset(&date2, 0, sizeof(tm))
scanf("%d-%d-%d", &date2.tm_year, &date2.tm_mon, &date2.tm_mday)
signed short car, speed, signal, other
int sum=total(driver, date1, date2, &car, &speed, &signal, &other)
printf("司机姓名:%-10s 违章停车:%2d 超速:%2d 不按信号灯行使:%2d 其它:%2d 共计:%2d\n",
driver, car, speed, signal, other, car+speed+signal+other)
printf("\n")
}
printMenu()
break
case '5':
printList(begin)
printMenu()
break
case '6':
printf("---修改交通处罚单信息。(输入 0 结束)\n")
while (1)
{
signed short stid=0
printf("请输入要修改的交通处罚单号:")
fflush(stdin)
scanf("%d", &stid)
if (!stid)
break
Node* updataNode=lookupSheet(&stid, NULL)
if (!updataNode)
printf("对不起!你要修改的记录不存在。\n")
else
{
printNode(updataNode->data)
memset(&sheet, 0, sizeof(Sheet))
sheet.id=stid
printf("请修改违章车辆号: ")
fflush(stdin)
gets(sheet.carNo)
printf("请修改违章司机姓名: ")
fflush(stdin)
gets(sheet.driver)
printf("请修改开单交警编号: ")
fflush(stdin)
gets(sheet.police)
printf("请修改违章类型(1-违章停车 2-超速 3-不按信号灯行使 4-其它):")
scanf("%d", &sheet.type)
printf("请修改开单日期(例 2007-12-24): ")
scanf("%d-%d-%d", &sheet.time.tm_year, &sheet.time.tm_mon, &sheet.time.tm_mday)
updataNode->data=sheet
printNode(updataNode->data)
}
printf("\n")
}
printMenu()
break
case '7':
printBye()
freeList(begin)
exit(0)
break
default:
printMenu()
}
}
}
void printMenu()
{
printf("----------------------------------------------------------------------------\n")
printf("--- 1、添加交通处罚单信息。2、删除交通处罚单信息。 ---\n")
printf("--- 3、查询交通处罚单信息。4、统计司机处罚单信息。 ---\n")
printf("--- 5、浏览交通处罚单信息。6、修改交通处罚单信息。 ---\n")
printf("--- 7、退出。 ---\n")
printf("----------------------------------------------------------------------------\n")
printf(">>>")
}
void printBye()
{
printf("----------------------------------------------------------------------------\n")
printf("感谢您使用交通罚单管理系统 \n")
printf("再 见 \n")
printf("----------------------------------------------------------------------------\n")
}
void printNode(Sheet sheet)
{
char* type=NULL
switch (sheet.type)
{
case s_park:
type="违章停车"
break
case s_speed:
type="超速"
break
case s_signal:
type="不按信号灯行使"
break
case s_other:
default:
type="其它"
}
printf("罚单号:%-6d 车牌号:%-10s 司机名:%-10s 交警名:%-10s\n"
"罚单类型:%-15s 开单日期:%d-%d-%d\n",
sheet.id, sheet.carNo, sheet.driver, sheet.police,
type, sheet.time.tm_year, sheet.time.tm_mon, sheet.time.tm_mday)
printf("--------------------------------------------------------------------------\n")
}
void printList(Node* bg)
{
Node* temp=bg->next
if (!temp)
{
printf("暂时无记录可供浏览!\n")
}
while (temp)
{
printNode(temp->data)
temp=temp->next
}
}
Node* lookupSheet(void* pInfo, signed short* n, FindType type)
{
Node* result=NULL
if (type)
{
result=(Node*)malloc(sizeof(Node))
result->next=NULL
*n=0
}
Node* temp=begin->next
switch (type)
{
case f_id:
while (temp)
{
if (temp->data.id==*(signed long*)pInfo)
{
if (n)
*n=1
return temp
}
temp=temp->next
}
break
case f_car:
while (temp)
{
if (!strcmp(temp->data.carNo,(char*)pInfo))
{
addNode(result, temp->data)
(*n)++
}
temp=temp->next
}
break
case f_driver:
while (temp)
{
if (!strcmp(temp->data.driver,(char*)pInfo))
{
addNode(result, temp->data)
(*n)++
}
temp=temp->next
}
break
case f_police:
while (temp)
{
if (!strcmp(temp->data.police,(char*)pInfo))
{
addNode(result, temp->data)
(*n)++
}
temp=temp->next
}
break
}
return result
}
int addNode(Node* bg, Sheet sheet)
{
Node* temp=(Node*)malloc(sizeof(Node))
if (!temp)
return 0
if (bg==begin)
{
if (bg->next)
{
sheet.id=bg->next->data.id+1
}
else
sheet.id=1
}
temp->data=sheet
temp->next=bg->next
bg->next=temp
return ++len
}
int deleteNode(signed short stid)
{
Node* temp=begin
Node* pos=NULL
while (temp->next)
{
if (temp->next->data.id==stid)
{
pos=temp->next
temp->next=pos->next
free(pos)
return --len
}
temp=temp->next
}
return -1
}
int getListlen(Node* bg)
{
Node* temp=bg
int i=0
while (temp->next)
{
i++
temp=temp->next
}
return i
}
void freeList(Node* bg)
{
Node* temp=NULL
while (bg)
{
temp=bg->next
free(bg)
bg=temp
}
}
int total(char* driver, tm date1, tm date2, signed short* icar,
signed short* ispeed, signed short* isignal, signed short* iother)
{
signed short result=0
*icar=0
*ispeed=0
*isignal=0
*iother=0
Node* findNode=lookupSheet(driver, &result, f_driver)
if (!findNode)
{
free(findNode)
return 0
}
time_t o,n,nw
date1.tm_year-=1900
date2.tm_year-=1900
o=mktime(&date1)
n=mktime(&date2)
if (o<n)
{
time_t tp=o
o=n
n=tp
}
Node* temp=findNode->next
while (temp)
{
tm t=temp->data.time
t.tm_year-=1900
nw=mktime(&t)
if (nw>o||nw<n)
{
temp=temp->next
continue
}
switch (temp->data.type)
{
case 1:
(*icar)++
break
case 2:
(*ispeed)++
break
case 3:
(*isignal)++
break
case 4:
default:
(*iother)++
}
temp=temp->next
}
freeList(findNode)
return result
}
#include<stdio.h>#include<stdlib.h>
#include<string.h>
typedef struct Traffic
{
char driver[10] //司机姓名
int car //车牌号
char police[10] //交警姓名
int num //罚单编号
int year
int mounth
int date
}
int MAX = 10
struct Traffic* Input(struct Traffic*, int*) //输入函数
void Display(struct Traffic*,int) //输出函数
int Delete(struct Traffic*, int, char*)
int Delete_a_record(struct Traffic*, int) //删除函数
void Sort_by_name(struct Traffic*,int) //排序函数
int Insert(struct Traffic*, int, struct Traffic*)
struct Traffic* Insert_a_record(struct Traffic*, int*) //插入函数
int Query(struct Traffic*, int, char*)
void Query_a_record(struct Traffic*, int) //查询函数
struct Traffic* AddfromText(struct Traffic*, int*, char*) //从文件添加
void WritetoText(struct Traffic*, int, char*) //输出到文件
void Quit(struct Traffic*) //退出释放空间
/*以下是自定义函数*/
void Display_Main_Menu() //打印菜单
void GetRecordInfo(struct Traffic*) //接收信息函数
int copyTraffic(struct Traffic*, struct Traffic*) //结构体复制
void main()
{
int n, *np, key
struct Traffic* trafDynList
char* filename1 = "data.txt", *filename2 = "records.txt"
n = 0
np = &n
//初始化空间
trafDynList = (struct Traffic *)malloc(MAX*sizeof(struct Traffic))
system("color 02")
while(1){
//system("cls")
Display_Main_Menu()
scanf("%d",&key)
//getchar()
switch(key){
case 1:
trafDynList = Input(trafDynList, np) // intput records
break
case 2:
Display(trafDynList, n) //display all records
break
case 3:
n = Delete_a_record(trafDynList, n)
break
case 4:
Sort_by_name(trafDynList, n)
break
case 5:
trafDynList = Insert_a_record(trafDynList,np)
break
case 6:
Query_a_record(trafDynList, n)
break
case 7:
trafDynList = AddfromText(trafDynList, np, filename1)
break
case 8:
御兄 WritetoText(trafDynList, n, filename2)
break
case 0:
Quit(trafDynList)
//添加退出语句
printf(" i am here!")
镇派袭 break
default:
printf(" choice error!\n")
}
if( key == 0)
break
}
}
//输入函数
struct Traffic* Input(struct Traffic* trafDynList, int *np)
/* Traffic* trafDynList为动态存储空间首地址,int* n 为记录数,函数返回动态存储空间首地址,从键盘输入若干条记录*/
{
羡陪int i
struct Traffic *trafp, traf
char keych
//trafp = &traf
do{
if((*np+1) > MAX){ //如果记录数据即将超出空间
trafp = trafDynList
trafDynList = (struct Traffic*)malloc((MAX+10)*sizeof(struct Traffic)) //开辟新空间
if(trafDynList == NULL){
printf(" space full!")
exit(0)
}
MAX = MAX + 10
for( i = 0 i < *np i++){ //复制数据
copyTraffic(trafDynList+i, trafp +i)
}
//释放原有空间
free(trafp)
}
trafp = &traf
//开始插入
GetRecordInfo(&traf)
//Display(&traf, 1)
if(copyTraffic(trafDynList + (*np), &traf )){
//Display(&traf,1)
printf(" Input success! \n")
(*np) ++
}
//判断是否继续输入
printf(" again ? please type Y or N : ")
do
{
scanf(" %c", &keych)
if(keych != 'Y' && keych != 'N')
printf(" error! please type again\n")
} while (keych != 'Y' && keych != 'N')
}while(keych == 'Y')
return trafDynList
}
void GetRecordInfo(struct Traffic* trafp)
{
//接收结构体信息
printf(" please input driver's name: ")
scanf("%s",(trafp->driver))
printf(" please car number: ")
scanf("%d",&(trafp->car))
printf(" please input police's name: ")
scanf("%s",(trafp->police))
printf(" please input toket number: ")
scanf("%d",&(trafp->num))
printf(" please input year mounth date: ")
scanf("%d %d %d",&(trafp->year), &(trafp->mounth), &(trafp->date))
}
int copyTraffic(struct Traffic* trafDynList, struct Traffic* trafp)
{
//拷贝结构体,后面的拷贝到前面的
strncpy(trafDynList->driver, trafp->driver ,10)
trafDynList->car = trafp->car
strncpy(trafDynList->police,trafp->police,10)
trafDynList->num = trafp->num
trafDynList->year = trafp->year
trafDynList->mounth = trafp->mounth
trafDynList->date = trafp->date
return 1
}
//输出数据
void Display(struct Traffic* trafDynList, int n)
/*显示所有姓名记录,每十个暂停一下,n为数组元素个数*/
{
int i
//printf("I am here! %d\n", n)
for(i = 0 i < n i++){
if(i%10 == 0 && i != 0)
getchar()
//格式化显示
printf(" %d ", i+1)
printf(" drivername: %s\n",(trafDynList + i)->driver)
printf(" car: %09d\n",(trafDynList + i)->car)
printf(" police: %s\n",(trafDynList+i)->police)
printf(" num: %09d\n", (trafDynList + i)->num)
printf(" time: %d/%d/%d\n",(trafDynList+i)->year, (trafDynList + i)->mounth, (trafDynList + i)->date)
}
}
//删除记录函数
int Delete(struct Traffic* trafDynList, int n, char* name)
/*删除记录函数,删除姓名为name的记录,返回数组中的记录数*/
{
int i,j
char keych
for( i = 0 i < n i ++){
if(strcmp((trafDynList + i)->driver , name) == 0){
Display(trafDynList + i,1)
printf(" Delete? type Y or N: ")
getchar() //接收之前的换行符
do
{
scanf("%c",&keych)
if(keych != 'Y' && keych != 'N')
printf(" error! please type again\n")
} while (keych != 'Y' && keych != 'N')
if(keych == 'Y'){
//删除记录,把后面的记录往前挪
for(j = i j < n j++){
copyTraffic(trafDynList +j, trafDynList + j+1)
n = n -1
}
}//if
}
}//for
return n
}
int Delete_a_record(struct Traffic* trafDynList, int n)
/*输入待删除的姓名,经确认后调用Delete函数删除该姓名的记录,输出成功与否的信息,返回数组中的记录数*/
{
char name[10]
int newnum
printf(" please input the name who you want to delete:\n ")
scanf("%s",name)
if(strlen(name) > 9){
printf(" you input a too long name!\n")
return n
}
newnum = Delete(trafDynList,n,name)
if( newnum < n)
printf(" delete successfully!\n")
return newnum
}
//排序
void Sort_by_name(struct Traffic* trafDynList,int n)
/*按姓名作升序排序,要求选择排序*/
{
char name[10]
int k,j,i
struct Traffic traf
for( i = n i >0 i--){
strncpy(name,trafDynList->driver,10)
k = 0
for(j = 1 j < i j++){
if( strcmp((trafDynList + j)->driver, name) > 0){
k = j
strncpy(name,(trafDynList + j)->driver, 10)
}
}
//将最大的和最后一个交换
copyTraffic(&traf, trafDynList + i -1)
copyTraffic(trafDynList + i -1, trafDynList + k)
copyTraffic(trafDynList + k, &traf)
}
}
//有序插入
int Insert(struct Traffic* trafDynList, int n, struct Traffic* s)
/*按姓名序插入记录s,返回记录条数*/
{
int i,newnum
newnum = n
for(i = n i > 0 i--){
if(strcmp((trafDynList + i-1 )->driver,s->driver) > 0){
copyTraffic(trafDynList+i, trafDynList+i-1)
}
else
break
}//for
copyTraffic(trafDynList+i, s)
newnum++
return newnum
}
struct Traffic* Insert_a_record(struct Traffic* trafDynList, int* np)
/*输入待插入的司机姓名等信息,调用Insert函数,输出成功与否的信息,返回动态存储空间首地址*/
{
int i, newnum
struct Traffic traf, *trafp
char keych
GetRecordInfo(&traf)
do{
if((*np+1) > MAX){ //如果空间不够
trafp = trafDynList
//开辟新空间
trafDynList = (struct Traffic*)malloc((MAX+10)*sizeof(struct Traffic))
if(trafDynList == NULL){
printf(" space full!")
system("PAUSE")
exit(0)
}
MAX = MAX +10
for( i = 0 i < *np i++) //复制数据
copyTraffic(trafDynList +i, trafp + i)
//释放原有空间
free(trafp)
MAX = MAX + 10
}
newnum = Insert(trafDynList,*np,&traf)
if(newnum > *np){
printf(" Insert successfully\n")
*np = newnum
}
//判断用户是否还要插入 *** 作
printf(" again ? please type Y or N: ")
getchar() //接收之前的换行符
do
{
scanf("%c", &keych)
if(keych != 'Y' && keych != 'N')
printf(" error ! please type again\n")
} while (keych != 'Y' && keych != 'N')
} while (keych == 'Y')
return trafDynList
}
//数据查询
int Query(struct Traffic* trafDynList,int n, char *name)
/*查找姓名为name的记录,查找成功返回该记录下标,否则返回-1*/
{
int i
for( i = n-1 i >= 0 i--){
if(strcmp((trafDynList+i)->driver,name) == 0)
break
}
return i
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)