我不知道爲什麼,但此程序奇怪地行爲並返回所有插入的主函數的2或3倍,我不知道是什麼問題。除此之外,我不知道如何在字符串中使用排序選項。我想寫一個函數來完成書的名字。我需要將這個函數(排序函數)添加到主函數中。任何幫助將不勝感激。這是一個程序,它可以獲得書名,作者和翻譯者以及ISBN和主題,並進行搜索或報告。意外地返回主函數和排序字符串函數
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct Library
{
string Book_Name;
string Author;
string Translator;
string ISBN;
string Subject;
struct Library *fl, *bl;
}*start, *cur, *p;
void insert()
{
p = new struct Library;
p->fl = NULL;
p->bl = cur;
cur->fl = p;
cur = p;
cout << "Enter the specified informations for Books" << endl;
cout << endl;
cout << "The Name of the Book " << endl;
getline(cin, p->Book_Name);
cin.ignore();
cout << "Author" << endl;
getline(cin, p->Author);
cin.ignore();
cout << "The Name of the Translator " << endl;
getline(cin, p->Translator);
cin.ignore();
cout << "International Standard Book Number (ISBN) " << endl;
getline(cin, p->ISBN);
cin.ignore();
cout << "Enter the Subject of the Book " << endl;
getline(cin, p->Subject);
cin.ignore();
}
void report_number_1()
{
cout << "The list of all Books in Library are as below" << endl;
for (p = start->fl; p != NULL; p = p->fl)
{
cout << "Book Name " << p->Book_Name << endl;
cout << "Author Name " << p->Author << endl;
cout << "Translator Name " << p->Translator << endl;
cout << "ISBN of the Book " << p->ISBN << endl;
cout << "Book Subject " << p->Subject << endl;
}
}
void delete_number_1()
{
struct Library *ap, *bp;
char is[15];
int sw = 0;
cout << "Enter ISBN" << endl;
gets_s(is);
for (p = start->fl; p != NULL&&!sw; p = p->fl)
{
if (p->ISBN == is)
{
sw = 1;
ap = p->fl;
bp = p->bl;
bp->fl = ap;
ap->bl = bp;
p->fl = NULL;
p->bl = NULL;
}
cout << "Book Name " << p->Book_Name << endl;
cout << "Author Name " << p->Author << endl;
cout << "Translator Name " << p->Translator << endl;
cout << "ISBN of the Book " << p->ISBN << endl;
cout << "Book Subject " << p->Subject << endl;
}
}
void report_number_2()
{
string title;
int sw = 0;
cout << "Enter Book's Title " << endl;
getline(cin,title);
for (p = start->fl; p != NULL; p = p->fl)
{
if (p->Subject == title)
{
sw = 1;
cout << "Book Name " << p->Book_Name << endl;
cout << "Author Name " << p->Author << endl;
cout << "Translator Name " << p->Translator << endl;
cout << "ISBN of the Book " << p->ISBN << endl;
cout << "Book Subject " << p->Subject << endl;
}
if (!sw)
{
cout << "ERROR 404 - NOT FOUND" << endl;
}
}
}
void delete_number_2()
{
struct Library *ap, *bp;
string name;
int sw = 0;
cout << "Enter Author's Name or the Translator's Name so that search begins and delete" << endl;
getline(cin,name);
for (p = start->fl; p != NULL; p->fl = p)
{
if ((p->Author == name) || (p->Translator == name))
{
sw = 1;
ap = p->fl;
bp = p->bl;
bp->fl = ap;
ap->bl = bp;
p->fl = NULL;
p->bl = NULL;
}
cout << "Book Name " << p->Book_Name << endl;
cout << "Author Name " << p->Author << endl;
cout << "Translator Name " << p->Translator << endl;
cout << "ISBN of the Book " << p->ISBN << endl;
cout << "Book Subject " << p->Subject << endl;
delete(p);
}
if (!sw)
{
cout << "ERROR 404 - NOT FOUND" << endl;
}
}
void main()
{
char ch;
start = new struct Library;
start->fl = NULL;
start->bl = NULL;
cur = start;
do
{
cout << "Enter I/i for Insert " << endl;
cout << "Enter R/r for Report that is Sorted by Name of the Book " << endl;
cout << "Enter S/s for Search by ISBN and delete the Specific Book " << endl;
cout << "Enter U/u for search " << endl;
cout << "Enter W/w to delete the Specific Book" << endl;
cout << "Enter X/x for Terminating the Program " << endl;
cin >> ch;
switch (ch)
{
case 'I':
case 'i':
insert();
break;
case'R':
case'r':
report_number_1();
case 'S':
case 's':
delete_number_1();
break;
case 'U':
case 'u':
report_number_2();
break;
case 'W':
case 'w':
delete_number_2();
break;
}
} while (ch != 'X' && ch != 'x');
}
首先請使用調試器,詢問這裏之前! –
您應該給我們一些示例輸入和您期望的輸出。這是一項家庭作業,對嗎? – Rook
「以2/3次主函數返回我插入的所有內容」 - 這非常含糊。如果您想使用標準庫對書籍進行排序,或者想要自行排序,也是不明確的。請考慮發佈您的輸入和輸出。如果您需要編寫自己的排序,請考慮其他資源(問題說明「編寫我的排序算法」將被關閉),或嘗試編寫算法並詢問是否卡住。投票結束。 – utnapistim