Switch case

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cool17
    New Member
    • Oct 2006
    • 24

    Switch case

    Is it possible to use sign like +,-,*,/ in the switch case?


    is it something like this, but it cant works. So can someone help me with it.

    Code:
    	switch(inSign)
    	{
    		case '-': 
    		case '+':
    		case '*':
    		case '/':
    		
    	}
  • RADAR
    New Member
    • Oct 2006
    • 21

    #2
    yes you can...
    define the arithmetic operators as character and prompt the user to enter the artihmetic operator
    printf("\n Arithmetic operation ( + - / * % ) : ");
    scanf("%c",&ope ration);
    fflush(stdin);

    switch( operation )
    {
    case '+': result= number1 + number2; break;
    case '-': result = number1 - number2; break;
    case '*': result= number * number2; break;
    case '/':
    {
    if( number2 > 0)/*resultant must exist so no division with zero*/
    result = number1/number2;
    else
    printf("\n ERROR : Division With Zero");
    } ; break;
    default: printf("\n Enter an appropriate number");/*suppose user entered a character*/
    }

    Comment

    • cool17
      New Member
      • Oct 2006
      • 24

      #3
      Is this how i do the switch case for this program?


      #include "expression .h"
      #include <stdlib.h>
      #include <stdio.h>
      #include <string.h>


      xContainer splitTerm(strin g,int,int);



      expContainer createExpressio n()
      {
      expContainer exp;

      /*allocate memory*/
      exp = (expContainer)m alloc(sizeof(st ruct expressions));
      if(exp != NULL)
      {
      /*set inital terms to zero*/
      exp->numberOfTerm s = 0;
      return exp; /*return pointer*/
      }
      else
      printf("Out Of Heap!!!\n");
      return NULL;

      }

      void string2Expressi on(string inString,expCon tainer* inExp)
      {
      string tempString;
      int length;
      int i=0, j=0,sign=0;

      tempString = (string)calloc( 1,sizeof(char)) ;
      /*get string length*/
      length = strlen(inString );

      /*determine first term sign*/

      if(inString[0] == '-')
      sign = '-';

      else if(inString[0] == '+')
      sign = 1;

      else if (inString[0] == '(')
      sign = 2;


      /*The operation down there is to splite the whole string into separate term*/
      /*the term is split by detecting the sign*/

      for(i=0; i < length; i++)
      {
      /*pass if char not '+','-','*'and not '/' and current pos is not the end of the string*/
      if(inString[i] != '+' && inString[i] != '-' && inString[i] != '*' && inString[i] != '/' && i != length-1)
      {

      tempString = (char*)realloc( tempString,size of(char)*j+1);
      tempString[j] = inString[i];
      j++;
      continue;
      }
      else
      {
      /*to add the last value before the end of the string*/
      if(i == length-1)
      {
      tempString = (string)realloc (tempString,siz eof(char)*j+1);
      tempString[j] = inString[i];
      j++;
      }

      if(j != 0)
      {
      /*save the term into expression format*/
      if((*inExp)->numberOfTerm s == 0)
      (*inExp)->exp = (xContainer*)ca lloc(3,sizeof(x Container));
      else
      (*inExp)->exp = (xContainer*)re alloc((*inExp)->exp,sizeof(xCo ntainer)*((*inE xp)->numberOfTerms+ 1));


      (*inExp)->exp[(*inExp)->numberOfTerm s] = splitTerm(tempS tring,sign,j);
      (*inExp)->numberOfTerms+ +;
      if(inString[i] == '-')
      sign = '-';

      else if(inString[i] == '+')
      sign = '+';

      else if(inString[i] == '*')
      sign = '*';

      else
      sign = '/';

      }
      j = 0;
      tempString = (string)realloc (tempString,siz eof(char)*0);
      }
      }
      }

      /*function to handle term*/
      xContainer splitTerm(strin g inTerm,int Operator, int length)
      {
      string tempString;
      xContainer part;
      int xFlag=0,powerFl ag=0;
      int i,j=0;

      part = (xContainer)mal loc(sizeof(stru ct terms));
      part->KnowX = 0;
      part->positive = Operator;
      part->isX = 0;
      part->x_power = 1;
      part->x_times = 1;
      part->x_value = 0;

      tempString = (string)calloc( 1,sizeof(char)) ;

      /*function below is to determine x and square root*/
      for(i=0; i < length; i++)
      {
      if(inTerm[i] != 'x' && inTerm[i] != '^')
      {
      tempString = (string)realloc (tempString,siz eof(char)*j+1);
      tempString[j] = inTerm[i];
      j++;

      if(i == length-1 && !xFlag && !powerFlag)
      {
      part->x_value = atof(tempString );
      part->KnowX = 1;
      }
      continue;
      }
      else
      {
      if(inTerm[i] == 'x' && strlen(tempStri ng) > 0)
      {
      part->x_times = atof(tempString );
      xFlag = 1;
      }
      else if(inTerm[i] == 'x')
      xFlag = 1;

      if(inTerm[i] == '^')
      {
      powerFlag = 1;
      if(!xFlag)
      part->x_value = atof(tempString );
      }

      j = 0;
      tempString = (string)realloc (tempString,siz eof(char)*0);

      }
      }

      if(xFlag)
      {
      part->isX = 1;
      part->KnowX = 0;
      }

      if(powerFlag)
      part->x_power = atof(tempString );

      switch(Operator )
      {
      case '-':
      case '+':
      case '*':
      case '/':
      }


      return part;


      }

      Comment

      • RADAR
        New Member
        • Oct 2006
        • 21

        #4
        this header #include "expression .h"
        is defined as such a file or directory i suppose because when compiled, it is occured a problem.
        what compiler do you use?I mean C or C++ compiler or both? sometimes after execution there exists a problem in C++ compilers executing C.
        There are so many variables which made me confused truly.
        Respects,

        Comment

        • cool17
          New Member
          • Oct 2006
          • 24

          #5
          Originally posted by RADAR
          this header #include "expression .h"
          is defined as such a file or directory i suppose because when compiled, it is occured a problem.
          what compiler do you use?I mean C or C++ compiler or both? sometimes after execution there exists a problem in C++ compilers executing C.
          There are so many variables which made me confused truly.
          Respects,


          but i am including another file that i had recreated which is call expression.h as my ADT... i am using C compiler. Thank

          Comment

          Working...