问题描述
- C++程序 能否将下面代码改成用链表写的
-
#include
#include
#include
#include
#include
#include
using namespace std;
const int Maxb=10000; //最多的财务收支次数//财务类,实现对财务的描述,财务的编号,使用者等
class Finance
{
private:
int tag; //删除标记 1:已删 0:未删
int no;//财务编号
char name[20]; //姓名
int acount;
int ontag;
int date; //财务日期
public:
Finance(){}
char *getname() { return name; } //获取姓名
int getno(){ return no; } //获取财务编号
int getacout(){return acount; }
int gettag(){ return tag; } //获取删除标记
int getontag(){ return ontag; }
int getdate(){ return date; }//获取日期
void setname(char na[])//设置
{
strcpy(name,na);
}void delfinance(){ tag=1;} //删除财务
void addfinance(int da,int n,char *na,int nu) //增加财务
{
char ch;
tag=0;
date=da;
no=n;
strcpy(name,na);
acount=nu;
ontag=1;
cout<<" 记录为支出按(y),收入(其他键)"<
cin>>ch;if(ch=='Y'||ch=='y')
ontag=0;}
int borrowfinance()//支出操作
{
if (ontag==1)
{
ontag=0;
return 1;
}
return 0;
}
void retfinance()//收入操作
{
ontag=1;
}
void disp()//输出财务
{
cout << setw(6) << date <<setw(6) << no << setw(18) << name << setw(10)
<<(ontag==1? "收入":"支出") << setw(10)<<acount<<endl;
}
};//财务库类,实现对财务的维护,查找,删除等
class BDatabase
{
private:
int top; //财务记录指针Finance finance[Maxb]; //财务记录
public:
BDatabase()//构造函数,将finance.txt读到finance[]中
{
Finance b;
top=-1;
fstream file("finance.txt",ios::in);
while (1)
{
file.read((char *)&b,sizeof(b));
if (!file) break;
top++;
finance[top]=b;
}
file.close();
}void clear()//全部删除
{
top=-1;
}
int addfinance(int da,int n,char *na,int nu) //增加收入
{
Finance *p=query(n);
if (NULL==p)
{
top++;
finance[top].addfinance(da,n,na,nu); //查找财务
return 1;
}
return 0;
}Finance *query(int financeid)
{
for (int i=0;i<=top;i++)
if (finance[i].getno()==financeid &&finance[i].gettag()==0)
{
return &finance[i];
}
return NULL;
}
void financedata();//财务维护
void financecount()// 财务统计
{
int Bnum=0;
int Rnum=0;
int Mcout=0;
int Bcout=0;
int Rcout=0;
for(int i=0;i<=top;i++)
{
if(finance[i].getontag()==1)
{
Bcout+=finance[i].getacout();
Bnum++;
finance[i].disp();
}
else{
Rcout+=finance[i].getacout();
Rnum++;
finance[i].disp();
}
}
Mcout=Bcout-Rcout;
cout<<"收入总金额"<<Bcout<<endl;
cout<<"支出总金额:"<<Rcout<<endl;
cout<<"收支结余:"<<Mcout<<endl;
}
void financecount(int data) // 按月财务统计
{
int da=data;
int Bnum=0;
int Rnum=0;
int Mcout=0;
int Bcout=0;
int Rcout=0;
for(int i=0;i<=top;i++)
{
if(finance[i].getdate()==da)
{
if(finance[i].getontag()==1)
{
Bcout+=finance[i].getacout();
Bnum++;
finance[i].disp();
}
else{
Rcout+=finance[i].getacout();
Rnum++;
finance[i].disp();
}
}
}
Mcout=Bcout-Rcout;
cout<<"收入总金额"<<Bcout<<endl;
cout<<"支出总金额:"<<Rcout<<endl;
cout<<"收支结余:"<<Mcout<<endl;
}
void disp()
{
for (int i=0;i<=top;i++)
if (finance[i].gettag()==0)
finance[i].disp();
}
~BDatabase() //析构函数,将finance[]写到finance.txt文件中{ fstream file("finance.txt",ios::out); for (int i=0;i<=top;i++) if (finance[i].gettag()==0) file.write((char *)&finance[i],sizeof(finance[i])); file.close(); }
};
void BDatabase::financedata()
{
char choice;
char bname[40];
int danum;
int financeid;
int acount;
int date;
Finance *b;
while (choice!='0')
{cout<<" -----------------------------"<
cout
cout
cout
cout
cout
cout
cout
cout
cout
cout
cout
cout
cout
cout
cout
cout
cin >> choice;
switch (choice)
{
case '1':
cout << "输入财务编号:"<
cin >> financeid;
cout << "输入财务使用者:"<
cin >> bname;
cout << "输入金额:"<
cin >> acount;
cout<<"输入时间:格式:2008年12月请写成 200812"<
cin >>danum;
addfinance(danum,financeid,bname,acount);
break;
case '2':
cout << "输入财务编号:"<
cin >> financeid;
b=query(financeid);
if (b==NULL)
{
cout << " 该财务不存在 "<
break;
}
cout
cin >> bname;
b->setname(bname);
break;
case '3':
cout <<" 读入财务编号:"<
cin >> financeid;
b=query(financeid);
if (b==NULL)
{
cout <<" 该财务不存在" << endl;
break;
}
b->delfinance();
break;
case '4':
cout << " 读入财务编号:"<
cin >> financeid;
b=query(financeid);
if (b==NULL)
{
cout <<" 该财务不存在"<< endl;
break;
}
b->disp();
break;
case '5':
cout << setw(6) <<"时间"<
disp();
break;
case '6':
financecount();
break;
case '7':
cout
cin>>date;
financecount(date);
break;
case '8':
clear();
cout<<"删除成功!"<<endl;
break;
default:
cout<<"请重新输入:";
}
}
}void main() //main() 函数的实现,程序的主界面的引导
{
system("color 3b");
char choice;
int financeid;
BDatabase BookDB;
while(choice!='0')
{cout<<"ntt 欢迎使用个人财务管理系统 n";
cout<<" /*******************************************************/"<
cout
cout
cout
cout
cout
cout
cout
cin >> choice;
switch (choice)
{
case '1':
BookDB.financedata();
break;
case '0':
break;
default:return;
}
}
}