算法与数据结构火车车次管理系统

本文探讨如何运用算法与数据结构设计并实现一个火车车次管理系统,涵盖了系统的主要功能和实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

算法与数据结构 火车车次管理系统

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
   
	char train_number[20]; //车次
    char departure_station[50];//始发站
	char destination_station[50];//终点站
	char run_time[50]; //运行时间
} Train;

typedef struct Node{
   
	Train data;   //结点数据
	struct Node *next;   //结点指针 
}ListNode,*LinkList;
ListNode *p ;/*指向结点的指针变量*/ 
LinkList head;/*指向单链表的头指针*/


LinkList CreatList(void);                                              // 完成
void InsertNode(LinkList head,ListNode *p);                            // 完成
ListNode *ListFind(LinkList head);                                     // 完成
void DeleteNode(LinkList head);                                        // 完成
void ChangeNode(LinkList head);                                        // 完成
void PrintList(LinkList head);                                         // 完成                                             
int menu_Train();                                                      // 完成


/*尾插法建立火车车次查询链表*/
LinkList CreatList(void )
{
   
	LinkList head=(LinkList)malloc(sizeof(ListNode));
	ListNode *p,*rear;
	char f='y';
	rear=head;
	while(f=='y'||f=='Y')
	{
   
		p=(ListNode*)malloc(sizeof(ListNode));
		printf("请按顺序输入车次,始发站,终点站,运行时间:\n");
		printf("--------------------------------------------\n");
		scanf("%s%s%s%s",p->data.train_number,p->data.departure_station,p->data.destination_station,p->data.run_time);
		rear->next=p;
		rear=p;
		printf("是否继续输入?(y/n)");
		getchar();
		scanf
     目录 第一章 列车车次管理系统需求分析 2 1.1 需求分析和概述 2 1.2 系统分析 2 第二章 数据库的设计 4 2.1 E-R图 4 2.2 表的结构说明 4 第三章 前台主界面的设计 5 3.1主界面概述 5 3.2前台主界面的实现 5 3.3数据库的连接 8 3.4界面事件监听 8 附录 14 第一章 列车车次管理系统需求分析 1.1 需求分析和概述    中国地域辽阔,铁路线纵横繁多,给人们的出行带来了极大的方便,但随着火车站客流量的不断增长,越来越多的弊端暴露在人们面前,如:    火车查询效率低下    浪费乘客大量时间    浪费大量人力资源 在这样的情景下更需要良好的系统来管理列车的时间及列车所到车站。      本系统对于指定的列车车次,可以对它的出发时间、到站时间、途经车站等进行添加、修改和删除。 1.2 系统分析 本系统是针对管理员对列车车次的管理的开发的,其主要功能是:   1)增加、修改、删除一个列车车次信息。   2)按条件显示车次信息(条件有按车次、出发时间、到站时间、途经车站等)。       系统结构图如下 第二章 数据库的设计 2.1 E-R图   这一设计阶段是在系统功能结构图的基础上进行的,设计出能够满足用户需求的各种实体以及它们之间的关系,为后面的逻辑结构设计打下基础。根据以上的分析设计结果,得到列车实体。下面来介绍车次实体的E-R图。 列车实体包括车次,出发时间,结束时间,途径车站。E-R图如图2.1.1所示。    2.2 表的结构说明   在本系统中,。train数据表中保存婴儿出生信息,该表的结构如下表 表2.2.1 train表结构 字段名 信息说明 类型 最大字符数 备注 number 列车车次 char 4 主键 starttime 出发时间 char 10 允许为空 endtime 到站时间 char 10 允许为空 via 途经车站 char 10 允许为空 第三章 前台主界面的设计 3.1主界面概述   界面是用户程序进行交互的接口,用户可以从界面中查看程序显示给用户的信息,程序可以从界面中获取用户输入的数据。通常用户不会去关心程序的界面是如何实现的,更多的会在意界面中提供的信息、功能及界面的布局是否合理。所以在进行界面设计时,不仅要从程序开发的角度上分析,还要考虑到界面的美观及布局。   本系统前台主界面的运行效果如图4.1所示。    前台主界面效果图 3.2前台主界面的实现 为了方便的实现查询功能,在jtable里面显示数据库信息,主界面里自定义了CreateTable类。 class CreateTable{ public Vector vector = null; public AbstractTableModel tm = null; public JScrollPane scroll; public String[] title;//表格表头 public CreateTable(String[] head) { JTable table; title=(String[])head.clone(); vector = new Vector(); tm = new AbstractTableModel() { //通过重写table来定义自己的表格模式 public int getColumnCount() { return title.length; } public int getRowCount() { return vector.size(); } public Object getValueAt(int row, int column) { if (!vector.isEmpty()) { return ((Vector) vector.elementAt(row)).elementAt(column); } else { return null; } } public void setValueAt(Object value, int row, int column) { } public String getColumnName(int column) { return title[column]; } public Class getColumnClass(int c) { return getValueAt(0, c).getClass(); } public boolean isCellEditable(int row, int column) { return false; } }; table = new JTable(tm); scroll = new JScrollPane(table); } } 主界面代码如下class Train extends JFrame implements ActionListener{ public JTextField jt_num,jt_stt,jt_ent,jt_pas; public JButton jb_seh,jb_add,jb_rec,jb_del; Train(){ super("列车车次管理系统"); String[] cond={"车次","出发时间","到站时间","途径车站"}; Object[][] data=new Object[4][4]; JLabel jl_num=new JLabel("车次"); JLabel jl_stt=new JLabel("出发时间"); JLabel jl_ent=new JLabel("到站时间"); JLabel jl_pas=new JLabel("途经车站"); jt_num=new JTextField(6); jt_stt=new JTextField(10); jt_ent=new JTextField(10); jt_pas=new JTextField(20); jb_seh=new JButton("查询"); jb_seh.addActionListener(this); jb_add=new JButton("添加"); jb_add.addActionListener(this); jb_rec=new JButton("修改"); jb_rec.addActionListener(this); jb_del=new JButton("删除"); jb_del.addActionListener(this); CreateTable table=new CreateTable(cond); JPanel jp1=new JPanel(); JPanel jp2=new JPanel(); JPanel jp3=new JPanel(); JPanel jp4=new JPanel(); jp1.add(jl_num); jp1.add(jt_num); jp1.add(jl_stt); jp1.add(jt_stt); jp2.add(jl_ent); jp2.add(jt_ent); jp2.add(jl_pas); jp2.add(jt_pas); jp3.add(jb_seh); jp3.add(jb_add); jp3.add(jb_rec); jp3.add(jb_del); jp4.add(table.scroll); setLayout(new GridLayout(4,1)); this.add(jp1); this.add(jp2); this.add(jp3); this.add(jp4); setResizable(false); } } 3.3数据库的连接 数据库的连接有DBConnect类实现提代码如下 import java.sql.*; public class DBConnect { public static Connection getConn() throws Exception{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); return DriverManager.getConnection("jdbc:odbc:123",""); } } 3.4界面事件监听 在前台中,每个按钮都有库的操作的事件。当用户想要增或删除时,点击按钮,而系统会对这些按钮进行监听,并做应的处理,其实现代码如下: public void actionPerformed(ActionEvent e){ if(e.getSource()==jb_add){ num=jt_num.getText(); stt=jt_stt.getText(); ent=jt_ent.getText(); pas=jt_pas.getText(); try { con = DBConnect.getConn(); st= con.createStatement(); st.executeUpdate("insert into Train values(&#39;"+num+"&#39;,&#39;"+stt+"&#39;,&#39;"+ent+"&#39;,&#39;"+pas+"&#39;)"); jt_num.setText(""); jt_stt.setText(""); jt_ent.setText(""); jt_pas.setText(""); JOptionPane.showMessageDialog(this, "添加成功!"); }catch(SQLException ae){ while (ae != null) { JOptionPane.showMessageDialog(this, "ERROR" ); ae = ae.getNextException(); } }catch(Exception ex){ ex.printStackTrace(); }finally { try { if (st != null) { st.close(); } if (con != null) { con.close(); } } catch (SQLException t) { JOptionPane.showMessageDialog(this, "ERROR:" + "----- SQLException -----\r\n" ); } } } if(e.getSource()==jb_del){ num= jt_num.getText(); if (JOptionPane.showConfirmDialog(this, "确认要删除吗?") == JOptionPane.YES_OPTION) { try { Connection con = DBConnect.getConn(); Statement st = con.createStatement(); st.executeUpdate("delete from Train where number=&#39;"+ num + "&#39;"); JOptionPane.showMessageDialog(this, "已删除!"); jt_num.setText(""); }catch(SQLException ae){ while (ae!= null) { JOptionPane.showMessageDialog(this, "ERROR" ); ae = ae.getNextException(); } }catch(Exception ex){ ex.printStackTrace(); }finally { try { if (st != null) { st.close(); } if (con != null) { con.close(); } } catch (SQLException ex) { JOptionPane.showMessageDialog(this, "ERROR"); } } } } if(e.getSource()==jb_rec){ num=jt_num.getText(); stt=jt_stt.getText(); ent=jt_ent.getText(); pas=jt_pas.getText(); try { Connection con = DBConnect.getConn(); Statement st = con.createStatement(); st.executeUpdate("update Train set starttime=&#39;"+stt+"&#39;,endtime=&#39;"+ent+"&#39;,via=&#39;"+pas+"&#39; where number=&#39;"+num+"&#39;"); jt_num.setText(""); jt_stt.setText(""); jt_ent.setText(""); jt_pas.setText(""); JOptionPane.showMessageDialog(this, "修改成功!"); }catch (SQLException ex) { while (ex != null) { JOptionPane.showMessageDialog(this, "ERROR"); ex = ex.getNextException(); } } catch (Exception ex) { ex.printStackTrace(); } finally { try { if (st != null) { st.close(); } if (con != null) { con.close(); } } catch (SQLException ex) { JOptionPane.showMessageDialog(this, "ERROR" ); } } } if(e.getSource()==jb_seh){ num=jt_num.getText(); try { Connection con = DBConnect.getConn(); Statement st = con.createStatement(); ResultSet rst = null; rst=st.executeQuery("select * from Train where number=&#39;"+num+"&#39;"); if (rst.next() == false) { JOptionPane.showMessageDialog(this, "找不到该列车"); }else{ ResultSet rs=st.executeQuery("select * from Train where number=&#39;"+num+"&#39;"); jt_num.setText(""); table.vector.removeAllElements(); table.tm.fireTableStructureChanged(); while (rs.next()) { Vector vector=new Vector(); vector.addElement(rs.getString(1)); vector.addElement(rs.getString(2)); vector.addElement(rs.getString(3)); vector.addElement(rs.getString(4)); table.vector.addElement(vector); } table.tm.fireTableStructureChanged(); } }catch (SQLException ex) { while (ex != null) { JOptionPane.showMessageDialog(this, "ERROR" ); ex = ex.getNextException(); } } catch (Exception ex) { ex.printStackTrace(); } finally { try { if (st != null) { st.close(); } if (con != null) { con.close(); } } catch (SQLException ex) { JOptionPane.showMessageDialog(this, "ERROR:" + "----- SQLException -----\r\n"); } } } }
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值