| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 682 人关注过本帖
标题:用this指针和建立对象调用输出结果却不一样
只看楼主 加入收藏
lihaibao1991
Rank: 1
等 级:新手上路
帖 子:7
专家分:3
注 册:2013-3-21
结帖率:0
收藏
已结贴  问题点数:20 回复次数:7 
用this指针和建立对象调用输出结果却不一样
void Student::insert1(Bookji *a) //学生插入所借入的书本
{
 Bookji *p=this->phead->pnext;
 this->phead->pnext=a;
 a->pnext=p;
 phead->pnext->display_1f();
}
void Student::borrow_process(admin &p,string namebook4,int outnum4)//借书过程及记录
{
    Book *q=p.head_book();
    Bookji A;
    A.namebook1=namebook4;
    A.num1=outnum4;
    A.outnum1=outnum4;
   
    time_t rawtime;
    struct tm * timeinfo;
    time (&rawtime );
    timeinfo = localtime ( &rawtime );
    A.borrow_time=asctime (timeinfo);
   
     this->insert1(&A);
    q=q->next;
    while(q->namebook!=namebook4)
        q=q->next;
    int temp;
    temp=q->outnum;
    q->outnum=(temp+outnum4);
    temp=q->num;
    q->num=temp-outnum4;
    borrow_num=borrow_num+outnum4;
    num_pend=num_pend+outnum4;
    this->display_1f(); //   输出时可以输出学生的借书的信息
}
void Student::display_1f()
{   
    cout<<student_num<<" "<<student_name<<" ";
    Bookji *p=this->phead->pnext;
    p->display_1f();
    cout<<borrow_num<<" "<<num_pend<<endl;  
}
int main()
{
       Book a("001","古龙","dfdf","天津出版社","2010.12",10);
    Book a1("002","qwe","eret","asd","dfs",10);
    Book a2("003","dfd","erty","dfds","2010120",10);
    admin people;
    people.insert(&a);
    people.insert(&a1);
    people.insert(&a2);
     Student zhang("54110812","lihaibao");
     zhang.borrow_process(people,"dfdf",4);
     zhang.display_1f(); //为什么这里调用却输出乱码?
return 0;
}
其中admin是管理员对象
Book是定义的书对象
搜索更多相关主题的帖子: void 记录 admin 
2013-03-21 21:08
yuccn
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:何方
等 级:版主
威 望:167
帖 子:6815
专家分:42393
注 册:2010-12-16
收藏
得分:20 
部分代码的吧,你这样叫人家看什么?

我行我乐
公众号:逻辑客栈
我的博客:
https://blog.yuccn. net
2013-03-22 12:05
lihaibao1991
Rank: 1
等 级:新手上路
帖 子:7
专家分:3
注 册:2013-3-21
收藏
得分:0 
对不起,各位高手!下面是全部代码
#ifndef BOOK_H
#define BOOK_H

#include<iostream>
#include<string>
using namespace std;
class Book  
{
public:
 string number_book;
 string nameauthor;
 string namebook;
 string  wherebook;
 string timebook;
 int num;
 int outnum;
 Book *next;
 Book();
 Book(string a4,string a,string a1,string a2,string a3,int num1=0,int outnum1=0,Book *p=NULL)
{
 number_book=a4;
 nameauthor=a;
 namebook=a1;
 wherebook=a2;
 timebook=a3;
 num=num1;
 outnum=outnum1;
 next=p;
}
 friend ostream &operator <<(ostream &output,Book &c1);
 void display();
 ~Book();

};
#endif
////////////////////////////////////////////
/////////////////////////////////////////////
// Book.cpp: implementation of the Book class.
//
//////////////////////////////////////////////////////////////////////

