0

chars入力からの配列と、最初の配列の対応するポインタを含むもう1つの配列がありcharsます。この部分はうまくいきました。

しかし、配列(ポインターの配列)をバブルソートしたいchar**ので、元の配列は変更されませんが、何か問題が発生します(テキストはソートされません)。

EDIT: Please discuss only the sorting algorithm


 char tab[200];//array of chars 
 char** t = new char*[tabLength];
 //SOMETHING
....

....
int n = tabLength;//length of tab(length of the word it contains)
//TILL HERE EVERYTHING IS FINE -----------------------------------
         //bubble sorting below
do{
    for(int i = 0; i < n -1; i++){
        if(*t[i] > *t[i+1]){
            char* x = t[i];
            t[i] = t[i+1];
            t[i+1] = x;
        }
        n--;
    }
}while(n>1);

cout<<"sorted input";
for(int i = 0; i < tabLength; i++)
    cout<<t[i];
    cout<<endl;

cout<<"original"<<tab<<endl;
4

2 に答える 2

1

ポインターが指す値を必ず出力してください。

for(int i = 0; i < tabLength; i++)
  cout << *t[i];
于 2013-03-16T17:35:08.403 に答える
0

標準ライブラリで既に自由に使用できる機能を使用するだけです。

#include <iostream>
#include <string>
#include <algorithm>

int main()
{
    std::string original;
    std::getline(std::cin, original);

    std::cout << '|' << original << "|\n";

    std::string sorted = original;
    std::sort(std::begin(sorted), std::end(sorted));

    std::cout << "Sorted  : " << sorted << '\n';
    std::cout << "Original: " << original << '\n';
}

テスト走行:

|こんにちは、今日の調子はどうですか?|
並べ替え: ,?Haadddeeghilllnoooooorrtuwwyy
原文:ハローワールド、今日の調子はどう?
于 2013-03-16T17:52:34.340 に答える