华为机试之if语句条件判断

来源:互联网 发布:市场调研数据分析 编辑:程序博客网 时间:2024/05/18 04:59
if else 语句符号判断
#include<iostream>using namespace std;#define MAX 40typedef char T;typedef struct Stack{T stack[MAX];int top;}Stack;Stack *S;void InitStack(Stack *S){S->top = 0;}void Push(Stack *S,T e){if(S->top >= MAX)printf("栈已满\n");else{S->stack[S->top] = e;(S->top)++;}}bool Pop(Stack *S,T &e){if(S->top == 0){return false;}else{(S->top)--;e = S->stack[S->top];return true;}}bool Empty(Stack *S){if(S->top == 0){return true;}elsereturn false;}bool PuanDuan(char * str,int &left,int &right){S = (Stack*)malloc(sizeof(Stack));InitStack(S);int len = strlen(str);char e;bool status;for(int i=0;i<len;i++){if(str[i] == '(') //入栈{left++;}else if(str[i]== ')'){right++;}}for(int i=0;i<len;i++){if(str[i] == '(') //入栈{Push(S,str[i]);}else if(str[i]== ')'){bool status = Pop(S,e);if(status == false){return  false;}if(e == ')'){return false;}if(Empty(S)&&i<len-1){return false;}}}if(!Empty(S))return false;elsereturn true;}int main(){char str[MAX]="if((a==b))&&(b==c))";cout<<str<<endl;int left = 0,right = 0;bool status = PuanDuan(str,left,right);if(status)cout<<"True"<<endl;elsecout<<"False"<<endl;cout<<left<<" "<<right<<endl;system("pause");return 0;}

0 0
原创粉丝点击