Hey , I urgently need a program to calculate a Bodmas Expression In C

Do not ask for code. We are not a coding service. We will help you fix your code.

If anyone posts a complete working solution for you, they are enabling cheaters.
If you use that code you are a cheater.

Read me

Do not ask for code. We are not a coding service. We will help you fix your code.

If anyone posts a complete working solution for you, they are enabling cheaters.
If you use that code you are a cheater.

Read me

If you are going to quote the BoilerPlate, quote the entire thing. He has at least 4 issues that it touches on...

[boilerplate_help_info]

Posting requests for help must be well thought out if you want help quickly and correctly.  Your post did not meet the criteria for quality help. You may get some posts, but are they going to be useful?  Check your post with these checkpoints - what is it [i]you[/i] missed:
[list=1]
[*]Ask a question that can be answered. Do not ask
- What's wrong with my code?
- Why doesn't this work?
- Anything else that does not give us useful information
[*]Post your code.  If we don't know what you did, how can we possibly help?
- Use [b]PROPER FORMATTING[/b] --  see this 
- Use CODE Tags so your formatting is preserved.
If we can't follow your code, it's difficult to help. We don't care that you're still working on it. If you want us to read it, it must be readable
[*]Explain what the code is supposed to do.  If we don't know where the target is, how can we help you hit it?
[*]Explain what actually happened! If we don't know where the arrow went when you shot it, how can we tell what went wrong and how far from the target you are?
[*]If you have errors, post them! We can't see your screen.  We can't read your mind. You need to tell us what happened.
[*]Do [b]not[/b] ask for code. We are not a coding service. We will help you fix your code. 
    If anyone posts working code for you, they are a cheater. 
    If you use that code [i]you[/i] are a cheater.
[*]Do [b]not[/b] bore us with how new you are. We can tell by your code.
- Do not apologize. We were all new, and unless you are completely 
  brain dead you will get better.
- Do not ask us to "take it easy on you."
- Do not say "I don't know what's going on." That's obvious since
  you posted for help. Use that time wisely by [b]explaining[/b] as best 
  you can so we can help.
[*][b]Do not post your requirements and nothing else. [/b]We view that as a lazy do-nothing student that wants us to do their work for them. That's cheating and we [i]will[/i] be hard on you.
[*]Do not attach files except when absolutely necessary. Most of us are not going to download file.  Add the information to your post.
[*][b]Do not tell us how urgent it is.[/b] Seriously, for us there is no urgency at all. Many that can help will ignore any URGENT or ASAP requests.
[/list]
Think more about your next post so we don't have to play 20 questions to get the info we need to help you.

[/boilerplate_help_info]

Hey..i made a prog for bodmas expression..bt i dunnot know how to make it reads brackets..

