帮忙修改一下这个C++程序

2025-05-12 23:22:51
推荐回答(2个)
回答(1):

下面是修改过以后的,修改部分都用//**//标出来了。
struct student
{
char name[5];
char number[6];
int chn;
int math;
int eng;
int total;
int list;//**//
struct student*next;
};
typedef struct student Node;
typedef Node *pNode;

void oper(pNode &pHead)
{
pNode ptr;
pNode newnode=new Node;
ptr=NULL;
cin>>newnode->name;
cin>>newnode->number;
cin>>newnode->chn;
cin>>newnode->math;
cin>>newnode->eng;
newnode->total=newnode->math+newnode->eng+newnode->chn;
newnode->list=1;//**//
if(pHead)
{
ptr=pHead;
while (ptr->next)
{
if(ptr->total > newnode->total)//**//
newnode->list++;//**//
else if(ptr->total < newnode->total)//**//
ptr->list++;//**//
ptr=ptr->next;
}
newnode->next=NULL;
ptr->next=newnode;
}
else
{
pHead = newnode;
newnode->next=NULL;
ptr=pHead;
}
cout<list;//*输出名次*//
}

回答(2):

你的代码咋一看还真有点摸不着头脑的同,而且也没有说明做什么,没注释。
原来类型有点乱,我统一用double了,你觉得不妥再换吧
我大致猜测你的要做的目的,改了下(代码VC6.0调试通过)
#include
using std::cin;
using std::cout;
using std::endl;

// #include
// using std::string;

class Account
{
public:
Account(void){};

//double accountBalance;
double presentAccountBalance;

void Initial(double initialBalance)
{
if(initialBalance>=0)
presentAccountBalance = initialBalance; // accountBalance=initialBalance;
if(initialBalance<0)
{
presentAccountBalance = 0; //accountBalance=0;
cout<<"the initial balance was invalid"< }
}

void Credit(double deposit)
{
presentAccountBalance=presentAccountBalance+deposit; //presentAccountBalance=accountBalance+deposit;
}
void debit(double money)
{
if(money<=presentAccountBalance)
presentAccountBalance=presentAccountBalance-money;
if(money>presentAccountBalance)
{
cout<<"Debit amount exceeded account balance"< }
}
void getBalance()
{
cout<<"the present account balance is:"< }
};

int main()
{
double initialBalance = 0.0;
double deposit = 0.0;
double money = 0.0;

Account account1;
Account account2;

cout<<"please enter the initial balance for the account1:"< cin >> initialBalance;
account1.Initial(initialBalance);
cout<<"please enter the initial balance for the account2:"< cin >> initialBalance;
account2.Initial(initialBalance);

cout<<"please enter the deposit for the account1:"< cin >> deposit;
account1.Credit(deposit);
cout<<"please enter the deposit for the account2:"< cin >> deposit;
account2.Credit(deposit);

cout<<"please enter the money you want to take out from account1:"< cin >> money;
account1.debit(money);
cout<<"please enter the money you want to take out from account2:"< cin >> money;
account2.debit(money);

account1.getBalance();
account2.getBalance();
return 0;
}