生活随笔
收集整理的這篇文章主要介紹了
C++课程设计,12306模拟写起来就是这么粗暴
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這篇文章很詳細,也很多希望可以好好看看!看完C++穩過!
一、12306應該具備那些功能 1.查詢(一個月以內的): 1.查車票:出發地+目的地+出發時間->顯示經過兩站車票信息 (余票,車次信息可直接查詢直接打印,是否為過路車,歷時不是總歷時,是兩站間歷時)(只有這種查詢方式能查詢余票) 2.車站車次查詢: 車站+出發時間段+到達時間段->打印始發站與終點站信息(車站與時間)。 (查詢的的車站即作為始發站,又作為終點站) 3.車次查詢:車次+出發日期->打印該車次詳細信息 (經過站點序號,站名,到時,發時,停留,終點站,起始站,歷時) 2.操作 1.購票(某個區間的票-1)一天限購五張,一個月以內的。 2.退票 (某個區間的票+1) 火車開了之后,不能退票,只能改簽。 3.改簽(相當于退票+從新購票)一張票僅能改簽一次 4.刷新 相當于重新讀入 5.換乘 兩個城市不能接聯通,需要借助最短,需要找出中轉城市,可能一個在佳木斯,一個在宜賓,這不一定是換乘一次,只要有火車站無非是換乘的次數多一點,反正能到(間隔大于10min)。 3.管理員操作 1.增刪查改站點(查)可以繼承一個查詢類 票一旦起售,不可修改站點,雖然沒怎么見過火車站被拆得,但是還是有,但是拆除不可能是突然的拆除,所以會提前收到消息,先停售,再刪除,修改名稱可以直接修改。站點到站時間只能修改一個月以后的。 2.放票 無特殊需要,無需操作,每天的列車表都是一樣的。 3.增加車次 (沒有特殊要求,國家鐵路局規定站點名唯一,車次名唯一) 4.用戶端操作 1.各種查詢 2.買退改,重點是換乘(這個不一定好寫) 2.查詢已買到的票 二、根據數據寫出大體思路
時間類:
數據: a) 應包含年月日時分 函數: a) 買票時間為系統時間,應該寫一個自動獲取函數。 b) 應該寫一個打印時間函數 c) 按時間查詢的時候需要獲取時間,需要些get函數 d) 為了便于管理員修改信息,需要設置set函數 e) 方便輸入輸出重載輸入輸出 f) 需要按照時間排序,可能需要重載小于號。 g) 因為提前30天放票,所以這里要考慮寫判斷閏年的函數,和加減日期的函數(還有分鐘的單獨寫,用于停靠時間,沒見過一個車停靠超過一個小時的)
站點數據類
數據 a) 站點名稱 (本來想寫一個id來著,但是中國的火車站名字唯一,自己就可以當作編號) b) 經過車次(int 型vector的下標)寫成vector比較合適 函數 a) 打印車次信息,理論上實可以返回一個Vector容器,這樣就可以得到車次序號,因為在數據類中看不到列車數據。如果不行可以以引用的形式傳入一個vector進行復制。 b) 重載輸入輸出 c) Set函數 修改數據 d) Insert 函數 增添 e) Delete 函數 刪除
列車站臺數據類 1) 數據 a) 出發時間 b) 到站時間 c) 出發時段 1-4 d) 到達時段1-4 e) 站臺偽指針(int 下標實現) 2) 函數 Get set函數 加重載輸入輸出全家桶 列車類包含
數據 a) 車次(唯一) b) 停靠站 到達每一個站的時間(多個列車經過相同的車站,但是時間不一定一樣)。停靠站可以寫一個Vector但是vector寫一個pair類型pair(string,pair(int,時間)),因為站名唯一,所以可以將站名用map映射. 寫成一個列車站臺數據類,這里用vector 存即可。 c) 該車次某個站的余票 每個車次的每張票都是獨立的這里應該寫到列車類。 d) 每張票賣給了誰,用戶的信息(存vector,vector的下標1,存某個身份證,代表第一張票,關于什么靠窗,站票臥鋪,每節車廂都一樣,不用考慮,需要考慮吧基于某種函數關系即可算出在什么位置)。 函數 a) 買票(判斷是否大于五張) b) 退票( 合起來就是改簽 ) c) 查詢余票,給定區間。 d) 刷新 e) 獲得用戶信息,方法跟上一條站點信息一樣。 f) 打印停靠站以及信息 g) 重載輸入輸出 h) 增查改站點(這個不需要條件,可以直接操作修改站名,同時修改,還有站點信息) i) 刪站點,改時間這些需要調整一個月以后的。 關于用Vector存火車站點的問題要調整一下。String一般按24字節每輛火車按平均20個站點計算,中國客運火車6000+(某度不知地道準不準),就會有2.7MB的火車數據文件讀取,刷新一次讀取這么多。而中國一共就有5000+的火車站,四舍五入算他1萬,用一個usigned short 存6萬,肯定夠用,這樣讀取數據會快一點,所以string 非常好也不能濫用。
車票類: 數據: a) 車票編號:基于的hash算法(這里保證每一張票的編號不一樣即可) b) 購票日期: c) 車次信息: d) 出發站: e) 目標站: f) 乘車時間: 只存一個月的(到時候按時間排序,超過乘車時間一個月,存成日志文件) g) 購票人信息:int 指針; 函數 a) 打印車票 b) Get所有的信息多個函數; c) 不設置set函數,改簽,跟退票之后hash值不同,直接銷毀即可。 d) 重載輸入輸出 e) 無參初始化 f) 含參初始化 用戶數據類: 數據 姓名 身份證號碼 已買到的票: 手機號碼: 函數 a) 重載輸入輸出 b) Get set函數(僅限于手機號的修改,get是全部) c) 無參初始化 d) 含參初始化 中規中矩沒有特殊操作 查詢基類: a) 車站數據 b) 列車數據 c) 用戶數據 d) 車票數據(也是記錄) 函數 a) 查車票:出發地+目的地+出發時間->顯示經過兩站車票信息余票,車次信息,目的地,出發地模糊查詢,一個市的站都會出來。 能到達的車就是出發站與目的站的列車表中重合的部分 補充:如果不能直接到達,還需要考慮換乘,換乘實現實現的話,一開始沒思路,用BFS的思想,用一個queue將初始站的所有車次的所有站點放到queue里,找出第一個車放到queue里,找到到目的地的車就儲存,當第一層既出發站的所有經過地點都沒有可換乘車輛時,才進行二次檢索,只能換乘一次。 b) 車站車次查詢: 車站+出發時間段+到達時間段->打印始發站與終點站信息(車站與時間)。 (查詢的的車站即作為始發站,又作為終點站) 出發時段有四個0-6 6-12 12-18 18-24 編號1-4,0-24就不做限定 實現直接在車站信息中調取車次數據的下標,判斷即可,可以find_if()。 c) 車次查詢 打印車次詳細信息,直接車站數據,find()即可。 8.用戶端: 繼承查詢基類, 登陸: 查詢自己的信息: 購票: 先查詢,找到合適的車,輸入傳入起始站,目標站,車次,利用之前的 函數,配合可以完成操作。 退票: 從已買到的票中把票退了,數據類中以之前寫了相關的函數 改簽:先退后買。 換乘://把他寫到了查詢類里了 管理端: 增刪查改站點(查)可以繼承一個查詢類 票一旦起售,不可修改站點,雖然沒怎么見過火車站被拆得,但是還是有,但是拆除不可能是突然的拆除,所以會提前收到消息,先停售,再刪除,修改名稱可以直接修改。站點到站時間只能修改一個月以后的。 2.放票 無特殊需要,無需操作,每天的列車表都是一樣的。 3.增加車次 (沒有特殊要求,國家鐵路局規定站點名唯一,車次名唯一) 能夠查詢所有的用戶信息(按姓名,身份證號)。 4.戶信息,不對用戶信息進行修改。 5 Map 按車次 查記錄 Map 按身份證 查記錄 Map 按姓名(一名多人)查記錄 按車站 查記錄 12306管理端能查到好幾年之前的,這就得需要動態讀取,文件,和動態生成文件名,目前還沒有找到實現的方法,如果不行,就分成這個月,和這個月之前的。
#include <bits/stdc++.h.>
using namespace std
;
class Time
{ int year
, month
, day
; int hour
, mini
;
public
: Time ( ) { loadtime ( ) ; } Time ( int y
, int m
, int d
) : year ( y
) , month ( m
) , day ( d
) { } ; void loadtime ( ) ; friend istream
& operator
>> ( istream
& in
, Time
& a
) ; friend stringstream
& operator
>> ( stringstream
& in
, Time
& a
) ; friend ostream
& operator
<< ( ostream
& out
, Time
& a
) ; bool operator
< ( const Time
& d
) const ; int judge ( ) const ; int judge ( int year
) const ; Time operator
+ ( int a
) ; bool operator
== ( Time
& a
) ; friend
int operator
- ( Time a
, Time b
) ; void print ( ) const ; bool operator
> ( Time
& a
) ; int getday ( ) { return day
; } int getmonth ( ) { return month
; } int getyear ( ) { return year
; } int gethour ( ) { return hour
; } void set ( ) ; void setmonth ( int a
) { month
= a
; } pair
< int , int > tksj ( Time
& b
) ; void seth ( int a
, int b
) { hour
= a
; mini
= b
; }
} ; bool Time
: : operator
> ( Time
& a
) { return ( year
!= a
. year
) ? year
> a
. year
: month
!= a
. month
? month
> a
. month
: day
!= a
. day
? day
> a
. day
: 0 ; }
bool Time
: : operator
== ( Time
& a
)
{ return ( year
!= a
. year
) ? 0 : month
!= a
. month
? 0 : day
!= a
. day
? 0 : hour
!= a
. hour
? 0 : mini
== a
. mini
;
}
Time Time
: : operator
+ ( int a
)
{ int m
[ ] = { 0 , 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 } ; day
+ = a
; int flag
= 1 ; while ( day
> m
[ month
] ) { if ( month
== 2 ) { if ( day
> m
[ month
] + judge ( ) ) { day
- = m
[ month
] + judge ( ) ; month
++ ; } } else { day
= day
- m
[ month
] ; month
++ ; } if ( month
> 12 ) { month
- = 12 ; year
++ ; } }
}
int operator
- ( Time a
, Time b
)
{ int monthdays
[ 2 ] [ 12 ] = { { 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 } , { 31 , 29 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 } } ; int yeardays
[ 2 ] = { 365 , 366 } ; int sumdays
= 0 ; if ( a
. year
== b
. year
&& a
. month
== b
. month
) { sumdays
= b
. day
- a
. day
; } else if ( a
. year
== b
. year
) { sumdays
+ = monthdays
[ a
. judge ( a
. year
) ] [ a
. month
- 1 ] - a
. day
; for ( int i
= a
. month
; i
< b
. month
- 1 ; i
++ ) sumdays
+ = monthdays
[ a
. judge ( a
. year
) ] [ i
] ; sumdays
+ = b
. day
; } else { sumdays
+ = monthdays
[ a
. judge ( a
. year
) ] [ a
. month
- 1 ] - a
. day
; for ( int i
= a
. month
; i
< 12 ; i
++ ) sumdays
+ = monthdays
[ a
. judge ( a
. year
) ] [ i
] ; for ( int i
= a
. year
+ 1 ; i
< b
. year
; i
++ ) sumdays
+ = yeardays
[ a
. judge ( i
) ] ; for ( int i
= 0 ; i
< b
. month
- 1 ; i
++ ) sumdays
+ = monthdays
[ a
. judge ( b
. year
) ] [ i
] ; sumdays
+ = b
. day
; } return sumdays
;
}
int Time
: : judge ( ) const { if ( year
% 4 == 0 && year
% 100 != 0 || year
% 400 == 0 ) return 1 ; else return 0 ;
}
int Time
: : judge ( int year
) const { if ( year
% 4 == 0 && year
% 100 != 0 || year
% 400 == 0 ) return 1 ; else return 0 ;
}
void Time
: : loadtime ( ) { time_t rawtime
; struct tm
* ptminfo
; time ( & rawtime
) ; ptminfo
= localtime ( & rawtime
) ; year
= ptminfo
-> tm_year
+ 1900 ; month
= ptminfo
-> tm_mon
+ 1 ; day
= ptminfo
-> tm_mday
; hour
= ptminfo
-> tm_hour
; mini
= ptminfo
-> tm_min
;
}
istream
& operator
>> ( istream
& in
, Time
& a
) { in
>> a
. year
>> a
. month
>> a
. day
>> a
. hour
>> a
. mini
; return in
;
}
ostream
& operator
<< ( ostream
& out
, Time
& a
)
{ out
<< a
. year
<< " " << a
. month
<< " " << a
. day
<< " " << a
. hour
<< " " << a
. mini
<< ' ' ; return out
;
}
stringstream
& operator
>> ( stringstream
& in
, Time
& a
) { in
>> a
. year
>> a
. month
>> a
. day
>> a
. hour
>> a
. mini
; return in
;
}
bool Time
: : operator
< ( const Time
& d
) const { return year
!= d
. year
? year
< d
. year
: month
!= d
. month
? month
< d
. month
: day
!= d
. day
? hour
< d
. hour
: mini
< d
. mini
;
}
void Time
: : print ( ) const
{ cout
<< year
<< "年" << month
<< "月" << day
<< "日" ; printf ( "%02d:%02d\n" , hour
, mini
) ;
}
void Time
: : set ( )
{ int monthdays
[ 2 ] [ 12 ] = { { 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 } , { 31 , 29 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 } } ; cout
<< "請依次輸入" << " 年" << " 月" << " 日" << " 小時" << " 分鐘" << endl
; cin
>> year
; while ( year
< 2019 ) { cout
<< "不是正確時間,請重新輸入年" << endl
; cin
>> year
; } cin
>> month
; while ( month
> 12 || month
< 1 ) { cout
<< "不是正確時間,請重新輸入月" << endl
; cin
>> month
; } cin
>> day
; while ( monthdays
[ judge ( ) ] [ month
- 1 ] < day
|| day
< 1 ) { cout
<< "不是正確時間,請重新輸入日" << endl
; cin
>> day
; } cin
>> hour
; while ( hour
> 24 || hour
< 0 ) { cout
<< "不是正確時間,請重新輸入小時" << endl
; cin
>> hour
; } cin
>> mini
; while ( mini
> 60 || mini
< 0 ) { cout
<< "不是正確時間,請重新輸入分鐘" << endl
; cin
>> mini
; } cout
<< "時間已更改為" << endl
; print ( ) ;
}
pair
< int , int > Time
: : tksj ( Time
& b
)
{ pair
< int , int > w
; if ( mini
< b
. mini
) { w
. second
= mini
- b
. mini
+ 60 ; w
. first
= hour
- b
. hour
- 1 ; return w
; } w
. second
= mini
- b
. mini
; w
. first
= hour
- b
. hour
; return w
;
}
struct piao
{ string name
; int sum
[ 31 ] ; bool operator
< ( const piao
& a
) const { return name
< a
. name
; }
} ;
class Station
{ string name
; int crotrain
; vector
< piao
> checi
;
public
: Station ( string na
, int n
) ; Station ( ) : name ( "" ) , crotrain ( 0 ) { checi
. clear ( ) ; } void print ( int riqi
) ; void insert ( string a
) ; vector
< piao
> & quecheci ( ) ; friend istream
& operator
>> ( istream
& in
, Station
& a
) ; friend ostream
& operator
<< ( ostream
& out
, Station
& a
) ; bool
del ( string a
) ; bool
set ( string a
, int b
, int c
) ; void setname ( string a
) { this
-> name
= a
; } string
getname ( ) { return name
; } void buytic ( string a
, int b
) ; void rebac ( string a
, int b
) ;
} ;
void Station
: : rebac ( string a
, int b
)
{ for ( auto po
= checi
. begin ( ) ; po
!= checi
. end ( ) ; po
++ ) if ( po
-> name
== a
) po
-> sum
[ b
] ++ ;
}
void Station
: : buytic ( string a
, int b
)
{ for ( auto po
= checi
. begin ( ) ; po
!= checi
. end ( ) ; po
++ ) if ( po
-> name
== a
) po
-> sum
[ b
] -- ;
}
Station
: : Station ( string na
, int n
)
{ name
= na
; crotrain
= n
; for ( int i
= 0 ; i
< n
; i
++ ) { cout
<< "輸入車次 余票:\n" ; piao w
; cin
>> w
. name
; for ( int i
= 0 ; i
< 31 ; i
++ ) cin
>> w
. sum
[ i
] ; checi
. push_back ( w
) ; }
}
istream
& operator
>> ( istream
& in
, Station
& w
)
{ in
>> w
. name
>> w
. crotrain
; for ( int i
= 0 ; i
< w
. crotrain
; i
++ ) { piao qq
; in
>> qq
. name
; for ( int i
= 0 ; i
< 31 ; i
++ ) in
>> qq
. sum
[ i
] ; w
. checi
. push_back ( qq
) ; } return in
;
}
ostream
& operator
<< ( ostream
& out
, Station
& w
)
{ out
<< w
. name
<< " " << w
. crotrain
<< endl
; for ( auto po
= w
. checi
. begin ( ) ; po
!= w
. checi
. end ( ) ; po
++ ) { out
<< po
-> name
<< endl
; for ( int i
= 0 ; i
< 31 ; i
++ ) out
<< po
-> sum
[ i
] << ' ' ; out
<< endl
; } return out
;
}
void Station
: : print ( int b
)
{ cout
<< name
<< ":" << endl
; for ( auto po
= checi
. begin ( ) ; po
!= checi
. end ( ) ; po
++ ) { cout
<< "車次:" << po
-> name
<< " 余票:" << po
-> sum
[ b
] << endl
; }
}
vector
< piao
> & Station
: : quecheci ( )
{ return checi
;
}
void Station
: : insert ( string a
)
{ piao w
; w
. name
= a
; for ( int i
= 0 ; i
< 31 ; i
++ ) cin
>> w
. sum
[ i
] ; checi
. push_back ( w
) ;
}
bool Station
: : del ( string a
)
{ for ( auto po
= checi
. begin ( ) ; po
!= checi
. end ( ) ; po
++ ) { if ( po
-> name
== a
) { checi
. erase ( po
) ; break ; } } if ( checi
. size ( ) != crotrain
) { crotrain
= checi
. size ( ) ; return 1 ; } else return 0 ;
} bool Station
: : set ( string a
, int b
, int c
) { for ( auto po
= checi
. begin ( ) ; po
!= checi
. end ( ) ; po
++ ) { if ( po
-> name
== a
) { po
-> sum
[ c
] = b
; return 1 ; } else return 0 ; } }
class Zt
{ pair
< int , int > arrive
; pair
< int , int > leave
; string station
; int piao
[ 31 ] ;
public
: Zt ( string a
, int c
, int d
, int e
, int f
) : station ( a
) { setarrive ( c
, d
) ; setleave ( e
, f
) ; fill ( piao
, piao
+ 31 , 100 ) ; } Zt ( ) { fill ( piao
, piao
+ 31 , 100 ) ; } pair
< int , int > & getarrive ( ) { return arrive
; } pair
< int , int > & getleave ( ) { return leave
; } int getarr ( ) ; int getlea ( ) ; string
getsta ( ) const { return station
; } void setarrive ( int a
, int b
) { arrive
. first
= a
; arrive
. second
= b
; } void setleave ( int a
, int b
) { leave
. first
= a
; leave
. second
= b
; } void setsta ( string a
) { station
= a
; } void printtk ( ) ; void print ( ) ; void printa ( ) ; void printl ( ) ; int getpiao ( int i
) { return piao
[ i
] ; } void setpiao ( int a
, int b
) { piao
[ b
] = a
; } friend istream
& operator
>> ( istream
& in
, Zt
& a
) ; friend ostream
& operator
<< ( ostream
& out
, Zt
& a
) ;
} ;
void Zt
: : print ( )
{ cout
<< station
<< endl
; printf ( "%02d:%02d " , arrive
. first
, arrive
. second
) ; printf ( "%02d:%02d\n" , leave
. first
, leave
. second
) ;
}
void Zt
: : printl ( )
{ cout
<< station
<< endl
; printf ( "%02d:%02d\n" , leave
. first
, leave
. second
) ;
}
void Zt
: : printa ( )
{ cout
<< station
<< endl
; printf ( "%02d:%02d " , arrive
. first
, arrive
. second
) ;
}
int Zt
: : getarr ( )
{ if ( arrive
. first
>= 0 || arrive
. first
< 6 ) return 1 ; else if ( arrive
. first
>= 6 || arrive
. first
< 12 ) return 2 ; else if ( arrive
. first
>= 12 || arrive
. first
< 18 ) return 3 ; else return 4 ;
}
int Zt
: : getlea ( )
{ if ( leave
. first
>= 0 || leave
. first
< 6 ) return 1 ; else if ( leave
. first
>= 6 || leave
. first
< 12 ) return 2 ; else if ( leave
. first
>= 12 || leave
. first
< 18 ) return 3 ; else return 4 ;
}
void Zt
: : printtk ( )
{ cout
<< leave
. second
- arrive
. second
<< "分鐘 " << endl
;
}
istream
& operator
>> ( istream
& in
, Zt
& a
)
{ in
>> a
. station
; in
>> a
. arrive
. first
; in
>> a
. arrive
. second
;
in
>> a
. leave
. first
; in
>> a
. leave
. second
; for ( int i
= 0 ; i
< 31 ; i
++ ) in
>> a
. piao
[ i
] ; return in
;
}
ostream
& operator
<< ( ostream
& out
, Zt
& a
)
{ out
<< a
. station
<< ' ' << a
. arrive
. first
<< ' ' << a
. arrive
. second
<< ' ' << a
. leave
. first
<< ' ' << a
. leave
. second
<< ' ' ; for ( int i
= 0 ; i
< 31 ; i
++ ) out
<< a
. piao
[ i
] << ' ' ; out
<< endl
; return out
;
}
class Train
{ string train
; vector
< Zt
> station
; vector
< string
> people
; map
< string
, int > ys
; struct node
{ int val
; int len
; int lazy
; int l
, r
; } tree
[ 31 ] [ 300005 ] ;
public
: Train ( ) : train ( "" ) { people
. clear ( ) ; station
. clear ( ) ; } Train ( string tr
) : train
( tr
) { people
. clear ( ) ; station
. clear ( ) ; } pair
< int , int > buytic ( string a
, string b
, int riqi
) ; void reback ( string a
, string b
, int riqi
) ; void insert ( string a
, int d
, int f
, int g
, int h
, string w
) ; bool
del ( string a
) ; void cx ( string a
) ; void setarrive ( string a
) ; void setleave ( string a
) ; void setsta ( string a
) ; int cxyp ( string a
, string b
, int c
) ; vector
< string
> & getpeople ( ) { return people
; } vector
< Zt
> & getzt ( ) { return station
; } friend istream
& operator
>> ( istream
& in
, Train
& a
) ; friend ostream
& operator
<< ( ostream
& out
, Train
& a
) ; void settr ( string a
) { train
= a
; } string
gettr ( ) { return train
; }
} ;
pair
< int , int > Train
: : buytic ( string a
, string b
, int c
)
{ pair
< int , int > www
; www
= station
[ ys
[ a
] ] . getleave ( ) ; return www
;
}
void Train
: : reback ( string a
, string b
, int riqi
)
{
}
void Train
: : insert ( string a
, int d
, int f
, int g
, int h
, string w
)
{ Zt
tem ( a
, d
, f
, g
, h
) ; if ( w
== "END" || w
== "end" || w
== "End" ) { station
. push_back ( tem
) ; ys
. insert ( make_pair ( a
, station
. size ( ) - 1 ) ) ; } else { station
. insert ( station
. begin ( ) + ys
[ w
] , tem
) ; for ( auto po
= station
. begin ( ) + ys
[ w
] - 1 ; po
!= station
. end ( ) ; po
++ ) { ys
. insert ( make_pair ( po
-> getsta ( ) , po
- station
. begin ( ) ) ) ; } }
}
int Train
: : cxyp ( string a
, string b
, int c
)
{
}
bool Train
: : del ( string a
)
{ bool flag
= 0 ; for ( auto po
= station
. begin ( ) ; po
!= station
. end ( ) ; po
++ ) { if ( po
-> getsta ( ) == a
) { station
. erase ( po
) ; flag
= 1 ; break ; } } return flag
;
}
void Train
: : cx ( string a
)
{ for ( auto po
= station
. begin ( ) ; po
!= station
. end ( ) ; po
++ ) { if ( po
-> getsta ( ) == a
) { po
-> print ( ) ; po
-> printtk ( ) ; break ; } }
}
void Train
: : setarrive ( string a
)
{ for ( auto po
= station
. begin ( ) ; po
!= station
. end ( ) ; po
++ ) { if ( po
-> getsta ( ) == a
) { int a
, b
; cin
>> a
>> b
; po
-> setarrive ( a
, b
) ; break ; } }
}
void Train
: : setleave ( string a
)
{ for ( auto po
= station
. begin ( ) ; po
!= station
. end ( ) ; po
++ ) { if ( po
-> getsta ( ) == a
) { int a
, b
; cin
>> a
>> b
; po
-> setleave ( a
, b
) ; break ; } }
}
void Train
: : setsta ( string a
)
{ for ( auto po
= station
. begin ( ) ; po
!= station
. end ( ) ; po
++ ) { if ( po
-> getsta ( ) == a
) { po
-> setsta ( a
) ; break ; } }
}
istream
& operator
>> ( istream
& in
, Train
& a
)
{ a
. ys
. clear ( ) ; int m
, n
; Zt demo
; in
>> a
. train
>> n
>> m
; while ( n
-- ) { in
>> demo
; a
. station
. push_back ( demo
) ; a
. ys
. insert ( make_pair ( demo
. getsta ( ) , a
. station
. size ( ) - 1 ) ) ; } while ( m
-- ) { string w
; in
>> w
; a
. people
. push_back ( w
) ; }
return in
;
}
ostream
& operator
<< ( ostream
& out
, Train
& a
)
{ out
<< a
. train
<< ' ' << a
. station
. size ( ) << ' ' << a
. people
. size ( ) << endl
; for ( auto po
= a
. station
. begin ( ) ; po
!= a
. station
. end ( ) ; po
++ ) out
<< * po
<< ' ' ; for ( auto po
= a
. people
. begin ( ) ; po
!= a
. people
. end ( ) ; po
++ ) out
<< * po
<< ' ' ; out
<< endl
; return out
;
}
int main ( )
{ Train demo
; cin
>> demo
; string a
, b
; int c
; int d
, e
, f
, g
; cin
>> a
>> d
>> e
>> f
>> g
>> b
; demo
. insert ( a
, d
, e
, f
, g
, b
) ; cin
>> a
>> b
>> c
; cout
<< demo
. cxyp ( a
, b
, c
) << endl
; cin
>> a
>> b
>> c
; cout
<< demo
. cxyp ( a
, b
, c
) << endl
; demo
. buytic ( a
, b
, c
) ; cout
<< demo
. cxyp ( a
, b
, c
) << endl
; demo
. reback ( a
, b
, c
) ; cout
<< demo
. cxyp ( a
, b
, c
) << endl
;
}
class Tick
{ Time buy
; Time leave
; string from
; string to
; string id
; string name
;
public
: Tick ( ) : from ( " " ) , to ( " " ) , id ( " " ) { } Tick ( Time b
, Time l
, string f
, string t
, string i
, string w
) : buy ( b
) , leave ( l
) , from ( f
) , to ( t
) , id ( i
) , name ( w
) { } Time
& getbuy ( ) { return buy
; } Time
& getlea ( ) { return leave
; } string
getfrom ( ) { return from
; } string
getto ( ) { return to
; } string
getid ( ) { return id
; } string
getname ( ) ; void print ( ) ; string
getna ( ) { return name
; } bool operator
== ( Tick
& a
) ; friend istream
& operator
>> ( istream
& in
, Tick
& a
) ; friend ostream
& operator
<< ( ostream
& out
, Tick
& a
) ;
} ;
string Tick
: : getname ( )
{ string w
; stringstream tem
; w
. clear ( ) ; tem
. clear ( ) ; tem
<< leave
. getday ( ) ; tem
<< leave
. getmonth ( ) ; tem
<< leave
. getyear ( ) ; tem
>> w
; w
= w
+ id
+ from
+ to
; return w
;
}
bool Tick
: : operator
== ( Tick
& a
)
{ return a
. buy
== buy
? ( a
. leave
== leave
? ( from
== a
. from
? ( to
== a
. to
? id
== a
. id
: 0 ) : 0 ) : 0 ) : 0 ;
}
istream
& operator
>> ( istream
& in
, Tick
& a
)
{ in
>> a
. buy
>> a
. leave
>> a
. from
>> a
. to
>> a
. id
>> a
. name
; return in
;
}
ostream
& operator
<< ( ostream
& out
, Tick
& a
)
{ out
<< a
. buy
<< ' ' << a
. leave
<< ' ' << a
. from
<< ' ' << a
. to
<< ' ' << a
. id
<< ' ' << a
. name
<< endl
; return out
;
}
void Tick
: : print ( )
{ cout
<< leave
<< endl
; cout
<< from
<< "----->" << to
<< endl
; cout
<< id
<< endl
;
}
class User
{ string name
; string id
; vector
< string
> tic
; string phone
; string key
; bool ban
;
public
: User ( ) : name ( "" ) , id ( "" ) , phone ( "" ) { tic
. clear ( ) ; } User ( string a
, string b
, string c
) : name ( a
) , id ( b
) , phone ( c
) { tic
. clear ( ) ; } void buytic ( string a
) { tic
. push_back ( a
) ; } void setph ( string p
) { phone
= p
; } vector
< string
> & gettic ( ) { return tic
; } string
getname ( ) { return name
; } string
getphone ( ) { return phone
; } string
getid ( ) { return id
; } string
getkey ( ) { return key
; } void del ( string a
) ; int getboupiao ( ) { return tic
. size ( ) ; } bool
getban ( ) { return ban
; } friend istream
& operator
>> ( istream
& in
, User
& a
) ; friend ostream
& operator
<< ( ostream
& out
, User
& a
) ;
} ;
void User
: : del ( string a
)
{ for ( auto po
= tic
. begin ( ) ; po
!= tic
. end ( ) ; po
++ ) { if ( * po
== a
) tic
. erase ( po
) ; }
}
istream
& operator
>> ( istream
& in
, User
& a
)
{ int w
; in
>> a
. name
>> a
. id
>> a
. phone
>> a
. key
>> a
. ban
>> w
; while ( w
-- ) { string q
; in
>> q
; int dday
= ( q
[ 0 ] - '0' ) * 10 + ( q
[ 1 ] - '0' ) ; int dmonth
= ( q
[ 2 ] - '0' ) * 10 + ( q
[ 3 ] - '0' ) ; int dyear
= ( ( ( q
[ 4 ] - '0' ) * 10 + ( q
[ 5 ] - '0' ) ) * 10 + ( q
[ 6 ] - '0' ) ) * 10 + ( q
[ 7 ] - '0' ) ; Time
tem ( dyear
, dmonth
, dday
) ; Time now
; now
. loadtime ( ) ; if ( now
> tem
) continue ; a
. tic
. push_back ( q
) ; } return in
;
}
ostream
& operator
<< ( ostream
& out
, User
& a
)
{ out
<< a
. name
<< ' ' << a
. id
<< ' ' << a
. phone
<< ' ' << a
. key
<< ' ' << ' ' << a
. ban
<< ' ' << a
. tic
. size ( ) << endl
; for ( auto po
= a
. tic
. begin ( ) ; po
!= a
. tic
. end ( ) ; po
++ ) out
<< * po
<< ' ' ; return out
;
}
class Baseque
{
protected
: vector
< Station
> station
; map
< string
, int > natosta
; vector
< Train
> train
; map
< string
, int > checitotrain
;
public
: Baseque ( ) { station
. clear ( ) ; natosta
. clear ( ) ; train
. clear ( ) ; checitotrain
. clear ( ) ; load ( ) ; } void czcc ( string a
, int b
, int d
) ; void cxcx ( string a
) ; bool
cxyp ( string a
, string b
, int c
) ; void load ( ) ; void save ( ) ; ~ Baseque ( ) { save ( ) ; }
} ;
void Baseque
: : czcc ( string a
, int b
, int d
)
{ vector
< piao
> w
= station
[ natosta
[ a
] ] . quecheci ( ) ; for ( auto po
= w
. begin ( ) ; po
!= w
. end ( ) ; po
++ ) { vector
< Zt
> q
= train
[ checitotrain
[ po
-> name
] ] . getzt ( ) ; for ( auto pi
= q
. begin ( ) ; pi
!= q
. end ( ) ; pi
++ ) { if ( a
== pi
-> getsta ( ) ) { if ( pi
-> getarr ( ) == b
|| b
== 0 || pi
-> getlea ( ) == d
|| d
== 0 ) { cout
<< po
-> name
<< endl
; if ( pi
!= q
. begin ( ) ) { q
. begin ( ) -> printl ( ) ; if ( pi
== q
. end ( ) - 1 ) { ( q
. end ( ) - 1 ) -> printa ( ) ; cout
<< endl
; } else { pi
-> print ( ) ; ( q
. end ( ) - 1 ) -> printa ( ) ; } cout
<< endl
; } else { q
. begin ( ) -> printl ( ) ; ( q
. end ( ) - 1 ) -> printa ( ) ; cout
<< endl
; } cout
<< endl
; } } } }
}
void Baseque
: : cxcx ( string a
)
{ if ( checitotrain
. find ( a
) == checitotrain
. end ( ) ) { cout
<< "NO Train" << endl
; return ; } vector
< Zt
> w
= train
[ checitotrain
[ a
] ] . getzt ( ) ; for ( auto po
= w
. begin ( ) ; po
!= w
. end ( ) ; po
++ ) { cout
<< station
[ natosta
[ po
-> getsta ( ) ] ] . getname ( ) << ' ' ; if ( po
!= w
. begin ( ) && po
!= w
. end ( ) - 1 ) { po
-> print ( ) ; } else if ( po
== w
. begin ( ) ) { cout
<< po
-> getleave ( ) . first
<< ' ' << po
-> getleave ( ) . first
; } else cout
<< po
-> getarrive ( ) . first
<< ' ' << po
-> getarrive ( ) . second
<< ' ' ; } cout
<< endl
;
}
bool Baseque
: : cxyp ( string a
, string b
, int riqi
)
{ auto w
= station
[ natosta
[ a
] ] . quecheci ( ) ; auto v
= station
[ natosta
[ b
] ] . quecheci ( ) ; set
< piao
> tem
; vector
< piao
> temp
; temp
. clear ( ) ; for ( auto po
= w
. begin ( ) ; po
!= w
. end ( ) ; po
++ ) tem
. insert ( * po
) ; for ( auto po
= v
. begin ( ) ; po
!= v
. end ( ) ; po
++ ) { if ( tem
. insert ( * po
) . second
== 0 ) temp
. push_back ( * po
) ; } if ( temp
. size ( ) == 0 ) { cout
<< "No ticket" << endl
; return 0 ; } for ( auto po
= temp
. begin ( ) ; po
!= temp
. end ( ) ; po
++ ) { auto x
= train
[ checitotrain
[ po
-> name
] ] . getzt ( ) ; int mini
= po
-> sum
[ riqi
] ; int ss
= train
[ checitotrain
[ po
-> name
] ] . cxyp ( a
, b
, riqi
) ; mini
= min ( mini
, ss
) ; cout
<< train
[ checitotrain
[ po
-> name
] ] . gettr ( ) << endl
; ; for ( auto pi
= x
. begin ( ) ; pi
!= x
. end ( ) ; pi
++ ) { pi
-> print ( ) ; } cout
<< mini
<< endl
; } return 1 ;
}
void Baseque
: : load ( ) { int x
, y
, n
; ifstream
in1 ( "station.txt" , ios
: : in
) ; in1
>> n
; while ( n
-- ) { Station tem1
; in1
>> tem1
; if ( tem1
. getname ( ) == "$$" ) continue ; station
. push_back ( tem1
) ; natosta
. insert ( make_pair ( tem1
. getname ( ) , station
. size ( ) - 1 ) ) ; } in1
. close ( ) ; ifstream
in2 ( "train.txt" , ios
: : in
) ; in2
>> n
; while ( n
-- ) { Train tem2
; in2
>> tem2
; if ( tem2
. gettr ( ) == "$$" ) continue ; train
. push_back ( tem2
) ; checitotrain
. insert ( make_pair ( tem2
. gettr ( ) , train
. size ( ) - 1 ) ) ; } in2
. close ( ) ;
}
void Baseque
: : save ( ) { ofstream
out1 ( "station.txt" , ios
: : out
) ; out1
<< station
. size ( ) << endl
; for ( auto po
= station
. begin ( ) ; po
!= station
. end ( ) ; po
++ ) out1
<< * po
<< endl
; out1
. close ( ) ; ofstream
out2 ( "train.txt" , ios
: : out
) ; out2
<< train
. size ( ) << endl
; for ( auto po
= train
. begin ( ) ; po
!= train
. end ( ) ; po
++ ) out2
<< * po
<< endl
; out2
. close ( ) ; }
class Client
: public Baseque
{
protected
: vector
< User
> user
; map
< string
, int > idtouse
; vector
< Tick
> tick
; map
< string
, int > hashtotick
; int now
; string now2
;
public
: Client ( ) { load2 ( ) ; } void load2 ( ) ; void save2 ( ) ; bool
buytic ( string a
, string b
, int c
) ; void reback ( ) ; void replacetic ( ) ; virtual
void login ( ) ; void setphone ( string a
) ; void printmp ( ) ; ~ Client ( ) { save2 ( ) ; }
} ; void Client
: : printmp ( ) { auto ww
= user
[ now
] . gettic ( ) ; for ( auto po
= ww
. begin ( ) ; po
!= ww
. end ( ) ; po
++ ) { cout
<< po
- ww
. begin ( ) + 1 << endl
; tick
[ hashtotick
[ * po
] ] . print ( ) ; } }
bool Client
: : buytic ( string a
, string b
, int c
)
{ if ( ! ( cxyp ( a
, b
, c
) ) ) { cout
<< "無票" << endl
; return 0 ; } if ( user
[ now
] . getboupiao ( ) > 5 || user
[ now
] . getban ( ) ) { cout
<< "購票限制" << endl
; return 0 ; } else { string id
; cin
>> id
; pair
< int , int > www
= train
[ checitotrain
[ id
] ] . buytic ( a
, b
, c
) ; station
[ natosta
[ a
] ] . buytic ( a
, c
) ; Time no1
, no2
; no1
. loadtime ( ) ; no2
. loadtime ( ) ; no2
. seth ( www
. first
, www
. second
) ; if ( no2
. getday ( ) < c
) no2
. setmonth ( no2
. getmonth ( ) + 1 ) ; Tick
tem ( no1
, no2
, a
, b
, now2
, id
) ; tick
. push_back ( tem
) ; hashtotick
. insert ( make_pair ( tem
. getname ( ) , tick
. size ( ) - 1 ) ) ; user
[ now
] . buytic ( tem
. getname ( ) ) ; } return 1 ;
}
void Client
: : reback ( )
{ printmp ( ) ; string id
= now2
; int n
; cout
<< "退哪一張票" << endl
; cin
>> n
; n
= n
- 1 ; auto ww
= user
[ now
] . gettic ( ) ; string a
= tick
[ hashtotick
[ ww
[ n
] ] ] . getfrom ( ) ; cout
<< 1 << endl
; ; string b
= tick
[ hashtotick
[ ww
[ n
] ] ] . getto ( ) ; cout
<< 2 << endl
; ; int c
= tick
[ hashtotick
[ ww
[ n
] ] ] . getlea ( ) . getday ( ) ; cout
<< 3 << endl
; ; train
[ checitotrain
[ id
] ] . reback ( a
, b
, c
) ; cout
<< 4 << endl
; ; string e
= tick
[ hashtotick
[ ww
[ n
] ] ] . getna ( ) ; station
[ natosta
[ a
] ] . rebac ( e
, c
) ; cout
<< 5 << endl
; ; }
void Client
: : replacetic ( )
{ string a
, b
, id
; int c
; cin
>> a
>> b
>> c
; if ( buytic ( a
, b
, c
) ) { cin
>> a
>> b
>> c
; reback ( ) ; }
}
void Client
: : setphone ( string a
)
{ user
[ now
] . setph ( a
) ;
}
void Client
: : login ( )
{ string phone
; string key
; while ( cin
>> phone
>> key
) { for ( auto po
= user
. begin ( ) ; po
!= user
. end ( ) ; po
++ ) { if ( po
-> getphone ( ) == phone
) if ( po
-> getkey ( ) == key
) { cout
<< "Login prosperity" << endl
; now
= po
- user
. begin ( ) ; now2
= po
-> getid ( ) ; return ; } } cout
<< "Wrong password" << endl
; }
}
void Client
: : load2 ( )
{ ifstream
in ( "user.txt" , ios
: : in
) ; int n
; if ( ! in
) return ; in
>> n
; while ( n
-- ) { User tem
; in
>> tem
; user
. push_back ( tem
) ; idtouse
. insert ( make_pair ( tem
. getname ( ) , user
. size ( ) - 1 ) ) ; } in
. close ( ) ; ifstream
in2 ( "tick.txt" , ios
: : in
) ; if ( ! in2
) return ; in2
>> n
; while ( n
-- ) { Tick tem
; in2
>> tem
; if ( tem
. getname ( ) == "$$$" ) continue ; tick
. push_back ( tem
) ; hashtotick
. insert ( make_pair ( tem
. getname ( ) , tick
. size ( ) - 1 ) ) ; } in
. close ( ) ;
}
void Client
: : save2 ( )
{ ofstream
out ( "user.txt" , ios
: : out
) ; out
<< user
. size ( ) << endl
; for ( auto po
= user
. begin ( ) ; po
!= user
. end ( ) ; po
++ ) out
<< * po
<< endl
; out
. close ( ) ; ofstream
out2 ( "tick.txt" , ios
: : out
) ; stringstream sss
; Time now
; now
. loadtime ( ) ; string temp
; sss
<< now
. getyear ( ) << "年" << now
. getmonth ( ) << "月" << now
. getday ( ) << "日" ; sss
>> temp
; ofstream
out3 ( temp
, ios
: : out
) ; int sum
= 0 ; for ( auto po
= tick
. begin ( ) ; po
!= tick
. end ( ) ; po
++ ) { if ( now
> po
-> getlea ( ) ) { out3
<< * po
<< endl
; } else sum
++ ; } out3
. close ( ) ; out2
<< sum
<< endl
; for ( auto po
= tick
. begin ( ) ; po
!= tick
. end ( ) ; po
++ ) { if ( now
> po
-> getlea ( ) ) continue ; out2
<< * po
<< endl
; } out2
. close ( ) ;
}
class Admin
: public Client
{
public
: void login ( ) { } void insertsta ( ) ; bool
delsta ( string a
) ; void setsta ( string a
) ; void inserttr ( ) ; bool
deltr ( string a
) ; void settr ( string a
) ;
} ;
void Admin
: : insertsta ( )
{ Station tem1
; cin
>> tem1
; station
. push_back ( tem1
) ; natosta
. insert ( make_pair ( tem1
. getname ( ) , station
. size ( ) - 1 ) ) ;
}
bool Admin
: : delsta ( string a
)
{ for ( auto po
= station
. begin ( ) ; po
!= station
. end ( ) ; po
++ ) if ( po
-> getname ( ) == a
) { vector
< piao
> ww
= po
-> quecheci ( ) ; for ( auto pi
= ww
. begin ( ) ; pi
!= ww
. end ( ) ; pi
++ ) train
[ checitotrain
[ pi
-> name
] ] . del ( a
) ; station
. erase ( po
) ; return 1 ; } cout
<< "無此車站" << endl
; return 0 ;
}
void Admin
: : setsta ( string a
)
{ if ( ! delsta ( a
) ) return ; insertsta ( ) ; return ;
}
void Admin
: : inserttr ( )
{ Train tem2
; cin
>> tem2
; train
. push_back ( tem2
) ; checitotrain
. insert ( make_pair ( tem2
. gettr ( ) , train
. size ( ) - 1 ) ) ; return ;
}
bool Admin
: : deltr ( string a
)
{ for ( auto po
= train
. begin ( ) ; po
!= train
. end ( ) ; po
++ ) if ( po
-> gettr ( ) == a
) { vector
< Zt
> ww
= po
-> getzt ( ) ; for ( auto pi
= ww
. begin ( ) ; pi
!= ww
. end ( ) ; pi
++ ) station
[ natosta
[ pi
-> getsta ( ) ] ] . del ( a
) ; train
. erase ( po
) ; return 1 ; } cout
<< "無此高鐵" << endl
; return 0 ;
}
void Admin
: : settr ( string a
)
{ if ( ! deltr ( a
) ) return ; inserttr ( ) ;
}
總結
以上是生活随笔 為你收集整理的C++课程设计,12306模拟写起来就是这么粗暴 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。