Home › Forums › C Programming › function
- This topic has 10 replies, 2 voices, and was last updated 16 years, 5 months ago by
Humayan.
- AuthorPosts
- September 2, 2008 at 1:46 am #2130
VidaVOXeql
Participanthello..po..great day..
ahm..i need some help of my project..
ahm..can you help me in this program:
example program of:
infix to postfix
merge sort
binary,hexa,octa convert to decimal..
tnx… - September 2, 2008 at 1:26 pm #3431
Humayan
Participantbinary conversion:
1234567891011121314151617181920#include <iostream.h><br />void main()<br />{<br />int i;<br />cout << "Enter a number to convert ";<br />cin >> i;<br />int binaryArray [ 20 ] ; //whatever<br />int j = 0;<br />while ( i )<br />{<br />binaryArray [ j ++ ] = i % 2 ;<br />i /= 2;<br />}<br /><br />cout << endl << "Binary Version: " ;<br />while ( j -- > 0 )<br />cout << binaryArray [ j ];<br />cout << endl;<br />}<br />
hex conversion:1234567891011121314151617181920212223242526#include <iostream.h><br />void printHex ( int i );<br />void main()<br />{<br />cout << "Enter a character: " ;<br />char ch;<br />cin >> ch;<br />cout << endl << "Here is hex form of ASCII: " << endl;<br />printHex ( ( int ) ch );<br />cout << endl;<br /><br /><br /><br />}<br />void printHex ( int i )<br />{<br />char base16 [ 17 ] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' };<br /><br />if ( i == 0 )<br />return;<br />else<br />{<br />printHex ( i / 16 );<br />cout << base16 [ i % 16 ];<br />}<br />}
Is the merge sort on an array?? - September 2, 2008 at 9:48 pm #3432
VidaVOXeql
Participantahmm thanks for the reply…cman..it help me a lot but,,,can you transfer the codes in C language…not C++…thank..you..and another is ung program po na conversion of octa,hexa,binary to decimal ai iisang program lang po..ndi po hiwahiwalay ung tatlo…tnx po..
ahm..our instructor told us that it depend on us,,,kung ano ang gagawin nming program about that…basta kaya nming ipaliwanag po…ung program..nmin..
and another thing is pwede po bang pkipaliwanag na din ng flow nung program..tnxx…a lot..
have a nice day… - September 4, 2008 at 11:36 am #3433
Humayan
Participantmerge sort:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150/****************************************************************<br />* File Name : c:programshelpmergeSort.cpp<br />* Date : September,3,2008<br />* Comments : new project<br />* Compiler/Assembler :<br />* Program Shell Generated At: 6:27:41 p.m.<br />=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br /><br />#include < iostream ><br />#include < stdlib.h ><br />#include < time.h ><br />//#include < math.h ><br />//#include < iomanip ><br />//#include < ctype.h ><br />#define MAX_NUMBER 50<br />#define ARRAY_SIZE 20<br />using namespace std;<br /><br />//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ FUNCTION PROTOTYPES @@@@@@@@@@@@@@@@@@@@@@@@@@<br />void mergeSort ( int * array , int * temp , int left , int right );<br />void mergeSort ( int * array );<br />void merge ( int * array , int * temp , int left , int right , int rightEnd );<br />void initialize ( int * array , int size );<br />//##################################################################################<br /><br />//main function ******************************<br />int main ( )<br />{<br />int array [ ARRAY_SIZE ];<br />initialize ( array , ARRAY_SIZE );<br />int i = 0;<br />while ( i < ARRAY_SIZE )<br />{<br />printf ( "%i" , array [ i ] );<br />printf ( "n" );<br />i ++;<br />}<br />mergeSort ( array );<br />i = 0;<br />while ( i < ARRAY_SIZE )<br />{<br />printf ( "%i" , array [ i ] );<br />printf ( "n" );<br />i ++;<br />}<br />return 0 ;<br />}<br /><br />/******************************* FUNCTION DEFINITION ******************************<br />Name : mergeSort<br />Parameters :<br />array a(n) int * ,<br />temp a(n) int * ,<br />left a(n) int ,<br />right a(n) int<br /><br />Returns: Void type<br />Comments:<br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void mergeSort ( int * array , int * temp , int left , int right )<br />{<br />if ( left < right )<br />{<br />int center = ( left + right ) / 2;<br />mergeSort ( array , temp , left , center );<br />mergeSort ( array , temp , center + 1 , right );<br />merge ( array , temp , left , center + 1 , right );<br />}<br />return;<br />}<br />/******************************* FUNCTION DEFINITION ******************************<br />Name : mergeSort<br />Parameters :<br />array a(n) int *<br /><br />Returns: Void type<br />Comments:<br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void mergeSort ( int * array )<br />{<br />int temp [ ARRAY_SIZE ];<br />mergeSort ( array , temp , 0 , ARRAY_SIZE - 1 );<br /><br />return;<br />}<br />/******************************* FUNCTION DEFINITION ******************************<br />Name : merge<br />Parameters :<br />array a(n) int * ,<br />temp a(n) int * ,<br />left a(n) int ,<br />right a(n) int ,<br />rightEnd ( rightEnd )<br /><br />Returns: Void type<br />Comments:<br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void merge ( int * array , int * temp , int left , int right , int rightEnd )<br />{<br />int leftEnd = right - 1;<br />int tempPos = left;<br />int numberElements = rightEnd - left + 1;<br /><br />while ( left <= leftEnd && right <= rightEnd )<br />if ( array [ left ] <= array [ right ] )<br />temp [ tempPos ++ ] = array [ left ++ ];<br />else<br />temp [ tempPos ++ ] = array [ right ++ ];<br />while ( left <= leftEnd )<br />temp [ tempPos ++ ] = array [ left ++ ];<br /><br />while ( right <= rightEnd )<br />temp [ tempPos ++ ] = array [ right ++ ];<br /><br />int i = 0;<br />while ( i < numberElements )<br />{<br />array [ rightEnd ] = temp [ rightEnd ] ;<br />rightEnd --;<br />}<br /><br />return;<br />}<br />/******************************* FUNCTION DEFINITION ******************************<br />Name : test<br />Parameters :<br />array a(n) int * ,<br />size a(n) int<br /><br />Returns: Void type<br />Comments:<br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void initialize ( int * array , int size )<br />{<br /><br />srand ( time ( NULL ) );<br />int i = 0;<br />while ( i < size )<br />{<br />array [ i ] = rand () % MAX_NUMBER;<br />i ++;<br />} <br /><br />return;<br />}<br /> - September 5, 2008 at 9:10 am #3434
Humayan
Participantare you supposed to use a stack data structure for the postfix to infix conversion?? STL or homemade??
- September 8, 2008 at 4:08 am #3435
VidaVOXeql
Participantahm..sir cman….tnx for ur,,,reply…..i think it will help me a lot..but.i think you should transfer that source codes in C language….
tnx….
sir,,,, - September 8, 2008 at 4:10 am #3436
VidaVOXeql
Participantahm..sir cman….tnx for ur,,,reply…..i think it will help me a
lot..but.i think you should transfer that source codes in C language….
tnx….
sir,,,,
and also,,,
sir,,,
nalilito po ako..dun sa mga codes…sa merge sort….
ahm..can you,,,,make it clear….tnx… - September 8, 2008 at 4:13 am #3437
VidaVOXeql
Participantahm,,,sir,,,tnx,,,,4 the reply,,
ahm,,,,wat do you mean b,,,STL?
anhm,,,,its ok,,,when you use the stack structure..
tnx…. - September 9, 2008 at 12:14 pm #3438
Humayan
ParticipantSo you need a version of the postfix program with a homemade stack? I’ll see if I can write one today/tommorow…….
- September 10, 2008 at 1:44 am #3439
VidaVOXeql
Participanttnxxxx….po…kua….
- September 10, 2008 at 12:22 pm #3440
Humayan
ParticipantHeres a version of the infix to postfix conversion I found on the web. The implementation is pretty typical ( but could be simpler ). Ask if you have any questions:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261/*************************************************************/<br />/*************************************************************/<br />/* ASSUMING INFIX EXPRESSION */<br />/* IS VALID */<br />/* !!!!!!!! */<br />/*************************************************************/<br />/*************************************************************/<br />/* include necessary preprocessor header files */<br />#include <stdio.h><br />#include <stdlib.h><br />#include <string.h><br />#include <ctype.h><br />/* constants */<br />#define TRUE 1<br />#define FALSE 0<br />/* structure for stack */<br />typedef struct<br />{<br />char data[20]; /* array to hold stack contents */<br />int tos; /* top of the stack pointer */<br />} STACK;<br /><br />/* function prototypes */<br />void initStack(STACK *stack);<br />void get_infix(char infix[]);<br />void convertToPostfix(char infix[], char postfix[]);<br />int isOperator(char c);<br />int precedence(char operator1, char operator2);<br />int pred_level(char ch);<br />void push(STACK *stack, char value);<br />char pop(STACK *stack);<br />char stackTop(STACK *stack);<br />int isEmpty(STACK *stack);<br />int isFull(STACK *stack);<br />void printResult(char infix[], char postfix[]);<br />void print_msg(void);<br />/* program entry point */<br />int main(void)<br />{<br />char infix[20], postfix[20]="";<br />/* convert from infix to postfix main function */<br />convertToPostfix(infix, postfix);<br />/* display the postfix equivalent */<br />infix[strlen(infix)-2] = ' ';<br />printResult(infix, postfix);<br />return EXIT_SUCCESS;<br />}<br />/* initalise the stack */<br />void initStack(STACK *stack)<br />{<br />stack->tos = -1; /* stack is initially empty */<br />}<br />/* get infix expression from user */<br />void get_infix(char infix[])<br />{<br />int i;<br />printf("Enter infix expression below (max 18 characters excluding spaces) : n");<br />fflush(stdin);<br />/* to read in only 18 characters excluding spaces */<br />for ( i=0; i<18; )<br />{<br />if ( (infix = getchar()) == 'n' )<br />{<br />i++;<br />break;<br />}<br />else if ( !(isspace(infix)) )<br />i++;<br />}<br />infix = ' ';<br />}<br />/* convert the infix expression to postfix notation */<br />void convertToPostfix(char infix[], char postfix[])<br />{<br />int i, length;<br />int j=0;<br />char tos_ch;<br />STACK stack;<br />initStack(&stack); /* initialise stack */<br />get_infix(infix); /* get infix expression from user */<br />length = strlen(infix);<br />/* if strlen if infix is more than zero */<br />if ( length )<br />{ <br />push(&stack, '(');<br />strcat(infix, ")");<br />length++;<br /><br />for ( i=0; i<length; i++ )<br />{<br />/* if current operator in infix is digit */<br />if ( isdigit(infix) )<br />{<br />postfix[j++] = infix;<br />}<br />/* if current operator in infix is left parenthesis */<br />else if ( infix == '(' )<br />{<br />push(&stack, '(');<br />}<br />/* if current operator in infix is operator */<br />else if ( isOperator(infix) )<br />{<br />while ( TRUE )<br />{<br />/* get tos */<br />tos_ch = stackTop(&stack);<br />/* no stack left */<br />if ( tos_ch == ' ' )<br />{<br />printf("nInvalid infix expressionn");<br />print_msg();<br />exit(1);<br />}<br />else<br />{<br />if ( isOperator(tos_ch) )<br />{<br />if ( pred_level(tos_ch) >= pred_level(infix) )<br />postfix[j++] = pop(&stack);<br />else<br />break;<br />}<br />else<br />break;<br />}<br />}<br />push(&stack, infix);<br />}<br />/* if current operator in infix is right parenthesis */<br />else if ( infix == ')' )<br />{<br />while ( TRUE )<br />{<br />/* get tos */<br />tos_ch = stackTop(&stack);<br />/* no stack left */<br />if ( tos_ch == ' ' )<br />{<br />printf("nInvalid infix expressionn");<br />print_msg();<br />exit(1);<br />}<br />else<br />{<br />if ( tos_ch != '(' )<br />{<br />postfix[j++] = tos_ch;<br />pop(&stack);<br />}<br />else<br />{<br />pop(&stack);<br />break;<br />}<br />}<br />}<br />continue;<br />}<br />}<br />}<br />postfix[j] = ' ';<br />}<br />/* determine if c is an operator */<br />int isOperator(char c)<br />{<br />if ( c == '+' || c == '-' || c == '*' ||<br />c == '/' || c == '%' || c == '^' )<br />{<br />return TRUE;<br />}<br />else<br />return FALSE;<br />}<br />/* determine precedence level */<br />int pred_level(char ch)<br />{<br />if ( ch == '+' || ch == '-' )<br />return 1;<br />else if ( ch == '^' )<br />return 3;<br />else<br />return 2;<br />}<br />/* determine if the precedence of operator1 is less than,<br />equal to, greater than the precedence of operator2 */<br />int precedence(char operator1, char operator2)<br />{<br />if ( pred_level(operator1) > pred_level(operator2) )<br />return 1;<br />else if ( pred_level(operator1) < pred_level(operator2) )<br />return -1;<br />else<br />return 0;<br />}<br />/* push a value on the stack */<br />void push(STACK *stack, char value)<br />{<br />if ( !(isFull(stack)) )<br />{<br />(stack->tos)++;<br />stack->data[stack->tos] = value;<br />}<br />}<br />/* pop a value off the stack */<br />char pop(STACK *stack)<br />{<br />char ch;<br />if ( !(isEmpty(stack)) )<br />{<br />ch = stack->data[stack->tos];<br />(stack->tos)--;<br />return ch;<br />}<br />else<br />return ' ';<br />}<br />/* return the top value of the stack without popping the stack */<br />char stackTop(STACK *stack)<br />{<br />if ( !(isEmpty(stack)) )<br />return stack->data[stack->tos];<br />else<br />return ' ';<br />}<br />/* determine if stack is empty */<br />int isEmpty(STACK *stack)<br />{<br />/* empty */<br />if ( stack->tos == -1 )<br />return TRUE;<br />/* not empty */<br />else<br />return FALSE;<br />}<br />/* determine if stack is full */<br />int isFull(STACK *stack)<br />{<br />/* full */<br />if ( stack->tos == 19 )<br />return TRUE;<br />/* not full */<br />else<br />return FALSE;<br />}<br />/* display the result postfix expression */<br />void printResult(char infix[], char postfix[])<br />{<br />/*system("cls");*/<br />printf("nn");<br />printf("Infix notation : %sn", infix);<br />printf("Postfix notation: %snn", postfix);<br />print_msg();<br />}<br />/* print exit message */<br />void print_msg(void)<br />{<br />printf("Hit <return> to exit......");<br />fflush(stdin);<br />getchar();<br />}</return>
- AuthorPosts
- The forum ‘C Programming’ is closed to new topics and replies.