#include "Book.h"
#include<iostream>
#include<string>
using namespace std;

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Book::Book()
{
}
void Book::display()
{
    cout<<number_book<<" "<<nameauthor<<" "<<namebook<<" "<<wherebook<<" "<<timebook<<" "<<num<<" "<<outnum<<endl;
}
ostream &operator<<(ostream &output,Book &c1)
{
    output<<c1.number_book<<" "<<c1.nameauthor<<" "<<c1.namebook<<" "<<c1.wherebook<<" "<<c1.timebook<<" "<<c1.num<<" "<<c1.outnum<<endl;
    return output;
}
Book::~Book()
{
    //dtor
}
2013-03-22 13:46
lihaibao1991
Rank: 1
等 级:新手上路
帖 子:7
专家分:3
注 册:2013-3-21
收藏
得分:0 
// Bookji.h: interface for the Bookji class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_BOOKJI_H__7F888AAE_2702_4183_9298_8BB594431E5C__INCLUDED_)
#define AFX_BOOKJI_H__7F888AAE_2702_4183_9298_8BB594431E5C__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include<iostream>
#include<string>
using namespace std;
class Bookji  
{
public:
    string namebook1;
    int outnum1;
    int num1;
    string borrow_time;
    Bookji *pnext;
    Bookji(string namebook="",int outnum2=0,int num=0,string borrow_time1="")
    {
        namebook1=namebook;
        outnum1=outnum2;
        num1=num;
        borrow_time=borrow_time1;
        pnext=NULL;
    }
    void display_1f();
    friend ostream &operator <<(ostream &output,Bookji &c1);
    virtual ~Bookji();

};

#endif // !defined(AFX_BOOKJI_H__7F888AAE_2702_4183_9298_8BB594431E5C__INCLUDED_)
////////////////////////////////
/////////////////////////////////
// Bookji.cpp: implementation of the Bookji class.
//
//////////////////////////////////////////////////////////////////////

#include "Bookji.h"
#include<iostream>
using namespace std;

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
ostream &operator<<(ostream &output,Bookji &c1)
{
    output<<c1.namebook1<<" "<<c1.outnum1<<" "<<c1.num1<<" "<<c1.borrow_time<<endl;
    return output;
}
void Bookji::display_1f()
{
    cout<<namebook1<<" "<<num1<<" "<<outnum1<<" "<<borrow_time<<endl;
}
Bookji::~Bookji()
{  
}
2013-03-22 13:47
lihaibao1991
Rank: 1
等 级:新手上路
帖 子:7
专家分:3
注 册:2013-3-21
收藏
得分:0 
// admin.h: interface for the admin class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_ADMIN_H__3BC668C2_8740_4117_9C30_65ACDAC8CD50__INCLUDED_)
#define AFX_ADMIN_H__3BC668C2_8740_4117_9C30_65ACDAC8CD50__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include<iostream>
#include<string>
#include"Book.h"
using namespace std;
class Student;
class admin//:public Book
{
private:
    Book *head;
public:
    admin();
    void insert(Book *a);
    void Research(string namebook2);
    void Delete(string namebook1);
    void Re_book(Student &a,string namebook2,int re_num);
    void display();
    Book* head_book()
    {
        return head;
    }
    virtual ~admin();

};

#endif // !defined(AFX_ADMIN_H__3BC668C2_8740_4117_9C30_65ACDAC8CD50__INCLUDED_)

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// admin.cpp: implementation of the admin class.
//
//////////////////////////////////////////////////////////////////////

#include "admin.h"
#include "Student.h"
#include"Book.h"
#include<iostream>
#include<string>
using namespace std;

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

admin::admin()
{
 head=new Book();
 head->next=NULL;
}
void admin::insert(Book *a)
{
 Book *p=head->next;
 head->next=a;
 a->next=p;
}
void admin::Research(string namebook2)
{
    Book *p=head->next;
    while(p->next!=NULL&&p->namebook!=namebook2)
     p=p->next;
   if(p->namebook==namebook2)
       p->display();
    else
        cout<<"it's not exist"<<endl;
}        
void admin::Re_book(Student &q,string namebook2,int re_num)
{
    Book *p1=this->head->next;
    Bookji *q1=q.phead_book();
    q1=q1->pnext;
    while(p1->namebook!=namebook2)
        p1=p1->next;
    p1->outnum=p1->outnum-re_num;
    p1->num=p1->num+re_num;
    while(q1->namebook1!=namebook2)
        q1=q1->pnext;
    q1->num1=q1->num1-re_num;
    q1->outnum1=q1->outnum1-re_num;
    cout<<q1->num1<<endl;
    //q1->display_1f();
}
void admin::Delete(string namebook1)
{
    Book *p,*q;
    p=head->next;
    while(p!=NULL&&p->namebook!=namebook1)
    {
        q=p;
        p=p->next;
    }
    if(p->namebook==namebook1)
        q->next=p->next;
    else
        cout<<"it's not exist"<<endl;
        
}
   
