字符串括号匹配c语言,数据结构C语言括号的检验源程序 , 数据结构(C) 请用类C语言实现括号匹配的检验这个算法...
問題標(biāo)題
數(shù)據(jù)結(jié)構(gòu)C語言括號(hào)的檢驗(yàn)源程序 , 數(shù)據(jù)結(jié)構(gòu)(C) 請(qǐng)用類C語言實(shí)現(xiàn)括號(hào)匹配的檢驗(yàn)這個(gè)算法
2019-8-21來自ip:13.149.196.77的網(wǎng)友咨詢
瀏覽量:270?手機(jī)版
問題補(bǔ)充:
數(shù)據(jù)結(jié)構(gòu)C語言括號(hào)的檢驗(yàn)源程序 , 數(shù)據(jù)結(jié)構(gòu)(C) 請(qǐng)用類C語言實(shí)現(xiàn)括號(hào)匹配的檢驗(yàn)這個(gè)算法
網(wǎng)友答案
#include#includeusing namespace std;#define Maxsize 50class stack{private:char *st;int top;public:stack(){top=-1;st=new charMaxsize;}void clear(){top=-1;}bool isempty(){if(top==-1)return true;elsereturn false;}bool isfull(){if(top==Maxsize-1)return true;elsereturn false;}bool push(int item){if(top==Maxsize-1){coutreturn false;}else{st++top=item;return true;}}bool pop(char &item){if(top==-1){coutreturn false;}else{item=sttop--;return true;}}bool rtop(char &item){if(top==-1){coutreturn false;}else{item=sttop;return true;}}~stack(){delete st;}};bool match(char *p,int c){char m;int i = 0 ;stack q;while(i{switch(pi){case '{':case '':case '(':q.push(pi);break;case '}':if(!q.isempty()&&q.pop(m)&&m=='{'){break;}elsereturn false;case '':if(!q.isempty()&&q.pop(m)&&m==''){break;}else return false;case ')':if(!q.isempty()&&q.pop(m)&&m==')'){break;}else return false;}i++;} return q.isempty(); }void main(){char arrMaxsize;char *s=arr;int c;coutcin.getline(arr,Maxsize);c=strlen(s);if(match(s,c)){coutelse{cout}望。
。
。
網(wǎng)友答案
#include#includeusing namespace std;#define Maxsize 50class stack{private: char *st; int top;public: stack() { top=-1; st=new charMaxsize; } void clear() { top=-1; } bool isempty() { if(top==-1) return true; else return false; } bool isfull() { if(top==Maxsize-1) return true; else return false; } bool push(int item) { if(top==Maxsize-1) { cout
。
網(wǎng)友答案
這是一個(gè)簡(jiǎn)單的括號(hào)匹配程序, 不過我認(rèn)為對(duì)于你學(xué)習(xí)足夠了#include<stdio.h> #define MAX 20 typedef struct { char dataMAX; //棧中的數(shù)組data int top; //棧頂位置 }sstack; //順序棧 void initstack(sstack *s)//初始化順序棧 { s->top= 0; }void push(sstack *s, char x) { // s為棧指針,x為入棧的新元素,將x壓入到s棧頂 if(s->top==MAX-1) //檢查是否棧滿 printf"棧已滿,不能入棧!"; else { s->top++; //使top指向新的位置 s->datas->top-1=x; //把x放入新棧頂 } } void pop(sstack *s) { //若棧s不為空,則刪除棧頂元素 if(s->top==0 ) //檢查是否棧空 printf"棧為空,不能出棧!";/*棧空*/ else s->top--;//修改棧頂指針 } char gettop(sstack *s) { //若棧s不為空,則取得棧頂元素 if(s->top==0) //棧空 printf"棧為空!"; else return(s->datas->top); //返回棧頂元素 } void check(sstack *s) { int bool; char ch; push(s,'#'); //在棧底壓入#,做為結(jié)束標(biāo)志 scanf("%c",&ch); //讀入一個(gè)字符 bool=1; //假設(shè)括號(hào)匹配 while(ch!='#'&&bool) //當(dāng)讀入的字符不是結(jié)束符且目前括號(hào)匹配 { if(ch=='(') push(s,ch); //如果是左括號(hào)則入棧 if(ch==')' //如果是右括號(hào) if(ch=gettop(s)=='#') bool=0; //如果棧頂是結(jié)束符,即沒有匹配的'',則設(shè)置不匹配標(biāo)志,此時(shí)表示''多于'' else pop(s); //左括號(hào)出棧,表與右括號(hào)配對(duì) scanf("%c",&ch); //讀入下一個(gè)字符,繼續(xù)檢查 } if(gettop(s)!='#')&&bool==1 ; //棧頂還有'',則''多于'',設(shè)置不匹配標(biāo)志 if(ch==')') printf"括號(hào)不匹配"; else printf"括號(hào)匹配"; } void main() { sstack st,*s; s=&st; initstack(s); //初始化棧 printf"請(qǐng)輸入由''和''組成的字符串,該字符串以'#'結(jié)束,程序?qū)z查字符串是否括號(hào)匹配\n"); check(s);//進(jìn)行括號(hào)匹配的檢查 }希望對(duì)你有幫助
網(wǎng)友答案
#includestruct T{int tag;};struct T t128;int top=0;int isEmpty(){if(top=127) return 1;else return 0;}void push(int tag){if(isFull()) {return;}ttop.tag=tag;top++;}int out(){if(isEmpty()) {return -1;}int number=0;number=ttop.tag;ttop.tag=0;top--;return number;}int check(char *p){int x128={0};int temp=0;int k=0;while(*p!='\0'){if(*p=='{') push(temp);if(*p=='}') k=out();if(k==-1) break;temp++;p++;}if(top!=0){while(!isEmpty()){printf("%d處的{沒有匹配,缺少}\n",out()-1);}}else{printf("%d處的}沒有匹配,缺少{\n",temp);}return 0;}main(){char *x="main(){int x=0;while(true){int x=324;}printf('dsgdg');}}}";printf("%d\n",check(x));}
網(wǎng)友答案
輸入:串中只含有'', ''兩個(gè)符號(hào),判斷是否括號(hào)匹配并不需要真正的入棧、出棧,用top移動(dòng)模擬即可#include <stdio.h>int main(){char s256, *p;int top = 0;scanf("%s", s);for(p=s; *p; p++){if(*p == '(')++top; //入else --top; //出if(top < 0)break; //無左括號(hào)與之匹配}if(!*p && !top)puts("Yes");else puts("No");}
網(wǎng)友答案
從你的代碼可以看出,你想在開始將‘’壓棧,作為結(jié)束條件。
在循環(huán)中采集每次的輸入字符,如果不匹配,則壓棧輸入字符;如果匹配,為‘’字符,則將棧中所有字符全部出棧。
上面代碼的主要問題是循環(huán)條件的判斷的問題,你可以分析下。
下面是我在你的代碼基礎(chǔ)上進(jìn)行了小小修改后的代碼,提供給你參考下。
保存為CPP文件,用C++編譯測(cè)試通過。
#include <stdio.h>#include <stdlib.h>#include <malloc.h>#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0typedef char SElemType;typedef int Status;#define STACK_INIT_SIZE 100#define STACKINCREMENT 10typedef struct _SqStack{ SElemType *base; SElemType *top; int stacksize;}SqStack;Status InitStack(SqStack &S){ S.base = (SElemType *)malloc(STACK_INIT_SIZE *sizeof(SElemType)); if(!S.base) return FALSE; S.top = S.base; S.stacksize = STACK_INIT_SIZE; return OK;}Status Push(SqStack &S, SElemType e){ *S.top++ = e; printf("0x%.8x: PUSH %c\n", S.top, e); return OK;}Status Pop(SqStack &S, SElemType &e){ if(S.top == S.base) return ERROR; e = *--S.top; printf("0x%.8x: POP %c\n", S.top, e); return OK;}Status StackEmpty(SqStack &S){ if(S.base == S.top) return TRUE; else return FALSE;}main(){ SqStack S; SElemType e; //初始化棧 InitStack(S); //將 壓入棧底,作為結(jié)束條件 e=''; Push(S, e); //循環(huán)接收鍵盤字符, //不匹配則將字符壓棧, //匹配則將 與 之間字符全部出棧,只到棧底結(jié)束 while(S.top > S.base) { fflush(stdin); e = getchar(); if (e != '') { Push(S, e); } else { printf("\n"); printf("----Get matched char ''\n"); printf("----POP Stack----\n"); S.top --; while(*(S.top)!= '') { Pop(S, e); } } fflush(stdout); } Pop(S, e); printf("\n"); //釋放棧 if(StackEmpty(S)) return TRUE; else return FALSE;}
網(wǎng)友答案
#include #include int isLeftBracket(char ch){ return ch=='(' || ch=='' || ch=='{' || ch=='';}int mathcBracket(char left,char right){return (left==')' && right == '(') || (left=='' && right == '') || (left=='}' && right == '{') || (left=='>' && right == ' 0 && mathcBracket(stri,stacktop-1) ){top--;continue;}else{break;}}}if(stri == '\0'){ printf("Yes\n"); }else{ printf("No\n");} }return 0;}應(yīng)該沒有什么問題。
我來回答
驗(yàn)證碼:
大家還關(guān)注
總結(jié)
以上是生活随笔為你收集整理的字符串括号匹配c语言,数据结构C语言括号的检验源程序 , 数据结构(C) 请用类C语言实现括号匹配的检验这个算法...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 联合解决方案系列|VMware Mult
- 下一篇: 不同网段下的pc机如何通过路由器互通