Click here to Skip to main content
15,120,478 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My code is as below:

The logic for the code is in the function evaluate(). The code is working for +ve number, but not working for -ve numbers. What can be the issue?
C++
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>

int evaluate(char p[],int size, int num1);
void read_values();
void calc();
int values[100];
int i,temp,num,j,k,l=0,result=0,temp_position=0,values_size=0;
char p1[100],arr_temp[100],a[100];

int main(){
	scanf("%s",p1);
	read_values();
	calc();
	values[values_size]=evaluate(p1,100, values[values_size]);
	printf("%s\n",p1);
	for(j=0;j<values_size;j++)
	{
		printf("%d\n",values[j]);
	}
	return 0;//end of main
}
void read_values(){	
	do
	{
		scanf("%d",&temp);
		if(temp==1000)
			break;
		else
		{
			values[values_size]=temp;
			values_size++;
		}
	}
	while(values[values_size]!=1000);
}	

void calc(){
	k=0;
	for(l=0;p1[l]!='\0';l++)
	{	
		if((p1[l]=='('))//opening bracket encountered
		{	temp_position=0;
			while(p1[++l]!=')')
			{
				a[temp_position++]=p1[l];
			}
			arr_temp[k++]=evaluate(a,temp_position,values[values_size])+48;
		}
		else 
			arr_temp[k++]=p1[l];//copy directly until opening bracket		
	}
	values[values_size]=evaluate(arr_temp,100, values[values_size]);
	values_size++;
}

int evaluate(char p[],int size, int num1){
	for(i=0;p[i+1]!='\0';i++)
	{
		if(i==0)
		{
			if(p[0]>='a'&&p[0]<='z')
				result = num1;
			else
				result =p[0]-48;
		}
		if (p[i]=='^')
		{
			if(p[i+1]>='a'&&p[i+1]<='z')
				result=pow(result,num1);
			else
				result=pow(result,(p[i+1]-48));
		}
	}
	return result;
}


What I have tried:

Input:
(x-2)^3-(x-3)
-1
1000
----------
Output:
Should have been -23, but I am getting -1.
Posted
Updated 25-Jul-16 22:28pm
Comments
[no name] 26-Jul-16 4:30am
   
What is this mass of code supposed to do? This is called Q&A because clear questions get answers. Debugging is different and what you should learn.

Use the debugger.
The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

With the debugger, make sure what the code really do match what you think it should.

Try to reduce the code to just enough to exhibit the problem.
   
Comments
CPallini 26-Jul-16 3:25am
   
5
Patrice T 26-Jul-16 3:38am
   
Thank you again.
Member 11413685 26-Jul-16 3:43am
   
5??
CPallini 26-Jul-16 3:47am
   
I gave him five stars for his suggestion: you really need to use the debugger!
Patrice T 26-Jul-16 3:48am
   
People thinking that a solution is good or bad can upvote or downvote the solution.
See the stars on right of "Solution 1" 1or 2 stars is downvote, 4 or 5 stars is upvote.
I have no final solution because it would be too hard to follow the program execution by just examining the code. But I have a few tips:

Don't use global variables. Use local variables in your functions. This makes it much easier to read and debug the code.

It seems that there is no code that handles the subtraction operation. What happens when your parser encounters the '-' character? It treats it as digit and subtracts 48 which will be -3!

You are passing values[value_size] to your evaluate function which is used there as num1. But that array member is not initialised when calling the function. It will be initialised with the return value afterwards. Once you have assigned a value to an array item, you are incrementing the index so that it points to the next not yet initialised item.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900