473,387 Members | 3,787 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Switch case

24
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.

Expand|Select|Wrap|Line Numbers
  1.     switch(inSign)
  2.     {
  3.         case '-': 
  4.         case '+':
  5.         case '*':
  6.         case '/':
  7.  
  8.     }
  9.  
Oct 25 '06 #1
4 4753
RADAR
21
yes you can...
define the arithmetic operators as character and prompt the user to enter the artihmetic operator
printf("\n Arithmetic operation ( + - / * % ) : ");
scanf("%c",&operation);
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*/
}
Oct 25 '06 #2
cool17
24
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(string,int,int);



expContainer createExpression()
{
expContainer exp;

/*allocate memory*/
exp = (expContainer)malloc(sizeof(struct expressions));
if(exp != NULL)
{
/*set inital terms to zero*/
exp->numberOfTerms = 0;
return exp; /*return pointer*/
}
else
printf("Out Of Heap!!!\n");
return NULL;

}

void string2Expression(string inString,expContainer* 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,sizeof(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,sizeof(char)*j+1);
tempString[j] = inString[i];
j++;
}

if(j != 0)
{
/*save the term into expression format*/
if((*inExp)->numberOfTerms == 0)
(*inExp)->exp = (xContainer*)calloc(3,sizeof(xContainer));
else
(*inExp)->exp = (xContainer*)realloc((*inExp)->exp,sizeof(xContainer)*((*inExp)->numberOfTerms+1));


(*inExp)->exp[(*inExp)->numberOfTerms] = splitTerm(tempString,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,sizeof(char)*0);
}
}
}

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

part = (xContainer)malloc(sizeof(struct 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,sizeof(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(tempString) > 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,sizeof(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;


}
Oct 25 '06 #3
RADAR
21
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,
Oct 25 '06 #4
cool17
24
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
Oct 25 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

35
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except...
10
by: clueless_google | last post by:
hello. i've been beating my head against a wall over this for too long. setting the variables 'z' or 'y' to differing numbers, the following 'if/else' code snippet works fine; however, the ...
7
by: Colin King | last post by:
Amusingly, one can use a while(0) statement to allow one to perform a switch statement without breaks. The while (0) enables the continue statements to break out of the switch. Ugly and...
3
by: pgraeve | last post by:
I am a convert from VB to C# so bear with me on this "conversion" question C# switch statement seems to be the closest relative to VB's Select Case. I used VB's Select Case statement liberally. ...
10
by: Evie | last post by:
I understand that when a switch statement is used without breaks, the code continues executing even after a matching case is found. Why, though, are subsequent cases not evaluated? I wrote a...
11
by: ME | last post by:
In C# the following code generates a compiler error ("A constant value is expected"): public void Test(string value) { switch (value) { case SimpleEnum.One.ToString(): MessageBox.Show("Test...
6
by: asit | last post by:
please modify to get the correct output.(switch case is compulsory) #include <stdio.h> int main() { char ch; printf("Enter any character : "); ch=getch(); switch(ch)
7
by: Rohit | last post by:
Hi, I am working on a switch module which after reading voltage through a port pin and caterogizing it into three ranges(open,low or high), passes this range to a function switch_status() with...
11
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
Can switch statements be nested? I've got a large routine that runs off of a switch statement. If one of the switches in switch #1 is true, that enters switch statement #2. Some of the...
13
by: Satya | last post by:
Hi everyone, This is the first time iam posting excuse me if iam making any mistake. My question is iam using a switch case statement in which i have around 100 case statements to compare. so...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.