plzz help..

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
   char a[20];
   int i,j,k,n,l,m;
   int t,b=0,c=1,d;
   clrscr();
   printf("\n------m------\n\n For integers only..");
   printf("\n\n To Find : ");
   scanf("%s",&a);
   n=strlen(a);

   for(i=0;i<n;i++)
   {
     //j=i+1;
     if((i==0)&&(a[0]!='('))
	b=int(a[0])-48;

     if((i==0)&&(a[0]=='-'))
     {
	b=0-int(a[1])-48+96;
	i+=2;
     }
g:
{
     switch(a[i])
     {
       case '(' :  j=i+1;
		   switch(a[j+1])                  //  48  0
		   {                               //  49  1
		      case '*' : c=(int(a[j])-48)*(int(a[j+2])-48);
				 break;
		      case '+' : c=(int(a[j])-48)+(int(a[j+2])-48);
				 printf(" %d \n",c);
				 break;
		      case '-' : c=(int(a[j])-48)-(int(a[j+2])-48);
				 break;
		      case '/' : c=(int(a[j])-48)/(int(a[j+2])-48);
				 break;
		   }

		   if(a[j-2]=='+')
		   {printf(" %d  %d  ",b,c);   b=b+c; }
		   else if(a[j-2]=='-')
		      b=b-c;
		   else if(a[j-2]=='*')
		      b=b*c;
		   else if(a[j-2]=='/')
		      b=b/c;
		   else
		      b=c;

		   i=i+3;
		   break;
	 case '1':
	 case '2':
	 case '3':
	 case '4':
	 case '5':
	 case '6':
	 case '7':
	 case '8':
	 case '9':
		   j=i;
		   switch(a[j+1])           
		   {                        
		      case '*' : if(b==0)
				    b=1;
				 b=b*(int(a[j+2])-48);
				 break;
		      case '+' : b=b+(int(a[j+2])-48);
				 break;
		      case '-' : b=b-(int(a[j+2])-48);
				 break;
		      case '/' : b=b/(int(a[j+2])-48);
				 break;
		   }
		   i=i+2;
		   break;

       case '+' :
				 if(a[i+1]!='(')
				   b=b+(int(a[i+1])-48);
				 else
				 {
				   goto g;
				
				 }
				 i=i+1;
				 break;


       case '-' :
				 if(a[i+1]!='(')
				   b=b-(int(a[i+1])-48);
				 i=i+1;
				 break;


       case '*' :

		  if(a[i-2]=='+')
		  {
		       c=int(a[i-1]-48)*int(a[i+1]-48);
		       b=b-int(a[i-1]-48)+c;
		       break;
		  }
		  else if(a[i-2]=='-')
		  {
		       c=a[i-1]*a[i+1];
		       b=b+a[i-1]+c;
		       break;
		  }
		   else


		       {  if(i==1)
			  {
			     b=(int(a[i-1])-48)*(int(a[i+1])-48);
			     break;
			  }
			  else
			  {
			    //b=b*(int(a[i+1])-48);
			    if(a[i+1]!='(')
			      b=b*(int(a[i+1])-48);
			    else if(a[i+1]=='(')
			      {i++; goto g;}
			    break;
			  }
		       }


       case '/' :
		  if(a[i-2]=='+')
		  {
		       c=int(a[i-1]-48)/int(a[i+1]-48);
		       b=b-int(a[i-1]-48)+c;
		       break;
		  }
		  else if(a[i-2]=='-')
		  {
		       c=a[i-1]/a[i+1];
		       b=b+a[i-1]+c;
		       break;
		  }
		   else


		       {  if(i==1)
			  {
			     b=(int(a[i-1])-48)/(int(a[i+1])-48);
			     break;
			  }
			  else
			  {
			    //b=b*(int(a[i+1])-48);
			    b=b/(int(a[i+1])-48);
			    break;
			  }
		       }


       case ')' :  j=i;
		   switch(a[j+1])               
		   {                            
		      case '*' : b=b*(int(a[j+2])-48);
				 break;
		      case '+' : b=b+(int(a[j+2])-48);
				 break;
		      case '-' : b=b-(int(a[j+2])-48);
				 break;
		      case '/' : b=b/(int(a[j+2])-48);
				 break;
		   }
		   i=i+2;
		   break;

}


     }

   }
    printf("\n\n %d",b);

   getch();
}

Well your program does read brackets but it doesn't always output the right answer now does it?

You use a lot of unnecessary expressions in solving an equation
It's hard to narrow down which part of your code makes the problem

use comments on your codes for us to check properly what your doing

(6+2) : it will read it...
but (6*2+4) : it wnt...

see the
case '('
part..

Hey guys ,
plzz help modify my code..

Hey guys ,
plzz help modify my code..

I don't see anywhere where you

use comments on your codes for us to check properly what your doing

If you are getting suggestions, I would suggest you don't ignore them. It just tells us all you want us to do is give you a working program.

Try outputting values at key points in your program to see what your program is actually doing. If the values aren't what you expect, you know what part of the code to look at.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[20];
int i,j,k,n,l,m;
int t,b=0,c=1,d;
clrscr();
printf("\n------m------\n\n For integers only..");
printf("\n\n To Find : ");
scanf("%s",&a);
n=strlen(a);
a[x] - 48 : has been done to convert a number from character(ascii) to integer...

