小弟初学,帮我解决一下吧!..我在线等谢谢啊! 给我完整的程序!我不知道这个程序那里错了

csxisunbao 2008-12-31 05:01:13
# include <iostream.h>
# include <string.h>
# include "bank.h"

class Account
{
private:
int ID;
char *Name;
double Balance;
public:
Account();
Account(int ,const char *,double);
void Saving();
int Withdraw();
void Showme();
int getID();
};
class Bank
{
private:
Account *account[50];

int maxid;
int accNum;
public:
bank();
void Append();
void Delete();
void Query();
};
Account::Account()
{
ID = 0;
strcpy(Name,"");
Balance = 0;
}

Account::Account( int id,const char *name,double balance)
{
ID = id;
Name = new char[50];
strcpy (Name,name);
Balance = balance;
}

void Account ::Saving ()
{
double number;
cout<<"please input saving number:";
cin>>number;
Balance = Balance+number;
cout<<"Now the balance of you account is"<<Balance <<endl;
return;
}

int Account :: Withdraw()
{
double number;
cout<<"please input withdraw number;";
cin>>number;
if(Balance<number)
{
cout<<"sorry,now the balance of you account is less than"<<number<<
"."<<endl<<"so you can not withdraw!"<<endl;
return 0;
}
Balance = Balance -number;
cout<<"now the balance of you account is "<<Balance <<endl;
return 1;
}

void Account::Showme()
{
cout<<"Account ID:"<<ID <<endl;
cout<<"Name:"<<Name<<endl;
cout<<"Balance:"<<Balance<<endl;
return;
}

int Account::getID()
{
return ID;
}

Bank::bank()
{
for(int i=0;i<50;i++)
account[i]= NULL;


maxid = 0;
accNum = 0;
}

void Bank ::Append()
{
if( accNum ==50)
{
cout<<"sorry ,the bank is full ,so can not add new accont !"<<endl;
return;
}
int id;
char *name = new char [50];
double balance;
cout<<"please input the name of the account;";
cin>>name;
id = maxid;
cout<<"please input the money number;";
cin>>balance;

Account *acc =new Account(id,name,balance);
account[accNum]=acc;
cout<<"append successful !"<<endl<<endl;
account[accNum]->Showme();
maxid++;
accNum++;
return;
}

void Bank :: Delete()
{
int ID;
cout<<"please input the account ID that you want to delete:";
cin>>ID;
int flag = 1;
int i =0;
while((i<accNum)&&(flag))
if(ID ==account[i]->getID())
flag =0;
else
i++;
if(flag)
{
cout<<"the account does not exist!"<<endl<<endl;
return;
}
for(int j=i;j<accNum;j++)
account[j] = account[j+1];
delete account[accNum-1];
accNum--;
cout<<"delete successful!"<<endl<<endl;
return;
}
void Bank::Query()
{
int ID;
cout<<"please input the account ID that you want to query:";
cin>>ID;
int flag =1;
int i =0;
while((i<accNum)&& (flag))
if(ID == account[i]->getID())
flag = 0;
else
i++;
if(flag)
{
cout<<"the account does not exist !"<<endl<<endl;
return;
}


account[i] ->Showme();
int choice = 0;
while(choice !=3)
{
cout << "1:save money" <<endl;
cout <<"2: Withdraw money"<<endl;
cout <<"3: return "<<endl<<endl;
cin>>choice;
switch(choice)
{
case 1:
account[i]->Saving();
break;
case 2:
account[i]->Withdraw();
break;
case 3 :
break;
}
}
}
void main()
{
Bank bank;
int choice =0;
cout<<"welcom to bank system! "<<endl;
while(choice!=4)
{
cout<<"1,add an account "<<endl;
cout<<"2.delete anaccount "<<endl;
cout<<"3.query an account "<<endl;
cout<<"4.exit"<<endl<<endl;
cout<<"please input your choice :";
cout<<endl;
cin>>choice;
switch(choice)
{
case 1:
bank.Append();
break;
case 2:
bank.Delete();
break;
case 3:
bank.Query();
break;
case 4:
break;
}
cout<<endl;
}
return ;
}
...全文
62 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenchangxiong 2008-12-31
  • 打赏
  • 举报
回复
ms有很多的内存泄漏 还有你的void Bank :: Delete() 写的有问题,哪有
for(int j=i;j <accNum;j++)
account[j] = account[j+1];
delete account[accNum-1
这么释放的
pTemp=account[i];
for(int j=i;j <accNum;j++)
account[j] = account[j+1];
if(pTemp){
delete pTemp;
pTemp=NULL;
}

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