码迷,mamicode.com
首页 > 其他好文 > 详细

栈的应用实例

时间:2017-07-08 21:51:29      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:return   clu   printf   rom   sizeof   exit   ctr   class   match   

十进制转二进制

十进制通过除而取余数得到的二进制,最后需要倒过来展示。

#include <stdio.h>
#include <stdlib.h>

#define OK 1
#define ERROR 0
#define MAX 100

typedef int ElemType;
typedef int Status;

typedef struct{
   ElemType * base;
   ElemType * top;
} Stack;


Status initStack(Stack *s){
  s->base = (ElemType*)malloc(MAX * sizeof(ElemType));
  s->top = s->base;
  return OK;
}

Status Push(Stack *s,ElemType e){
  if( s->top - s->base == MAX )return ERROR;
  *s->top++ = e;
  return OK;
}

Status Pop(Stack *s,ElemType *e){
  if( s->top - s->base == 0 )return ERROR;
  *e = *(--s->top);
  return OK;
}

Status convertion(int val){
  int popvalue;
  Stack s;
  initStack(&s);

  do{
    Push(&s,val%2);
  }while(val/=2);

  while(s.top!=s.base){
    Pop(&s,&popvalue);
    printf("%d",popvalue);
  }
 
  printf("\n");
  return OK;
}

int main(){
  int value;
  
  printf("This is binary convertioner\n");
  printf("enter Ctrl + C to stop programe\n");
  while(1){
    printf("enter a value:");
    scanf("%d",&value);
    convertion(value);
  }
}


/*
8 / 2 = 4 ------ 0  push first
4 / 2 = 2 ------ 0  push second
2 / 2 = 1 ------ 0  push third
1 / 2 = 0 ------ 1  push fourth
*/

/*
1  pop first
0  pop second
0  pop third
0  pop fourth
*/

判断是否为回文

回文是指无论是正读,还是倒读都是一样的。

#include <stdio.h>
#include <stdlib.h>

#define OK 1
#define ERROR 0
#define MAX 100

typedef char ElemType;
typedef int Status;
typedef struct{
  ElemType * base;
  ElemType * top;
} Stack;

Status initStack(Stack * s){
  s->base = (ElemType*)malloc(MAX * sizeof(ElemType));
  s->top  = s->base;
  return OK;
}

Status Push(Stack *s,ElemType e){
  if(s->top - s->base == MAX)return ERROR;
  *(s->top++)=e;
  return OK;
}

Status Pop(Stack *s,ElemType *e){
  if(s->top - s->base == 0)return ERROR;
  *e = *(--s->top);
  return OK;
}

Status checkPalindrome(char * str){
  char c;
  Stack s;
  char * p = str;
  initStack(&s);

  while( *p != \n)
    Push(&s,*p++);

  while(s.top != s.base){
    Pop(&s,&c);
    if(c != *str++)return ERROR;
  }

  return OK;
}

int main(){
  char text[100];

  printf("This is check text if it is palindarome\n");
  printf("enter the Ctrl + C to exit progrme\n");
  while(1){
    printf("enter a string:");
    fgets(text,100,stdin);
    if(checkPalindrome(text))
      printf("yes it is palindarome\n");
    else 
      printf("no it isn‘t palindarome\n");
  }
}

判断括号是否匹配

括号有:圆括号,方括号,花括号三种

下面的程序可以判断括号是否一对一对存在且不乱序。

#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0

typedef char ElemType;
typedef int Status;

typedef struct Node{
  ElemType data;
  struct Node * next;
} Node;

Node * initStack(){
  Node * s = (Node*)malloc(sizeof(Node));
  s->next =NULL;
  return s;
}

Status Push(Node *s,ElemType e){
  Node * new = (Node*)malloc(sizeof(Node));
  new->data = e;
  new->next = s->next;
  s->next = new;
  return OK;
}

Status Pop(Node *s,ElemType *e){
  Node * top = s->next;
  *e = top->data;
  s->next = top->next;
  free(top);
  return OK;
}

Status stackEmpty(Node *s){
  if(!s->next)return OK;
  else return ERROR;
}

Status checkBrackets(char str[]){
  Node * s = initStack();
  char v;
  int i = 0;
  while(str[i] != \n){
    switch(str[i]){
      case (:
        Push(s,str[i]);
        break;

      case [:
        Push(s,str[i]);
        break;
     
      case {:
        Push(s,str[i]);
        break;

      case ):
        if(stackEmpty(s))return ERROR;
        else{
           Pop(s,&v);
           if(v == ()break;
           else return ERROR;
        }
       
      case ]:
        if(stackEmpty(s))return ERROR;
        else{
           Pop(s,&v);
           if(v==[)break;
           else return ERROR;
        }

      case }:
        if(stackEmpty(s))return ERROR;
        else{
           Pop(s,&v);
           if(v == {)break;
           else return ERROR;
        }

    }
    i++;
  }

  if(stackEmpty(s))return OK;
}

int main(){
  char str[100];
  printf("enter a string:");
  fgets(str,100,stdin);
  if(checkBrackets(str))printf("it is matches all\n");
  else printf("sorry it didn‘t matches\n");
  return 0;
}

 

栈的应用实例

标签:return   clu   printf   rom   sizeof   exit   ctr   class   match   

原文地址:http://www.cnblogs.com/demonxian3/p/7136144.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!