for(i=0;i<n;i++)
{
if((i==0)&&(a[0]!='('))
b=int(a[0])-48;

if((i==0)&&(a[0]=='-'))
{
b=0-int(a[1])-48+96;
i+=2;
}
g:
{
switch(a[i])
{
case '(' : j=i+1;
switch(a[j+1]) //            Evaluating brackets  eg.  (4+6)
{ // 49 1
case '*' : c=(int(a[j])-48)*(int(a[j+2])-48);          //   multiplying in bracket
break;
case '+' : c=(int(a[j])-48)+(int(a[j+2])-48);          //   adding in bracket
printf(" %d \n",c);
break;
case '-' : c=(int(a[j])-48)-(int(a[j+2])-48);          //   subtracting in bracket
break;
case '/' : c=(int(a[j])-48)/(int(a[j+2])-48);          //   dividing in bracket
break;
}

//          checking out pre bracket signs.. eg.  4*(7+3)

if(a[j-2]=='+')
{printf(" %d %d ",b,c); b=b+c; }
else if(a[j-2]=='-')
b=b-c;
else if(a[j-2]=='*')
b=b*c;
else if(a[j-2]=='/')
b=b/c;
else
b=c;

i=i+3;
break;
case '1':              //   in case the current element is a no. and not a sign
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
j=i;
switch(a[j+1])
{
case '*' : if(b==0)
b=1;
b=b*(int(a[j+2])-48);
break;
case '+' : b=b+(int(a[j+2])-48);
break;
case '-' : b=b-(int(a[j+2])-48);
break;
case '/' : b=b/(int(a[j+2])-48);
break;
}
i=i+2;
break;

case '+' :           //  if current element is a + sign..
if(a[i+1]!='(')
b=b+(int(a[i+1])-48);
else
{
goto g;

}
i=i+1;
break;


case '-' :          //  if current element is a - sign..
if(a[i+1]!='(')
b=b-(int(a[i+1])-48);
i=i+1;
break;


case '*' :          //  if current element is a * sign..

 //         in this case we'll take bodmas into account...
 //    eg.  4+2*3  will first be evaluated as 4+2 = 6   and then it will check
 //         the * sign after which it will subtract back 2 and then multiply 
 //         2 and 3    which will later be added to 4 to get the desired result..


if(a[i-2]=='+')
{
c=int(a[i-1]-48)*int(a[i+1]-48);
b=b-int(a[i-1]-48)+c;
break;
}
else if(a[i-2]=='-')
{
c=a[i-1]*a[i+1];
b=b+a[i-1]+c;
break;
}
else


{ if(i==1)
{
b=(int(a[i-1])-48)*(int(a[i+1])-48);
break;
}
else
{
//b=b*(int(a[i+1])-48);
if(a[i+1]!='(')
b=b*(int(a[i+1])-48);
else if(a[i+1]=='(')
{i++; goto g;}
break;
}
}


case '/' :         //     same as *
if(a[i-2]=='+')
{
c=int(a[i-1]-48)/int(a[i+1]-48);
b=b-int(a[i-1]-48)+c;
break;
}
else if(a[i-2]=='-')
{
c=a[i-1]/a[i+1];
b=b+a[i-1]+c;
break;
}
else


{ if(i==1)
{
b=(int(a[i-1])-48)/(int(a[i+1])-48);
break;
}
else
{
//b=b*(int(a[i+1])-48);
b=b/(int(a[i+1])-48);
break;
}
}


case ')' : j=i;                //  end of bracket..not working..
switch(a[j+1])
{
case '*' : b=b*(int(a[j+2])-48);
break;
case '+' : b=b+(int(a[j+2])-48);
break;
case '-' : b=b-(int(a[j+2])-48);
break;
case '/' : b=b/(int(a[j+2])-48);
break;
}
i=i+2;
break;

}


}

}
printf("\n\n %d",b);

getch();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.