【课程设计|C++】C++程序设计基础:火车票售票系统设计(MFC)

海轰Pro

共 14457字,需浏览 29分钟

 · 2021-07-17

课设要求

程序设计要求:

  1. 用户管理
  2. 车票信息的录入、修改和删除
  3. 根据不同需求对车票信息进行浏览、查询
  4. 购票业务管理
  5. 退票管理
  6. 按车次统计售票数量
  7. 按车次查询车票余量
  8. 车票信息的导入和导出

程序运行界面

演示效果图(部分)

演示-1
演示-2
演示-2

核心代码

pay.cpp

// pay.cpp : implementation file
//

#include "stdafx.h"
#include "火车票售票系统.h"
#include "pay.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/
 // pay dialog

 class Studentx
{

public:
 CString phone;
 CString name;
};

vector<Studentx> stu;
int m = -1;
pay::pay(CWnd *pParent /*=NULL*/)
 : CDialog(pay::IDD, pParent)
{
 //{{AFX_DATA_INIT(pay)
 //}}AFX_DATA_INIT
}

void pay::DoDataExchange(CDataExchange *pDX)
{
 CDialog::DoDataExchange(pDX);
 //{{AFX_DATA_MAP(pay)
 DDX_Control(pDX, IDC_LIST2, m_list);
 DDX_Control(pDX, IDC_COMBO1, m_name);
 DDX_Control(pDX, IDC_LIST1, m_pay);
 //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(pay, CDialog)
//{{AFX_MSG_MAP(pay)
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
ON_BN_CLICKED(IDC_BUTTON10, OnButton10)
ON_BN_CLICKED(IDC_BUTTON8, OnButton8)
ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
ON_BN_CLICKED(IDC_BUTTON9, OnButton9)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

 /
 // pay message handlers
 BOOL pay::OnInitDialog()
{
 CDialog::OnInitDialog();

 // TODO: Add extra initialization here
 m_pay.InsertColumn(0"车次", LVCFMT_CENTER, 100);
 m_pay.InsertColumn(1"始发站", LVCFMT_CENTER, 100);
 m_pay.InsertColumn(2"终点站", LVCFMT_CENTER, 100);
 m_pay.InsertColumn(3"时间", LVCFMT_CENTER, 100);
 m_pay.InsertColumn(4"余票数量", LVCFMT_CENTER, 100);
 m_pay.InsertColumn(5"价格", LVCFMT_CENTER, 95);
 m_pay.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);

 m_list.InsertColumn(0"姓名", LVCFMT_CENTER, 100);
 m_list.InsertColumn(1"车次", LVCFMT_CENTER, 95);
 m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);

 showname();
 ShowCai();
 show();
 m_name.SetCurSel(0);
 //    m_flag.SetCurSel(0);
 return TRUE; // return TRUE unless you set the focus to a control
     // EXCEPTION: OCX Property Pages should return FALSE
}
void pay::showname()
{

 // m_list.DeleteAllItems(); // 全部清空
 ifstream fin("members.txt", ios_base::in);
 string a, b;
 fin >> a >> b;
 for (int i = 0; !fin.fail(); ++i)
 {

  m_name.AddString(_T(a.data()));
  rname = a.data();

  Studentx s1;

  s1.phone = a.data();
  s1.name = b.data();
  stu.reserve(stu.size() + 1); //增加容量以防止溢出

  stu.push_back(s1);
  fin >> a >> b;
 }
 fin.close();
}
void pay::ShowCai()
{
 m_pay.DeleteAllItems();
 int num = ReadCai();
 for (int i = 0; i < num; i++)
 {
  m_pay.InsertItem(i, 0);
  m_pay.SetItemText(i, 0, cai[i].id);    // 车次
  m_pay.SetItemText(i, 1, cai[i].name);  // 始发站
  m_pay.SetItemText(i, 2, cai[i].type);  // 终点站
  m_pay.SetItemText(i, 3, cai[i].price); // 时间
  m_pay.SetItemText(i, 4, cai[i].flag);  // 余票
  m_pay.SetItemText(i, 5, cai[i].time);  // 价格
 }
}

int pay::ReadCai()
{

 FILE *file = fopen("chepiao.txt""r");
 int a = 0;
 while (!feof(file))
 {
  fscanf(file, "%s%s%s%s%s%s", &cai[a].id, &cai[a].name, &cai[a].type, &cai[a].price, &cai[a].flag, &cai[a].time);
  a++;
 }
 fclose(file);
 a--;
 return a;
}
void pay::OnButton1()
{
 // TODO: Add your control notification handler code here

 CString id, name, type, price, flag, time; // flag为输入or支出

 GetDlgItem(IDC_EDIT4)->GetWindowText(id);
 GetDlgItem(IDC_EDIT5)->GetWindowText(name);
 GetDlgItem(IDC_EDIT6)->GetWindowText(type);
 GetDlgItem(IDC_EDIT7)->GetWindowText(price);
 GetDlgItem(IDC_EDIT8)->GetWindowText(flag);
 GetDlgItem(IDC_EDIT9)->GetWindowText(time);

 if (id == "" || name == "" || price == "" || type == "" || flag == "" || time == "")
 {
  MessageBox("添加信息不能为空");
  return;
 }

 FILE *file = fopen("chepiao.txt""a+");
 fprintf(file, "%s   %s   %s   %s   %s   %s\r\n", id, name, type, price, flag, time);
 fclose(file);
 MessageBox("添加成功");
 ShowCai();
}

void pay::OnButton3()
{
 CString txt_name; // 临时文件名
 txt_name = "chepiao.txt";
 // TODO: Add your control notification handler code here

 int sel = m_pay.GetSelectionMark();
 if (sel == -1)
 {
  MessageBox("请选择需要修改的车票!");
 }
 else
 {

  CString id, name, type, price, flag, time;
  GetDlgItem(IDC_EDIT4)->GetWindowText(id);
  GetDlgItem(IDC_EDIT5)->GetWindowText(name);
  GetDlgItem(IDC_EDIT6)->GetWindowText(type);
  GetDlgItem(IDC_EDIT7)->GetWindowText(price);
  GetDlgItem(IDC_EDIT8)->GetWindowText(flag);
  GetDlgItem(IDC_EDIT9)->GetWindowText(time);
  if (id == "" || name == "" || price == "" || type == "" || flag == "" || time == "")
  {
   MessageBox("修改信息不能为空");
   return;
  }
  int num = ReadCai();
  FILE *file = fopen(txt_name, "w");
  for (int i = 0; i < num; i++)
  {
   if (i == sel)
   {
    strcpy(cai[i].name, name);
    strcpy(cai[i].type, type);
    strcpy(cai[i].price, price);
    strcpy(cai[i].flag, flag);
    strcpy(cai[i].id, id);
    strcpy(cai[i].time, time);
   }
   fprintf(file, "%s   %s   %s   %s   %s   %s\r\n", cai[i].id, cai[i].name, cai[i].type, cai[i].price, cai[i].flag, cai[i].time);
  }
  fclose(file);

  MessageBox("修改成功");
  ShowCai();
 }
}

void pay::OnButton5()
{
 CString txt_name; // 临时文件名
 txt_name = "chepiao.txt";

 // TODO: Add your control notification handler code here
 int sel = m_pay.GetSelectionMark();
 if (sel == -1)
 {
  MessageBox("请先选择需要删除的车票信息");
 }
 else
 {

  CString id = m_pay.GetItemText(sel, 0);
  int num = ReadCai();
  FILE *file = fopen(txt_name, "w+");

  for (int i = 0; i < num; i++)
  {
   if (strcmp(id, cai[i].id) == 0)
    continue;
   fprintf(file, "%s   %s   %s   %s   %s   %s\r\n", cai[i].id, cai[i].name, cai[i].type, cai[i].price, cai[i].flag, cai[i].time);
  }
  fclose(file);
  MessageBox("删除成功""提示");
  ShowCai();
 }
}

void pay::OnButton6()
{
 // TODO: Add your control notification handler code here
 m_pay.DeleteAllItems();
 CString id;
 GetDlgItem(IDC_EDIT10)->GetWindowText(id);
 if (id == "")
 {
  ShowCai();
 }
 else
 {
  int i = 0;
  for (int a = 0; a < ReadCai(); a++)
  {
   if (strcmp(id, cai[a].id) == 0)
   {

    m_pay.InsertItem(i, 0);
    m_pay.SetItemText(i, 0, cai[a].id);
    m_pay.SetItemText(i, 1, cai[a].name);
    m_pay.SetItemText(i, 2, cai[a].type);
    m_pay.SetItemText(i, 3, cai[a].price);
    m_pay.SetItemText(i, 4, cai[a].flag);
    m_pay.SetItemText(i, 5, cai[a].time);
    i++;
   }
  }
 }
}

