📄 5_3.cpp
字号:
/*第3题 股票交易系统--源代码及关键源代码注解如下:*/
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <fstream.h>
#include <iomanip.h>
#define MAX 5
class Stock
{
friend class Customer;
private:
char stock_name[20];
char stock_code[6];
char code_for_analysis[6];
char choice;
int num_of_stock;
long int stock_volume;
long int volume_available;
public:
Stock();
void Interface();
void Stock_Exchange_Market();
void Switch_choice();
void Input_For_New_Stock();
void Market_Analysis();
};
class Customer
{
private:
char customer_name[30];
char password[10];
char share_holding_name[30];
char share_holding_code[5];
long int share_holding_value;
public:
Customer();
void Log_in(char * ,Customer) const;
void Register(Customer *);
void Stock_Portfolio(Customer) const; // Portfolio--有价证券
void Switch_function();
};
Customer::Customer()
{
strcpy(Customer::customer_name," ");
strcpy(Customer::share_holding_name," ");
strcpy(Customer::share_holding_code," ");
share_holding_value=0;
}
void Customer::Log_in(char *password,Customer cust) const
{
ifstream read_customer("customer.dat");
while(!read_customer.eof())
{
read_customer.read((char*)(&cust),sizeof(cust));
if(read_customer.fail())
{
cout<<"No record at all!";
break;
}
if(strcmp(password,cust.password)==0)
Stock_Portfolio(cust);
}
read_customer.close();
}
void Customer::Register(Customer *cust)
{
system("cls"); //执行系统命令:清屏=cls
cin.get();
cout<<"Welcome to ********************* Stock Exchange System"<<endl;
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
cout<<"Enter user name: ";
cin.getline(customer_name,30);
cout<<"Enter your own password(less than 8 words): ";
cin>>password;
cin.get();
system("cls");
cout<<endl<<endl;
cout<<"Your account have been registered..."<<endl;
cout<<"Press any key to proceed..."<<endl;
getch();
}
void Customer::Stock_Portfolio(Customer cust) const
{
Stock share[MAX],temp;
Customer s[MAX];
int i;
char ch,share_code[6];
long int volume;
start:
system("cls");
cout<<endl<<endl;
cout<<"\t\t\t Welcome Back!!!!!"<<endl;
cout<<"\t\t******************** Stock Exchange System"<<endl;
cout<<"\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
cout<<"\t\t User: "<<cust.customer_name<<endl<<endl;
cout<<"\t\t Buy Shares..........................[b]"<<endl;
cout<<"\t\t Sell Shares.........................[s]"<<endl;
cout<<"\t\t View PortFolio......................[p]"<<endl;
cout<<"\t\t Log Out.............................[l]"<<endl;
cout<<endl<<"\t\t Enter your choice: ";
cin>>ch;
if((ch!='p')&&(ch!='b')&&(ch!='s')&&(ch!='l'))
goto start;
else
{
switch(ch)
{
case 'b':{
system("cls");
cout<<"\n\n\t\t******************* Stock Exchange System"<<endl;
cout<<"\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
cout<<" Company\t\tCode\tIssued Volume\tVolume Available"<<endl;
cout<<" ~~~~~~~\t\t~~~~\t~~~~~~~~~~~~~\t~~~~~~~~~~~~~~~~\t"<<endl;
ifstream dataFile("Stock_File.dat"); //Subject to change
dataFile.read((char*)(&share),sizeof(share));
ifstream dataFile_for_share(cust.customer_name); //Subject to change
dataFile_for_share.read((char*)(&s),sizeof(s)); // open file for s[x]
for(i=0;i<MAX;++i)
{
share[i].Stock_Exchange_Market();
}
cin.get();
cout<<endl<<"Enter the Company's code you want to purchase: ";
cin.getline(share_code,6);
cout<<"Enter the volume to purchase: ";
cin>>volume;
cin.get();
i=0;
while((strcmp(share[i].stock_code,share_code)==0) || (i<MAX))
{
if(strcmp(share[i].stock_code,share_code)==0)
{
if(share[i].volume_available>volume)
{
s[i].share_holding_value=s[i].share_holding_value+volume; // cust change to s[x]
strncpy(s[i].share_holding_name,share[i].stock_name,30); // cust change to s[x]
strcpy(s[i].share_holding_code,share[i].stock_code); // cust change to s[x]
share[i].volume_available=share[i].volume_available-volume;
ofstream dataFile (cust.customer_name,ios::binary); //Subject to change
for(i=0;i<5;++i)
{
dataFile.write((char*)(&s[i]),sizeof(s[i]));
} // create or save file for all transactions
ofstream file("Stock_File.dat");
for(i=0;i<5;++i)
{
file.write((char*)(&share[i]),sizeof(share[i]));
}
break;
}
else
{
cout<<"Volume available for "<<share[i].stock_name
<<" is less than the volume you want to purchase. "<<endl;
cout<<"Transaction cancel... ";
getch();
break;
}
}
else
{
i++;
while(i==MAX)
{
cout<<"The code you type may be wrong... "<<endl;
cout<<"Transaction cancel... ";
getch();
break;
}
}
}
break;
}
case 's':{
system("cls");
ifstream dataFile_for_share(cust.customer_name); //Subject to change
dataFile_for_share.read((char*)(&s),sizeof(s)); // open file for s[x]
ifstream dataFile("Stock_File.dat"); //Subject to change
dataFile.read((char*)(&share),sizeof(share));
cout<<"\t\t******************** Stock Exchange System"<<endl;
cout<<"\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
cout<<" Company\t\tCode\tVolume Holding"<<endl;
cout<<" ~~~~~~~\t\t~~~~\t~~~~~~~~~~~~~~\t"<<endl;
for(int i=0;i<MAX;i++)
{
cout<<" "<<s[i].share_holding_name<<"\t\t"<<s[i].share_holding_code
<<"\t"<<s[i].share_holding_value<<endl;
}
cin.get();
cout<<endl<<"Enter share code to sell: ";
cin.getline(share_code,6);
cout<<"Enter the volume to sell: ";
cin>>volume;
cin.get();
i=0;
while((strcmp(share[i].stock_code,share_code)==0) || (i<MAX))
{
if(strcmp(share[i].stock_code,share_code)==0)
{
if(s[i].share_holding_value>volume)
{
s[i].share_holding_value=s[i].share_holding_value-volume;
// cust change to s[x]
share[i].volume_available=share[i].volume_available+volume;
ofstream data(cust.customer_name,ios::binary); //Subject to change
for(i=0;i<5;++i)
{
data.write((char*)(&s[i]),sizeof(s[i]));
} // create or save file for all transactions
ofstream file("Stock_File.dat");
for(i=0;i<5;++i)
{
file.write((char*)(&share[i]),sizeof(share[i]));
}
}
else
{
cout<<"You don have so much volume to sell, ";
cout<<"or you did not buy this share before. "<<endl;
cout<<"Transaction cancel... "<<endl;
break;
}
}
else
{
i++;
while(i==MAX)
{
cout<<"You did not buy this share or you have type in wrong share code. "<<endl;
cout<<"Transcation cancel... ";
break;
}
}
}
getch();
break;
}
case 'p':{
system("cls");
ifstream dataFile_for_share(cust.customer_name); //Subject to change
dataFile_for_share.read((char*)(&s),sizeof(s)); // open file for s[x]
cout<<"\t\t******************** Stock Exchange System"<<endl;
cout<<"\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
cout<<" Company\t\tCode\tVolume Holding"<<endl;
cout<<" ~~~~~~~\t\t~~~~\t~~~~~~~~~~~~~~\t"<<endl;
for(int i=0;i<MAX;i++)
{
cout<<" "<<s[i].share_holding_name<<"\t\t"<<s[i].share_holding_code
<<"\t"<<s[i].share_holding_value<<endl;
}
getch();
break;
}
case 'l':{
Stock s;
s.Interface();
}
}
goto start;
}
}
Stock::Stock()
{
num_of_stock=0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -