基于C++语言实现机动车违章处罚管理系统

关键代码如下所示:

#include 
#include 
#include 
#include 
#include 
using namespace std;
class Violation{
public:
Violation(){points=fine=0;
number=carNumber=tel=location=isSpot
=police=camera=type=isTreated=treatmentDate=notes="";}
Violation(string snumber,string sdate="",string scarNumber="",string stel="",
string slocation="",int ipoints=-1,string sisSpot="",
string spolice="",string scamera="",string itype="",int ifine=0,
string sisTreated="",string streatmentDate="",string snotes="")
:number(snumber),date(sdate),carNumber(scarNumber),tel(stel),
location(slocation),points(ipoints),isSpot(sisSpot),police(spolice),
camera(scamera),type(itype),fine(ifine),isTreated(sisTreated),
treatmentDate(streatmentDate),notes(snotes){}
bool operator==(const Violation& vltn); //查找用 
friend bool compNumber(Violation& vltn1,Violation& vltn2); //编号比较规则 
friend bool compDate(Violation& vltn1,Violation& vltn2); //日期比较规则 
friend bool compCarNumber(Violation& vltn1,Violation& vltn2); //车牌号比较规则 
friend bool compLocation(Violation& vltn1,Violation& vltn); //地点比较规则 
friend bool compPoints(Violation& vltn1,Violation& vltn); //扣分比较规则 
friend istream& operator>>(istream& input,Violation& vltn);
friend ostream& operator<<(ostream& output,Violation& vltn); 
friend class List;
friend int saveFile();
private:
string number; //编号
string date; //日期
string carNumber; //车牌号 
string tel; //联系电话-
string location; //地点 
int points; //扣分 
string isSpot; //现场 
string police; //现场执勤交警 
string camera; //非现场摄像头编号 
string type; //违章类别 
int fine; //罚款
string isTreated; //是否处理 
string treatmentDate; //处理日期 
string notes; //备注 
};
bool Violation::operator==(const Violation& vltn){
if(location==""||vltn.location=="")
if(tel==""||vltn.tel=="")
if(carNumber==""||vltn.carNumber=="")
if(date==""||vltn.date=="")
return number==vltn.number;
else return date==vltn.date;
else return carNumber==vltn.carNumber;
else return tel==vltn.tel;
else return location==vltn.location;
}
bool compNumber(Violation& vltn1,Violation& vltn2){
return vltn1.number>vltn2.number; 
} 
bool compDate(Violation& vltn1,Violation& vltn2){
return vltn1.date>vltn2.date; 
} 
bool compCarNumber(Violation& vltn1,Violation& vltn2){
return vltn1.carNumbervltn2.location; 
} 
bool compPoints(Violation& vltn1,Violation& vltn2){
return vltn1.points>vltn2.points; 
}
istream& operator>>(istream& input,Violation& vltn){
input>>vltn.number>>vltn.date>>vltn.carNumber>>vltn.tel>>vltn.location
>>vltn.points>>vltn.isSpot>>vltn.police>>vltn.camera>>vltn.type
>>vltn.fine>>vltn.isTreated>>vltn.treatmentDate>>vltn.notes;
return input;
}
ostream& operator<<(ostream& output,Violation& vltn){
output< vltnlist;
static List* m_list;
List(){};
List(const List&){}
List& operator=(const List&){}
};
const char* filepath="violation.dat"; //指定数据文件路径 
bool checkNumber(string s,int size){
if(s.size()!=size) return false;
for(int i=0;i'9') return false;
return true;
} 
List* List::m_list=NULL;
List* List::getList(){
if(NULL==m_list) m_list=new List;
return m_list;
}
int List::loadFile(){
ifstream infile(filepath,ios::in);
if(infile)
for(;!infile.eof();){
Violation vltn;infile>>vltn;
vltnlist.push_back(vltn);
}
infile.close();
return 0;
}
int List::saveFile(){
ofstream outfile(filepath,ios::out);
list::iterator it;
for(it=vltnlist.begin();it!=vltnlist.end();it++)
outfile<number<<' '<date<<' '<carNumber<<' '
<tel<<' '<location<<' '<points<<' '<isSpot<<' '
<police<<' '<camera<<' '<type<<' '<fine<<' '
<isTreated<<' '<treatmentDate<<' '<notes;
outfile.close();
return 0;
}
int List::insert(){
list::iterator it;
string number,date,carNumber,tel,location,isSpot,police,camera,type,
isTreated,treatmentDate,notes;
int points,fine;
cout<<"请输入违章编号【放弃添加 0】【5位数字,如16384】:";
for(;;){
cin>>number;cin.sync();
if(number=="0") return 0;
if(!checkNumber(number,5)) {
cout<<"请输入有效编号:"; 
continue;
}
Violation l(number);
it=find(vltnlist.begin(),vltnlist.end(),l);
if(it!=vltnlist.end())
cout<<"编号已存在,请重新输入:";
else break;
}
cout<<"请输入违章日期【8位数字,如20160630】:";
while(1){
cin>>date;cin.sync();
if(!checkNumber(date,8)){
cout<<"请输入有效日期:";
continue;
}
else break;
} 
cout<<"请输入违章车辆车牌号:";cin>>carNumber;cin.sync();
cout<<"请输入登记联系电话【11位数字,如13751516464】:";
while(1){
cin>>tel;cin.sync();
if(!checkNumber(tel,11)){
cout<<"请输入有效电话:";
continue;
}
else break;
}
cout<<"请输入违章地点:";cin>>location;cin.sync();
cout<<"请输入处罚扣分【0~12分】:";
while(1){
if(cin>>points){
cin.sync();
if(points<0||points>12){
cout<<"请输入有效扣分:";
continue;
}
else break;
}
else{
cin.clear();cin.sync();
cout<<"请输入有效扣分:";
continue;
}
}
cout<<"请输入是否现场【是 1】:";cin>>isSpot;cin.sync();
if(isSpot=="1"){
cout<<"请输入现场执勤交警:";cin>>police;cin.sync(); 
isSpot="是";camera="现场";
}
else{
cout<<"请输入非现场摄像头编号【6位数字,如646464】:";
while(1){
cin>>camera;cin.sync();
if(!checkNumber(camera,6)){
cout<<"请输入有效编号:";
continue;
}
else break;
}
isSpot="否";police="非现场"; 
}
cout<<"*******************************************************"<>n;cin.sync();
switch(n){
case '1': type="超速";Mark=0;break;
case '2': type="酒驾";Mark=0;break;
case '3': type="闯红灯";Mark=0;break;
case '4': type="违规变道";Mark=0;break; 
case '5': type="未系安全带";Mark=0;break;
case '6': type="超载";Mark=0;break;
default : cout<<"输入有误,请重新输入:";
}
}
cout<<"请输入罚款金额【0~5000元】:";
while(1){
if(cin>>fine){
cin.sync();
if(fine<0||fine>50000){
cout<<"请输入有效金额:";
continue;
}
else break;
}
else{
cin.clear();cin.sync();
cout<<"请输入有效金额:";
continue;
}
}
cout<<"请输入是否处理【是 1】:";cin>>isTreated;cin.sync();
if(isTreated=="1"){
cout<<"请输入处理日期【8位数字,如20160630】:";//cin>>treatmentDate;cin.sync();
while(1){
cin>>treatmentDate;cin.sync();
if(!checkNumber(treatmentDate,8)){
cout<<"请输入有效日期:";
continue;
}
else break;
} 
isTreated="是"; 
}
else{
isTreated="否";treatmentDate="未处理"; 
}
cout<<"请输入备注【无 0】:";cin>>notes;cin.sync();
if(notes=="0") notes="无";
Violation vltn(number,date,carNumber,tel,location,points,isSpot,police,camera,
type,fine,isTreated,treatmentDate,notes);
vltnlist.push_back(vltn);
saveFile();
cout<<"车辆违章信息添加成功!";getchar();
return 0;
}
int List::change(){
list::iterator it;
if(!vltnlist.size()){
cout<<"还没有记录,不能修改!"<>number;cin.sync();
if(number=="0") return 0;
Violation vltn(number);
it=find(vltnlist.begin(),vltnlist.end(),vltn);
if(it==vltnlist.end()){
cout<<"此编号不存在!";getchar();
return 0;
}
while(1){
system("cls");
cout<<" 当前信息 请输入"<>n;cin.sync();
switch(n){
case '0': return 0; 
case '1': cout<<"请输入违章编号【5位数字,如16384】:";//cin>>(*it).number;cin.sync();
for(;;){
cin>>number;cin.sync();
if(!checkNumber(number,5)) {
cout<<"请输入有效编号:"; 
continue;
}
Violation l(number);
list::iterator nit;
nit=find(vltnlist.begin(),vltnlist.end(),l);
if(nit!=vltnlist.end())
cout<<"编号已存在,请重新输入:";
else {
(*it).number=number;break;
}
}
Mark=0;break;
case '2': cout<<"请输入违章日期【8位数字,如20160630】:";//cin>>(*it).date;cin.sync();
while(1){
cin>>date;cin.sync();
if(!checkNumber(date,8)){
cout<<"请输入有效日期:";
continue;
}
else {
(*it).date=date;break;
}
} 
Mark=0;break;
case '3': cout<<"请输入车辆车牌号:";cin>>(*it).carNumber;cin.sync();
Mark=0;break;
case '4': cout<<"请输入登记联系电话:";//cin>>(*it).tel;cin.sync();
while(1){
cin>>tel;cin.sync();
if(!checkNumber(tel,11)){
cout<<"请输入有效电话【11位数字,如13751516464】:";
continue;
}
else {
(*it).tel=tel;break;
}
}
Mark=0;break;
case '5': cout<<"请输入违章地点:";cin>>(*it).location;cin.sync();
Mark=0;break;
case '6': cout<<"请输入处罚扣分【0~12分】:";//cin>>(*it).points;cin.sync();
while(1){
if(cin>>points){
cin.sync();
if(points<0||points>12){
cout<<"请输入有效扣分:";
continue;
}
else{
(*it).points=points;break;
}
}
else{
cin.clear();cin.sync();
cout<<"请输入有效扣分:";
continue;
}
}
Mark=0;break;
case '7': cout<<"请输入现场执勤交警:";cin>>(*it).police;cin.sync();
(*it).isSpot="是";(*it).camera="现场";
Mark=0;break;
case '8': cout<<"请输入非现场摄像头编号【6位数字,如646464】:";//cin>>(*it).camera;cin.sync();
while(1){
cin>>camera;cin.sync();
if(!checkNumber(camera,6)){
cout<<"请输入有效编号:";
continue;
}
else{
(*it).camera=camera;break;
}
}
(*it).isSpot="否";(*it).police="非现场"; 
Mark=0;break;
case '9': {
cout<<"*******************************************************"<>n1;cin.sync();
switch(n1){
case '1': (*it).type="超速";Mark1=0;break;
case '2': (*it).type="酒驾";Mark1=0;break;
case '3': (*it).type="闯红灯";Mark1=0;break;
case '4': (*it).type="违规变道";Mark1=0;break; 
case '5': (*it).type="未系安全带";Mark1=0;break;
case '6': (*it).type="超载";Mark1=0;break;
default : cout<<"输入有误,请重新输入:";
}
}
Mark=0;break;
}
case 'a': cout<<"请输入罚款金额【0~5000元】:";//cin>>(*it).fine;cin.sync();
while(1){
if(cin>>fine){
cin.sync();
if(fine<0||fine>50000){
cout<<"请输入有效金额:";
continue;
}
else{
(*it).fine=fine;break;
}
}
else{
cin.clear();cin.sync();
cout<<"请输入有效金额:";
continue;
}
}
Mark=0;break;
case 'b': cout<<"请输入是否处理【是 1】:";cin>>(*it).isTreated;cin.sync();
if((*it).isTreated=="1"){
cout<<"输入处理日期:";//cin>>(*it).treatmentDate;cin.sync();
while(1){
cin>>treatmentDate;cin.sync();
if(!checkNumber(treatmentDate,8)){
cout<<"请输入有效日期:";
continue;
}
else {
(*it).treatmentDate=treatmentDate;break;
}
} 
(*it).isTreated="是"; 
}
else{
(*it).isTreated="否";(*it).treatmentDate="未处理"; 
}
Mark=0;break;
case 'c': cout<<"请输入处理日期【8位数字,如20160630】:";//cin>>(*it).treatmentDate;cin.sync();
while(1){
cin>>treatmentDate;cin.sync();
if(!checkNumber(treatmentDate,8)){
cout<<"请输入有效日期:";
continue;
}
else{
(*it).treatmentDate=treatmentDate;break;
}
} 
Mark=0;break;
case 'd': cout<<"请输入备注:";cin>>(*it).notes;cin.sync();
Mark=0;break;
default : cout<<"输入有误,请重新输入:";
}
}
saveFile();
cout<<"修改成功!"<>ch;cin.sync();
if(ch=='0') return 0;
}
}
int List::del(){
list::iterator it;
if(!vltnlist.size()){
cout<<"还没有记录,不能删除!"<>number;cin.sync();
if(number=="0") return 0;
Violation vltn(number);
it=find(vltnlist.begin(),vltnlist.end(),vltn);
if(it==vltnlist.end()){
cout<<"此编号不存在!";getchar();
return 0;
}
vltnlist.erase(it);
if(!vltnlist.size()) remove(filepath);
else saveFile();
cout<<"删除记录成功!";getchar();
return 0;
}
int List::browse(){
list::iterator it;
if(!vltnlist.size()){
cout<<"还没有记录!"<>n;cin.sync();
switch(n)
{
case '1': searchDate();Mark=0;break;
case '2': searchCarNumber();Mark=0;break;
case '3': searchTel();Mark=0;break;
case '4': searchLocation();Mark=0;break;
case '0': return 0;
default : cout<<"输入有误,请重新输入:";
}
}
}
vltnlist.sort(compNumber);
return 0;
getchar();return 0;
}
int List::searchDate(){
list::iterator it;
string date;
cout<<"请输入日期:";cin>>date;cin.sync();
Violation vltn("",date);
it=find(vltnlist.begin(),vltnlist.end(),vltn);
if(it!=vltnlist.end()){ 
cout<<"已找到日期为 "<::iterator it;
string carNumber;
cout<<"请输入车牌号:";cin>>carNumber;cin.sync();
Violation vltn("","",carNumber);
it=find(vltnlist.begin(),vltnlist.end(),vltn);
if(it!=vltnlist.end()){ 
cout<<"已找到车牌号为 "<::iterator it;
string tel;
cout<<"请输入联系电话:";cin>>tel;cin.sync();
Violation vltn("","","",tel);
it=find(vltnlist.begin(),vltnlist.end(),vltn);
if(it!=vltnlist.end()){ 
cout<<"已找到联系电话为 "<::iterator it;
string location;
cout<<"请输入地点:";cin>>location;cin.sync();
Violation vltn("","","","",location);
it=find(vltnlist.begin(),vltnlist.end(),vltn);
if(it!=vltnlist.end()){ 
cout<<"已找到地点为 "<::iterator it;
if(!vltnlist.size()){
cout<<"还没有记录,不能统计!"<=12分的记录 4 | *"<>n;cin.sync();
switch(n)
{
case '1': countDate();Mark=0;break;
case '2': countCarNumber();Mark=0;break;
case '3': countLocation();Mark=0;break;
case '4': countPoints();Mark=0;break;
case '0': return 0;
default : cout<<"输入有误,请重新输入:";
}
}
}
return 0;
getchar();return 0;
}
int List::countDate(){
list::iterator it;
vltnlist.sort(compDate);
int sumVltn=0;string y,m1,m2,date1,date2;
cout<<"输入日期范围:"<>y;cin.sync();
cout<<"输入起始月份:";cin>>m1;cin.sync();
cout<<"输入结束月份:";cin>>m2;cin.sync();
date1=y+m1;date2=y+m2;
for(it=vltnlist.begin();it!=vltnlist.end();it++){
if((*it).date>=date1&&(*it).date<=date2) sumVltn++;
}
cout<<"该段时间内违章数量为:"<::iterator it;
vltnlist.sort(compCarNumber);
vltnlist.push_back(Violation("cutme"));
it=vltnlist.begin();
cout<<"已按车牌号统计违章次数:"<::iterator it;
vltnlist.sort(compLocation);
vltnlist.push_back(Violation("cutme"));
it=vltnlist.begin();
string location=(*it).location;int sumVltn=0;
cout<<"已按地点统计违章数量:"<::iterator it;
vltnlist.sort(compCarNumber);
vltnlist.push_back(Violation("cutme"));
it=vltnlist.begin();
string carNumber=(*it).carNumber,tel=(*it).tel;
int sumPoints=0;
cout<<"已显示处罚>=12分的违章记录:"<11)
cout<loadFile();
menu();
myList->saveFile();
return 0;
}
int menu(){
while(1)
{
system("cls");
cout<<"*******************************************************"<>n;cin.sync();
List* myList=List::getList();
switch(n)
{
case '1': myList->insert();Mark=0;break;
case '2': myList->change();Mark=0;break;
case '3': myList->del();Mark=0;break;
case '4': myList->browse();Mark=0;break; 
case '5': myList->searchMenu();Mark=0;break;
case '6': myList->countMenu();Mark=0;break;
case '0': exit(0);
default : cout<<"输入有误,请重新输入:";
}
}
}
return 0;
}

以上代码是基于C++语言实现机动车违章处罚管理系统的关键代码,代码附有注释,有不明白的地方欢迎给我留言,小编会及时回复大家的,大家可以更加需求适当的添加修改删除。

你可能感兴趣的:(基于C++语言实现机动车违章处罚管理系统)