void admin::display()
{
    Book *p=head;
    while(p->next!=NULL)
    {
        p->next->display();
        p=p->next;
    }
}
admin::~admin()
{
   delete head;
}
2013-03-22 13:47
lihaibao1991
Rank: 1
等 级:新手上路
帖 子:7
专家分:3
注 册:2013-3-21
收藏
得分:0 
// Student.h: interface for the Student class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_STUDENT_H__64B07981_EE99_4F32_9A0F_36F25D92D7AA__INCLUDED_)
#define AFX_STUDENT_H__64B07981_EE99_4F32_9A0F_36F25D92D7AA__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include<iostream>
#include"Book.h"
//#include"admin.h"
#include"bookji.h"
#include<string>
using namespace std;
class admin;
class Student   
{
private:
    string student_num; //学生学号
    string student_name; //学生姓名
    int borrow_num;   //借书的总数
    int num_pend;    //计算总共多少没还
    Bookji *phead;
public:
    Student();
    string stu_num();
    Student(string,string,int borrow_num=0,int num_pend=0);
    Bookji* phead_book()
    {
        return phead;
    }
    void student_research(admin &p);
    void insert1(Bookji *a);
    void borrow_process(admin &p,string bookname4,int outnum4);
    Book* borrow_information(admin &p,string bookname4);
    void display_1f();
    //friend ostream &operator <<(ostream &output,Student &c1);
    virtual ~Student();

};

#endif // !defined(AFX_STUDENT_H__64B07981_EE99_4F32_9A0F_36F25D92D7AA__INCLUDED_)
//////////////////////////////
////////////////////////////////
// Student.cpp: implementation of the Student class.
//
//////////////////////////////////////////////////////////////////////

#include "Student.h"
#include"Book.h"                              
#include"admin.h"
#include"bookji.h"
#include "ctime"
#include<iostream>
#include<string>
using namespace std;

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Student::Student(string student_num,string student_name,int borrow_num,int num_pend)
{
this->student_num=student_num;
this->student_name=student_name;
this->borrow_num=borrow_num;
this->num_pend=num_pend;
phead=new Bookji();
phead
}
string Student::stu_num()
{
    return student_num;
}
void Student::student_research(admin &p)
{
    cout<<"what do you need:";
    string bookname3;
    cin>>bookname3;
    p.Research(bookname3);
}
void Student::insert1(Bookji *a) //学生插入所借入的书本
{
 Bookji *p=this->phead->pnext;
 this->phead->pnext=a;
 a->pnext=p;
 phead->pnext->display_1f();
}
void Student::borrow_process(admin &p,string namebook4,int outnum4)//借书过程及记录
{
    Book *q=p.head_book();
    Bookji A;
    A.namebook1=namebook4;
    A.num1=outnum4;
    A.outnum1=outnum4;
   
    time_t rawtime;
    struct tm * timeinfo;
    time (&rawtime );
    timeinfo = localtime ( &rawtime );
    A.borrow_time=asctime (timeinfo);
   
     this->insert1(&A);
    q=q->next;
    while(q->namebook!=namebook4)
        q=q->next;
    int temp;
    temp=q->outnum;
    q->outnum=(temp+outnum4);
    temp=q->num;
    q->num=temp-outnum4;
    borrow_num=borrow_num+outnum4;
    num_pend=num_pend+outnum4;
    this->display_1f();
}
void Student::display_1f()
{   
    cout<<student_num<<" "<<student_name<<" ";
    Bookji *p=phead;
    while(p->pnext!=NULL)
    {
        p->pnext->display_1f();
        p=p->pnext;
    }
    cout<<borrow_num<<" "<<num_pend<<endl;  
}
/*ostream &operator <<(ostream &output,Student &c1)
{
    output<<c1.student_num<<" ";
    output<<c1.student_name<<" ";
    output<<c1.borrow_time<<" ";
    output<<c1.borrow_num<<" ";
    output<<c1.num_pend<<" ";
    return output;
}*/
Book* Student::borrow_information(admin &p,string namebook4)
{
    Book*q=p.head_book();
    while(q->next->namebook!=namebook4)
        q=q->next;
    return q->next;
}
Student::~Student()
{

}
2013-03-22 13:48
lihaibao1991
Rank: 1
等 级:新手上路
帖 子:7
专家分:3
注 册:2013-3-21
收藏
得分:0 
知道了是生命周期出了问题
2013-03-31 09:20
liqi2009
Rank: 2
等 级:禁止访问
帖 子:71
专家分:15
注 册:2012-2-21
收藏
得分:0 
是的
2013-04-01 09:07
快速回复:用this指针和建立对象调用输出结果却不一样
数据加载中...
 
   



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

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