C++第14週プロジェクト6——銀行システム、ファイルで永久保存

9142 ワード

コースのトップページアドレス:http://blog.csdn.net/sxhelijian/article/details/7910565
【プロジェクト6-銀行システム】本格的なマルチユーザー銀行システムを実現できます.具体的な業務では、多くの変更を行わず、主にユーザー管理に変更する:(1)ユーザー情報を3つのグローバル配列で格納し、現在ログインしているアカウントを1つのグローバル変数で表す
const int NUM=5;   //    5   ,  ,    
int account[NUM]={37001,37002, 37020, 37245,37888}; //  
int pwd[NUM]={123456,654321, 456789, 987654, 234432}; //  
double balance[NUM]={1000., 300, 23.67, 42.30, 56789}; //  
int accountIndex; //                   

ヒント:アカウントを入力した後、account配列で検索し、ヒントが見つからず、ログインに失敗しました.見つかったら、accountIndexで下付きを覚え、その後の様々なビジネスではpwd[accountIndex]、balance[accountIndex]がそのアカウントに関する情報です.
(2)振替は本当に2つの口座間の振替を実現することができます.
(1)と(2)の参考を満たす:http://blog.csdn.net/sxhelijian/article/details/8240764
(3)-選択問題:プログラムの実行が完了し、変更後のデータがすべて失われました!ファイル保存に関する情報を作成し、プログラム開始時にファイルに保存されているアカウント、パスワード、残高を読み込み、プログラム終了時に変更後のデータをファイルに書き込むことができます.このアプローチは、実際のアプリケーションの要件に合致し、より多くのユーザーをサポートすることができます.(添付のデータは、プログラムで使用できるように、自己構築用のテキストファイルをコピーできます.)
リファレンスコード:このセクションでは、初期化によって与えられた情報ではなく、ファイルからユーザー情報を直接読み出すための2つの関数と、実行中に変更されたデータをファイルに保存するための関数を主に追加します.この2つの関数は、次のとおりです.
//        ,        
void getInformation()  //        
{
	ifstream infile("bank.dat",ios::in);  //          
	if(!infile)       //        
	{
		cerr<<"   ,        !"<<endl;
		exit(1);
	}
	int i=0;
	while(!infile.eof())
	{
		infile>>account[i]>>pwd[i]>>balance[i];
		++i;
	}
	infile.close();
	accountNum=i;  //    
	return;
}

