SOL9 Sample: QuickSort

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * QuickSort.cpp 
 * Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL++2000
// 2000.02.18

#include <sol\ApplicationView.h>
#include <sol\Stdio.h>
#include <sol\QuickSorter.h>
#include <sol\ScrolledText.h>
#include <sol\String.h>
#include <sol/StringConverter.h>
#include <sol/StringT.h>
#include "resource.h"

namespace SOL {

class AppView :public ApplicationView {
private:
  ScrolledText text;

public:
  /**
   * Constructor
   */
  AppView(Application& applet, const TCHAR* label, Args& args)
        :ApplicationView(applet, label, args)
  {
    Args ar;
    text.create(this, NULL, ar);
    add(text);

    // Add an eventhandler start to handle a user-defined event.
    addEventHandler(WM_USER+300, this, 
      (Handler)&AppView::start, NULL);

    addCallback(XmNmenuCallback, IDM_EXIT, this, 
      (Callback)&AppView::exit, NULL);

    // Post the user-defined event.
    post(WM_USER+300, 0, 0);
  }

private:
  long start(Event& event) 
  {
    static  int integers[] = {
      1, 200, 3, 11, 0,
      500, 20, 600, 30
    };

    static wchar_t* strings[] = {
      L"orange", L"banana",  L"apple", L"melon", L"grape",
    };

    static char* cstrings[] = {
      "USA", "Russia", "China", "Taiwan", "England",
      "Japan", "France", "Germany",
    };

    QuickSorter qsorter1;
    // Sort an array of integers.
    qsorter1.sort(integers, XtNumber(integers));
    qsorter1.wait();

    // Sort an array of TCHAR*.
    QuickSorter  qsorter2;
    qsorter2.sort(strings,  XtNumber(strings));
    qsorter2.wait();

    static Object* objects[10];
    int num = 0;
    objects[num++] = new String("dog");
    objects[num++] = new String("sheep");
    objects[num++] = new String("pig");
    objects[num++] = new String("cat");
    objects[num++] = new String("hen");
    objects[num++] = new String("ox");

    // Sort an array of instances of String.
    QuickSorter qsorter3;
    qsorter3.sort(objects, num);
    qsorter3.wait();

    // Sort an array of instances of String.
    QuickSorter qsorter4;
    qsorter4.sort(cstrings, XtNumber(cstrings));
    qsorter4.wait();

    int i = 0;
    for (i = 0; i<XtNumber(integers); i++) {
        text.printf(_T("%d \r\n"), integers[i]);
    }

    text.printf(_T("--------------------\r\n"));

    for (i = 0; i<XtNumber(strings); i++) {
      text.printf(_T("%s \r\n"), strings[i]);
    }
  
    text.printf(_T("--------------------\r\n"));

    for (i = 0; i<num; i++) {
      String* string = (String*)objects[i];
      text.printf(_T("%s \r\n"), (const TCHAR*)(*string));
    }

    text.printf(_T("--------------------\r\n"));

    for (i = 0; i<XtNumber(cstrings); i++) {
      StringConverter converter;
      StringT<TCHAR> tstring;
      converter.convert(cstrings[i], tstring);
      text.printf(_T("%s \r\n"), (const TCHAR*)tstring);
    }

    return 0L;
  }
};

}

//
void Main(int argc, TCHAR** argv)
{
  ModuleFileName module(argv[0]);
  const TCHAR* name = module.getFileName();
  try {
    Application applet(name, argc, argv);

    Args  args;
    AppView appView(applet, name, args);
    appView.realize(); 

    applet.run();

  } catch (Exception& ex) {
    caught(ex);
  } catch (...) {
    caught(UnknownException());
  }
}

Last modified: 1 Feb 2017

Copyright (c) 2017 Antillia.com ALL RIGHTS RESERVED.