【在线答疑网站源码】【动网源码.net】【仿人人逛街源码】iofstream源码
1.求一个小型学生理财系统的在线答疑网站源码面向对象程序设计的源代码
求一个小型学生理财系统的面向对象程序设计的源代码
#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <stdlib.h>
const unsigned int COUNT = 5;//账户数量为5
typedef struct Money{
int Date;
float money;
Money *next;
}Income,Expense;
typedef struct{
Income *income;//收入记录
Expense *expense;//支出记录
float incomeaccount;//收入统计
float expenseaccount;//支出统计
int incomelenght;
int expenselenght;
}Account;
class AccountInformation{
private:
Account L[COUNT];
public:
AccountInformation();
~AccountInformation();
int InitAccount();
void getExpense(int choice[]);
void getIncome(int choice[]);
void addExpense(int choice,Expense *elem);
void addIncome(int choice,Expense *elem);
void updateIncome(int choice,Expense elem);
void updateExpense(int choice,Expense elem);
void deleteIncome(int choice,int date);
void deleteExpense(int choice,int date);
void countAll();
void saveInfo();
};
AccountInformation::AccountInformation()
{
InitAccount();
}
AccountInformation::~AccountInformation()
{
}
int AccountInformation::InitAccount()
{
for(int i=0;i<COUNT;i++)
{
L[i].income=new Income;
L[i].expense=new Expense;
if(L[i].income==NULL||L[i].expense==NULL)
{
cout<<"分配内存失败."<<endl;
return 0;
}
L[i].incomelenght=0;
L[i].expenselenght=0;
L[i].incomeaccount=0;
L[i].expenseaccount=0;
}
Money *Q,*P;
char s[2];
//读取收入信息
ifstream fin1("income.txt");
if(fin1.fail())
{
cout<<"文件打开失败!"<<endl;
return 0;
}
for(i=0;i<COUNT;i++)
{
fin1>>s;
Q=L[i].income;
while(s[0]!='#')
{
if((int)s[1]==0)
Q->Date=(int)s[0]-;
else
Q->Date=((int)s[0]-)*+(int)s[1]-;
fin1>>Q->money;
Q->next=new Income;
L[i].incomelenght++;
L[i].incomeaccount+=Q->money;
P=Q;
Q=Q->next;
fin1>>s;
}
P->next=NULL;
}
fin1.close();
//读取支出信息
ifstream fin2("expense.txt");
if(fin2.fail())
{
cout<<"文件打开失败!"<<endl;
return 0;
}
for(i=0;i<COUNT;i++)
{
fin2>>s;
Q=L[i].expense;
while(s[0]!='#')
{
if((int)s[1]==0)
Q->Date=(int)s[0]-;
else
Q->Date=((int)s[0]-)*+(int)s[1]-;
fin2>>Q->money;
Q->next=new Income;
L[i].expenselenght++;
L[i].expenseaccount+=Q->money;
P=Q;
Q=Q->next;
fin2>>s;
}
P->next=NULL;
}
fin2.close();
return 1;
}
void AccountInformation::getExpense(int choice[])
{
Expense *Q;
float m=0.0;
for(int i=0;i<COUNT;i++)
if(choice[i]!=0)
{
Q=L[choice[i]-1].expense;
cout<<"账户"<<choice[i]<<"的支出信息为"<<endl;
cout<<"DATE\tMONEY"<<endl;
while(Q!=NULL)
{
cout<<Q->Date<<"\t"<<Q->money<<endl;
Q=Q->next;
}
cout<<"账户"<<choice[i]<<"的总支出信息为"<<L[choice[i]-1].expenseaccount<<endl<<endl;
m+=L[choice[i]-1].expenseaccount;
}
cout<<"总支出信息为"<<m<<endl;
}
void AccountInformation::getIncome(int choice[])
{
Income *Q;
float m=0.0;
for(int i=0;i<COUNT;i++)
if(choice[i]!=0)
{
Q=L[choice[i]-1].income;
cout<<"账户"<<choice[i]<<"的收入信息为"<<endl;
cout<<"DATE\tMONEY"<<endl;
while(Q!=NULL)
{
cout<<Q->Date<<"\t"<<Q->money<<endl;
Q=Q->next;
}
cout<<"账户"<<choice[i]<<"的总收入信息为"<<L[choice[i]-1].incomeaccount<<endl<<endl;
m+=L[choice[i]-1].incomeaccount;
}
cout<<"总收入信息为"<<m<<endl;
}
void AccountInformation::addExpense(int choice,Expense *elem)
{
Expense *Q,*P;
Q=L[choice-1].expense;
while(Q!=NULL)
{
if(Q->Date==elem->Date)
{
Q->money+=elem->money;
L[choice-1].expenseaccount+=elem->money;
return ;
}
if(Q->Date>elem->Date&&P->Date<elem->Date)
break;
P=Q;
Q=Q->next;
}
P->next=elem;
elem->next=Q;
L[choice-1].expenseaccount+=elem->money;
L[choice-1].expenselenght++;
}
void AccountInformation::addIncome(int choice,Expense *elem)
{
Expense *Q,*P;
Q=L[choice-1].income;
while(Q!=NULL)
{
if(Q->Date==elem->Date)
{
Q->money+=elem->money;
L[choice-1].incomeaccount+=elem->money;
return ;
}
if(Q->Date>elem->Date&&P->Date<elem->Date)
break;
P=Q;
Q=Q->next;
}
P->next=elem;
elem->next=Q;
L[choice-1].incomeaccount+=elem->money;
L[choice-1].incomelenght++;
}
void AccountInformation::updateExpense(int choice,Expense elem)
{
Expense *Q;
Q=L[choice-1].expense;
while(Q!=NULL)
{
if(Q->Date==elem.Date)
{
Q->money=elem.money;
L[choice-1].expenseaccount=elem.money-Q->money;
return ;
}
Q=Q->next;
}
}
void AccountInformation::updateIncome(int choice,Expense elem)
{
Expense *Q;
Q=L[choice-1].income;
while(Q!=NULL)
{
if(Q->Date==elem.Date)
{
Q->money=elem.money;
L[choice-1].incomeaccount=elem.money-Q->money;
return ;
}
Q=Q->next;
}
}
void AccountInformation::deleteExpense(int choice,int date)
{
Expense *Q,*P;
Q=L[choice-1].expense;
if(Q->Date==date)
{
L[choice-1].expense=NULL;
L[choice-1].expenseaccount=0.0;
L[choice-1].expenselenght=0;
}
while(Q!=NULL)
{
if(Q->Date==date)
{
P->next=Q->next;
L[choice-1].expenseaccount-=Q->money;
L[choice-1].expenselenght--;
return ;
}
P=Q;
Q=Q->next;
}
}
void AccountInformation::deleteIncome(int choice,int date)
{
Expense *Q,*P;
Q=L[choice-1].income;
if(Q->Date==date)
{
L[choice-1].income=NULL;
L[choice-1].incomeaccount=0.0;
L[choice-1].incomelenght=0;
}
while(Q!=NULL)
{
if(Q->Date==date)
{
P->next=Q->next;
L[choice-1].incomeaccount-=Q->money;
L[choice-1].incomelenght--;
return ;
}
P=Q;
Q=Q->next;
}
}
void AccountInformation::countAll()
{
Expense *Q;
float allincome=0.0;//总收入
float allexpense=0.0;//总支出
float a[COUNT],b[COUNT],c[COUNT],d[COUNT];
int date1,date2;
for(int i=0;i<COUNT;i++)
{
//收入信息
Q=L[i].income;
date1=Q->Date;
date2=Q->Date;
while (Q!=NULL)
{
if(Q->Date<date1)
date1=Q->Date;
if(Q->Date>date2)
date2=Q->Date;
Q=Q->next;
}
a[i]=L[i].incomeaccount/(date2-date1);//单位时间收入
c[i]=L[i].incomeaccount;//账户总收入
allincome+=L[i].incomeaccount;//总收入
//支出信息
Q=L[i].expense;
date1=Q->Date;
date2=Q->Date;
while (Q!=NULL)
{
if(Q->Date<date1)
date1=Q->Date;
if(Q->Date>date2)
date2=Q->Date;
Q=Q->next;
}
b[i]=L[i].expenseaccount/(date2-date1);//单位时间支出
d[i]=L[i].expenseaccount;//账户总支出
allexpense+=L[i].expenseaccount;//总支出
}
int k[COUNT]={ 1,2,3,4,5};
int l[COUNT]={ 1,2,3,4,5};
int t;
float f;
for(i=0;i<COUNT-1;i++)
for(int j=i+1;j<COUNT;j++)
if(a[i]>a[j])
{
f=a[j];
a[j]=a[i];
a[i]=f;
t=k[j];
k[j]=k[i];
k[i]=t;
}
else if(c[i]>c[j])
{
f=c[j];
c[j]=c[i];
c[i]=f;
t=l[j];
l[j]=l[i];
l[i]=t;
}
cout<<"总收入为:"<<allincome<<endl;
cout<<"账户收入分别为:\t\t单位时间内账户收入为:"<<endl;
for(i=0;i<COUNT;i++)
cout<<"账户"<<l[i]<<"的收入为:"<<c[i]<<"\t账户"<<k[i]<<"的收入为"<<a[i]<<endl;
for(i=0;i<COUNT;i++)
{
k[i]=i+1;
l[i]=i+1;
}
for(i=0;i<COUNT-1;i++)
for(int j=i+1;j<COUNT;j++)
if(b[i]>b[j])
{
f=a[j];
a[j]=a[i];
a[i]=f;
t=k[j];
k[j]=k[i];
k[i]=t;
}
else if(d[i]>d[j])
{
f=c[j];
c[j]=c[i];
c[i]=f;
t=l[j];
l[j]=l[i];
l[i]=t;
}
cout<<"总支出为:"<<allincome<<endl;
cout<<"账户支出分别为:\t\t单位时间内账户支出为:"<<endl;
for(i=0;i<COUNT;i++)
cout<<"账户"<<l[i]<<"的支出为:"<<d[i]<<"\t账户"<<k[i]<<"的支出为"<<b[i]<<endl;
}
void AccountInformation::saveInfo()
{
Money *Q;
ofstream fout1("income.txt",ios::trunc);
if(fout1.fail())
{
cout<<"文件打开失败!"<<endl;
return ;
}
for(int i=0;i<COUNT;i++)
{
Q=L[i].income;
while(Q!=NULL)
{
fout1<<Q->Date<<" "<<Q->money<<'\n';
Q=Q->next;
}
fout1<<"#\n";
}
fout1.close();
ofstream fout2("expense.txt",ios::trunc);
if(fout2.fail())
{
cout<<"文件打开失败!"<<endl;
return ;
}
for(i=0;i<COUNT;i++)
{
Q=L[i].expense;
while(Q!=NULL)
{
fout2<<Q->Date<<" "<<Q->money<<'\n';
Q=Q->next;
}
fout2<<"#\n";
}
fout2.close();
}
void menu1(int choice[COUNT])
{
char s[5];
cout<<"