【課程設(shè)計(jì)|C++】C++程序設(shè)計(jì)基礎(chǔ):火車票售票系統(tǒng)設(shè)計(jì)(MFC)
課設(shè)要求
程序設(shè)計(jì)要求:
用戶管理 車票信息的錄入、修改和刪除 根據(jù)不同需求對(duì)車票信息進(jìn)行瀏覽、查詢 購(gòu)票業(yè)務(wù)管理 退票管理 按車次統(tǒng)計(jì)售票數(shù)量 按車次查詢車票余量 車票信息的導(dǎo)入和導(dǎo)出
程序運(yùn)行界面
演示效果圖(部分)



核心代碼
pay.cpp
// pay.cpp : implementation file
//
#include "stdafx.h"
#include "火車票售票系統(tǒng).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, "始發(fā)站", LVCFMT_CENTER, 100);
m_pay.InsertColumn(2, "終點(diǎn)站", LVCFMT_CENTER, 100);
m_pay.InsertColumn(3, "時(shí)間", LVCFMT_CENTER, 100);
m_pay.InsertColumn(4, "余票數(shù)量", LVCFMT_CENTER, 100);
m_pay.InsertColumn(5, "價(jià)格", 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); // 始發(fā)站
m_pay.SetItemText(i, 2, cai[i].type); // 終點(diǎn)站
m_pay.SetItemText(i, 3, cai[i].price); // 時(shí)間
m_pay.SetItemText(i, 4, cai[i].flag); // 余票
m_pay.SetItemText(i, 5, cai[i].time); // 價(jià)格
}
}
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; // 臨時(shí)文件名
txt_name = "chepiao.txt";
// TODO: Add your control notification handler code here
int sel = m_pay.GetSelectionMark();
if (sel == -1)
{
MessageBox("請(qǐng)選擇需要修改的車票!");
}
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; // 臨時(shí)文件名
txt_name = "chepiao.txt";
// TODO: Add your control notification handler code here
int sel = m_pay.GetSelectionMark();
if (sel == -1)
{
MessageBox("請(qǐng)先選擇需要?jiǎng)h除的車票信息");
}
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("請(qǐng)選擇需要購(gòu)票的車次!");
}
else
{
// 將此購(gòu)票信息存入buy.txt
FILE *file = fopen("buy.txt", "a+");
fprintf(file, "%s %s\r\n", rname, m_pay.GetItemText(sel, 0));
fclose(file);
// 修改余票數(shù)量
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)); //轉(zhuǎn)化成10進(jìn)制;
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); // 始發(fā)站
}
}
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("當(dāng)前車次售票張數(shù)為:%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("請(qǐng)選擇需要退票的車次!");
}
else
{
// 刪除buy.tex中的已購(gòu)買車票
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("用戶切換成功", "提示");
}
運(yùn)行環(huán)境
軟件:vc 6.0
注:
vs code運(yùn)行會(huì)報(bào)錯(cuò) vs 有的版本可以正常運(yùn)行 建議還是運(yùn)行vc 6.0
運(yùn)行方式:
記得好像是先進(jìn)入vc 6.0 選擇打開(kāi)文件 選擇運(yùn)行mfc文件 具體操作建議百度/谷歌打開(kāi)mfc工程的方法 代碼肯定是可以正常運(yùn)行的,已測(cè)試
工程文件下載
點(diǎn)擊下方 源碼下載
進(jìn)入小程序即可查看下載鏈接
結(jié)語(yǔ)
代碼均為原創(chuàng)
版權(quán)歸作者所有!
僅供小伙伴們參考學(xué)習(xí)使用!
請(qǐng)勿用于其他用途!
希望對(duì)您有所幫助
如有錯(cuò)誤歡迎小伙伴指正~
我是 海轟?(?ˊ?ˋ)?
創(chuàng)作不易
點(diǎn)個(gè)贊吧
謝謝支持 ??

評(píng)論
圖片
表情
