linux c写的一个航班查询的程序

之所以把这个代码称为一个程序而不叫一个项目,是因为鄙人还是个还没入行的新人,不敢妄自菲薄,只因一时兴起上来感慨吐槽一番,如果有什么不对的地方,请各位大牛高台贵手,如果能提点一番,小弟感激不尽大笑

废话不多,拿到题目就开干。

刚学完内核链表,就拿它来开刀。内核给你写好封装一些链表操作的接口函数,拿来用多爽,不用自己写大笑。这个写好的东西在哪呢,在内核目录的include/linux/下。

#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      
#include "kernel_list.h"

struct flight
{
	char flight_id[15];		//航班号					//航班号
	char flight_place[15];	//航班起飞站->终点站		//买票者的名字
	char flight_time[15];	//起飞时间					//买到的票的座位号
	int  flight_seat;		//航班总座位数				//票的张数
	float price;			//机票价格					//花费的金额
	int remain;				//剩余座位数
	char st[15];			//座位标志
	struct list_head mylist;
};
// 内核链表的初始化
struct flight * list_init()
{
	struct flight *head=malloc(sizeof(struct flight));
	INIT_LIST_HEAD(&(head->mylist));
	return head;
}
//判断链表是否为空
int kernellist_empty(struct flight *head)
{
	return list_empty(&(head->mylist));
}
// 创建新的节点
struct flight * new_node(char *flight_id,char *flight_place,char *flight_time,int  flight_seat,float price)
{
	struct flight *new=malloc(sizeof(struct flight));
	strcpy(new->flight_id,flight_id);
	strcpy(new->flight_place,flight_place);
	strcpy(new->flight_time,flight_time);
	new->flight_seat=flight_seat;
	new->remain=flight_seat;
	new->price=price;
	bzero(new->st,15);
	INIT_LIST_HEAD(&(new->mylist));
	return new;
}
// 添加新的节点到内核链表中 添加到head的前面
int kernellist_add(struct flight *new,struct flight *head)
{
	list_add_tail(&(new->mylist), &(head->mylist));
	return 0;
}
//添加到head的后面
int kernellist_add_tail(struct flight *new,struct flight *head)
{
	list_add(&(new->mylist), &(head->mylist));
	return 0;
}
//删除一个节点
int kernellist_del(struct flight *del)
{
	list_del(&(del->mylist));
	return 0;
}
// 打印航班信息
int show(struct flight *head)
{
	if(kernellist_empty(head))
	{
		printf("no any flight exist!\n");
		return 1;
	}
	printf("----------------------------------------------------------\n");	
	printf("flight_id	flight_place	flight_time	flight_seat  price\n");
	struct flight *pos;
	list_for_each_entry(pos,&(head->mylist),mylist) //该宏函数实际是个for循环
	//for (pos = list_entry((head)->next, typeof(*pos), member);  &pos->member != (head);  pos = list_entry(pos->member.next, typeof(*pos), member))
	{
		printf("%-16s%-16s%-16s%-13d%.2f\n",pos->flight_id,pos->flight_place,pos->flight_time,pos->flight_seat,pos->price);
	}
	printf("----------------------------------------------------------\n");	
	
	return 0;
}
//打印卖票信息
int show_ticket(struct flight *head)
{
	int i;
	if(kernellist_empty(head))
	{
		printf("no bodey buy ticket!\n");
		return 0;
	}
	printf("----------------------------------------------------------\n");	
	printf("flight_id	name		num		price   ticketnum\n");
	struct flight *pos;
	list_for_each_entry(pos,&(head->mylist),mylist) //该宏函数实际是个for循环
	//for (pos = list_entry((head)->next, typeof(*pos), member);  &pos->member != (head);  pos = list_entry(pos->member.next, typeof(*pos), member))
	{
		printf("%-16s%-16s%-16d%-8.2f",pos->flight_id,pos->flight_place,
										pos->flight_seat,pos->price);
		for(i=0;i<15;i++)
		{
			if(pos->flight_time[i] == 1)
				printf("%d ",i+1);
		}
		printf("\n");
	}
	printf("----------------------------------------------------------\n");	
	
	return 0;
}
//翻转航班信息
int reversal(struct flight *head)
{
	struct list_head *pos;
	pos = head->mylist.next;
	while(pos->next != &(head->mylist))
		list_move_tail(head->mylist.prev,pos);
	return 0;
}
//航班信息排序
int flight_sort(struct flight *head)
{
	int choose;
	struct list_head *posi;
	struct list_head *posj;
	struct list_head *temp;
	while(1)
	{
		printf(	"====================================================================\n"
				"[1]flight_id  [2]flight_time  [3]price  [4]reversal  [5]show [0]quit\n"					
				"====================================================================\n");
		if(scanf("%d",&choose) != 1 || getchar()!='\n')	//判断是否正确输入
		{
			choose=6;
			while(getchar() !=  '\n');
		}
		switch(choose)
		{
			case 0:
				return 0;		
				break;
			case 1:
				list_for_each(posi,&(head->mylist))
				//for (posi = head->mylist.next; posi != &(head->mylist);posi = posi->next)
				{
					for (posj = posi->next; posj != &(head->mylist);posj = posj->next)
					{
						if(strcmp(list_entry(posi,struct flight,mylist)->flight_id,list_entry(posj,struct flight,mylist)->flight_id)==1)
						{
							temp = posi->prev;
							list_move(posi,posj);
							list_move(posj,temp);
							
							temp=posi;
							posi = posj;
							posj = temp;	
						}		
					} 
				}
				break;
			case 2:
				list_for_each(posi,&(head->mylist))
				//for (posi = head->mylist.next; posi != &(head->mylist);posi = posi->next)
				{
					f
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来用中文回答您的问题。如果您想用C语言一个Linux智能称重程序,可以按照以下步骤: 1. 包含必要的头文件 ``` #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <sys/ioctl.h> #include <linux/hidraw.h> ``` 2. 定义常量和变量 ``` #define VENDOR_ID 0x1234 // 设备的制造商ID #define PRODUCT_ID 0x5678 // 设备的产品ID #define MAX_READ 16 // 最大读取字节数 int main() { int fd, res; char buf[MAX_READ]; char device[] = "/dev/hidraw0"; // HID设备的路径 int value; // 读取到的值 } ``` 3. 打开HID设备 ``` fd = open(device, O_RDWR); if (fd < 0) { perror("Unable to open device"); exit(1); } ``` 4. 设置HID设备的属性 ``` struct hidraw_devinfo info; memset(&info, 0x0, sizeof(info)); if (ioctl(fd, HIDIOCGRAWINFO, &info) < 0) { perror("Could not get device information"); exit(1); } if (info.vendor != VENDOR_ID || info.product != PRODUCT_ID) { printf("Device not found\n"); exit(1); } ``` 5. 读取HID设备的值 ``` res = read(fd, buf, MAX_READ); if (res < 0) { perror("Unable to read device"); exit(1); } value = buf[0]; // 假设设备只返回一个字节的值 ``` 6. 根据读取到的值执行相应的操作 ``` if (value == 0x01) { printf("Weight is 100g\n"); } else if (value == 0x02) { printf("Weight is 200g\n"); } else { printf("Unknown weight\n"); } ``` 7. 关闭HID设备 ``` close(fd); ``` 这是一个简单的Linux智能称重程序的示例,具体的实现可能因设备的不同而有所不同。希望这个示例对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值