0
我需要的書名排序的C++代碼:冒泡排序
- 顯示書名。
- 按字母順序顯示書名。
我能夠做第一次,但我在顯示書籍的字母順序時遇到困難。
這裏是我的代碼:
#include"stdafx.h"
#include <iostream>
#include <string>
#include <conio.h>
#include <algorithm>
#include <cstring>
#include <map>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int MAX = 5;
void BubbleSort(string books, int max);
int main(int argc, const char * argv[])
{
string books[MAX];
//inputs
std::cout << "Enter Book titles: \n";
for (int i = 0; i < MAX; i++)
{
std::cout << "Book [" << i << "]: ";
//cin >> books[i];
getline(std::cin, books[i]);
}
// print the titles stored in books[i] variable
cout << "Book Titles Entered \n\n";
for (int i = 0; i < MAX; i++)
{
std::cout << "Book No." << i << ": ";
cout << books[i] << endl;
}
// print the titles after sort
cout << "Book Titles In Sort Ascending \n\n";
for (int i = 0; i < MAX; ++i)
cout << books[i] << "\n";
}
void BubbleSort(string books, int size)
{
int result;
for (int pass = 0; pass < size - 1; ++pass)
{
for (int i = 0; i < MAX - 1 - pass; ++i)
{
result = string (books[i], books[i + 1]);
if (result > 0) {
swap(books[i], books[i + 1]);
}
}
}
system("pause");
}
字符串比較應該是:'result = string(books [i],books [i + 1]);'? – xander