Saturday, April 21, 2018

Postfix To Prefix Converter in C

A program that will convert a format from postfix to prefix in C language.

I am currently accepting programming work, it project, school programming projects , thesis and capstone projects, IT consulting work and web development work kindly contact me in the following email address for further details. Thank you.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.


Program Listing

postfix.c


#include <stdio.h>
#include <conio.h>
#include <string.h>

#define MAX 50

struct postfix
{
char stack[MAX][MAX], target[MAX] ;
char temp1[2], temp2[2] ;
char str1[MAX], str2[MAX], str3[MAX] ;
int i, top ;
} ;

void initpostfix ( struct postfix * ) ;
void setexpr ( struct postfix *, char * ) ;
void push ( struct postfix *, char * ) ;
void pop ( struct postfix *, char * ) ;
void convert ( struct postfix * ) ;
void show ( struct postfix ) ;

void main( )
{
struct postfix q ;
char expr[MAX] ;

clrscr( ) ;

initpostfix ( &q ) ;

printf ( "\nEnter an expression in postfix form: " ) ;
gets ( expr ) ;

setexpr ( &q, expr ) ;
convert ( &q ) ;

printf ( "\nThe Prefix expression is: " ) ;
show ( q ) ;

getch( ) ;
}

/* initializes the elements of the structure */
void initpostfix ( struct postfix *p )
{
p -> i = 0 ;
p -> top = -1 ;
strcpy ( p -> target, "" ) ;
}

/* copies given expr. to target string */
void setexpr ( struct postfix *p, char *c )
{
strcpy ( p -> target, c ) ;
}

/* adds an operator to the stack */
void push ( struct postfix *p, char *str )
{
if ( p -> top == MAX - 1 )
printf ( "\nStack is full." ) ;
else
{
p -> top++ ;
strcpy ( p -> stack[p -> top], str ) ;
}
}

/* pops an element from the stack */
void pop ( struct postfix *p, char *a )
{
if ( p -> top == -1 )
printf ( "\nStack  is empty." ) ;
else
{
strcpy ( a, p -> stack[p -> top] ) ;
p -> top-- ;
}
}

/* converts given expr. to prefix form */
void convert ( struct postfix *p )
{
while ( p -> target[p -> i] != '\0' )
{
/* skip whitespace, if any */
if ( p -> target[p -> i] == ' ')
p -> i++ ;
if( p -> target[p -> i] == '%' || p -> target[p -> i] == '*' ||
p -> target[p -> i] == '-' || p -> target[p -> i] == '+' ||
p -> target[p -> i] == '/' || p -> target[p -> i] == '$' )
{
pop ( p, p -> str2 ) ;
pop ( p, p -> str3 ) ;
p -> temp1[0] = p -> target[ p -> i] ;
p -> temp1[1] = '\0' ;
strcpy ( p -> str1, p -> temp1 ) ;
strcat ( p -> str1, p -> str3 ) ;
strcat ( p -> str1, p -> str2 ) ;
push ( p, p -> str1 ) ;
}
else
{
p -> temp1[0] = p -> target[p -> i] ;
p -> temp1[1] = '\0' ;
strcpy ( p -> temp2, p -> temp1 ) ;
push ( p, p -> temp2 ) ;
}
p -> i++ ;
}
}

/* displays the prefix form of expr. */
void show ( struct postfix p )
{
char *temp = p.stack[0] ;
while ( *temp )
{
printf ( "%c ", *temp ) ;
temp++ ;
}

}

Linked List as Stack in C

A simple program to demonstrate the concept of linked list as stack in C.

I am currently accepting programming work, it project, school programming projects , thesis and capstone projects, IT consulting work and web development work kindly contact me in the following email address for further details. Thank you.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.


Program Listing

stack.c

#include <stdio.h>
#include <conio.h>
#include <alloc.h>

/* structure containing data part and linkpart */
struct node
{
int data ;
struct node *link ;
} ;

void push ( struct node **, int ) ;
int pop ( struct node ** ) ;
void delstack ( struct node ** ) ;

