C程序检查paranthesis的平衡 [英] C program to check the balancing of paranthesis

查看:139
本文介绍了C程序检查paranthesis的平衡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了ac proram以检查paranthesis的平衡并且它没有显示任何错误和没有警告但我没有得到任何输出,如果你们可以帮助我,那将是非常有帮助的,我是编程的新手/>
注意我对checkbalanced功能有疑问似乎对我来说都是正确的



我尝试了什么:



I made a c proram to check the balancing of paranthesis and It is showing no error and no warning but i am not getting any output ,please it will be really helpful if you guys can help me ,i am new to programming
Note I have doubt in checkbalanced function all seems to correct to me

What I have tried:

 #include<stdio.h>
 #include<stdlib.h>
 #include<string.h>
 struct Stack{
 int top;
 int capacity;
 int *array;
 };
 struct Stack* createstack(int capacity){
 struct Stack* stack=(struct Stack*)malloc(sizeof(struct Stack));
 if(!stack)
   return NULL;
 stack->top=-1;
 stack->capacity=capacity;

stack->array=(int*)malloc(sizeof(int)*stack->capacity);
if(!stack->array)
  return NULL;
return stack;
}
int isempty(struct Stack* stack){
return stack->array[stack->top--];
}
char pop(struct Stack* stack){
return stack->array[stack->top--];

}
void push(struct Stack* stack,char exp){

stack->array[++stack->top]=exp;

}
char peek(struct Stack* stack){

return stack->array[stack->top];

}



用于检查堆栈顶部的paranthesis和exp [i]
$ b的匹配功能$ b


function to check the matching of paranthesis at top of stack and exp[i]

int ismatchingpair(char char1,char char2){
if(char1=='('&&char2==')')
   return 1;
if(char1=='['&&char2==']')
   return 1;
if(char1=='{'&&char2=='}')
   return 1;
return 0;
}



检查paranthesis平衡的功能


function to check balancing of paranthesis

int checkBalanced(char *exp){

struct Stack* stack=createstack(strlen(exp));
if(!stack)
  return NULL;

 for(int i=0;exp[i];++i){
if(exp[i]=='('||exp[i]=='{'||exp[i]=='[')
  push(stack,exp[i]);
else if(exp[i]==')'||exp[i]=='}'||exp[i]==']')
{
 if(isempty(stack)||!ismatchingpair(peek(stack),exp[i]))
     return -1;

 else
     pop(stack);

 }
 }
 printf("parenthesis are balanced");


 }



主要功能


main function

int main()
{
char exp[50]="(){}";

checkBalanced(exp);
 return 0;

}

推荐答案

看起来isempty和pop正在代码中执行操作。

Looks like isempty and pop are doing the thing in your code.
int isempty(struct Stack* stack){
	return stack->array[stack->top--];
}

char pop(struct Stack* stack){
	return stack->array[stack->top--];
}




Quote:

显示没有错误,没有警告但我没有得到任何输出

It is showing no error and no warning but i am not getting any output



第一:改变你的代码总是有输出,不仅仅是成功。



你的代码没有你想象的那样,或者你不明白为什么!



有一个几乎通用的解决方案:在调试器上一步一步运行你的代码,检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

没有魔法调试器,它不知道你的代码应该做什么,它没有找到错误,它只是通过向你展示发生了什么来帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行并在执行时检查变量。



此解决方案的缺点:

- 这是一个DIY,你是跟踪问题并找到根源的那个,这导致了解决方案。

这个解决方案的优点:

- 它也是一个很好的学习工具,因为它告诉你现实,你可以看到哪种期望与现实相符。



次要效果

- 你会为自己找到虫子感到自豪。

- 你的学习技巧会提高。



你应该很快就会发现什么是错的。



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



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

1.11 - 调试你的程序(踩踏和断点)|学习C ++ [ ^ ]

调试器只显示你的代码正在做什么,你的任务是与它应该做什么进行比较。

-----

学会正确缩进代码,显示其结构,有助于阅读和理解。它还有助于发现结构错误。


First: change your code to always have an output, not only on success.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

The downside of this solution:
- It is a DIY, you are the one tracking the problem and finding its roots, which lead to the solution.
The upside of this solution:
- It is also a great learning tool because it show you reality and you can see which expectation match reality.

secondary effects
- Your will be proud of finding bugs yourself.
- Your learning skills will improve.

You should find pretty quickly what is wrong.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
-----
Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Stack{
	int top;
	int capacity;
	int *array;
};
struct Stack* createstack(int capacity){
	struct Stack* stack=(struct Stack*)malloc(sizeof(struct Stack));
	if(!stack)
		return NULL;
	stack->top=-1;
	stack->capacity=capacity;

	stack->array=(int*)malloc(sizeof(int)*stack->capacity);
	if(!stack->array)
		return NULL;
	return stack;
}

int isempty(struct Stack* stack){
	return stack->array[stack->top--];
}

char pop(struct Stack* stack){
	return stack->array[stack->top--];
}

void push(struct Stack* stack,char exp){
	stack->array[++stack->top]=exp;
}

char peek(struct Stack* stack){
	return stack->array[stack->top];
}

int ismatchingpair(char char1,char char2){
	if(char1=='('&&char2==')')
		return 1;
	if(char1=='['&&char2==']')
		return 1;
	if(char1=='{'&&char2=='}')
		return 1;
	return 0;
}

int checkBalanced(char *exp){

	struct Stack* stack=createstack(strlen(exp));
	if(!stack)
		return NULL;

	for(int i=0;exp[i];++i){
		if(exp[i]=='('||exp[i]=='{'||exp[i]=='[')
			push(stack,exp[i]);
		else if(exp[i]==')'||exp[i]=='}'||exp[i]==']')
		{
			if(isempty(stack)||!ismatchingpair(peek(stack),exp[i]))
				return -1;
			else
				pop(stack);
		}
	}
	printf("parenthesis are balanced");
}

int main()
{
	char exp[50]="(){}";

	checkBalanced(exp);
	return 0;
}



专业程序员的编辑器具有此功能,其他功能包括括号匹配和语法高亮。

Notepad++主页 [ ^ ]

ultraedit [ ^ ]


您的 isempty()函数正在修改堆栈:

Your isempty() function is modifying the stack:
int isempty(struct Stack* stack){
    return stack->array[stack->top--];
}

这实际上是一个流行操作。因此,表达式循环在到达字符串结尾之前终止。因为你没有打印,所以你没有输出。



应该可能是

It is effectively a pop operation. So your expression loop terminates before reaching the end of the string. Because you are not printing in that case, you have no output.

It should be probably

int isempty(const struct Stack* stack){
    return stack->top < 0;
    /* Or with NULL pointer check: */
    return !stack || stack->top < 0;

}

注意这里使用const。它告诉读者代码结构未被修改,并且在修改函数体中的结构时会抛出编译器错误。

Note the use of const here. It tells the reader of the code that the structure is not modified and would throw a compiler error when modifying the structure in the function body.


这篇关于C程序检查paranthesis的平衡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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