【发布时间】:2021-01-19 05:28:27
【问题描述】:
我有一个程序可以输入 5 个参数。输入是视频标题、网址、评论、长度和评分。然后根据标题对它们进行排序。用户需要指定 insert(输入视频信息)、lookup(按标题查找视频并仅打印该视频及其相关信息)、或 print(只需简单地打印所有内容)。
例如
输入:
insert
Arthur Benjamin: Lightning calculation and other "Mathemagic"
http://www.youtube.com/watch?v=M4vqr3_ROIk
Hard to believe.
15.25
4
lookup
Arthur Benjamin: Lightning calculation and other "Mathemagic"
输出:
Arthur Benjamin: Lightning calculation and other "Mathemagic" , http://www.youtube.com/watch?v=M4vqr3_ROIk, Hard to believe., 15.25, 4
我的问题是处理 lookup in main
if(user == "lookup")
{
getline(cin, title);
if(vlistObj -> lookup(videoObj))
{
vlistObj->print();
}
}
以及在我的链接列表中查找
bool Vlist::lookup(Video *other)
{
Node *node = m_head;
return node->m_next -> m_video->GetTitle() == other-> GetTitle();
}
老实说,我对如何让 lookup 搜索特定标题(假设已经给出了很多视频标题/信息)并只打印我要求的内容(假设它在列表中)感到非常迷茫.
完整代码如下:
#include <iostream>
#include <stdlib.h>
#include <cstring>
using namespace std;
class Video {
public:
Video(string video_title, string video_link, string video_comment, double video_length, int video_number);
void print();
const string& GetTitle() const { return title; }
private:
std::string title;
string link;
string comment;
double length;
int rating;
};
Video::Video(string video_title, string video_link, string video_comment, double video_length, int video_number)
: title(video_title), link(video_link), comment(video_comment), length(video_length), rating(video_number)
{
}
void Video::print(){
cout << title << ", " << link << ", " << comment << ", " << length << ", " << rating << endl;
}
class Vlist {
public:
Vlist() {m_head = nullptr; }
bool lookup(Video *other);
void Insert(Video *video);
void print();
private:
class Node {
public:
Node(Video *video, Node *next) {m_video = video; m_next = next; }
Video *m_video;
Node *m_next;
};
Node *m_head;
};
void Vlist::Insert(Video* video)
{
if (m_head == NULL || m_head->m_video -> GetTitle() > video->GetTitle())
{
m_head = new Node(video, m_head);
}
else
{
Node *node = m_head;
while (node->m_next != NULL && node->m_next -> m_video->GetTitle() < video->GetTitle())
{
node = node->m_next;
}
node->m_next = new Node(video, node->m_next);
}
}
bool Vlist::lookup(Video *other)
{
Node *node = m_head;
return node->m_next -> m_video->GetTitle() == other-> GetTitle();
}
void Vlist::print()
{
Video *video;
Node *node = m_head;
while(node != NULL)
{
node -> m_video-> Video::print();
node = node->m_next;
}
}
int main()
{
string sort_type, url, comment, title, user;
int rating;
double length;
int initial = 0, last = 0, number;
Vlist *vlistObj= new Vlist();
Video *videoObj;
while (getline(cin,user)) {
if(user == "insert")
{
getline(cin,title);
getline(cin, url);
getline(cin, comment);
cin >> length;
cin >> rating;
cin.ignore();
videoObj = new Video(title,url, comment, length, rating);
vlistObj->Insert(videoObj);
}
if(user == "lookup")
{
getline(cin, title);
if(vlistObj -> lookup(videoObj))
{
vlistObj->print();
}
}
if(user == "print")
{
vlistObj->print();
}
}
}
我还想指出,我收到了分段错误。但我确实知道这是因为我在查找中的代码。如果我不键入 lookup
,程序会正确运行并输出
【问题讨论】:
-
if (user == "lookup") ...为什么是变量user而不是function或action之类的?这是我对你想做的事情的第一个心理障碍。您可以将其分为两个问题:1. 您希望单词“lookup”触发查找功能,以及 2.您希望查找功能实际查找项目。如果其中一个已经有效,请将其从您的问题中删除。它只会使水变得浑浊。 -
不要给出“完整代码”。给minimal reproducible example。提供其中之一有两件事:它使您的问题的读者更容易了解正在发生的事情,并且通常制作minimal reproducible example 的做法会使您更好地理解问题。当人们制作minimal reproducible examples 时,有一半的时间是他们解决了自己的问题。
-
进入你的调试器。告诉我当你进入
lookup函数时other的值是多少。 -
@JohnFilleau 哦,好吧,我不知道人们是否会在不包括所有内容的情况下理解我的问题。注意到
-
@JohnFilleau 老实说,我对编程很陌生。如何进入调试器进行检查?我的教授说我们可以使用随机指针变量来检查 Title 是否会返回 true。这就是
other所做的一切
标签: c++ class linked-list