void main( )
{
struct node *s = NULL ;
int i ;

clrscr( ) ;

push ( &s, 14 ) ;
push ( &s, -3 ) ;
push ( &s, 18 ) ;
push ( &s, 29 ) ;
push ( &s, 31 ) ;
push ( &s, 16 ) ;

i = pop ( &s ) ;
printf ( "\nItem popped: %d", i ) ;

i = pop ( &s ) ;
printf ( "\nItem popped: %d", i ) ;

i = pop ( &s ) ;
printf ( "\nItem popped: %d", i ) ;

delstack ( &s ) ;

getch( ) ;
}

/* adds a new node to the stack as linked list */
void push ( struct node **top, int item )
{
struct node *temp ;
temp = ( struct node * ) malloc ( sizeof ( struct node ) ) ;

if ( temp == NULL )
printf ( "\nStack is full." ) ;

temp -> data = item ;
temp -> link = *top ;
*top = temp ;
}

/* pops an element from the stack */
int pop ( struct node **top )
{
struct node *temp ;
int item ;

if ( *top == NULL )
{
printf ( "\nStack is empty." ) ;
return NULL ;
}

temp = *top ;
item = temp -> data ;
*top = ( *top ) -> link ;

free ( temp ) ;
return item ;
}

/* deallocates memory */
void delstack ( struct node **top )
{
struct node *temp ;

if ( *top == NULL )
return ;

while ( *top != NULL )
{
temp = *top ;
*top = ( *top ) -> link ;
free ( temp ) ;
}
}

Array as Stack in C


A simple program how to implement the concept of array as stack using C language.


I am currently accepting programming work, it project, school programming projects , thesis and capstone projects, IT consulting work and web development work kindly contact me in the following email address for further details. Thank you.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.



Program Listing

array.c

#include <stdio.h>
#include <conio.h>

#define MAX 10

struct stack
{
int arr[MAX] ;
int top ;
} ;

void initstack ( struct stack * ) ;
void push ( struct stack *, int item ) ;
int pop ( struct stack * ) ;

void main( )
{
struct stack s ;
int i ;

clrscr( ) ;

initstack ( &s ) ;

push ( &s, 11 ) ;
push ( &s, 23 ) ;
push ( &s, -8 ) ;
push ( &s, 16 ) ;
push ( &s, 27 ) ;
push ( &s, 14 ) ;
push ( &s, 20 ) ;
push ( &s, 39 ) ;
push ( &s, 2 ) ;
push ( &s, 15 ) ;
push ( &s, 7 ) ;

i = pop ( &s ) ;
printf ( "\n\nItem popped: %d", i ) ;

i = pop ( &s ) ;
printf ( "\nItem popped: %d", i ) ;

i = pop ( &s ) ;
printf ( "\nItem popped: %d", i ) ;

i = pop ( &s ) ;
printf ( "\nItem popped: %d", i ) ;

i = pop ( &s ) ;
printf ( "\nItem popped: %d", i ) ;

getch( ) ;
}

/* intializes the stack */
void initstack ( struct stack *s )
{
s -> top = -1 ;
}

/* adds an element to the stack */
void push ( struct stack *s, int item )
{
if ( s -> top == MAX - 1 )
{
printf ( "\nStack is full." ) ;
return ;
}
s -> top++ ;
s -> arr[s ->top] = item ;
}

/* removes an element from the stack */
int pop ( struct stack *s )
{
int data ;
if ( s -> top == -1 )
{
printf ( "\nStack is empty." ) ;
return NULL ;
}
data = s -> arr[s -> top] ;
s -> top-- ;
return data ;
}

Postfix To Infix Conversion in C

In  this article I would like to share with you a sample program written in C to convert postfix to infix format.

I am currently accepting programming work, it project, school programming projects , thesis and capstone projects, IT consulting work and web development work kindly contact me in the following email address for further details. Thank you.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.


Program Listing

postfix.c


#include <stdio.h>
#include <conio.h>
#include <string.h>

#define MAX 50

struct postfix
{
char stack[MAX][MAX], target[MAX] ;
char temp1[2], temp2[2] ;
char str1[MAX], str2[MAX], str3[MAX] ;
int i, top ;
} ;

void initpostfix ( struct postfix * ) ;
void setexpr ( struct postfix *, char * ) ;
void push ( struct postfix *, char * ) ;
void pop ( struct postfix *, char * ) ;
void convert ( struct postfix * ) ;
void show ( struct postfix ) ;