//   ,             ,    ,            
void saveInformation() //       
{
	ofstream outfile("bank.dat",ios::out);  //          
	if(!outfile)       //        
	{
		cerr<<"   ,         !"<<endl;
		exit(1);
	}
	int i=0;
	while(i<accountNum)
	{
		outfile<<account[i]<<'\t'<<pwd[i]<<'\t'<<balance[i]<<'
'; ++i; } outfile.close(); return; }

完全なリファレンス:
#include<iostream>   
#include<fstream>
using namespace std; 
//                ,          (       )
//           ,         
//              ,         
const int NUM=100;   //      100   ,  ,    
int account[NUM]; //  
int pwd[NUM]; //  
double balance[NUM]; //  
int accountNum;  //     ,      
int accountIndex; //                   
int currentAccount; //              

//       
int checkStatus();
int seekUser(int);
void work();
void showbalance();
void drawmoney();
void deposit();
void transferAccounts();
void updatePassword();
void getInformation();  //        
void saveInformation(); //       

int main()  
{  
	int status;
	char ch;
	cout<<"         ……(   bank.dat      ,           !)"<<endl;
	getInformation();  //        
	while(1)  //            
	{
		status=checkStatus();
		switch(status)
		{
		case 1:  //     
			work();
			break;
		case 2:  //      
			cout<<"      ……"<<endl;
			break;
		case 3:
			cout<<"        ……"<<endl;
			break;
		case 4:
			cout<<"  ,         ……"<<endl;
		}
		cout<<"=====     ,   Q,            ======"<<endl;
		fflush( stdin );  //       ,  getchar()    
		ch=getchar();
		if(ch=='q'||ch=='Q')break;
	}
	saveInformation();
	return 0;
}

/*    
 *     :
1 -    、    ,       
2 -            
3 -        
4 -            
*/
int checkStatus()
{
	int iStatus=4;		//       
	int iPass;
	int num=1;
	char goOn;
	cout<<"     ";
	cin>>currentAccount;  //currentAccount     
	accountIndex=seekUser(currentAccount);
	if(accountIndex < 0)   //  seekUser   ,          
		iStatus=3; 
	else
	{		
		do
		{	
			if(num==1)
				cout<<"     ";
			else
			{
				cout<<"    :    "<<num<<"     ,       。"<<endl;
				cout<<"       (    Y,    N,    ):";
				cin>>goOn;
				if('Y'==goOn||'y'==goOn)
					cout<<"       ";
				else
				{
					iStatus=2; //      
					break;
				}
			}
			cin>>iPass;
			num++;
			if(iPass==pwd[accountIndex])   //  
			{
				iStatus=1;  //    
				break;
			}
		}while(num<4);  //                
		//       num==4     ,iStatus       3,    
	}
	return iStatus; //    
}	

//          ,
//    ,        
//     ,  -1(   )
int seekUser(int iAccount)
{
	int i;
	for(i=0;i<accountNum;++i)//      ,       account[]  ,      
	{
		if(iAccount==account[i])
			break;
	}
	if(i>=accountNum)
		i=-1;  //          
	return i;
}

//    
void work()
{
	char cChoice;
	bool bExit = false;
	do  
	{  
		cout<<endl<<"*            :"<<endl;  
		cout<<"*  1.  "<<endl;  
		cout<<"*  2.  "<<endl;  
		cout<<"*  3.  "<<endl;
		cout<<"*  4.  "<<endl;
		cout<<"*  5.    "<<endl;
		cout<<"*  0.  "<<endl;  
		cout<<"*     (0-4):";  
		cin>>cChoice;
		switch(cChoice)
		{
		case '1':
			showbalance();
			break;  
		case '2':
			drawmoney();
			break;  
		case '3':
			deposit();
			break;  
		case '4':
			transferAccounts();
			break;  
		case '5':
			updatePassword();
			break;  
		case '0':
			cout<<"        . "<<endl;
			bExit=true;
		}		
	}while(!bExit);  
	return;
}

//    
void showbalance()
{
	cout<<"       :"<<balance[accountIndex]<<" "<<endl;
}

//  
void drawmoney()
{  
	double money;  
	cout << "       :";  
	cin >> money;  
	//       ,    ,             ,        
	if(money<=balance[accountIndex])
	{
		balance[accountIndex]-=money; //    
		cout<<"   ,     :"<<balance[accountIndex]<<" 。"<<endl;
	}
	else
	{
		cout<<"      ,    。"<<endl;
	}
}	
//  
void deposit()
{
	double money;  
	cout << "       :";  
	cin >> money;  
	//       ,    ,             ,        
	balance[accountIndex]+=money; //    
	cout<<"   ,     :"<<balance[accountIndex]<<" 。"<<endl;
}

//  
void transferAccounts()
{
	double money;  
	int iAccount2,iAccount2Index;
	cout << "       :";
	cin >> money;  
	if(money>balance[accountIndex]) //     ,    
	{
		cout<<"      ,    。"<<endl;
	}
	else
	{
		cout << "       :";
		cin >> iAccount2; //               ,    ,       
		iAccount2Index=seekUser(iAccount2);
		if(iAccount2Index==-1)
			cout<<"        ,    。"<<endl;
		else
		{
			balance[accountIndex]-=money; //     
			balance[iAccount2Index]+=money; //    
			//             ,   
			cout<<"   ,     :"<<balance[accountIndex]<<" ,     "<<balance[iAccount2Index]<<" 。"<<endl;
			//             ,       。
		}
	}
}			
//    
void updatePassword()
{
	int p1,p2;
	cout << "      :";
	cin >> p1;  
	cout << "      :";
	cin >> p2;  
	if(p1==p2)//      
	{
		pwd[accountIndex]=p1;   
		cout<<"      !"<<endl;
	}
	else
	{
		cout<<"       ,      。"<<endl;
	}
}

//        ,        
void getInformation()  //        
{
	ifstream infile("bank.dat",ios::in);  //          
	if(!infile)       //        
	{
		cerr<<"   ,        !"<<endl;
		exit(1);
	}
	int i=0;
	while(!infile.eof())
	{
		infile>>account[i]>>pwd[i]>>balance[i];
		++i;
	}
	infile.close();
	accountNum=i;  //    
	return;
}

//   ,             ,    ,            
void saveInformation() //       
{
	ofstream outfile("bank.dat",ios::out);  //          
	if(!outfile)       //        
	{
		cerr<<"   ,         !"<<endl;
		exit(1);
	}
	int i=0;
	while(i<accountNum)
	{
		outfile<<account[i]<<'\t'<<pwd[i]<<'\t'<<balance[i]<<'
'; ++i; } outfile.close(); return; }

添付:各行の3つの部分は、それぞれアカウント、パスワード、残高を表します.
37001 888888 4237.1437002 888888 5832.7237020 888888 2293.5137039 888888 2301.6437054 888888 3973.7937076 888888 3113.1437136 888888 1388.4437145 888888 6859.2437146 888888 4908.6937169 888888 72.1537186 888888 3849.1737210 888888 6504.137234 888888 8881.4837245 888888 1531.6437248 888888 9364.7337284 888888 1332.6537300 888888 8193.8837332 888888 9388.0337367 888888 300.4437388 888888 9332.5237405 888888 1025.2637415 888888 140.3237428 888888 5896.6637436 888888 3849.937454 888888 3248.837470 888888 1230.9437477 888888 666.6137538 888888 7203.5537564 888888 9806.9937577 888888 1582.3437592 888888 1969.637608 888888 7064.1537643 888888 8062.9637649 888888 2583.5137671 888888 7359.8937672 888888 8574.9937737 888888 6071.7937742 888888 259.4537787 888888 8283.6737844 888888 8834.2737848 888888 8898.7237860 888888 9492.837872 888888 4933.1237888 888888 3225.6837904 888888 1932.3937904 888888 5720.5437931 888888 6612.8937967 888888 8995.5537993 888888 9090.4337995 888888 2628.41