deposit.h
#pragma once #include "login.h" #include"mainMenu.h"
void deposit() { system("cls"); cout<<"mainmenu << deposit"<<endl<<endl; cout<<"--------------------------"<<endl; cout<<"| Please login at first |"<<endl; cout<<"--------------------------"<<endl; Sleep(1000); //修改金額部分
ifstream deposit1; ofstream deposit2; //定義讀取文件的變量 int number; char name[20]; char password[20]; double money; int type; char time[10]; //定義錯誤次數 int errorTime = 0; //調用登錄函數login(),用變量enterNumber接受登錄函數login()的返回值 int enterNumber = login(); //如果登錄失敗但未超過三次 while((enterNumber == 0) &&(errorTime <= 2)) { Sleep(2000); errorTime ++; enterNumber = login(); } //如果登錄失敗達到三次 if(enterNumber == 0) { cout<<"---------------------------------------------"<<endl; cout<<"▏Sorry,login failed,will back to main menu▕"<<endl; cout<<"---------------------------------------------"<<endl; Sleep(2000); outputMainMenu(); } //登錄成功,開始文件操作 deposit1.open("account.txt",ios::in); deposit2.open("account1.txt",ios::out); double depositMoney; cout<<"--------------------------"<<endl; cout<<"Please enter deposit money: "<<endl; cout<<"--------------------------"<<endl; cin>>depositMoney; //對比新舊數據,創建新數據庫 deposit1>>number; deposit1>>name; deposit1>>password; deposit1>>money; deposit1>>type; deposit1>>time;
while(!deposit1.eof()) { //如果number == enterNumber,即找到該賬戶,則不將該賬戶的信息寫入新文件 if(number == enterNumber) { if(depositMoney <= money) {
money -= depositMoney; deposit2<<number<<" "; deposit2<<name<<" "; deposit2<<password<<" "; deposit2<<money<<" "; deposit2<<type<<" "; deposit2<<time<<endl; } else { cout<<"-------------------------------------------"<<endl; cout<<" There is no enough money in your account! "<<endl; cout<<"-------------------------------------------"<<endl<<endl; system("PAUSE"); deposit(); } } //如果不是用戶所找的賬戶,則不加改動的寫入新文件 else { deposit2<<number<<" "; deposit2<<name<<" "; deposit2<<password<<" "; deposit2<<money<<" "; deposit2<<type<<" "; deposit2<<time<<endl;
} //讀取下一個用戶的數據,直到找到該用戶的賬戶 deposit1>>number; deposit1>>name; deposit1>>password; deposit1>>money; deposit1>>type; deposit1>>time; } //新文件名爲account1.txt,原文件名爲account.txt deposit1.close(); deposit2.close(); char fileName[] = "account.txt"; char fileName1[] = "account1.txt"; remove(fileName); rename(fileName1,fileName); //將原文件刪除,同時把新文件名字由account1.txt改爲account.txt cout<<"*******************"<<endl; cout<<"* Deposit success! *"<<endl; cout<<"*******************"<<endl; //註銷成功,讓用戶選擇下一步操作 cout<<"================================"<<endl<<endl; cout<<" ▎Please choose what to do:"<<endl<<endl; cout<<"●1.Back to main menu"<<endl<<endl; cout<<"●2.Continue to deposit"<<endl<<endl; cout<<"●3.Exit the game"<<endl; cout<<"================================"<<endl<<endl; int choose; cin>>choose; //若選擇1,則返回主菜單,選擇2,繼續註銷賬戶,選擇3就退出程序 if(choose == 1) { outputMainMenu(); } else if(choose == 2) { deposit(); } else if(choose == 3) {} } |
withdrawals.h
#pragma once #include "login.h" #include"mainMenu.h"
void withdrawals() { system("cls"); cout<<"mainmenu << withdrawals"<<endl<<endl; cout<<"----------------------"<<endl; cout<<"Please login at first"<<endl; cout<<"----------------------"<<endl; system("pause"); ifstream withdrawals1; ofstream withdrawals2; //定義讀取文件的變量 int number; char name[20]; char password[20]; double money; int type; char time[10]; //定義錯誤次數 int errorTime = 0; //調用登錄函數login(),用變量enterNumber接受登錄函數login()的返回值 int enterNumber = login(); //如果登錄失敗但未超過三次 while((enterNumber == 0) &&(errorTime <= 2)) { cout<<"login failed!"<<endl; Sleep(4000); errorTime ++; enterNumber = login(); } //如果登錄失敗達到三次 if(enterNumber == 0) { cout<<"Sorry,login failed,will back to main menu"<<endl; Sleep(3000); outputMainMenu(); } //登錄成功,開始文件操作 withdrawals1.open("account.txt",ios::in); withdrawals2.open("account1.txt",ios::out|ios::app); double withdrawalsMoney; cout<<"withdrawals money: "; cin>>withdrawalsMoney;
withdrawals1>>number; withdrawals1>>name; withdrawals1>>password; withdrawals1>>money; withdrawals1>>type; withdrawals1>>time;
while(!withdrawals1.eof()) { //如果number == enterNumber,即找到該賬戶,則不將該賬戶的信息寫入新文件 if(number == enterNumber) { money += withdrawalsMoney; withdrawals2<<number<<" "; withdrawals2<<name<<" "; withdrawals2<<password<<" "; withdrawals2<<money<<" "; withdrawals2<<type<<" "; withdrawals2<<time<<endl; } else { //如果不是用戶所找的賬戶,則不加改動的寫入新文件 withdrawals2<<number<<" "; withdrawals2<<name<<" "; withdrawals2<<password<<" "; withdrawals2<<money<<" "; withdrawals2<<type<<" "; withdrawals2<<time<<endl;
} //讀取下一個用戶的數據,直到找到該用戶的賬戶 withdrawals1>>number; withdrawals1>>name; withdrawals1>>password; withdrawals1>>money; withdrawals1>>type; withdrawals1>>time; } //新文件名爲account1.txt,原文件名爲account.txt char fileName[] = "account.txt"; char fileName1[] = "account1.txt"; withdrawals1.close(); withdrawals2.close(); //將原文件刪除,同時把新文件名字由account1.txt改爲account.txt remove(fileName); rename(fileName1,fileName); //註銷成功,讓用戶選擇下一步操作 cout<<"*******************"<<endl; cout<<"* Withdrawals success! *"<<endl; cout<<"*******************"<<endl; cout<<"================================"<<endl<<endl; cout<<" ▎Please choose what to do:"<<endl; cout<<"●1.Back to main menu"<<endl; cout<<"●2.Continue to withdrawal"<<endl; cout<<"●3.Exit the game"<<endl; cout<<"================================"<<endl<<endl; int choose; cin>>choose; //若選擇1,則返回主菜單,選擇2,繼續註銷賬戶,選擇3就退出程序 if(choose == 1) { outputMainMenu(); } else if(choose == 2) { withdrawals(); }
}
|
statistics.h
#pragma once #include"mainMenu.h"
void statistics() { //定義讀取文件的變量 int number; char name[20]; char password[20]; double money; int type; char time[10]; system("cls");
cout<<"main menu << statistic <<output"; cout<<"Please choose what to do"<<endl<<endl; cout<<"====================================="<<endl<<endl; cout<<"● 1.View the order by account number"<<endl<<endl; cout<<"● 2.View the order by account balances"<<endl<<endl; cout<<"====================================="<<endl<<endl; //讓用戶選擇按賬號順序輸出還是按餘額大小輸出 int choose; cin>>choose;
//按賬號順序輸出 if(choose == 1) { ifstream statistic; //定義賬戶數量,總金額 int numberOfAccount = 0; double totalMoney = 0; system("cls"); //輸出表頭 cout<<"*********************"; cout<<" All account information ********************"<<endl<<endl; cout<<left; cout<<setw(10)<<"number"<<setw(15)<<"name"<<setw(15)<<"password"; cout<<setw(10)<<"money"<<setw(10)<<"type"<<setw(15)<<"time"<<endl; statistic.open("account.txt",ios::in); //讀取文件 statistic>>number; statistic>>name; statistic>>password; statistic>>money; statistic>>type; statistic>>time; while(!statistic.eof()) { cout<<left; //輸出用戶的數據 cout<<setw(10)<<number<<setw(15)<<name<<setw(15)<<password; cout<<setw(10)<<money<<setw(10)<<type<<setw(15)<<time<<endl; //計算賬戶數量與餘額總量 numberOfAccount ++; totalMoney += money; //讀取下一個用戶的數據 statistic>>number; statistic>>name; statistic>>password; statistic>>money; statistic>>type; statistic>>time; } statistic.close(); //計算賬戶數量與餘額總量 cout<<endl<<endl<<"--------------------------"<<endl; cout<<numberOfAccount<<" accounts in total"<<endl; cout<<"--------------------------"<<endl; cout<<"--------------------------"<<endl; cout<<"total money is "<<totalMoney<<endl; cout<<"--------------------------"<<endl; system("PAUSE"); //讓用戶選擇下一步操作 cout<<"=============================="<<endl<<endl; cout<<"▏Please choose what to do:"<<endl<<endl; cout<<"●1.Back to main menu"<<endl<<endl; cout<<"●2.Exit the system"<<endl; cout<<"=============================="<<endl<<endl; int choose; cin>>choose; if(choose == 1) { outputMainMenu(); } else {} } //以上爲按賬號順序輸出
//按賬戶餘額排序 if(choose == 2) { ifstream statistic1; statistic1.open("account.txt",ios::in); //讀取文件,獲取賬戶數量 int i = 0; //計算賬戶數量i以確定數組大小 while(!statistic1.eof()) { statistic1>>number; statistic1>>name; statistic1>>password; statistic1>>money; statistic1>>type; statistic1>>time; i++; } //因爲eof()多讀取的一次文件 i--;
statistic1.close(); int size = i; //分別定義存放賬號和金額的兩個數組 int *numberList = new int [i]; double *moneyList = new double [i];
statistic1.open("account.txt",ios::in); //爲兩個數組賦值,將文件中的賬號和金額數據分別放在這兩個數組中 int index = 0; while(index <= size - 1) { statistic1>>number; statistic1>>name; statistic1>>password; statistic1>>money; statistic1>>type; statistic1>>time; numberList[index] = number; moneyList[index] = money; index++; }
statistic1.close(); //對moneyList中的元素進行排序,在對moneyList進行操作時,同步的對numberList進行完全相同的操作 for(i = size - 1;i >= 1;i--) { double currentMax = moneyList[0]; int currentMaxIndex = 0;
for(int j = 1;j <= i;j++) { if(currentMax < moneyList[j]) { currentMax = moneyList[j]; currentMaxIndex = j; } } if(currentMaxIndex != i) { moneyList[currentMaxIndex] = moneyList[i]; moneyList[i] = currentMax;
//對numberlist數組進行相同操作 int temp; temp = numberList[currentMaxIndex]; numberList[currentMaxIndex] = numberList[i]; numberList[i] = temp; }
}
//將排好序的數組存貯的信息按順序輸出 system("cls"); //輸出表頭 cout<<"*********************"; cout<<" All account information ********************"<<endl<<endl; cout<<left; cout<<setw(10)<<"number"<<setw(15)<<"name"<<setw(15)<<"password"; cout<<setw(10)<<"money"<<setw(10)<<"type"<<setw(15)<<"time"<<endl; //numberList數組中儲存的元素是按照金額大小排好順序的賬戶對應的賬號 for(i = 0;i <= size -1;i++) { statistic1.open("account.txt",ios::in); //尋找已知賬號的賬戶信息並輸出 while(!statistic1.eof()) { statistic1>>number; statistic1>>name; statistic1>>password; statistic1>>money; statistic1>>type; statistic1>>time;
if(number == numberList[i]) { cout<<left; cout<<setw(10)<<number<<setw(15)<<name<<setw(15)<<password; cout<<setw(10)<<money<<setw(10)<<type<<setw(15)<<time<<endl; break; } } statistic1.close(); } system("PAUSE"); //選擇下一步操作 cout<<"=============================="<<endl<<endl; cout<<"▏Please choose what to do:"<<endl<<endl; cout<<"●1.Back to main menu"<<endl<<endl; cout<<"●2.Exit the system"<<endl; cout<<"=============================="<<endl<<endl; int choose; cin>>choose; if(choose == 1) { outputMainMenu(); } else {} } } |
cancellation.h
#pragma once #include "login.h" #include"mainMenu.h"
void cancellation() { system("cls"); cout<<"mainmenu << cancellation"<<endl<<endl; cout<<"----------------------"<<endl; cout<<"Please login at first"<<endl; cout<<"----------------------"<<endl; system("pause");
ifstream cancellation1; ofstream cancellation2; //定義讀取文件的變量 int number; char name[20]; char password[20]; double money; int type; char time[10]; //定義錯誤次數 int errorTime = 0; //調用登錄函數login(),用變量enterNumber接受登錄函數login()的返回值 int enterNumber = login();
//如果登錄失敗但未超過三次 while((enterNumber == 0) &&(errorTime <= 2)) { cout<<"-----------"<<endl; cout<<"login failed!"<<endl; cout<<"-----------"<<endl; Sleep(4000); errorTime ++; enterNumber = login(); } //如果登錄失敗達到三次 if(enterNumber == 0) { cout<<"--------------------------------------------"<<endl; cout<<"Sorry,login failed,will back to main menu"<<endl; cout<<"--------------------------------------------"<<endl; Sleep(3000); outputMainMenu(); } //登錄成功,開始文件操作 cancellation1.open("account.txt",ios::in); cancellation2.open("account1.txt",ios::out|ios::app);
cancellation1>>number; cancellation1>>name; cancellation1>>password; cancellation1>>money; cancellation1>>type; cancellation1>>time;
while(!cancellation1.eof()) { //如果number == enterNumber,即找到該賬戶,則不將該賬戶的信息寫入新文件 if(number == enterNumber) {} else { //如果不是用戶所找的賬戶,則不加改動的寫入新文件 cancellation2<<number<<" "; cancellation2<<name<<" "; cancellation2<<password<<" "; cancellation2<<money<<" "; cancellation2<<type<<" "; cancellation2<<time<<endl;
} //讀取下一個用戶的數據,直到找到該用戶的賬戶 cancellation1>>number; cancellation1>>name; cancellation1>>password; cancellation1>>money; cancellation1>>type; cancellation1>>time; } //新文件名爲account1.txt,原文件名爲account.txt char fileName[] = "account.txt"; char fileName1[] = "account1.txt"; cancellation1.close(); cancellation2.close(); //將原文件刪除,同時把新文件名字由account1.txt改爲account.txt remove(fileName); rename(fileName1,fileName); //註銷成功,讓用戶選擇下一步操作 cout<<"***********************"<<endl; cout<<"* Cancellation success! *"<<endl; cout<<"***********************"<<endl; cout<<"================================"<<endl<<endl; cout<<" ▎Please choose what to do:"<<endl; cout<<"●1.Back to main menu"<<endl; cout<<"●2.Continue to cancellation"<<endl; cout<<"●3.Exit the game"<<endl; cout<<"================================"<<endl<<endl; int choose; cin>>choose; //若選擇1,則返回主菜單,選擇2,繼續註銷賬戶,選擇3就退出程序 if(choose == 1) { outputMainMenu(); } else if(choose == 2) { cancellation(); }
} |