void main( )
{
struct postfix q ;
char expr[MAX] ;

clrscr( ) ;

initpostfix ( &q ) ;

printf ( "\nEnter an expression in postfix form: " ) ;
gets ( expr ) ;

setexpr ( &q, expr ) ;
convert ( &q ) ;

printf ( "\nThe infix expression is: " ) ;
show ( q ) ;

getch( ) ;
}

/* initializes data member */
void initpostfix ( struct postfix *p )
{
p -> i = 0 ;
p -> top = -1 ;
strcpy ( p -> target, "" ) ;
}

/* copies given expression to target string */
void setexpr ( struct postfix *p, char *c )
{
strcpy ( p -> target, c ) ;
}

/* adds an expr. to the stack */
void push ( struct postfix *p, char *str )
{
if ( p -> top == MAX - 1 )
printf ( "\nStack is full." ) ;
else
{
p -> top++ ;
strcpy ( p -> stack[p -> top], str ) ;
}
}

/* pops an expr. from the stack */
void pop ( struct postfix *p, char *a )
{
if ( p -> top == -1 )
printf ( "\nStack  is empty." ) ;
else
{
strcpy ( a, p -> stack[p -> top] ) ;
p -> top-- ;
}
}

/* converts given expr. to infix form */
void convert ( struct postfix *p )
{
while ( p -> target[p -> i] )
{
/* skip whitespace, if any */
if( p -> target[p -> i] == ' ' )
p -> i++ ;
if ( p -> target[p -> i] == '%' || p -> target[p -> i] == '*' ||
p -> target[p -> i] == '-' || p -> target[p -> i] == '+' ||
p -> target[p -> i] == '/' || p -> target[p -> i] == '$' )
{
pop ( p, p -> str2 ) ;
pop ( p, p -> str3 ) ;
p -> temp1[0] = p -> target[p -> i] ;
p -> temp1[1] = '\0' ;
strcpy ( p -> str1, p -> str3 ) ;
strcat ( p -> str1, p -> temp1 ) ;
strcat ( p -> str1, p -> str2 ) ;
push ( p, p -> str1 ) ;
}
else
{
p -> temp1[0] = p -> target[p -> i] ;
p -> temp1[1] = '\0' ;
strcpy ( p -> temp2, p -> temp1 ) ;
push ( p, p -> temp2 ) ;
}
p -> i++ ;
}
}

/* displays the expression */
void show ( struct postfix p )
{
char *t ;
t = p.stack[0] ;
while ( *t )
{
printf ( "%c ", *t ) ;
t++ ;
}
}

Infix To Postfix Conversion in C

Here is a program that will convert value in infix format into it's postfix format written in C language.

I am currently accepting programming work, it project, school programming projects , thesis and capstone projects, IT consulting work and web development work kindly contact me in the following email address for further details. Thank you.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.



Program Listing

convert.c

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>

#define MAX 50

struct infix
{
char target[MAX] ;
char stack[MAX] ;
char *s, *t ;
int top ;
} ;

void initinfix ( struct infix * ) ;
void setexpr ( struct infix *, char * ) ;
void push ( struct infix *, char ) ;
char pop ( struct infix * ) ;
void convert ( struct infix * ) ;
int priority ( char ) ;
void show ( struct infix ) ;

void main( )
{
    struct infix p ;
char expr[MAX] ;

    initinfix ( &p ) ;

    clrscr( ) ;

    printf ( "\nEnter an expression in infix form: " ) ;
gets ( expr ) ;

setexpr ( &p, expr ) ;
convert ( &p ) ;

printf ( "\nThe postfix expression is: " ) ;
show ( p ) ;

    getch( ) ;
}

/* initializes structure elements */
void initinfix ( struct infix *p )
{
p -> top = -1 ;
strcpy ( p -> target, "" ) ;
strcpy ( p -> stack, "" ) ;
p -> t = p -> target ;
p -> s = ""  ;
}

/* sets s to point to given expr. */
void setexpr ( struct infix *p, char *str )
{
p -> s = str ;
}

/* adds an operator to the stack */
void push ( struct infix *p, char c )
{
if ( p -> top == MAX )
printf ( "\nStack is full.\n" ) ;
else
{
p -> top++ ;
p -> stack[p -> top] = c ;
}
}

/* pops an operator from the stack */
char pop ( struct infix *p )
{
if ( p -> top == -1 )
{
printf ( "\nStack is empty.\n" ) ;
return -1 ;
}
else
{
char item = p -> stack[p -> top] ;
p -> top-- ;
return item ;
}
}

/* converts the given expr. from infix to postfix form */
void convert ( struct infix *p )
{
    char opr ;

while ( *( p -> s ) )
{
if ( *( p -> s ) == ' ' || *( p -> s ) == '\t' )
{
p -> s++ ;
continue ;
}
if ( isdigit ( *( p -> s ) ) || isalpha ( *( p -> s ) ) )
{
while ( isdigit ( *( p -> s ) ) || isalpha ( *( p -> s ) ) )
{
*( p -> t ) = *( p -> s ) ;
p -> s++ ;
p -> t++ ;
}
}
if ( *( p -> s ) == '(' )
{
push ( p, *( p -> s ) ) ;
p -> s++ ;
}

if ( *( p -> s ) == '*' || *( p -> s ) == '+' || *( p -> s ) == '/' || *( p -> s ) == '%' || *( p -> s ) == '-' || *( p -> s ) == '$' )
{
if ( p -> top != -1 )
{
opr = pop ( p ) ;
while ( priority ( opr ) >= priority ( *( p -> s ) ) )
{
*( p -> t ) = opr ;
p -> t++ ;
opr = pop ( p ) ;
}
push ( p, opr ) ;
push ( p, *( p -> s ) ) ;
}
else
push ( p, *( p -> s ) ) ;
p -> s++ ;
}

if ( *( p -> s ) == ')' )
{
opr = pop ( p ) ;
while ( ( opr ) != '(' )
{
*( p -> t ) = opr ;
p -> t++ ;
opr =  pop ( p ) ;
}
p -> s++ ;
}
}

while ( p -> top != -1 )
{
char opr = pop ( p ) ;
*( p -> t ) = opr ;
p -> t++ ;
}

*( p -> t ) = '\0' ;
}

/* returns the priority of an operator */
int priority ( char c )
{
if ( c == '$' )
return 3 ;
if ( c == '*' || c == '/' || c == '%' )
return 2 ;
else
{
if ( c == '+' || c == '-' )
return 1 ;
else
return 0 ;
}
}

/* displays the postfix form of given expr. */
void show ( struct infix p )
{
    printf ( " %s", p.target ) ;
}

Saturday, April 14, 2018

Total Salary Computation in JQuery

Here is a simple program that I wrote using JQuery to compute the total salary of the employee's in the company. The code is very easy to understand and use.

I am currently accepting programming work, it project, school programming projects , thesis and capstone projects, IT consulting work and web development work kindly contact me in the following email address for further details. Thank you.

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.




Sample Program Output


Program Listing

index.htm

<html>
<head>
<title>Total Salary Computation in JQuery</title>
</head>
<body>
 <style type="text/css">
 
  body {
  font-family: arial;
  size:15px;
  font-weight: bold;
  background-color: lightgreen;
  }
 </style>
 <br>
 <h2> Total Salary Computation in JQuery </h2>
<table cellpadding="5">
<tr>
<td><b>Employee Name</b></td> 
<td><b>Salary</b></td>
</tr>
<tr><td></td> </tr>
<tr>
<td>Allie Pomperada</td>
<td><input type='text' class='salary' /></td>
</tr>
<tr>
<td>Jake Pomperada</td>
<td><input type='text' class='salary' /></td>
</tr>
<tr>
<td>Jacob Samuel Pomperada</td>
<td><input type='text' class='salary' /></td>
</tr>
<tr>
<td>Julianna Rae Pomperada</td>
<td><input type='text' class='salary' /></td>
</tr>

<tr>
<td>Total Salary PHP</td>
<td><input type='text' id='totalSalary' disabled/></td>
</tr>
</table>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js "></script>

<script>
$('.salary').keyup(function () {
    var sum = 0;
    $('.salary').each(function() {
        sum += Number($(this).val());
    });
$('#totalSalary').val(sum.toFixed(2));
});
</script>

</body>
</html>