标题:从txt文件中读取数据建立链表再输出,输出不能用循环语句,甚至空循环语句都 ...
只看楼主
wind97868
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2020-7-12
结帖率:100%
已结贴  问题点数:20 回复次数:8 
从txt文件中读取数据建立链表再输出,输出不能用循环语句,甚至空循环语句都不能出现
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include "account.h"

using namespace std;

int main()
{
//for(int i=0;i<2;i++);
  ifstream cin("acc.txt");
  char *type;unsigned int ac;float ba;
  for(;cin>>type>>ac>>ba;){
     cout<<type<<endl;
     if(strcmp(type,"Savings")==0)
        new Savings(ac,ba);
    else if(strcmp(type,"Checking")==0)
        new Checking(ac,ba);
    else{
       cout<<"error account type :"<<type<<endl;
        exit(1);
     }
  }
  Account *p=Account::First();
 /* while(p!=NULL)
  {
    p->Display();
    p=p->Next();
  }*/

  p->Display();
  p=p->Next();
  p->Display();
  p=p->Next();
  p->Display();
  p=p->Next();
  p->Display();
  p=p->Next();
  p->Display();
  p=p->Next();
  p->Display();
  p=p->Next();
}
搜索更多相关主题的帖子: type 语句 Next Display 循环 
2020-07-12 19:36
wind97868
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2020-7-12
得分:0 
输出结点时,逐条语句输出可以,但不能用循环语句输出
2020-07-12 19:39
wind97868
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2020-7-12
得分:0 
求高手给分析一下为什么不能用循环语句输出链表,甚至空循环语句都不能出现
2020-07-12 19:59
wind97868
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2020-7-12
得分:0 
account.h

#ifndef ACCOUNT
#define ACCOUNT
class Account{
  public:
      //链表类操作
      Account(unsigned int acntNo,float balan=0.0);
      static Account * First(){return pFirst;}
      Account * Next(){return pNext;}
      static void RemoveLink();//删除整个个链表
      static void SaveLink();//保存整个链表
      static void getLink();//从txt文件中读取数据建立链表
      static void linkDisplay();//输出整个链表上的帐户及其余额
      //账户类操作
      unsigned int AccountNo(){return acntNumber;}
      float AccountBalan(){return balance;}
      virtual void Display();//便于统一显示,设为虚函数
      void Deposit(float amount){balance+=amount;}
      virtual void Withdrawal(float amount)=0;
      static Account *getAccount(unsigned int acntNo);//返回指定帐号所在结点
      static void balanceAccount();//查询指定帐号的余额
      static void depositAccount();//向指定帐号存款
      static void WithdrawalAccount();//从指定帐号取款
      //为什么没定义析构函数?
  protected:
    //链表类数据
    static Account *pFirst;
    static Account *pTail;
    Account *pNext;
    //账户类数据
    unsigned int acntNumber;
    float balance;
    const char * acntType="Account";
};
//------------------------------------------------------------------
class Savings: public Account{
  public:
      Savings(unsigned acntNo,float balan=0.0);
      virtual void Withdrawal(float amount);
      virtual void Display();
  protected:
    static float minbalance;//设置透支范围
};
//----------------------------------------------------------------
enum REMIT{remitByPost,remintByCable,remintByother};//信汇、电汇,无

class Checking:public Account{
  public:
      Checking(unsigned acntNo,float balan);
      void SetRemit(REMIT re){remmitance=re;}
      virtual void Withdrawal(float amount);
      virtual void Display();
  protected:
    REMIT remmitance;//汇款方式
};
//--------------------------------------------------------------------

#endif // ACCOUNT
//------------------------------------------------------------------------------------------------------------------
account.cpp

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include "account.h"

using namespace std;

//静态数据成员
Account * Account::pFirst=0;
Account * Account::pTail=0;

Account::Account(unsigned acntNo,float balan):acntNumber(acntNo),balance(balan),pNext(NULL){
  if(pFirst==0)
    pFirst=pTail=this;
  else
    {
        pTail->pNext=this;
        pTail=this;//新建结点挂到链尾
    }
}

//-----------
void Account::Display(){
  cout<<"Account number:"<<acntNumber<<"   balance:"<<balance<<endl;
}

//-------------------------------------------------------------------------------
float Savings::minbalance=500;//设置透支范围

Savings::Savings(unsigned acntNo,float balan):Account(acntNo,balan){acntType="Savings";}
//
 void Savings::Withdrawal(float amount){
  if(amount>balance+minbalance)
    cout<<"Insufficient funds: balance "<<balance<<",withdrawal "<<amount<<endl;
  else
    balance-=amount;
}
//
 void Savings::Display(){
  cout<<"Savings  ";
  Account::Display();
}
//----------------------------------------------------------------------------------
Checking::Checking(unsigned acntNo,float balan):Account(acntNo,balan),remmitance(remintByother){acntType="Checking";}
//
void Checking::Withdrawal(float amount){
  float tmp=amount;
  if(remmitance==remintByCable)
    tmp+=60;
  if(remmitance==remitByPost)
    tmp+=30;
  if(balance<tmp)
    cout<<"Insufficisent fuds:balance "<<balance<<" ,withdrawal "<<tmp<<endl;
  else
    balance-=tmp;
}
//
void Checking::Display(){
  cout<<"Checking ";
  Account::Display();
}

