各位好 我是一名delphi爱好者但是今天朋友问了我一个c++的问题,我实在是不知道怎么实现了,希望各位帮忙给看看 题目如下:定义 一个二维字符窜数组,输入若干个字符窜,按升序排列后输出。设计一个通用排序函数,输入参数为字符穿数组和要排序的字符穿的个数

解决方案 »

  1.   

    写了一个简单的,肯定问题多多,但可以参考一下了。
    输入字符串时按Ctrl-Z结束。#include <iostream>
    using namespace std;#include <conio.h>const int MAX_STRING_NUM = 32;
    const int MAX_STRING_LEN = 128;void sort(char *sarr[32] , int n);int main()
    {
        char **strarray = new char*[MAX_STRING_NUM];
        char buf[MAX_STRING_LEN];
        
        cout << "Please input your strings(" << MAX_STRING_NUM
         << " at most), Ctrl-Z to stop." << endl;
        int count = 0;
        for(int i = 0 ; i < MAX_STRING_NUM ; i++)
        {
            cout << "String No." << i << ": ";
            if(cin >> buf)
            {
                count++;
                strarray[i] = new char[strlen(buf) + 1];
                strcpy(strarray[i] , buf);            
            }
            else
                break;        
        }
        
        sort(strarray , count);
        cout << endl << "Order after sort:" << endl;
        for(int i = 0 ; i < count ; i++)
            cout << "No." << i << ": " << strarray[i] << endl;
            
        cout << endl << "Press any key to exit..." << endl;
        getch();
        return 0;    
    }//Use bubble sort.
    void sort(char *sarr[32] , int n)
    {
        char buf[MAX_STRING_LEN];
        for(int i = 0 ; i < n ; i++)
        {
            bool bChanged = false;
            for(int j = 0 ; j < n - i - 1 ; j++)
            {
                if(strcmp(sarr[j] , sarr[j + 1]) > 0)
                {
                    strcpy(buf , sarr[j]);
                    strcpy(sarr[j] , sarr[j + 1]);
                    strcpy(sarr[j + 1] , buf);
                    bChanged = true;
                }
            }
            if(!bChanged)
                break;
        }
    }