为什么这个程序不适用于负数的电源? [英] Why is this program not working for power with negative numbers?

查看:66
本文介绍了为什么这个程序不适用于负数的电源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下:



代码的逻辑在函数evaluate()中。该代码适用于+ ve编号,但不适用于-ve数字。可能是什么问题?

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?

#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;
}





我的尝试:



输入:

(x-2)^ 3-(x-3)

-1

1000

----------

输出:

应该是-23,但我得-1。



What I have tried:

Input:
(x-2)^3-(x-3)
-1
1000
----------
Output:
Should have been -23, but I am getting -1.

推荐答案

使用调试器。

调试器允许您逐行执行,检查变量,您将看到有一个点它停止做你期望的事。

调试器 - 维基百科,免费的百科全书 [ ^ ]

在Visual Studio 2010中进行调试 - 初学者指南 [ ^ ]



使用调试器,确保代码真正符合你的想法。



尽量减少代码足以显​​示问题。
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.


我没有最终的解决方案,因为仅通过检查代码来执行程序就太难了。但我有一些提示:



不要使用全局变量。在函数中使用局部变量。这使得更容易阅读和调试代码。



似乎没有代码可以处理减法操作。当解析器遇到' - '字符时会发生什么?它将它视为数字并减去48,这将是-3!



您传递值[value_size] 到你的评估函数,在那里用作 num1 。但是在调用函数时,该数组成员未初始化。之后将使用返回值进行初始化。为数组项分配值后,您将递增索引,使其指向下一个尚未初始化的项。
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.


这篇关于为什么这个程序不适用于负数的电源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