Archive

Posts Tagged ‘Programming’

C++: Qt: "qBaseConverter"

August 17, 2009 Leave a comment

Today I implemented a Base Converter in C++ using Qt and after about 15 Minutes first successes have become apparent. You can convert from any Base you want to to any Base you want to (you only have to fix the “Number of Bases”-Variable if the default 30 Bases aren’t enough for you):

qBaseConverter

The basic Function (Slot) is – guess – “void convert()” that gets a Signal whenever a ComboBox or the Text at the LineEdit gets changed:

bool BaseConverter::getInputNumber(long long &number, int base) {
    bool ok;
    // Get Input-Number
    number = ui->lineEdit_input->text().toULongLong(&ok, base);
    // Return if Conversion ok
    return ok;
}
void BaseConverter::convert() {
    // Base to convert from
    int baseFrom = ui->comboBox_choiceFrom->currentIndex() + 2;
    // Base to convert to
    int baseTo = ui->comboBox_choiceTo->currentIndex() + 2;

    long long number;
    // If there is a valid Input
    if(getInputNumber(number, baseFrom)) {
        // Display the converted Number
        ui->lineEdit_output->setText(QString::number(number, baseTo));
    }
}

Lisp: Implementation Competition between Programming Languages

December 8, 2008 3 comments

Today I have found an ‘Implementation Competition between Programming Languages’ at fefe´s Blog.

A simple Problem has been implemented and tested in different Languages [ReadMe]:

  1. Read in from stdio
  2. Split into Words
  3. Count the Words
  4. Sort Words
  5. Print sorted Words

I saw that it wasn´t implemented in LISP yet so I asked Christopher if we don´t want to do that!

So here is the Program in LISP:

; Splits the given 'string' at 'split-string'-Sequence
(defun string-split (split-string string)
(loop with l = (length split-string)
   for n = 0 then (+ pos l)
   for pos = (search split-string string :start2 n)
   if pos collect (subseq string n pos)
   else collect (subseq string n)
   while pos))

; If 'word' in the Hashtable 'ht', increment + 1
; else create new Key-/Value-Pair with Value = 1
(defun insert-key-value (word ht)
  (when (and (stringp word)
	     (string/= word "")
	     (string/= word " ")
	     (not (null word))))   ; no blank words
    (if (gethash word ht)
	(incf (gethash word ht))
	(setf (gethash word ht) 1)))

; Gets a Hashtable 'ht' and a Sort-Function 'sort-fn'
; Optional Parameter: 'by' can be :keys or :values
; Returns a sorted Hashtable of key-value-Pairs
(defun sort-hash-table (ht sort-fn &optional &key (by :keys))
  (let ((sorted-entries nil))
    (maphash #'(lambda (k v) (push (cons k v) sorted-entries)) ht)
    (let ((sort-key #'car))
      (if (equal by :values)
	  (setf sort-key #'cdr))
      (sort sorted-entries sort-fn :key sort-key))))

; Prints a given Hash-Entry = Key-Value-Pair formatted
(defun print-hash-entry (key-value-pair)
  (format t "key: ~t~t~t~S  -  value: ~t~t~t~S~&" (cdr key-value-pair) (car key-value-pair)))

; Counts the Words in a Line and puts them into a Hashtable
;   - Gets a line 'line-in' and a Hashtable
;   - Splits the String into Words
;   - Puts the Words into a Hashmap
(defun count-words (line-in ht)
  (mapcar #'(lambda (word)
	      (insert-key-value word ht))
	  (string-split " " line-in)))

; Creates a Hashtable 'ht'
;   - Reads in Lines fom the Prompt
;   - Calls Count-Words
;   - Sorts and prints the Hash-Table
(let ((ht (make-hash-table :test 'equal)))
  (do ((line-in t))
      ((null line-in) nil)
    (setf line-in (read-line *query-io* nil nil nil))
    (count-words line-in ht))
  (mapcar #'print-hash-entry (sort-hash-table ht #'> :by :values)))

Here the other Implementations and here the Timings.

Follow

Get every new post delivered to your Inbox.