#include "Stack.h"
/*3.2.1*/
intConver10to2()
{int data = 0;
printf("please input the num :\n");
scanf("%d",&data);while(data < 0)
{
printf("your data must > 0 !\n");
scanf("%d",&data);
}
PStack p;
p= (PStack)malloc(sizeof(Stack));if(!p)
{return -1;
}
InitStack(p);while(data)
{
Push(p,data%2);
data= data/2;
}//PrintStack(p);
int N = 0;while(!IsStackEmpty(p))
{
Pop(p,&N);
printf("%d\t",N);
}
printf("\n");
DestoryStack(p);
}/*3.2.2*/
int IsBracketLegal(char *data)
{
PStack p;
p= (PStack)malloc(sizeof(Stack));if(!p)
{return -1;
}
InitStack(p);intc;while(*data != '\0')
{if(GetTop(p,&c) == 1)
{if((*data == '(') || (*data == '['))
{
Push(p,*data);
}else if((*data == ')') || (*data == ']'))
{if(c == '(')
{if(*data == ')')
{
Pop(p,&c);
}else{gotoexitFun;
}
}else{if(*data == ']')
{
Pop(p,&c);
}else{gotoexitFun;
}
}
}
data++;continue;
}else{if((*data == '(') || (*data == '['))
{
Push(p,*data);
}
}
data++;
}
DestoryStack(p);returnIsStackEmpty(p);
exitFun:
{
DestoryStack(p);return -1;
}
}voidDoBracket()
{char data[1024] ;//while(1)
{
fflush(stdin);
printf("please input a string like []() ...\n");//fgets(data,sizeof(data),stdin);
scanf("%s",data);//data[strlen(data) - 1] = '\0';//care !!! fgets(), gets(), data[len - 1] == '\n'
if(IsBracketLegal(data) == 1)
{
printf("Legal Brackets\n");
}else{
printf("Illegal Brackets %s\n",data);
}//break;
}
}/*3.2.3*/
intLineEdit()
{
PStack p;
p= (PStack)malloc(sizeof(Stack));if(!p)
{return -1;
}
InitStack(p);int i = 0;charc;
setbuf(stdin, NULL);//clear the stdin !!!!!!!!!!!!
c =getchar();while(c != '\n')
{while(c != '\n' && c !=EOF)
{switch(c)
{case '#':
Pop(p,&i);break;case '@':
ClearStack(p);break;default:
Push(p,c);break;
}
c=getchar();
}
PrintCharStack(p);
ClearStack(p);
}
DestoryStack(p);return 1;
}int main(int argc , char**argv)
{while(1)
{int a = 0;
printf("please input the fun you want to run , 0: Conver10to2(), 1: DoBracket(), 2:LineEdit() ,3: exit\n");
scanf("%d",&a);switch(a)
{case 0: Conver10to2();break;case 1: DoBracket();break;case 2: LineEdit();break;default: exit(0);break;
}
}return 1;
}