c语言浮点数的四则运算模板,C语言编写四则运算.doc

C语言编写四则运算

C语言编写四则运算

#include //计算器

#include

#define STACK_SIZE100//max size of the stack

#define STACK_INCREMENT10//realloc size

typedef struct FStack //stack of int

{

float* base;

float* top;

int stacksize;

}FStack;

void Init(FStack* s)

{

s->base = (float*)malloc(STACK_SIZE * sizeof(FStack));

if (!s->base)

{

printf("overflow!\n");

return;

}

s->top = s->base;

s->stacksize = STACK_SIZE;

}

bool isEmpty(FStack* s)

{

if (s->top == s->base)

{

return true;

}

else

{

return false;

}

}

void Push(FStack* s, float e)

{

if (s->top - s->base >= s->stacksize)

{

printf("stack is full!\nrealloc %d\n", STACK_INCREMENT);

s->base = (float*)realloc(s->base, (s->stacksize + sizeof(FStack)));

if (!s->base)

{

printf("overflow!\n");

return;

}

s->top = s->base + s->stacksize;

s->stacksize += STACK_INCREMENT;

}

*(s->top) = e;

(s->top)++;

}

float GetTop(FStack* s)

{

if (s->top == s->base)

{

printf("stack is empty!\n");

return 0;

}

float e = *(s->top - 1);

return e;

}

void Pop(FStack* s)

{

if (s->top == s->base)

{

printf("stack is empty!\n");

return;

}

s->top--;

}

typedef struct CStack //stack of char

{

char* base;

char* top;

int stacksize;

}CStack;

void Init(CStack* s)

{

s->base = (char*)malloc(STACK_SIZE * sizeof(CStack));

if (!s->base)

{

printf("overflow!\n");

return;

}

s->top = s->base;

s->stacksize = STACK_SIZE;

}

bool isEmpty(CStack* s)

{

if (s->top == s->base)

{

return true;

}

else

{

return false;

}

}

void Push(CStack* s, int e)

{

if (s->top - s->base >= s->stacksize)

{

printf("stack is full!\nrealloc %d\n", STACK_INCREMENT);

s->base = (char*)realloc(s->base, (s->stacksize + sizeof(CStack)));

if (!s->base)

{

printf("overflow!\n");

return;

}

s->top = s->base + s->stacksize;

s->stacksize += STACK_INCREMENT;

}

*(s->top) = e;

(s->top)++;

}

char GetTop(CStack* s)

{

if (s->top == s->base)

{

printf("stack is empty!\n");

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值