Thursday, July 3, 2008

Reverse a Stack in Place

/*
Reverse a stack in place
*/
//#include "stdafx.h"
#include "iostream"
#include "stack"
using namespace std;

void Push(stack &myStack , int a)
{
int m = (int)myStack.top();
myStack.pop();
if (myStack.size() != 0)
Push(myStack , a);
else {
myStack.push(a);}

myStack.push(m);

}

void reverse(stack &myStack)
{
int m = (int)myStack.top();
myStack.pop();
if (myStack.size() != 1)
reverse(myStack);
Push(myStack , m);
}

int main()
{
stack S;
//add the stack elements
for(int i = 1; i < 6; i++)
S.push(i);
//display the stack elements
cout << "Original Stack" << endl;
while (!S.empty()) {
cout << S.top() << '\t';
S.pop(); // remove top element
}
cout << endl;


//add the stack elements
for(int i = 1; i < 6; i++)
S.push(i);

reverse(S); //reverse the stack

//display the reversed stack
cout << "Reversed Stack" << endl;
while (!S.empty()) {
cout << S.top() << '\t';
S.pop(); // remove top element
}
cout << endl;
system("PAUSE");
return 0;
}

Wednesday, July 2, 2008

6/49 lottery number generator C++ code

When I visited Vancouver Canada in 2008, I played this 6/49 lottery. A friend of mine there asked me to write this code. It generates 6 random numbers range over [1,49].


/*
6/49 lottery number generator
*/

#include "iostream"
#include "vector"
#include
using namespace std;
int main ()
{
int iRndNum;
vector Numbers;
int i;

for(i=1; i <= 49; i++)
Numbers.push_back(i);


/* initialize random seed: */
srand (time(NULL) + rand());


cout << " The 6/49 numbers are" << endl;
for(i =1; i <= 6; i++){
/* generate a random number: */
iRndNum = rand() % (49 - i + 1) ;

cout << Numbers[iRndNum] << "\t";
Numbers.erase(Numbers.begin() + iRndNum, Numbers.begin() + iRndNum + 1);
}
cout << endl;

system("PAUSE");
return 0;
}

Thursday, February 28, 2008

Word Puzzle ACM problem

ACM Oceania - South Pacific - 2001/2002

In certain entertainment magazines there is a type of crossword puzzle where, instead of clues for words, the cells contain positive integers. There is a hidden one-to-one correspondence with numbers and letters, and the goal of the puzzle is to assign alphabet letters to these positive numbers so that sequences of two or more letters (across the rows and down the columns) form valid words. Your task is to write a program to help solve this type of puzzles.

Input

The input will consist of a series of games. Each game will have a valid dictionary of words (preceded by a size) that can be used, followed by a puzzle grid (preceded by r and c, the number of rows and columns). Each word will be at least 2 and at most 10 characters in length from the set `a'-`z'. The dictionary size will be at most 1000 words; the dictionary entries are unique but not necessarily sorted. Each puzzle grid consists of c columns, 1 c 10, and r rows, 1 r 10, of cell numbers in the range 0-26. A positive cell number denotes a letter and a cell number of `0' denotes a ` ' (blank character). The numbers form a continuous range, thus one can expect numbers such as 0, 1, 2, 3, 4, 5 but not 0, 1, 12, 13, 24, 25. The series of games is terminated by a ``game'' with dictionary size `0' and should not be processed.

Output

The output should consist of the guaranteed uniquely solved puzzle for each game, where the cell numbers are replaced with characters `a'-`z', ` '. A blank line should separate output solutions.


Sample Input

3

max

min

nix

3 4

1 2 3 0

4 0 0 0

5 4 3 0

12

boon

boonbar

boony

foobar

foorag

goon

goony

rannoo

ranoon

toon

toonbar

toony

6 6

1 2 2 3 4 5

2 0 0 2 0 0

2 0 0 2 0 6

5 4 7 7 2 2

4 0 0 8 0 2

9 0 0 0 0 7

0

Sample Output

max

i

nix

foobar

o o

o o t

rannoo

a y o

g n

Solution

Please refer the files def.h and main.cpp for solution. matrix.txt, words.txt and matrix1.txt, words1.txt are examples.

Download: puzzle.zip


02/28/2008

Saturday, February 16, 2008

C++ STL set example

/*
* C++ STL set example
* Anwar Mamat anwar@cse.unl.edu
*/
#include "set"
#include "iostream"
using namespace std;

class Student{
public:
int num;
string name;
};


class Comp
{
public:
bool operator()(Student s1, Student s2)
{
if(s1.num < s2.num )
return true;
else
return false;
}
};

int main(int argc, char* argv[])
{
set <Student, Comp> myStudent;

Student a1;
Student a2;

a1.num = 10;
a1.name = "Anwar";

a2.num = 5;
a2.name = "Ziale";

myStudent.insert(a1);
myStudent.insert(a2);
myStudent.insert(a1);

cout << "The number of students " << myStudent.size() << endl;

set <Student, Comp>::iterator it;
for( it = myStudent.begin(); it != myStudent.end(); it++ ) {
cout << it->num << "\t" << it->name << endl;
}
return 0;
}

Monday, February 4, 2008

Uyghur Input Text Box Linux C++ code

After so many years of MFC programming on Windows, I tried my first C++ GUI code on Linux. I installed the Qt4.3 on OpenSuse 10.3 and tested this (classic) Uyghur Input Text Box.

The Textbox works fine. It can map the keyboard input to the user defined strings. User can map the key press event to any string, so that this code can be modified to input any language. It is a free code, withoot fancy features. It is tested on OpenSuse 10.3 with Qt4.3. Please enjoy.

