主要实现了十进制到二进制的转换、对括号的合法性检测以及教材中的行编辑。
分别是这三个函数:int Conver10to2(),int IsBracketLegal(char *data),int LineEdit()。
具体源码如下:
Main_3_2.c:
#include "Stack.h" /*3.2.1*/ int Conver10to2() { 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); int c; 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 { goto exitFun; } } else { if(*data == ']') { Pop(p,&c); } else { goto exitFun; } } } data++; continue; } else { if((*data == '(') || (*data == '[')) { Push(p,*data); } } data++; } DestoryStack(p); return IsStackEmpty(p); exitFun: { DestoryStack(p); return -1; } } void DoBracket() { 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*/ int LineEdit() { PStack p; p = (PStack)malloc(sizeof(Stack)); if(!p) { return -1; } InitStack(p); int i = 0; char c; 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; }
Stack.c:
#include "Stack.h" /*3.1 3.2*/ int InitStack(PStack S) { S->base = (SElemType *)malloc(sizeof(SElemType) * STACK_INIT_SIZE); if(!S->base) { exit(0); } S->top = S->base; S->stackSize = STACK_INIT_SIZE; return 1; } int GetTop(PStack S,SElemType *e) { if(S->base == S->top) { return -1; } *e = *(S->top -1); return 1; } int Push(PStack S,SElemType e) { if(S->top - S->base >= S->stackSize) { S->base = (SElemType *)realloc(S->base,(S->stackSize + STACK_INCRE_SIZE) * sizeof(SElemType)); if(!S->base) { return -1; } S->top = S->base + S->stackSize; S->stackSize += STACK_INCRE_SIZE; } *S->top++ = e; return 1; } int Pop(PStack S,SElemType *e) { if(S->base == S->top) { return -1; } *e = * -- S->top; } int ClearStack(PStack S) { S->top = S->base; return 1; } int DestoryStack(PStack S) { free(S->base); } void PrintStack(PStack S) { PStack p; p = S; int i = 0; while(p->top != p->base) { i++; printf("%d\t",* -- p->top); } p->top += i; printf("\n"); } void PrintCharStack(PStack S) { PStack p; p = S; int i = 0; while(p->top != p->base) { i++; printf("%c\t",* -- p->top); if((i != 0) && (i % 10 == 0)) { printf("\n\t\t\t"); } } p->top += i; printf("\n"); } int IsStackEmpty(PStack S) { return ((S->base == S->top) ? 1 : 0); }
Stack.h:
#ifndef _STACK_H #define _STACK_H #include <stdlib.h> #include <string.h> #include <stdio.h> #define STACK_INIT_SIZE 100 #define STACK_INCRE_SIZE 50 typedef int SElemType ; typedef struct SqStack { SElemType *base; SElemType *top; int stackSize; }Stack,*PStack; int InitStack(PStack S); int GetTop(PStack S,SElemType *e); int Push(PStack S,SElemType e); int Pop(PStack S,SElemType *e); int ClearStack(PStack S); int DestoryStack(PStack S); void PrintCharStack(PStack S); #endif
编译运行方法如前几篇所述。GCC下运行结果如下:
root@ubuntu:/mnt/hgfs/E/Lessons/MyExercise/DS/3# ./main_3_2 please input the fun you want to run , 0: Conver10to2(), 1: DoBracket(), 2:LineEdit() ,3: exit 0 please input the num : 1989 1 1 1 1 1 0 0 0 1 0 1 please input the fun you want to run , 0: Conver10to2(), 1: DoBracket(), 2:LineEdit() ,3: exit 1 please input a string like []() ... (h)[e]([n[u]])_[0][9[0[8]]](T(i(g(e)))r) Legal Brackets please input the fun you want to run , 0: Conver10to2(), 1: DoBracket(), 2:LineEdit() ,3: exit 2 Hi#i,T#Tiger#r!#! ! r e g i T , i H please input the fun you want to run , 0: Conver10to2(), 1: DoBracket(), 2:LineEdit() ,3: exit 3 root@ubuntu:/mnt/hgfs/E/Lessons/MyExercise/DS/3#