C语言的题目不会做啊 15
Writeafunctionboolparenthesis(charstr[])thattakesinastringcontainingparentheses(括号的意思...
Write a function bool parenthesis(char str[]) that takes in a string containing parentheses(括号的意思), and determines if the parentheses are balanced. A string of parentheses is balanced if every parenthesis is closed in the reverse order that it is opened. Note that '(' is an open parenthesis and ')' is a closing parenthesis. For example, the string "(()()(()()))" is balanced, but the strings "(()))" and ")(" are not. Your function should ignore all characters except for the following: '(' and ')'. If the string is balanced, your function should return true; otherwise, it should return false
展开
展开全部
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
unsigned int balance(const char* str)
{
int flags=0;
while( *str != '\0' ){
if( flags > 0 ) return 0;
if( *str == ')' ) flags++;
else if( *str == '(' ) flags--;
else ;
str++;
}
if( flags != 0 ) return 0;
return 1;
}
int main(int argc, char *argv[]) {
char *str1="(()()(()()))";
char *str2=")(";
int err1=balance(str1);
int err2=balance(str2);
printf("%d,%d\n",err1,err2);
system("pause");
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询