I have written a calculater that gets an expression from the user and change it to polish form and then calculates the result it can calculate cos,sin,tan cot,+,-,*,/
the first part is very similar to a program i saw in web(function ReversePolish) it must be able to get () . it does work properly if you put everything in paranthesis however once you omit the paranthesis it does'nt work right. for example:
it can calculate: (1+(2*3))
but when you write it like this: 1+2*3 it gives wrong answer.
I will send my code
can you plz help me? I have short time

Attachments
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>

#define LEN 100

void initstack(int);
void push(int, int);  // second argument is a flag
int pop(int);         // argument is a flag

static int *s1; // stack for numbers
static int *s2; // stack for operators
static int N1;  // number stack counter
static int N2;  // operator stack counter

int PolishReverse(char polish[])              //after changing the expresion to polish notation it shows
{                                             //sin with s,cos with by c,tan with t,cot with C.

// flag = 0 will do operations on s1
// flag = 1 will do operations on s2

	char input[LEN];

	puts("Enter an expresion to calculate:");
	scanf("%s", input);

	int maxN = strlen(input);
	initstack(maxN);

	int i = 0;
	int k = 0;

	while (i < maxN)
	{
		if (input[i] == '*' || input[i] == '+'|| input[i]=='-'|| input[i]=='/'|| input[i]=='s'|| input[i]=='c'|| input[i]=='t'|| input[i]=='C'){
			push(input[i], 1);
			if(input[i]=='s'|| input[i]=='c'|| input[i]=='t'|| input[i]=='C'){
				i=i+2;
			}
		}
		if ((input[i] >= '0'&& input[i]<='9')||input[i]=='.')
		{
			push(input[i], 0);
			polish[k]=pop(0);
			printf("%c", polish[k]);
			if(input[i+1]=='+'|| input[i+1]=='*'|| input[i+1]=='/'|| input[i+1]=='-'|| input[i+1]=='s'|| input[i+1]=='c'|| input[i+1]=='t'|| input[i+1]=='C'){
				k++;
				polish[k]=' ';
				printf("%c",polish[k]);
			}
			k++;
		}
		if (input[i] == ')'){
			polish[k]=' ';
			printf("%c",polish[k]);
			k++;
			polish[k]=pop(1);
			printf("%c", polish[k]);
			k++;
			polish[k]=' ';
			printf("%c",polish[k]);
			k++;
			}
		i++;
	}
	puts("");
	return k;
}


void initstack(int maxN)
{
	s1 = (int *)malloc(maxN*sizeof(int));
	s2 = (int *)malloc(maxN*sizeof(int));
}

void push(int item, int flag)
{
	if (flag == 0)
		s1[N1++] = item;
	else
		s2[N2++] = item;
}

int pop(int flag)
{
	if (flag == 0)
		return s1[--N1];
	else
		return s2[--N2];
}


int convert_num(char a){

	int numbers[]={0,1,2,3,4,5,6,7,8,9};
	char characters[]={"0123456789"};

	int b;
	int i;
	for(i=0;i<10;i++){
		if(a==characters[i]){
			b=numbers[i];
		}
	}
	return b;
}

