-3

我的代码:
类“表”中有一个数组来保存新添加的书(结构);
只需在数组中的一个旁边添加一个;
search 可以使用 ISBN、title 或 author,这些都是 book (struct) 中的变量;
打印应该是计算书的信息

问题:print不能打印字符串(书中的变量都是字符串)

可能不是问题:插入,添加......这种功能应该很好用,因为当我搜索某本书时,它显示“找到书”

#include<iostream>
#include<string>

using namespace std;

struct book
{
    string isbn;
    string title;
    string author;
    string date;
};
class table
{
public:
    //member constant
    static const size_t CAPACITY = 30;
    //constructor
    table() {used = 0;}
    //modification
    bool insert(book entry);
    //constant
    size_t hash_isbn(string target_isbn);
    size_t hash_title(string target_title);
    size_t hash_author(string target_author);
    size_t search_isbn(string target_isbn);
    size_t search_title(string target_title);
    size_t search_author(string target_author);
    void print(size_t index);
private:
    //member variables
    book data[CAPACITY];
    size_t used;
};
//modification member functions
bool table::insert(book entry)
{
    if(search_isbn(entry.isbn))
        return false;
    data[used] = entry;
    used++;
    return true;
}
//constant member functions
size_t table::hash_isbn(string target_isbn)
{
    size_t index = 0;
    bool found = false;

    while((index < used) && (!found))
    {
        if(data[index].isbn == target_isbn)
        {
            found = true;
            continue;
        }
        index ++;
    }
    if(!found)
        index = -1;
    return index;
}
size_t table::hash_title(string target_title)
{
    size_t index = 0;
    bool found = false;
    while((index < used) && !found)
    {
        if(data[index].title == target_title)
        {
            found = true;
            continue;
        }
        index ++;
    }
    if(index == used)
        index = -1;
    return index;
}
size_t table::hash_author(string target_author)
{
    size_t index = 0;
    bool found = false;
    while((index < used) && !found)
    {
        if(data[index].author == target_author)
        {
            found = true;
            continue;
        }
        index ++;
    }
    if(index == used)
        index = -1;
    return index;
}
size_t table::search_isbn(string target_isbn)
{
    return hash_isbn(target_isbn)+1;
}
size_t table::search_title(string target_title)
{
    return hash_isbn(target_title)+1;
}
size_t table::search_author(string target_author)
{
    return hash_isbn(target_author)+1;
}
void table::print(size_t index)
{
    cout.flush();
    cout<<data[index].title<<endl;
    cout<<"Title: "<<data[index].title<<endl;
    cout<<"ISBN: "<<data[index].isbn<<endl;
    cout<<"Author: "<<data[index].author<<endl;
    cout<<"Publication data: "<<data[index].date<<endl;
    cout<<endl;
}
//nonmember functions
void add(table t)
{
    book entry;
    cout<<"Enter author name:"<<endl;
    cin>>entry.author;
    cout<<endl;
    cout<<"Enter book name:"<<endl;
    cin>>entry.title;
    cout<<endl;
    cout<<"Enter ISBN:"<<endl;
    cin>>entry.isbn;
    cout<<endl;
    cout<<"Enter the publication data:"<<endl;
    cin>>entry.date;
    cout<<endl;

    if(t.search_isbn(entry.isbn))
        cout<<"==== The book already exists !!! ==="<<endl;///////////////////////输入重复时,此处并未执行
    else
        t.insert(entry);
}
void search(table t)
{
    string option;
    cout<<"Seach by ISBN (I), book title (T), or author (A). Choice: ";
    cin>>option;
    cout<<endl;

    while((option != "I") && (option != "T") && (option != "A"))
    {
        cout<<"Not an accessible option, try again:"<<endl
            <<"Seach by ISBN (I), book title (T), or author (A). Choice: ";
        cin>>option;
        cout<<endl;
    }

    size_t index;

    if(option == "I")
    {
        string target_isbn;
        cout<<"Enter ISBN: ";
        cin>>target_isbn;
        cout<<endl;
        index = t.search_isbn(target_isbn);
    }
    if(option == "T")
    {
        string target_title;
        cout<<"Enter Title: ";
        cin>>target_title;
        cout<<endl;
        index = t.search_isbn(target_title);
    }
    if(option == "A")
    {
        string target_author;
        cout<<"Enter Author: ";
        cin>>target_author;
        cout<<endl;
        index = t.search_isbn(target_author);
    }


    if(index+1)
    {
        cout<<"Book found"<<endl;
        t.print(index);
    }
    else
        cout<<"==== The book does not exist !!! ==="<<endl;
}

int main()
{
    table hash_table;
    string action;
    bool done = false;
    while(!done)
    {
        cout<<"Add a new book (A), search (S), or end program (E)? ";
        cin>>action;
        cout<<endl;
        while((action != "A") && (action != "S") && (action != "E"))
        {
            cout<<"Not an accessible option, try again:"<<endl
                <<"Add a new book (A), search (S), or end program (E)? ";
            cin>>action;
            cout<<endl;
        }

        if(action == "A")
            add(hash_table);
        if(action == "S")
            search(hash_table);
        if(action == "E")
        {
            done = true;
            continue;
        }
    }

    hash_table.print(0); // this code just try to test my problem in a simple way


    system("pause");
    return 0;
}
4

1 回答 1

2

问题不在于print功能或与之相关的东西。在函数中addsearch也是),您按值传递table对象。只是通过参考。

void add(table& t)
//            ^
于 2013-05-08T14:14:28.250 回答