-

Sort Header

Posted by Abel Gancsos on Dec 19, 2011 in Blog

Here is a compiled header file for the most used sorting algorithms. Feel free to edit it to your needs:

#include
#include
using namespace std;

void quickSort(int a[],int left,int right);
void restoreup(int a[],int i);
void restoredown(int a[],int i,int n);
char *mergeSort(char letters[], char temp[], int array_size);
char *mSort(char letters[], char temp[], int left, int right);
char *merge(char letters[], char temp[], int left, int mid, int right);
void charBubbleSort(char a[],int max);
void bubbleSort(int a[],int max);

void restoreup(int a[],int i)
{
int value=a[i];

while((i>1)&&(a[i/2]{
a[i]=a[i/2];
i=i/2;
}

a[i]=value;
}

void restoredown(int a[],int i,int n)
{
int value=a[i],j=i*2;

while(j{
if((j j++;

if(a[j]break;

a[j/2]=a[j];
j=j*2;
}

a[j/2]=value;
}

void quickSort(int a[],int left,int right){
int pivot=a[(left+right)/2],tmp;
int i=left,j=right;

while(iwhile(a[i] i++;
}
while(a[j]>pivot){
j–;
}

if(itmp=a[i];
a[i]=a[i+1];
a[i+1]=tmp;
i++;
j–;
}
}

if(leftquickSort(a,left,j);

if(iquickSort(a,i,right);

//return a;
}

char *mergeSort(char letters[], char temp[], int array_size)
{
return mSort( letters, temp, 0, array_size – 1);
}

char *mSort(char letters[], char temp[], int left, int right)
{
int mid;

if (right > left)
{
mid = (right + left) / 2;
mSort( letters, temp, left, mid);
mSort( letters, temp, (mid+1), right);

merge( letters, temp, left, (mid+1), right);
}

return letters;
}

char *merge(char letters[], char temp[], int left, int mid, int right)
{
int i, leftEnd, elements, tmpPos;

leftEnd = (mid – 1);
tmpPos = left;
elements = (right – left + 1);

while ((left {
if ( letters[left] {
temp[tmpPos] = letters[left];
tmpPos += 1;
left += 1;
}
else
{
temp[tmpPos] = letters[mid];
tmpPos += 1;
mid += 1;
}
}

while (left {
temp[tmpPos] = letters[left];
left += 1;
tmpPos += 1;
}
while (mid {
temp[tmpPos] = letters[mid];
mid += 1;
tmpPos += 1;
}

for (i=0; i < elements; i++)
{
letters[right] = temp[right];
right -= 1;
}

return temp;
}

void bubbleSort(int a[],int max){

for(int i=0;ifor(int j=0;jif(a[j]>a[j+1]){
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}

void charBubbleSort(char a[],int max){

for(int i=0;ifor(int j=0;jif(a[j]>a[j+1]){
char temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}

You can download it from http://foxwebhosting.dyndns.org/lioncage/Packages/sort.h

 
-

N Queens Problem

Posted by Abel Gancsos on Dec 14, 2011 in Blog

//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
//$$Name: n-queens.cpp $
//$$Author: Abel Gancsos $
//$$Date: 12-15-2011 $
//$$Description: Solve the n-queens problem. $
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

#include
#include
#include
using namespace std;

int count2=0;
bool attack(int i,int j,int hist[],int col); //Checks the current values
void solve(int n,int col,int hist[]); //Solves the n-queens problem
int main()
{
int n,again=1;
timeval time;
double total,start,end;

//Loop until ready to quit
while(again==1){
cout<<"Enter number of queens: ";
cin>>n;

int hist[n];

gettimeofday(&time,NULL);
start=time.tv_sec+(time.tv_usec/1000000.0); //get start time in nanoseconds
solve(n, 0, hist);

gettimeofday(&time,NULL);
end=time.tv_sec+(time.tv_usec/1000000.0); //get end time in nanoseconds
total=end-start; //calculate total time

//Print out ellapsed time
cout<<"Elapsed time: "< cout<<"1-Again 2-Close: ";
cin>>again;
}

return 0;
}

void solve(int n, int col, int hist[])
{

if (col == n) {
cout<<“\nNo. “<< ++count2< for (int i = 0; i < n; i++, putchar(‘\n’))
for (int j = 0; j < n; j++){

//if is a queen, print out “Q”, otherwise print out “.”
putchar(j == hist[i] ? ‘Q’ : ((i + j) & 1) ? ‘ ‘ : ‘.’);
}

return;
}

//continue until complete
for (int i = 0, j = 0; i < n; i++) {
for (j = 0; j < col && !attack(i, j, hist, col); j++);
if (j < col) continue;

hist[col] = i;
solve(n, col + 1, hist);
}
}

bool attack(int i, int j,int hist[], int col){

if(hist[j]==i||abs(hist[j]-i)==col-j){

return true;
}
else{
return false;
}

}

 
-

Christmas Timer

Posted by Abel Gancsos on Dec 12, 2011 in Blog

 

<html>
<head>
<style type="text/css">
span{
	color:blue;
	font-family:chalked,cracked,comic sans ms,chiller;
	font-size: 120pt;
}
</style>

<script type="text/javascript" src="../jquery-1.6.4.min.js">
</script>
<script type="text/javascript">

function update ( )
{
  var now = new Date ( );

  var hours = now.getHours ( );
  var minutes = now.getMinutes ( );
  var seconds = now.getSeconds ( );

  // Pad the minutes and seconds with leading zeros, if required
  minutes = ( minutes < 10 ? "0" : "" ) + minutes;
  seconds = ( seconds < 10 ? "0" : "" ) + seconds;

  //

  var month=now.getMonth()+1;
  var day=now.getDate();
  var year=now.getFullYear();

  // Compose the string for display
  var currentTime = "Months: " + (12-month)+" Days: "+ (23-day)+" Hours: "+(24-hours)+ " Minutes: "+(60-minutes);

  // Update the time display
  document.getElementById("time").firstChild.nodeValue = currentTime;
}

</script>

<title>Christmas Timer</title>
</head>
<body onload="update(); setInterval('update()', 1000 )" bgcolor="red">
<center>
<br/><br/><br/><br/>
<font color="green" face="Arial"><h1>Christmas Timer</h1><br/><br/>

<span id="time">
</span>

</center>
</body>
</html>

 
-

Parser

Posted by Abel Gancsos on Dec 8, 2011 in Blog

////.h

#include
#include
#include
using namespace std;

char buffer[80];
int readflag=1;

//Reserved words
char *reserved[]={
“begin”,”end”,”char”,”default”,”else”,”enum”,”if”,”int”,”switch”,”while”
};

enum tokenType{
begin,end,reserve2,reserve3,reserve4,reserve5,reserve6,reserve7,reserve8,reserve9,
identifier,comment,realConst,stringConst,plusSign,minusSign,multiplication,assignment,
leftPar,comma,semicolon,rightPar,invalid,integer,division
};

void clear(void); //Clears buffer
int dispatcher(void);
void display(int input);

//Parser functions
void syntaxerror(int token);
void match(int token);
int readtoken(void);
int getnexttoken(void);
int peektoken(void);
void program(void);
void statementlist(void);
void statementtail(void);
void statement(void);
void expression(void);
void expressiontail(void);
void factor(void);
void factortail(void);
void primary(void);
void addop(void);
void multop(void);

//Recognizers
tokenType getReservedOrId(void); //Identifiers (L(L|D)*)
tokenType getRealLit(void); //Real Constants (D+|D+.D+)
tokenType getComments(void); //Comments (/* NOT(*/) */ )
tokenType getStrings(void); //String Constants (” NOT(“) “)

tokenType getPlus(void); //Plus (D+|D+.D+ + D+|D+.D+)
tokenType getMinus(void); //Minus (D+|D+.D+ – D+|D+.D+)
tokenType getMult(void); //Multiplication (D+|D+.D+ * D+|D+.D+)
tokenType getDiv(void); //Division (D+|D+.D+ / D+|D+.D+)
tokenType getAssignment(void); //Assignment (L(L|D)* := ” NOT(“) “|D+|D+.D+)

tokenType getLeftPar(void); //Left Parenthesis ( ( )
tokenType getRightPar(void); //Right Parenthesis ( ( )
//tokenType getLeftBrace(void); //Left Brace ( { )
//tokenType getRightBrace(void); //Right Brace ( } )
tokenType getSemicolon(void); //Semicolon ( ; )
tokenType getComma(void); //Comma ( , )

tokenType Invalid(void); //Invalid characters

int dispatcher(void)
{
int input;
int ch, ch2;

ch = cin.get();
while (isspace(ch)) // Skip spaces
ch = cin.get();

if (ch == EOF) // Have we reached the end of the file?
return EOF;

// Take a peek at the next character
ch2 = cin.peek();

// Put the first character back in the buffer
cin.putback(ch);

// Figure out which language recongnition function to call
if (isalpha(ch)) // Letter = Identifier
input = getReservedOrId();
else if (isdigit(ch)) // Digit = Real Constant
input = getRealLit();
else
{
switch (ch)
{

case ‘/’:

if (ch2 == ‘*’) input = getComments();
else input = getDiv();
break;
case ‘”‘: input = getStrings(); break;
case ‘+’: input = getPlus(); break;
case ‘-’: input = getMinus(); break;
case ‘*’: input = getMult(); break;
case ‘:’:

if (ch2 == ‘=’)
input = getAssignment();
else input = Invalid();
break;
case ‘(‘: input = getLeftPar(); break;
case ‘,’: input = getComma(); break;
case ‘;’: input = getSemicolon(); break;
case ‘)’: input = getRightPar(); break;
default: input = Invalid(); break;

}
}

return input;
}

void clear(void)
{
for(int i=0;i<80;i++){
buffer[i]=NULL;
}
}

void display(int input){
char *message[]={
“begin”,”end”,”Reserved”,”Reserved”,”Reserved”,”Reserved”,”Reserved”,
“Reserved”,”Reserved”,”Reserved”,”Indentifier”,”Comment”,”Real Constant”,
“String Constant”,”Plus”,”Minus”,”Multiplication”,”Assignment”,”Left Parenthesis”,
“Comma”,”Semicolon”,”Right Parenthesis”,”Invalid Character”,”Integer”,”Division”
};

cout.setf(ios::left);
cout< }

tokenType getAssignment(){
int ch=cin.get();

buffer[0]=ch;
ch=cin.get();

if(ch==’=')
buffer[1]=ch;

else
cin.putback(ch);

return assignment;
}

tokenType getComma(){
int ch=cin.get();

buffer[0]=ch;
return comma;
}

tokenType getDiv(){
int ch=cin.get();

buffer[0]=ch;
return division;
}

tokenType getComments(){
int ch,i=0;
ch=cin.get();

while(!buffer[i-1]==’*'&&ch==’/'){
buffer[i++]=ch;
ch=cin.get();
}

buffer[i++]=ch;

return comment;
}

tokenType getLeftPar(){
int ch=cin.get();

buffer[0]=ch;
return leftPar;
}

tokenType getRightPar(){
int ch=cin.get();

if(ch==’)')
buffer[0]=ch;

return rightPar;
}

tokenType getPlus(){
int ch=cin.get();

buffer[0]=ch;
return plusSign;
}

tokenType getReservedOrId(){
int ch,i=0;

ch=cin.get();
if(isalpha(ch))
{
buffer[i++]=ch;
ch=cin.get();
while(isalnum(ch))
{
buffer[i++]=ch;
ch=cin.get();
}
cin.putback(ch);

for(int i=0;i<10;i++)
if(strcmp(buffer,reserved[i])==0)
return (tokenType)i;
}

return identifier;
}

tokenType getMinus()
{
int ch=cin.get();
buffer[0]=ch;
return minusSign;
}

tokenType getMult()
{
int ch=cin.get();
buffer[0]=ch;
return multiplication;
}

tokenType getRealLit()
{
int ch,i=0;
ch=cin.get();

while(isdigit(ch)){
buffer[i++]=ch;
ch=cin.get();
}

if(ch==’.'){ //Continue
buffer[i++]=ch;
ch=cin.get();
while(isdigit(ch)){
buffer[i++]=ch;
ch=cin.get();
}
}

else if(ch!=’.’ &&isdigit(ch)){ //Integer
//cin.putback(ch); //put invalid character back into the buffer
return integer;
}

cin.putback(ch); //put invalid character back into the buffer
return realConst;

}

tokenType getSemicolon()
{
int ch=cin.get();
if(ch==’;')
buffer[0]=ch;

return semicolon;
}

tokenType getStrings()
{
int ch,i=0;

ch=cin.get();
if(ch==’”‘){
buffer[i++]=ch;
ch=cin.get();
while(ch!=’”‘){
buffer[i++]=ch;
ch=cin.get();
}

buffer[i++]=ch;
}

return stringConst;
}

tokenType Invalid()
{
buffer[0]=cin.get();
return invalid;
}

int getnexttoken(void)
{
static int nexttoken;
if(readflag==1){
clear();
nexttoken=dispatcher();
readflag=0;
}

return nexttoken;
}

int peektoken(void)
{
int input;
input=getnexttoken();

return input;
}

int readtoken(void)
{
int input;
input=getnexttoken();
readflag=1;

return input;
}

void match(int token)
{
int input=readtoken();
if(input!=token)
syntaxerror(token);
else{
cout< if(input==begin||input==end||input==semicolon)
cout< }
}

void syntaxerror(int token)
{
cout<<“\n”< }

void program(void)
{

match(begin);
statementlist();
match(end);

}

void statementlist(void)
{
int nexttoken;
nexttoken=peektoken();

switch(nexttoken)
{
case identifier:
statement();
statementtail();
break;
case end:
return;

default:
syntaxerror(identifier);
}
}

void statementtail(void)
{
int nexttoken;
nexttoken=peektoken();
switch(nexttoken)
{
case identifier:
match(identifier);
match(assignment);
expression();
match(semicolon);
break;
case end:
return;
default:
syntaxerror(identifier);
}
}

void statement(void)
{
int nexttoken;
nexttoken=peektoken();

switch(nexttoken)
{
case identifier:
match(identifier);
match(assignment);
expression();
match(semicolon);
break;
default:
syntaxerror(identifier);
}
}

void expression(void)
{
int nexttoken;
nexttoken=getnexttoken();

switch(nexttoken)
{
case identifier:
case integer:
case realConst:
factor();
expressiontail();
break;
default:
syntaxerror(identifier);
}
}

void expressiontail(void)
{
int nexttoken;
nexttoken=peektoken();
switch(nexttoken)
{
case plusSign:
case minusSign:
addop();
factor();
expressiontail();
break;
case semicolon:
case rightPar:
case comma:
return;
default:
syntaxerror(plusSign);
}
}

void factor(void)
{
primary();
factortail();
}

void factortail(void)
{
int nexttoken;
nexttoken=peektoken();
switch(nexttoken)
{
case multiplication:
multop();
primary();
factortail();
break;
case rightPar:
case semicolon:
case plusSign:
case minusSign:
case comma:
return;
default:
syntaxerror(multiplication);
}
}

void primary(void)
{
int nexttoken;
nexttoken=peektoken();
switch(nexttoken)
{
case leftPar:
match(leftPar);
expression();
match(rightPar);
break;
case identifier:
match(identifier);
break;
case realConst:
match(realConst);
break;
default:
syntaxerror(leftPar);
}
}

void addop(void)
{
int nexttoken;
nexttoken=peektoken();
switch(nexttoken)
{
case plusSign:
match(plusSign);
break;
case minusSign:
match(minusSign);
break;
default:
syntaxerror(plusSign);
}
}

void multop(void)
{
int nexttoken;
nexttoken=peektoken();
switch(nexttoken)
{
case multiplication:
match(multiplication);
break;

default:
syntaxerror(multiplication);
}
}

////.cpp

#include “parser.h”

int main()
{
readflag=1;
program(); //Calls the parser, which will continurally check the scanner for the lexems

return 0;
}

 
-

Heap Sort

Posted by Abel Gancsos on Dec 7, 2011 in Blog

////.h

#include
#include
using namespace std;

void restoreup(int a[],int i);
void restoredown(int a[],int i,int n);

//Takes an array and the last entered value and moves it to a sorted position
void restoreup(int a[],int i)
{
int value=a[i];

while((i>1)&&(a[i/2] {
a[i]=a[i/2];
i=i/2;
}

a[i]=value;
}

void restoredown(int a[],int i,int n)
{
int value=a[i],j=i*2;

while(j<=n)
{
if((j j++;

if(a[j] break;

a[j/2]=a[j];
j=j*2;
}

a[j/2]=value;
}

////.cpp

#include "heapsort.h"
#include
#include

int main()
{
int again=1,max,max2;
timeval time;
double total,start,end;

while(again==1)
{

cout<<"Enter max numbers to sort: ";
cin>>max;

max2=max;

int a[max];
gettimeofday(&time,NULL);
start=time.tv_sec+(time.tv_usec/1000000.0);

for(int i=1;i<=max;i++){
a[i]=rand()%9;
restoreup(a,i);
}

cout<<"Before the sort: ";
for(int i=1;i<=max;i++)
cout<

int j=max;
// start=clock();
for(int i=1;i<=j;i++){
int temp=a[1];
a[1]=a[max];
a[max]=temp;
max--;
restoredown(a,1,max);
}

gettimeofday(&time,NULL);
end=time.tv_sec+(time.tv_usec/1000000.0);
total=end-start;

cout< for(int i=1;i<=max2;i++)
cout<

cout<

cout< cin>>again;
}

return 0;
}

 
-

JavaScript Clock

Posted by Abel Gancsos on Dec 2, 2011 in Blog
<script type="text/javascript" src="../jquery-1.6.4.min.js">// <![CDATA[

// ]]></script>
<script type="text/javascript">// <![CDATA[

function update ( )
{
  var now = new Date ( );

  var hours = now.getHours ( );
  var minutes = now.getMinutes ( );
  var seconds = now.getSeconds ( );

  // Pad the minutes and seconds with leading zeros, if required
  minutes = ( minutes < 10 ? "0" : "" ) + minutes;
  seconds = ( seconds < 10 ? "0" : "" ) + seconds;

  // Choose either "AM" or "PM" as appropriate
  var tOD = ( hours < 12 ) ? "AM" : "PM";

  // Convert the hours component to 12-hour format if needed
  hours = ( hours > 12 ) ? hours - 12 : hours;

  // Convert an hours component of "0" to "12"
  hours = ( hours == 0 ) ? 12 : hours;

  // Compose the string for display
  var currentTime = "It is " + hours + ":" + minutes + ":" + seconds + " " + tOD + " on " + (now.getMonth()+1) + "-" + now.getDate() + "-" + now.getFullYear();

   currentTime=currentTime + "\n";
   currentTime=currentTime + "                                                                            PST: ";
   currentTime = currentTime + (hours-3) + ":" + minutes + ":" + seconds + " " + tOD + " on " + (now.getMonth()+1) + "-" + now.getDate() + "-" + now.getFullYear();

  // Update the time display
  document.getElementById("time").firstChild.nodeValue = currentTime;
}

// ]]></script>

<center>

</center>&nbsp;
<h1>JavaScript Time</h1>
<span id="time">
</span>

 
-

Scanner2

Posted by Abel Gancsos on Dec 2, 2011 in Blog

////.h

#include
#include
using namespace std;

//Token Types
enum tokenType{
reserve0,reserve1,reserve2,reserve3,reserve4,reserve5,reserve6,reserve7,reserve8,reserve9,
identifier,comment,realConst,stringConst,plusSign,minusSign,multiplications,assignment,leftPar,
comma,semicolon,rightPar,leftBrace,rightBrace,invalid,integer,divisions
};

//Displays the appropiate token that corresponds to the specified input and displays the lexem currently
//in the buffer

void display(int);
void displayHeaders(void);
void clear(void); //Resets the token

//Recognizers
tokenType getReservedOrId(void); //Identifiers (L(L|D)*)
tokenType getRealLit(void); //Real Constants (D+|D+.D+)
tokenType getComments(void); //Comments (/* NOT(*/) */ )
tokenType getStrings(void); //String Constants (” NOT(“) “)

tokenType getPlus(void); //Plus (D+|D+.D+ + D+|D+.D+)
tokenType getMinus(void); //Plus (D+|D+.D+ – D+|D+.D+)
tokenType getMult(void); //Plus (D+|D+.D+ * D+|D+.D+)
tokenType getDiv(void); //Plus (D+|D+.D+ / D+|D+.D+)
tokenType getAssignment(void); //Plus (L(L|D)* := ” NOT(“) “|D+|D+.D+)

tokenType getLeftPar(void); //Left Parenthesis ( ( )
tokenType getRightPar(void); //Right Parenthesis ( ( )
tokenType getLeftBrace(void); //Left Brace ( { )
tokenType getRightBrace(void); //Right Brace ( } )
tokenType getSemicolon(void); //Semicolon ( ; )
tokenType getComma(void); //Comma ( , )

// Reads in and displays the tokens
int scanner();

tokenType Invalid(void); //Invalid characters

char buffer[80]; //Buffer to hold the tokens
char *keywords[]={“double”,”char”,”int”,”string”};

void displayHeaders(void)
{

cout<<“TOKENS/TYPES”< cout<<“————–”< }

//Display buffer and character input
void display(int input)
{
static char *message[]={“Reserved”,”Reserved”,”Reserved”,”Reserved”,”",”",”",”",”",”",
“Identifier”,”Comment”,”Real Constant”,”String Constant”,”Plus”,”Minus”,
“Multiplication”,”Assignment”,”Left Parenthesis”,”Comma”,”Semicolon”,
“Right Parenthesis”,”Left Brace”,”Right Brace”,”Invalid Character”,”Integer”,”Division”};

cout.setf(ios::left);
cout< }

//Reset buffer
void clear()
{

for(int i=0;i<80;i++)
buffer[i]=NULL;
}

tokenType getReservedOrId()
{
int c,i=0;
c=cin.get();

if(isalpha(c)){
buffer[i++]=c;
c=cin.get();

while(isalnum(c)){ //Letter or digit
buffer[i++]=c;
c=cin.get();
}

}

cin.putback(c); //Put the invalid character back into the buffer

//Check to see if invalid character is reserved
for(int i=0;i<4;i++){
if(strcmp(buffer,keywords[i])==0)
return (tokenType)i;
}

return identifier;
}

tokenType getComments()
{
int c,i=0;
c=cin.get();

while(!(buffer[i-1]==’*’ && c==’/')){
buffer[i++]=c;
c=cin.get();
}

//If the last character is a ‘/’, place in the buffer
buffer[i++]=c;

return comment;
}

tokenType getRealLit()
{
int c,i=0;
c=cin.get();

while(isdigit(c)){
buffer[i++]=c;
c=cin.get();
}

if(c==’.'){ //Continue
buffer[i++]=c;
c=cin.get();
while(isdigit(c)){
buffer[i++]=c;
c=cin.get();
}
}

else{ //Integer
cin.putback(c); //put invalid character back into the buffer
return integer;
}

cin.putback(c); //put invalid character back into the buffer
return realConst;
}

tokenType getStrings()
{
int c,i=0;
c=cin.get();

if(c==’”‘){
buffer[i++]=c;
c=cin.get();
while(c !=’”‘){
buffer[i++]=c;
c=cin.get();
}

buffer[i++]=c;
}

return stringConst;
}

tokenType getPlus()
{
buffer[0]=cin.get();
return plusSign;
}

tokenType getMinus()
{
buffer[0]=cin.get();
return minusSign;
}

tokenType getMult()
{
buffer[0]=cin.get();
return multiplications;
}

tokenType getDiv()
{
buffer[0]=cin.get();
return divisions;
}

tokenType getAssignment()
{
int c,i=0;
c=cin.get();
buffer[i++]=c;

if(c==’:'){

c=cin.get();
buffer[i++]=c;
}

else
cin.putback(c);

return assignment;
}

tokenType getLeftPar()
{
buffer[0]=cin.get();
return leftPar;
}

tokenType getComma()
{
buffer[0]=cin.get();
return comma;
}

tokenType getSemicolon()
{
buffer[0]=cin.get();
return semicolon;
}

tokenType getRightPar()
{
buffer[0]=cin.get();
return rightPar;
}

tokenType getLeftBrace()
{
buffer[0]=cin.get();
return leftBrace;
}

tokenType getRightBrace()
{
buffer[0]=cin.get();
return rightBrace;
}

tokenType Invalid()
{
int i=0;

while(!isspace(cin.peek())) //Skip spaces
buffer[i++]=cin.get();

return invalid;
}

// The scanner
// Reads characters in from an input files and displays the
// appropiate token type for each character/characters.
int scanner()
{
int input;
int c, c2;

c = cin.get();
while (isspace(c)) // Skip spaces
c = cin.get();

if (c == EOF) // Have we reached the end of the file?
return EOF;

// Take a peek at the next character
c2 = cin.peek();

// Put the first character back in the buffer
cin.putback(c);

// Figure out which language recongnition function to call
if (isalpha(c)) // Letter = Identifier
input = getReservedOrId();
else if (isdigit(c)) // Digit = Real Constant
input = getRealLit();
else
{
switch (c)
{

default: input = Invalid(); break;

case ‘/’:

if (c2 == ‘*’) input = getComments();
else input = getDiv();
break;
case ‘”‘: input = getStrings(); break;
case ‘+’: input = getPlus(); break;
case ‘-’: input = getMinus(); break;
case ‘*’: input = getMult(); break;
case ‘:’:

if (c2 == ‘=’)
input = getAssignment();
else input = Invalid();
break;
case ‘(‘: input = getLeftPar(); break;
case ‘,’: input = getComma(); break;
case ‘;’: input = getSemicolon(); break;
case ‘)’: input = getRightPar(); break;
case ‘{‘: input = getLeftBrace(); break;
case ‘}’: input = getRightBrace(); break;

}
}

return input;
}

////.cpp

#include “scanner.h”

int main()
{

int input;

clear();
displayHeaders();

// Keep on calling the scanner until we reach EOF
input=scanner();
while (input!=EOF){
display(input);
clear();
input=scanner();
}

return 0;
}

Thank you for reading!

 
-

A simple little script

Posted by Abel Gancsos on Nov 29, 2011 in Blog

<script type="text/javascript" src="jquery-1.6.4.min.js"></script>

<script type="text/javascript">// <![CDATA[
function calculate(){
var test=document.test.cel.value;

if(test!=''){
var a=document.test.cel.value;
var temp=(((a/5)*9)+32);
document.test.far.value=temp;
}

else{
 a=document.test.cel.value;
temp=(((a-32)*5)/9);;
document.test.cel.value=temp;
}
}

function reset(){

document.test.far.value="";
document.test.cel.value="";
}
</script>

JavaScript Test

<center>

</center>&nbsp;

<form id="test" method="post" name="test">
<table>
<tbody>
<tr>
<td align="left">Celsius:</td>
<td align="right"><input id="cel" onclick="reset()" type="text" name="celsius" /></td>
</tr>
<tr>
<td align="left">Fahrenheit :</td>
<td align="right"><input id="far" onclick="reset()" type="text" name="far" /></td>
</tr>
<tr>
<td><button onclick="calculate()" name="submit" type="button">Calculate</button></td>
</tr>
</tbody>
</table>
</form>

 
-

Math Proof

Posted by Abel Gancsos on Nov 28, 2011 in Blog

#include “sort.h”
using namespace std;

int main ()
{

int again=1,max;

while(again==1){

cout<<"Enter max numbers: ";
cin>>max;

int array1[max],array2[max],array3[max],array4[max];
for(int i=0;i cout<<"Enter value "< cin>>array1[i];
}

//Sort
int b=max;
for(int i=0;i

for(int j=0;j

if(array1[j]>array1[j+1]){
int temp=array1[j];
array1[j]=array1[j+1];
array1[j+1]=temp;
}
}
}

//Reverse
int j=-1;
for(int i=max;i>=0;i–){
array3[j]=array1[i];
j++;

}

for(int i=0;i array4[i]=array1[i]-array3[i];
}

cout<<"Original sorted: ";
printArray(array1,max);
cout<<"Reverse sorted: ";
printArray(array3,max);

cout<<"Check: ";
printArray(array4,max);

cout< cin>>again;
}

return 0;
}

 
-

Spanish characters

Posted by Abel Gancsos on Nov 15, 2011 in Blog

With Spanish becoming more and more popular and more and more essays being typed on the computer, I thought it would be of some value to talk about how you can make Spanish characters on your machine.

First off, it varies from system to system. For example, On an Apple Macintosh, you simply press [Option] + [E] at the same time then choose the accented character. Simple.

On the other hand, Windows is a bit more difficult. For example, you need to use numeric codes:

Upper Vowels
Vwl Code
Á ALT+0193
É ALT+0201
Í ALT+0205
Ó ALT+0211
Ú ALT+0218
Ñ ALT+0209
Ü ALT+0220

Lower Vowels
Vwl Code
á ALT+0225
é ALT+0233
í ALT+0237
ó ALT+0243
ú ALT+0250
ñ ALT+0241
ü ALT+0252

Other Symbols
Sym Code
¿ ALT+0191
¡ ALT+0161
º ALT+0186 (Masculine Ordinal)
ª ALT+0170 (Feminine Ordinal)
« ALT+0171 (Left Angle Quote)
» ALT+0187 (Right Angle Quote)
€ ALT+0128

Or you can download a program that does the characters for you, but then you could either get a virus or you would have to constantly go back and forth from your word processor and that program.

Then the third option is enabling the international keyboard in Windows Vista and above.

Since Windows is numeric and has no hotkey functions, there’s no easy way to enter a Spanish character on Windows. Some applications on the web would have an internal keyboard developed into the system, but otherwise you would have to know the code numbers There is also the option of changing the whole language of the computer, but why would you want to do that only for a school project or something. Now, you could be thinking that it doesn’t matter. Accents aren’t really that important. Well, you’re completely wrong because one word accented means one thing, that same word unaccented would mean something completely different. And again, the popularity of Spanish is growing, you can even see it on social networking sites like Facebook and besides let’s say there is something that YOU want to say that most likely not a lot of people would bother to copy and paste into Google Translate. In such a case, you would write in a different language, which would most likely be one of the romance languages, primarily Spanish. You could also type in binary, but who else is nerdy enough to do that?

Thank you for reading!

Copyright © 2012 Abe's the Word All rights reserved. Theme by Laptop Geek.