问题描述
我的教授要求我们制作一个程序,它将接受用户的输入并继续阅读直到输入结束。只有这样,程序才能输出用户键入的内容。
输入内容应基于视频标题,URL,对视频的评论,时长(以分钟为单位)和评分(以*为单位)。
例如:
United Break guitars,https://www.youtube.com/watch?v+5YGc4zOqozo,一个人让一家大公司听的好例子,4.5,***,空格与制表符,https://www.youtube.com/watch?v=SsoOG6ZeyUl,自己决定:空格还是制表符?, 2.83,****
在输入任何视频描述之前,用户需要指定三种选择if isset
的排序方法。我已经完成了教授要求的大多数代码和排序方法(冒泡排序),但是当我要求程序按标题排序(这是字符串的三个选项中的唯一一个)时,它无法正确输出。
这是我的代码:
rating,Length,or title
video.cpp:
#include <iostream>
#include <stdlib.h>
#include <cstring>
using namespace std;
#include "video.h"
int main()
{
string user,url,comment,title;
int rating;
double length;
int i = 0,last = 0;
Video *videoObj[100];
// specifies how the videos should be sorted
cin >> user;
cin.ignore();
while (getline(cin,title) ) {
getline(cin,url);
getline(cin,comment);
cin >> length;
cin >> rating;
cin.ignore();
videoObj[i] = new Video(title,length,rating);
i++;
last++;
}
//------------------------------------------------------------------------
//--------------- Sorts the list based on rating (*) ---------------------
//------------------------------------------------------------------------
if(user=="rating"){
for(int i = 0; i < last - 1; i++){
for(int j = 0; j< last - i -1; j++){
if(videoObj[j +1]->rating(videoObj[j])){
swap(videoObj[j],videoObj[j+1]);
}
}
}
}
//------------------------------------------------------------------------
//--------------- Sorts the list based on length -------------------------
//------------------------------------------------------------------------
if(user=="length"){
for(int i = 0; i < last - 1; i++){
for(int j = 0; j< last - i -1; j++){
if(videoObj[j +1]->Length(videoObj[j])){
swap(videoObj[j],videoObj[j+1]);
}
}
}
}
//------------------------------------------------------------------------
//--------------- Sorts the list based on title --------------------------
//------------------------------------------------------------------------
if(user=="title"){
for(int i = 0; i < last - 1; i++){
for(int j = 0; j< last - i -1; j++){
if(videoObj[j +1]->Title(videoObj[j])){
swap(videoObj[j],videoObj[j+1]);
}
}
}
}
for(int i= 0; i < last; i++){
videoObj[i]->print();
}
//delete[] videoObj;
return 0;
}
video.h:
#include <iostream>
#include <algorithm>
using namespace std;
#include "video.h"
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)
{
m_title = title;
m_link = link;
m_comment = comment;
m_length = length;
m_rating = rating;
}
bool Video::rating(Video *other)
{
if(m_rating > other-> m_rating){
return true;
}
else
{
return false;
}
}
bool Video::Length(Video *other2)
{
if(m_length > other2-> m_length){
return true;
}
else
{
return false;
}
}
bool Video::Title(Video *other3)
{
if(m_length > other3-> m_length){
return true;
}
else
{
return false;
}
}
void Video::print(){
string star;
switch(rating){
case 1:
star = "*";
break;
case 2:
star = "**";
break;
case 3:
star = "***";
break;
case 4:
star = "****";
break;
case 5:
star = "*****";
break;
}
cout << title << "," << link << "," << comment << "," << length << "," << star << endl;
}
我不确定要对#ifndef VIDEO_H
#define VIDEO_H
using namespace std;
class Video {
public:
Video(string video_title,int video_number);
void print();
bool rating(Video *other);
bool Length(Video *other2);
bool Title(Video *other3);
private:
string m_title;
string m_link;
string m_comment;
double m_length;
int m_rating;
string title;
string link;
string comment;
double length;
int rating;
};
#endif
进行正确操作需要做什么。我当时在考虑按字符串比较,但是同样,不确定从哪里开始。
解决方法
这是错误的,可能只是拼写错误
bool Video::Title(Video *other3)
{
if(m_length > other3-> m_length){
return true;
}
else
{
return false;
}
}
应该是m_title
而不是m_length
bool Video::Title(Video *other3)
{
if(m_title > other3-> m_title){
return true;
}
else
{
return false;
}
}
此代码也可以简化,上面的代码可以写成一行
bool Video::Title(Video *other3)
{
return m_title > other3-> m_title;
}
if (xxx) return true; else return false;
与return xxx;
完全相同。初学者通常不会以这种方式用布尔值计算。