【发布时间】:2020-04-05 22:22:46
【问题描述】:
我正在编写一个 c++ 程序,它应该从 txt 文件中获取歌曲列表,并能够随机播放、排序和搜索列表中的歌曲。它使用对象向量来存储列表以对歌曲和艺术家进行分类。我想出了如何正确排序和洗牌;但是我应该使用二进制搜索,这给我带来了困难。
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstdlib>
#include <time.h>
#include <stdlib.h>
#include <random>
#include <bits/stdc++.h>
#include <algorithm>
#include "song.h"
using namespace std;
// given to you
void processFile(vector<Song> &playlist);
// you should create
void shuffle(vector<Song> &playlist);
void Sort(vector<Song> &playlist);
void displayPlaylist(vector<Song> playlist);
int binarySearch(vector<Song> &playlist, string songTitle);
int main()
{
vector<Song> playlist;
// sets up playlist
processFile(playlist);
cout << "\nInitial playlist: " << endl;
//displayPlaylist(playlist);
displayPlaylist(playlist);
cout << "Welcome to the playlist display manager." << endl << endl;
while(1)
{
int option;
cout << "0. Exit" << endl;
cout << "1. Sort Playlist" << endl;
cout << "2. Shuffle Playlist" << endl;
cout << "3. Search Playlist" << endl;
cout << "Which option would you like" << endl;
cin >> option;
if(option == 0)
{
break;
}
else if(option == 1)
{
Sort(playlist);
displayPlaylist(playlist);
}
else if(option == 2)
{
}
else if(option == 3)
{
string title;
cout << "what is the name of the song?" << endl;
getline(cin,title);
int songIndex = binarySearch(playlist, title);
if(songIndex != -1)
{
cout << playlist[songIndex].getTitle() << " - " << playlist[songIndex].getArtist() << endl;
}
else
{
cout << "Couldn't find the song, sorry! :'(" << endl;
}
}
else
{
cout << "invalid response...try again" << endl;
}
}
return 0;
}
void processFile(vector<Song> &playlist)
{
ifstream infile;
string line;
infile.open("songs.txt");
if(infile.is_open())
{
cout << "Successful songs opening." << endl;
}
else
{
cout << "Couldn't locate file. Program closing." << endl;
exit(EXIT_FAILURE);
}
while(getline(infile, line))
{
// first line --> song
// second line --> artist
if(line != "")
{
string song, artist;
song = line;
getline(infile, artist);
Song temp(song, artist);
playlist.push_back(temp);
}
}
return;
}
int binarySearch(vector<Song> &playlist, string songTitle)
{
Sort(playlist);
int size;
size = playlist.size();
int low = 0, high = size - 1, mid;
while(high >= low)
{
mid = (high + low) / 2;
if(playlist[mid].getTitle() < songTitle)
{
low = mid + 1;
}
else if(playlist[mid].getTitle() > songTitle)
{
high = mid - 1;
}
else
{
return mid;
}
}
return -1;
}
//sort playlist using bubble sort
void Sort(vector<Song>& playlist)
{
int size;
size = playlist.size();
//iter
for(int i= 0; i < size - 1; i++)
{
int smallIndex = i;
for(int j = i + 1; j < size; j++)
{
//if first is less than small index then it is replaced
if(playlist[j].getTitle() < playlist[smallIndex].getTitle())
{
smallIndex = j;
}
}
string song, artist;
Song temp(song, artist);
temp = playlist[i];
playlist[i] = playlist[smallIndex];
playlist[smallIndex] = temp;
}
}
//display songs
void displayPlaylist(vector<Song> playlist)
{
for(int i = 0; i < playlist.size(); i++)
{
cout << playlist[i].getTitle() << " - " << playlist[i].getArtist() << endl;
}
}
这是我的主要外观。每次我尝试选择一首歌曲来查找时,它只是结束程序,甚至不显示消息。我对编程很陌生,所以任何建议都将不胜感激。
【问题讨论】:
-
使用二分搜索,这给我带来了困难什么样的困难?
-
我的意思是说我不能让它做我想做的事情,即验证我输入的歌曲是否在播放列表中。
-
不相关:看起来你被cargo cult 所吸引,并且在不知道它做什么的情况下使用代码。
#include <bits/stdc++.h>几乎包含了整个 C++ 标准库,这是您不应该做的事情,这使得其他大多数包含毫无意义。 Here's more reading related to bits/stdc++.h 这样您就可以了解它的作用以及为什么不应该使用它.. -
如何判断歌曲是否被找到?你永远不会对
binarySearch返回的值做任何事情。 -
我建议将程序切割成一个行为不端的小程序,该程序的行为方式与完整程序完全一样。这消除了所有的歧义,并且减少了 bug 周围的噪音通常会在没有任何进一步帮助的情况下向您揭示问题。使用minimal reproducible example 作为灵感。
标签: c++ pass-by-reference binary-search