//--------------------------------------------------------------------------------
//链表类操作
//从txt文件中读取数据建立链表
void getLink(){
  ifstream cin("acc.txt");
  char *type;unsigned int ac;float ba;
  cout<<"hello"<<endl;
  for(;cin>>type>>ac>>ba;){cout<<type<<endl;
   if(!(strcmp(type,"Savings")))
        new Savings(ac,ba);
    else if(!(strcmp(type,"Checking")))
        new Checking(ac,ba);
    else{
        cout<<"error account type :"<<type<<endl;
        exit(1);
    }
  }
  cout<<"world"<<endl;
}
//把整个链表保存在文件acc.txt中
void Account::SaveLink(){

}
//删除整个链表
void Account::RemoveLink(){
  for(Account *q,*p=Account::First();p;p=q){
    q=p->pNext;
    delete p;
  }
}
//输出整个链表上的帐户及其余额
void Account::linkDisplay(){
   Account *p=Account::First();
   for(;p;p=p->Next())
    p->Display();
}
//账户类操作------
//返回指定帐号所在结点
Account * Account::getAccount(unsigned int acntNo){
    Account *p=Account::First();
    for(;p&&p->AccountNo()!=acntNo;p=p->Next());
    return p;
}
//查询指定帐号的余额
void Account::balanceAccount(){
  unsigned int acntNo;
  cout<<"input the account number:";
  cin>>acntNo;
  Account *p=getAccount(acntNo);
  if(!p)
    cout<<"account number :"<<acntNo<<" not exist!"<<endl;
  else
    p->Display();
}
//向指定帐号存款
void Account::depositAccount(){
  unsigned int acntNo;
  float amount;
  cout<<"input the account number and the amount to deposit:"<<endl;
  cin>>acntNo>>amount;
  Account *p=getAccount(acntNo);
  if(!p)
    cout<<"account number :"<<acntNo<<" not exist!"<<endl;
  else{
    p->Deposit(amount);
    p->Display();
  }
}
//从指定帐号取款
void Account::WithdrawalAccount(){
  unsigned int acntNo;
  float amount;
  cout<<"input the account number and the amount to withdrawal:"<<endl;
  cin>>acntNo>>amount;
  Account *p=getAccount(acntNo);
  if(!p)
    cout<<"account number :"<<acntNo<<" not exist!"<<endl;
  else{
    p->Withdrawal(amount);
    p->Display();
  }
}



//--------------------------------------------------------------------------------------------------
acc.txt

Savings 12345 12345.00
Savings 10000 10000.00
Savings 09988 9988.00
Checking 20032 20032.00
Checking 25678 25678.00
Savings 26100 26100.00
2020-07-12 21:14
wind97868
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2020-7-12
得分:0 
求高手给分析为什么主函数中输出链表不能用循环语句?甚至加个空循环都不行
2020-07-12 21:19
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:0 
  char *type;unsigned int ac;float ba;
  for(;cin>>type>>ac>>ba;)
这就开始不对了

而你在4楼的代码,既~又长,完全看不懂。
2020-07-13 08:20
wind97868
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2020-7-12
得分:0 
回复 6楼 rjsp
为什么输出时不用循环语句,可正常输出,一加上循环语句,甚至空语句都不行(没有输出)
2020-07-13 09:05
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:20 
以下是引用wind97868在2020-7-13 09:05:51的发言:
为什么输出时不用循环语句,可正常输出,一加上循环语句,甚至空语句都不行(没有输出)

对于错误的代码,讨论它“执行正常”或“执行不正常”是没有意义的。
不知道你能理解么,打个比方,一个景点,已经说了严禁跳山。
一个人跳崖,没死,给景区负责人一个嘴巴:“为什么我跳崖没死?”,景区负责人只能委屈地告诉他“我们只能保证你不跳崖就不会摔死,没能力保证你跳崖必须死”;
此人又跳崖,死了,头七托梦又找到负责人:“为什么我跳崖死了?”,景区负责人只能委屈地告诉他“我们只能保证你不跳崖就不会摔死,没能力保证你跳崖必须活”。

程序代码:
#include <iostream>
#include <string>

struct node
{
    std::string type;
    unsigned ac;
    float ba;

    friend std::istream& operator>>( std::istream& is, node& v )
    {
        node tmp;
        if( is >> tmp.type >> tmp.ac >> tmp.ba )
            v = tmp;
        return is;
    }
    friend std::ostream& operator<<( std::ostream& os, const node& v )
    {
        return os << v.type << " " << v.ac << " " << v.ba;
    }
};

#include <fstream>
#include <iterator>
#include <algorithm>
#include <functional>
#include <list>
using namespace std;

int main( void )
{
    ifstream fin( "acc.txt" );
    if( !fin )
    {
        cerr << "文件打开失败.\n";
        return 1;
    }

    // 从文件中读取数据 到 链表s 中
    list<node> s;
    copy( istream_iterator<node>(fin), istream_iterator<node>(), std::back_inserter(s) );

    // 从链表s中读取数据 到 屏幕 上
    copy( begin(s), end(s), ostream_iterator<node>(cout,"\n") );
}
2020-07-13 10:24
wind97868
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2020-7-12
得分:0 
谢谢
2020-07-13 17:05



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-502501-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.321691 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved