Home Forums C Programming function

Viewing 10 reply threads
  • Author
    Posts
    • #2130
      VidaVOXeql
      Participant

      hello..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…

    • #3431
      Humayan
      Participant

      binary conversion:
       

      #include 
      void main()
      {
       int i;
       cout << "Enter a number to convert ";
       cin >> i;
       int binaryArray [ 20 ] ; //whatever
       int j = 0;
       while ( i )
       {
        binaryArray [ j ++ ] = i % 2 ;
        i /= 2;
       }
       
       cout << endl << "Binary Version: " ;
       while ( j -- > 0 )
        cout << binaryArray [ j  ];
       cout << endl;
      }

       
       
      hex conversion:

      #include 
      void printHex ( int i );
      void main()
      {
       cout << "Enter a character: " ;
       char ch;
       cin >> ch;
       cout << endl << "Here is hex form of ASCII: " << endl;
       printHex ( ( int ) ch );
       cout << endl;
       
       
       
      }
      void printHex ( int i )
      {
       char base16 [ 17 ] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' };
       
       if ( i == 0 )
        return;
       else
       {
        printHex ( i / 16 );
        cout << base16 [ i % 16 ];
       }
      }

       
      Is the merge sort on an array??

    • #3432
      VidaVOXeql
      Participant

      ahmm 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…

    • #3433
      Humayan
      Participant

      merge sort:
       

      /****************************************************************
      * File Name : c:programshelpmergeSort.cpp
      * Date : September,3,2008
      * Comments : new project
      * Compiler/Assembler :
      * Program Shell Generated At: 6:27:41 p.m.
      =-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/

      #include < iostream >
      #include < stdlib.h >
      #include < time.h >
      //#include < math.h >
      //#include < iomanip >
      //#include < ctype.h >
      #define MAX_NUMBER 50
      #define ARRAY_SIZE 20
      using namespace std;

      //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ FUNCTION PROTOTYPES @@@@@@@@@@@@@@@@@@@@@@@@@@
       void mergeSort ( int * array , int * temp , int left , int right );
       void mergeSort ( int * array );
       void merge ( int * array , int * temp , int left , int right , int rightEnd );
       void initialize ( int * array , int size );
      //##################################################################################

       //main function ******************************
      int main ( )
      {
          int array [ ARRAY_SIZE ];
          initialize ( array , ARRAY_SIZE );
          int i = 0;
          while ( i < ARRAY_SIZE )
          {
              printf ( "%i" , array [ i ] );
              printf ( "n" );
              i ++;
          }
          mergeSort ( array );
          i = 0;
          while ( i < ARRAY_SIZE )
          {
              printf ( "%i" , array [ i ] );
              printf ( "n" );
              i ++;
          }
       return 0 ;
      }

      /******************************* FUNCTION DEFINITION ******************************
       Name : mergeSort
       Parameters :
        array a(n) int * ,
        temp a(n) int * ,
        left a(n) int ,
        right a(n) int

       Returns: Void type
       Comments:
       
      ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
      void mergeSort ( int * array , int * temp , int left , int right )
      {
          if ( left < right )
          {
              int center = ( left + right ) / 2;
              mergeSort ( array , temp , left , center );
              mergeSort ( array , temp , center + 1 , right );
              merge ( array , temp , left , center + 1 , right );
          }
          return;
      }
      /******************************* FUNCTION DEFINITION ******************************
       Name : mergeSort
       Parameters :
        array a(n) int *

       Returns: Void type
       Comments:
       
      ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
      void mergeSort ( int * array )
      {
          int temp [ ARRAY_SIZE ];
          mergeSort ( array , temp , 0 , ARRAY_SIZE - 1 );

       return;
      }
      /******************************* FUNCTION DEFINITION ******************************
       Name : merge
       Parameters :
        array a(n) int * ,
        temp a(n) int * ,
        left a(n) int ,
        right a(n) int ,
        rightEnd ( rightEnd )

       Returns: Void type
       Comments:
       
      ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
      void merge ( int * array , int * temp , int left , int right , int rightEnd )
      {
          int leftEnd = right - 1;
          int tempPos = left;
          int numberElements = rightEnd - left + 1;
         
          while ( left <= leftEnd && right <= rightEnd )
              if ( array [ left ] <= array [ right ] )
                  temp [ tempPos ++ ] = array [ left ++ ];
              else
                  temp [ tempPos ++ ] = array [ right ++ ];
          while ( left <= leftEnd )
              temp [ tempPos ++ ] = array [ left ++ ];
             
          while ( right <= rightEnd )
              temp [ tempPos ++ ] = array [ right ++ ];
         
          int i = 0;
          while ( i < numberElements )
          {
              array [ rightEnd ] = temp [ rightEnd ] ;
              rightEnd --;
          }
       
       return;
      }
      /******************************* FUNCTION DEFINITION ******************************
       Name : test
       Parameters :
        array a(n) int * ,
        size a(n) int

       Returns: Void type
       Comments:
       
      ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
      void initialize ( int * array , int size )
      {
         
          srand ( time ( NULL ) );
       int i = 0;
       while ( i < size )
          {
              array [ i ] = rand () % MAX_NUMBER;
              i ++;
          } 

       return;
      }

       

    • #3434
      Humayan
      Participant

      are you supposed to use a stack data structure for the postfix to infix conversion?? STL or homemade??

    • #3435
      VidaVOXeql
      Participant

      ahm..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,,,,

    • #3436
      VidaVOXeql
      Participant

      ahm..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…

    • #3437
      VidaVOXeql
      Participant

      ahm,,,sir,,,tnx,,,,4 the reply,,
      ahm,,,,wat do you mean b,,,STL?
      anhm,,,,its ok,,,when you use the stack structure..
      tnx….

    • #3438
      Humayan
      Participant

      So you need a version of the postfix program with a homemade stack? I’ll see if I can write one today/tommorow…….

    • #3439
      VidaVOXeql
      Participant

      tnxxxx….po…kua….

    • #3440
      Humayan
      Participant

      Heres 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:
       

      /*************************************************************/
      /*************************************************************/
      /*                 ASSUMING INFIX EXPRESSION                 */
      /*                             IS VALID                          */
      /*                              !!!!!!!!                          */
      /*************************************************************/
      /*************************************************************/
      /* include necessary preprocessor header files */
      #include
      #include
      #include
      #include
      /* constants */
      #define TRUE 1
      #define FALSE 0
      /* structure for stack */
      typedef struct
      {
            char data[20];  /* array to hold stack contents */
            int tos;        /* top of the stack pointer */
      } STACK;

      /* function prototypes */
      void initStack(STACK *stack);
      void get_infix(char infix[]);
      void convertToPostfix(char infix[], char postfix[]);
      int isOperator(char c);
      int precedence(char operator1, char operator2);
      int pred_level(char ch);
      void push(STACK *stack, char value);
      char pop(STACK *stack);
      char stackTop(STACK *stack);
      int isEmpty(STACK *stack);
      int isFull(STACK *stack);
      void printResult(char infix[], char postfix[]);
      void print_msg(void);
      /* program entry point */
      int main(void)
      {
            char infix[20], postfix[20]="";
            /* convert from infix to postfix main function */
            convertToPostfix(infix, postfix);
            /* display the postfix equivalent */
            infix[strlen(infix)-2] = '';
            printResult(infix, postfix);
            return EXIT_SUCCESS;
      }
      /* initalise the stack */
      void initStack(STACK *stack)
      {
            stack->tos = -1;  /* stack is initially empty */
      }
      /* get infix expression from user */
      void get_infix(char infix[])
      {
            int i;
            printf("Enter infix expression below (max 18 characters excluding spaces) : n");
            fflush(stdin);
            /* to read in only 18 characters excluding spaces */
            for ( i=0; i<18; )
            {
                  if ( (infix = getchar()) == 'n' )
                  {
                        i++;
                        break;
                  }
                  else if ( !(isspace(infix)) )
                        i++;
            }
            infix = '';
      }
      /* convert the infix expression to postfix notation */
      void convertToPostfix(char infix[], char postfix[])
      {
            int i, length;
            int j=0;
            char tos_ch;
            STACK stack;
            initStack(&stack); /* initialise stack */
            get_infix(infix);  /* get infix expression from user */
            length = strlen(infix);
            /* if strlen if infix is more than zero */
            if ( length )
            {     
                  push(&stack, '(');
                  strcat(infix, ")");
                  length++;
                 
                  for ( i=0; i             {
                        /* if current operator in infix is digit */
                        if ( isdigit(infix) )
                        {
                              postfix[j++] = infix;
                        }
                        /* if current operator in infix is left parenthesis */
                        else if ( infix == '(' )
                        {
                              push(&stack, '(');
                        }
                        /* if current operator in infix is operator */
                        else if ( isOperator(infix) )
                        {
                              while ( TRUE )
                              {
                                    /* get tos */
                                    tos_ch = stackTop(&stack);
                                    /* no stack left */
                                    if ( tos_ch == '' )
                                    {
                                          printf("nInvalid infix expressionn");
                                          print_msg();
                                          exit(1);
                                    }
                                    else
                                    {
                                          if ( isOperator(tos_ch) )
                                          {
                                                if ( pred_level(tos_ch) >= pred_level(infix) )
                                                      postfix[j++] = pop(&stack);
                                                else
                                                      break;
                                          }
                                          else
                                                break;
                                    }
                              }
                              push(&stack, infix);
                        }
                        /* if current operator in infix is right parenthesis */
                        else if ( infix == ')' )
                        {
                              while ( TRUE )
                              {
                                    /* get tos */
                                    tos_ch = stackTop(&stack);
                                    /* no stack left */
                                    if ( tos_ch == '' )
                                    {
                                          printf("nInvalid infix expressionn");
                                          print_msg();
                                          exit(1);
                                    }
                                    else
                                    {
                                          if ( tos_ch != '(' )
                                          {
                                                postfix[j++] = tos_ch;
                                                pop(&stack);
                                          }
                                          else
                                          {
                                                pop(&stack);
                                                break;
                                          }
                                    }
                              }
                              continue;
                        }
                  }
            }
            postfix[j] = '';
      }
      /* determine if c is an operator */
      int isOperator(char c)
      {
            if ( c == '+' || c == '-' || c == '*' ||
                   c == '/' || c == '%' || c == '^' )
            {
                  return TRUE;
            }
            else
                  return FALSE;
      }
      /* determine precedence level */
      int pred_level(char ch)
      {
            if ( ch == '+' || ch == '-' )
                  return 1;
            else if ( ch == '^' )
                  return 3;
            else
                  return 2;
      }
      /* determine if the precedence of operator1 is less than,
         equal to, greater than the precedence of operator2 */
      int precedence(char operator1, char operator2)
      {
            if ( pred_level(operator1) > pred_level(operator2) )
                  return 1;
            else if ( pred_level(operator1) < pred_level(operator2) )
                  return -1;
            else
                  return 0;
      }
      /* push a value on the stack */
      void push(STACK *stack, char value)
      {
            if ( !(isFull(stack)) )
            {
                  (stack->tos)++;
                  stack->data[stack->tos] = value;
            }
      }
      /* pop a value off the stack */
      char pop(STACK *stack)
      {
            char ch;
            if ( !(isEmpty(stack)) )
            {
                  ch = stack->data[stack->tos];
                  (stack->tos)--;
                  return ch;
            }
            else
                  return '';
      }
      /* return the top value of the stack without popping the stack */
      char stackTop(STACK *stack)
      {
            if ( !(isEmpty(stack)) )
                  return stack->data[stack->tos];
            else
                  return '';
      }
      /* determine if stack is empty */
      int isEmpty(STACK *stack)
      {
            /* empty */
            if ( stack->tos == -1 )
                  return TRUE;
            /* not empty */
            else
                  return FALSE;
      }
      /* determine if stack is full */
      int isFull(STACK *stack)
      {
            /* full */
            if ( stack->tos == 19 )
                  return TRUE;
            /* not full */
            else
                  return FALSE;
      }
      /* display the result postfix expression */
      void printResult(char infix[], char postfix[])
      {
            /*system("cls");*/
            printf("nn");
            printf("Infix notation  : %sn", infix);
            printf("Postfix notation: %snn", postfix);
            print_msg();
      }
      /* print exit message */
      void print_msg(void)
      {
            printf("Hit to exit......");
            fflush(stdin);
            getchar();
      }
Viewing 10 reply threads
  • The forum ‘C Programming’ is closed to new topics and replies.