main(){

	clrscr();

	char input[100]="";        //for storing the input.
	int fraction[10]={0};      //for holdint fraction part of the numbers given.
	int round[10]={0};         //for holding the integer part of the numbers given.
	float num[5]={0};
	float a=0;
	float b=0;
	float integer=0;
	float fract=0;
	float result=0;            //for storing the result in each state.
	int j=0;
	int k=0;
	int i=0;                   //input counter.
	int x=0;
	int count;

	int PolishReverse(char input[]);    //this function changes the expresion given by the user to polish notation and prints the polish expresion.
	int convert_num(char);              //this function changes the characters to numbers.

	PolishReverse(input);

		while(input[i]!=NULL){

		if((input[i]=='0')||(input[i]=='1')||(input[i]=='2')||(input[i]=='3')||(input[i]=='4')||(input[i]=='5')||(input[i]=='6')||(input[i]=='7')||(input[i]=='8')||(input[i]=='9')){
			round[j]=convert_num(input[i]);
			while((input[i+1]=='0')||(input[i+1]=='1')||(input[i+1]=='2')||(input[i+1]=='3')||(input[i+1]=='4')||(input[i+1]=='5')||(input[i+1]=='6')||(input[i+1]=='7')||(input[i+1]=='8')||(input[i+1]=='9')){
				i++;
				j++;
				round[j]=convert_num(input[i]);
			}
			i++;
			if(input[i]=='.'){
				i++;
				while((input[i]=='0')||(input[i]=='1')||(input[i]=='2')||(input[i]=='3')||(input[i]=='4')||(input[i]=='5')||(input[i]=='6')||(input[i]=='7')||(input[i]=='8')||(input[i]=='9')){
					fraction[k]=convert_num(input[i]);
					i++;
					k++;
				}
			}


			while(j>=0){
				a=round[x]*(pow(10,j));
				x++;
				j--;
				integer=integer+a;
			}
			x=0;
			while(x<k){
				b=fraction[x]*(pow(10,-(x+1)));
				x++;
				fract=fract+b;
			}
			num[count]=integer+fract;
			count++;
			i++;
			j=0;
			k=0;
			x=0;
			fract=0;
			integer=0;
		}
		else if((input[i]=='+')||(input[i]=='-')||(input[i]=='*')||(input[i]=='/')||(input[i]=='c')||(input[i]=='s')||(input[i]=='t')||(input[i]=='C')){

			switch(input[i]){
				case '+':
					result=num[count-1]+num[count-2];
					i=i+2;
					num[count-1]=result;
					break;
				case '-':
					result=num[count-1]+num[count-2];
					i=i+2;
					num[count-1]=result;
					break;
				case '*':
					result=num[count-1]*num[count-2];
					i=i+2;
					num[count-1]=result;
					break;
				case '/':
					if(num[count-1]==0){
						printf("zero diversion,illegal input\n");
						getch();
						return 0;
					}
					else{
						result=(float)num[count-2]/num[count-1];
						i=i+2;
						num[count-1]=result;
						break;
						}
				case 's':
					result=sin(num[count-1]);
					i=i+2;
					num[count-1]=result;
					break;
				case 'c':
					result=cos(num[count-1]);
					i=i+2;
					num[count-1]=result;
					break;
				case 't':
					result=tan(num[count-1]);
					i=i+2;
					num[count-1]=result;
					break;
				case 'C':
					if(count[num-1]==0){
						printf("cot(0) is undefined.\n");
						getch();
						return 0;
					}
					else{
						result=(float)1/tan(num[count-1]);
						i=i+2;
						num[count-1]=result;
						break;
						}
			}
		}
		else{
			printf("error,illegal input\n");
			getch();
			return 0;
		}
	}
	printf("the result is %f",result);
	getch();
	return 0;
}

i think use more codes but o don,t know that code but i have calculator to y dont u check this

where can i che it? and i want to use this code in another program which will draw the table in a way users ask and each part of the table is supose to work as a variable and this calculator is to be used on that. can you help me??
thank you for your respond.

Since you are using C, your file should be named "calculator.c", not "calculator.cpp".

The problem is that you haven't paid any attention to operator precedence. Line 37 tests all the operators as if they had the same precedence.

The whole point of RPN is that it makes precedence moot -- that is, operators are applied as they appear. In standard notation, however, it makes a great deal of difference.

You are not saving yourself any trouble to convert to RPN. You will still have to code against precedence properly when lexing the standard math program.

So your choices are:
1. Always use parentheses in the standard formulations
2. Write something that handles precedence (in which case you might as well forget the RPN stuff and just calculate the answer as you go). I recommend you google through "recursive descent" a little. This will be a bit of a learning curve. The wikipedia has some stuff which would be a good starting point for you.

Good luck.

This article has been dead for over six months. Start a new discussion instead.