-------------------------
File 1: main.cpp
------------------------
/*
* This is a free software. Please feel free to modify
* and improve it.
*
* UTextEdit class inherits the QTextEdit class and changes the
* key press event.
*
*
* compile:
* qmake -project
* qmake
* make
*
* Tested on OpenSuse10.3 with Qt4.3
*
* Anwar Mamat anwar@cse.unl.edu
* Feb 4,2008
*/

#include "interface.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget uEdit;

uEdit.show();

return app.exec();
}


--------------------------
file 2: utextedit.h
--------------------------
/*
* UTextEdit Class
* This class inherits the QTextEdit class, intercepts the
* keypress events, and changes the keypress events based
* on the user predefined mapping.
*
* This is a free code. Please feel free to modify it.
*
* Anwar Mamat anwar@cse.unl.edu
* Feb 4,2008
*/

#ifndef UTEXTEDIT_H
#define UTEXTEDIT_H

#include "QtCore/QVariant"
#include "QtGui/QApplication"
#include "QtGui/QTextEdit"
#include "QtGui/QWidget"
#include "QKeyEvent"

class UTextEdit : public QTextEdit
{
Q_OBJECT
public:
UTextEdit(QWidget *parent = 0);

protected:
void keyPressEvent(QKeyEvent *e);

};

#endif //UTEXTEDIT_H

File3: utextedit.cpp

#include "utextedit.h"

UTextEdit::UTextEdit(QWidget *parent)

: QTextEdit(parent)

{

}

void UTextEdit::keyPressEvent(QKeyEvent *e)

{

QString Latin = {"abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ/?.,:;"};

char Uyghur[][5] = {

"ھ",//a

"ب",//b

"غ",//c

"د",//d

"ې",//e

"ا",//f

"ە",//g

"ى",//h

"ڭ",//i

"ق",//j

"ك",//k

"ل",//l

"م",//m

"ن",//n

"و",//o

"پ",//p

"چ",//q

"ر",//r

"س",//s

"ت",//t

"ۇ",//u

"ۈ",//v

"ۋ",//w

"ش",//x

"ي",//y

"ز",//z

" ",// space

"",// A

"",// B

"",// C

"ژ",// D

"",//E

"ف",//F

"گ",//G

"خ",//H

"",//I

"ج",//J

"ۆ",//K

"لا",//L

"",//M

"",//N

"",//O

"",//P

"",//Q

"",//R

"",//S

"",//T

"",//U

"",//V

"",//W

"",//X

"",//Y

"",//Z

"ئ", // /

"؟",//?

".",//.

"،",//,

":",//:

"؛",//;

};

//Do something here.

switch (e->key()) {

case Qt::Key_Enter:

case Qt::Key_Return:

case Qt::Key_Escape:

case Qt::Key_Tab:

case Qt::Key_Backtab:

e->ignore();

return;

}

unsigned int iPos = Latin.indexOf(e->text()) ;

//Intercept the predefined keyevents

if(e->key() < style="color: blue;">sizeof(Uyghur) / 5 )

{

QKeyEvent ker(QKeyEvent::KeyPress, Qt::Key_0, 0, QObject::trUtf8(Uyghur[iPos]), 0);

QTextEdit::keyPressEvent(&ker);

}

else //other keys

QTextEdit::keyPressEvent(e);

}

----------------------

file 4: interface.4

#ifndef INTERFACE_H

#define INTERFACE_H

#include "QObject"

#include "QtGui/QApplication"

#include "QtGui/QWidget"

#include "QMessageBox"

#include "QPushButton"

class MyWidget : public QWidget

{

Q_OBJECT

public:

MyWidget(QWidget *parent = 0);

public slots:

void about();

};

#endif //INTERFACE_H

--------------------------

file 5: interface.cpp

--------------------------

#include "interface.h"

#include "utextedit.h"

void MyWidget::about()

{

QMessageBox::about(this, "UTextEdit","A simple Uyghur Input Program.");

}

MyWidget::MyWidget(QWidget *parent )

: QWidget(parent)

{

setFixedSize(640, 240);

QPushButton *quit = new QPushButton(tr("Quit"), this);

QPushButton *about = new QPushButton(tr("About"), this);

UTextEdit *txtBox = new UTextEdit(this);

quit->setGeometry(480, 200, 60, 30);

quit->setFont(QFont("Times", 14, QFont::Bold));

about->setGeometry(540, 200, 80, 30);

about->setFont(QFont("Times", 14, QFont::Bold));

txtBox->setGeometry(10,10,620,180);

txtBox->setFont(QFont("UKIJ Tuz", 24, QFont::Bold));

txtBox->setAlignment(Qt::AlignRight);

connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));

connect(about, SIGNAL(clicked()), this, SLOT(about(void)));

}


Sunday, January 20, 2008

Samsung Blackjack II MP3 ringtone

For my blackjack, I just copied the MP3 file to the \Application Data\Sounds folder, then I can use them as the ringtone. Few days ago, I got my balckjack II. I did the same thing, but the MP3 did not show up in the ringtone list. I don't know why Samsung or AT&T do that. They put a restriction on file size. There is how you remove that restirction. Please remember, you do it for your own risk. I will not be responsible for any damage.

1. download Mobile Registry Editor1.1 from
http://www.breaksoft.com/Download/MRE/MobileRegistryEditor.zip
2. Connect your balckjack to your computer
3. Run MobileRegistryEditor
4. Open HKEY_CurrentUser-->ControlPanel-->Sounds
5. Find the
values
6.
Delete the key of FileSizeLimit

Ok, it is all done. If you copy the MP3 files to to ApplicationData folder, they will show up in your ringtone list.

Wednesday, January 16, 2008

Write or review a paper?

If you want to choose between writing a paper or reviewing paper to say accept or reject, which one do you choose?