void pay::OnButton10()
{
 // TODO: Add your control notification handler code here

 int sel = m_pay.GetSelectionMark();
 if (sel == -1)
 {
  MessageBox("请选择需要购票的车次!");
 }
 else
 {

  // 将此购票信息存入buy.txt
  FILE *file = fopen("buy.txt""a+");
  fprintf(file, "%s   %s\r\n", rname, m_pay.GetItemText(sel, 0));
  fclose(file);

  // 修改余票数量
  int num = ReadCai();
  FILE *filex = fopen("chepiao.txt""w");
  for (int i = 0; i < num; i++)
  {
   if (i == sel)
   {

    int port = _ttoi(m_pay.GetItemText(sel, 4)); //转化成10进制;
    port = port - 1;
    CString x;
    x.Format("%d", port);
    strcpy(cai[i].flag, x);
   }
   fprintf(filex, "%s   %s   %s   %s   %s   %s\r\n", cai[i].id, cai[i].name, cai[i].type, cai[i].price, cai[i].flag, cai[i].time);
  }
  fclose(file);

  show();
  ShowCai();
  MessageBox("办理成功");
 }
}

void pay::show()
{
 m_list.DeleteAllItems();
 int num = readnum();
 for (int i = 0; i < num; i++)
 {
  m_list.InsertItem(i, 0);
  m_list.SetItemText(i, 0, caix[i].id); // 车次
  m_list.SetItemText(i, 1, caix[i].name); // 始发站
 }
}

int pay::readnum()
{

 FILE *file = fopen("buy.txt""r");
 int a = 0;
 while (!feof(file))
 {
  fscanf(file, "%s%s", &caix[a].id, &caix[a].name);
  a++;
 }
 fclose(file);
 a--;
 return a;
}

void pay::OnButton8()
{
 // TODO: Add your control notification handler code here
 CString str; //获取车次
 GetDlgItem(IDC_EDIT3)->GetWindowText(str);

 FILE *file = fopen("buy.txt""r");
 int a = 0;
 char x0[20];
 char x1[20];

 float count = 0;

 while (!feof(file))
 {
  fscanf(file, "%s%s", &x0, &x1);
  if (strcmp(x1, str) == 0)
  {

   count++;
  }
 }
 fclose(file);

 CString aaa;
 aaa.Format("当前车次售票张数为:%f", count - 1);

 MessageBox(aaa);
}

void pay::OnButton7()
{
 // TODO: Add your control notification handler code here
 int sel = m_list.GetSelectionMark();
 if (sel == -1)
 {
  MessageBox("请选择需要退票的车次!");
 }
 else
 {

  // 删除buy.tex中的已购买车票
  int num = readnum();
  FILE *file = fopen("buy.txt""w+");

  for (int i = 0; i < num; i++)
  {
   if (i == sel)
    continue;
   fprintf(file, "%s   %s\r\n", caix[i].id, caix[i].name);
  }
  fclose(file);

  MessageBox("删除成功""提示");
  ShowCai();
  show();
 }
}

void pay::OnButton9()
{
 // TODO: Add your control notification handler code here
 int index = m_name.GetCurSel();
 CString str_name;
 CString strI;
 m_name.GetLBText(index, str_name);
 rname = str_name;
 MessageBox("用户切换成功""提示");
}

运行环境

软件:vc 6.0

注:

  • vs code运行会报错
  • vs 有的版本可以正常运行
  • 建议还是运行vc 6.0

运行方式:

  • 记得好像是先进入vc 6.0
  • 选择打开文件
  • 选择运行mfc文件
  • 具体操作建议百度/谷歌打开mfc工程的方法
  • 代码肯定是可以正常运行的,已测试

工程文件下载

点击下方 源码下载

进入小程序即可查看下载链接

源码下载

结语

代码均为原创

版权归作者所有!

仅供小伙伴们参考学习使用!

请勿用于其他用途!

希望对您有所帮助

如有错误欢迎小伙伴指正~

我是 海轰ଘ(੭ˊᵕˋ)੭

创作不易

点个赞吧

谢谢支持 ❤️


浏览 78
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报