栈(stack)

/*
  Name: stack.c
  Copyright: personal
  Author: hojor
  Date: 07-06-10 10:22
  Description: stack
*/


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

#define EmptyTOS (-1)
#define MinStackSize (5)
#define ElementType int

struct StackRecord
{
       int Capacity;
       int TopOfStack;
       ElementType * Array;
};
typedef struct StackRecord * Stack;


//function.list
int            IsEmpty(Stack S);
int            IsFull(Stack S);
void           DisposeStack(Stack S);
void           MakeEmpty(Stack S);
void           Push(ElementType X,Stack S);
void           Pop(Stack S);
ElementType    Top(Stack S);
ElementType    TopAndPop(Stack S);
Stack          CreateStack(int MaxElements);

//create stack size of MaxElements
Stack CreateStack( int MaxElements)
{
      Stack S;
      
      if(MaxElements < MinStackSize)
           printf("Stack size is too small\n");         
      S = (Stack)malloc(sizeof(struct StackRecord));
      if(S == NULL)
           printf("Out of space!!\n");
      
      S->Array = (ElementType *)malloc(sizeof(ElementType)*MaxElements);
      if(S->Array == NULL)
           printf("Out of space!!\n");
      S->Capacity = MaxElements;
      MakeEmpty(S);
      
      return S; 
}

//release Stack routines
void DisposeStack(Stack S)
{
     if(S != NULL)
     {
          free(S->Array);
          free(S);
     }
}

//Determine whether the stack is empty
int IsEmpty(Stack S)
{
    return S->TopOfStack == EmptyTOS; 
}

//Determine whether the stack is full
int IsFull(Stack S)
{
    return S->TopOfStack == S->Capacity;
}
//create an empty stack routines
void MakeEmpty(Stack S)
{
     S->TopOfStack = EmptyTOS;
}

//push stack
void Push(ElementType x,Stack S)
{
     if(IsFull(S))
         printf("Full stack\n");
     else
         S->Array[++S->TopOfStack]=x;
}

//return top of stack
ElementType Top(Stack S)
{
    if(!IsEmpty(S))
        return S->Array[S->TopOfStack];
    printf("Empty stack");
    return 0;
} 

//pop stack
void Pop(Stack S)
{
     if(IsEmpty(S))
         printf("\nEmpty stack\n");
     else
         S->TopOfStack--;
}

//gave the top of stack and pop
ElementType TopAndPop(Stack S)
{
    if(!IsEmpty(S))
        return S->Array[S->TopOfStack--];
    printf("\nEmpty stack\n");
    return 0;
} 
int main(void)
{
    int i;
    Stack s =  CreateStack(10);
    for(i=0;i<10;i++)
        Push(i,s);
    for(i=0;i<10;i++) 
        printf("%d ",TopAndPop(s));
    DisposeStack(s);
    system("pause");
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这个问题非常适合用程序解决。以下是用stack实现简单计算器的c代码示例: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> #define STACKLEN 1000 /* 的最大容量 */ typedef struct { int top; /* 顶指针 */ double data[STACKLEN]; /* 元素数组 */ } Stack; void push(Stack *pstack, double value) { if (pstack->top == STACKLEN - 1) { printf("Error: stack overflow!\n"); exit(EXIT_FAILURE); } else { pstack->data[++pstack->top] = value; } } double pop(Stack *pstack) { if (pstack->top == -1) { printf("Error: stack underflow!\n"); exit(EXIT_FAILURE); } else { return pstack->data[pstack->top--]; } } bool is_digit(char c) { return (c >= '0' && c <= '9'); } int precedence(char op) { if (op == '+' || op == '-') return 1; else if (op == '*' || op == '/') return 2; else return 0; } double calculate(double left, char op, double right) { switch (op) { case '+': return left + right; case '-': return left - right; case '*': return left * right; case '/': return left / right; default: printf("Error: invalid operator!\n"); exit(EXIT_FAILURE); } } double eval(char *expr) { Stack operandStack; operandStack.top = -1; Stack operatorStack; operatorStack.top = -1; int len = strlen(expr); int i = 0; while (i < len) { char c = expr[i]; if (is_digit(c)) { double value = 0.0; while (i < len && is_digit(expr[i])) { value = value * 10.0 + (double)(expr[i] - '0'); i++; } push(&operandStack, value); } else { while (operatorStack.top != -1 && precedence(operatorStack.data[operatorStack.top]) >= precedence(c)) { char op = operatorStack.data[operatorStack.top--]; double right = pop(&operandStack); double left = pop(&operandStack); push(&operandStack, calculate(left, op, right)); } push(&operatorStack, c); i++; } } while (operatorStack.top != -1) { char op = operatorStack.data[operatorStack.top--]; double right = pop(&operandStack); double left = pop(&operandStack); push(&operandStack, calculate(left, op, right)); } return pop(&operandStack); } int main() { char s[1000]; printf("Please enter an expression: "); scanf("%s", s); double result = eval(s); printf("Result: %f\n", result); return 0; } ``` 这段代码定义了两个:一个操作数(operandStack)和一个操作符(operatorStack),通过不断入和出的操作,实现对表达式进行求值。其中,is_digit函数用于判断一个字符是否是数字;precedence函数用于比较两个运算符的优先级;calculate函数用于计算两个操作数和一个操作符的运算结果;eval函数是主函数,用于将输入的表达式转化为数字计算结果。 希望这个回答能够帮助您!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值