-

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!

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