c语言关于计算器的栈
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define STACK_INIT_SIZE 100
#define STACK_INCREMENT 10
#define S 100
typedef struct{
void* pBase;
void* pTop;
int sizeOfElement;
int sizeOfStack;
}Stack;
typedef enum{
ERROR=0,
OK=1
}Status;
Status Stack_Init(Stack *pStack,int sizeOfElement);
Bool Stack_IsEmpty(Stack *pStack);
Bool Stack_IsFull(Stack *pStack);
int Stack_Length(Stack *pStack);
Status Stack_GetTop(Stack *pStack,void *pElem);
void Stack_Free(Stack *pStack);
void Stack_Clear(Stack *pStack);
Status Stack_Push(Stack *pStack,void *pData);
Status Stack_Pop(Stack *pStack,void *pElem);
Status Stack_Traverse(Stack *pStack,Status(*visit)());
float ConvertToFloat(char s[S]);
Status VisitInteger(void *pElem);
Status VisitChar(void *pElem);
Status VisitFloat(void *pElem);
Status Stack_Init(Stack *pStack,int sizeOfElement){
pStack->pBase=(void*)malloc(sizeOfElement*STACK_INIT_SIZE);
if(pStack->pBase==NULL){
return ERROR;
}
pStack->pTop=pStack->pBase;
pStack->sizeOfStack=STACK_INIT_SIZE;
pStack->sizeOfElement=sizeOfElement;
}
Bool Stack_IsEmpty(Stack *pStack){
if(pStack->pTop==pStack->pBase){
return true;
}
else{
return false;
}
}
Bool Stack_IsFull(Stack *pStack){
if(pStack->pTop-pStack->pBase==pStack->sizeOfStack){
return true;
}
else{
return false;
}
}
int Stack_Length(Stack *pStack){
return (pStack->pTop-pStack->pBase)/pStack->sizeOfElement;
}
Status Stack_GetTop(Stack *pStack,void *pElem){
if(Stack_IsEmpty(pStack)){
return ERROR;
}
memcpy(pElem,pStack->pTop-pStack->sizeOfElement,pStack->sizeOfElement);
return OK;
}
void Stack_Free(Stack *pStack){
free(pStack->pBase);
pStack->pBase=NULL;
pStack->pTop=NULL;
pStack->sizeOfStack=0;
return;
}
void Stack_Clear(Stack *pStack){
pStack->pTop=pStack->pBase;
return;
}
Status Stack_Push(Stack *pStack,void *pData){
if(Stack_Length(pStack)>=pStack->sizeOfStack){
int newSize=pStack->sizeOfStack+STACK_INCREMENT*pStack->sizeOfElement;
void *Temp=realloc(pStack->pBase,newSize);
if(pTemp==NULL){
return ERROR;
}
pStack->pBase=pTemp;
pStack->pTop=pStack->pBase+pStack->sizeOfStack*pStack->sizeOfElement;
pStack->sizeOfStack=pStack->sizeOfStack+STACK_INCREMENT;
}
memcpy(pStack->pTop,pData,pStack->sizeOfElement);
pStack->pTop=pStack->pTop+pStack->sizeOfElement;
return OK;
}
Status Stack_Pop(Stack *pStack,void *pElem){
if(Stack_IsEmpty(pStack)){
return ERROR;
}
else{
pStack->pTop=pStack->pTop-pStack->sizeOfElement;
memcpy(pElem,pStack->pTop,pStack->sizeOfElement);
return OK;
}
}
Status Stack_Traverse(Stack *pStack,Status(*visit)()){
int i,j;
j=Stack_Length(pStack);
for(i=0;i<n;++i){
if(visit(pStack->pBase+i*pStack->sizeOfElement)==ERROR){
return ERROR;
}
}
return OK;
}
float ConvertToFloat(char s[S]){
int i=0;
float value=0;
while(s[i]!='\0'&&s[i]!='.'){
value=value*10+(s[i]-'0');
i=i+1;
}
if(s[i]=='\0'){
return value;
}
i=i+1;
float weight=0.1;
while(s[i]!='\0'){
value=value+(s[i]-'0')*weight;
weight=weight/10;
i=1+1;
}
return value;
}
Status VisitInteger(void *pElem){
printf("%d\t",*(int*)pElem);
return OK;
}
Status VisitChar(void *pElem){
printf("%c\t",*(char*)pElem);
return OK;
}
Status VisitFloat(void *pElem){
printf("%.12f\t",*(double*)pElem);
return OK;
}
int main(){
int i,j;
j=100;
Stack stack;
int iData;
Stack_Init(&stack,sizeof(int));
for(i=0;i<j;++i){
iData=rand()%100+1;
printf("%d\t",iData);
Stack_Push(&stack,&iData);
}
printf("\n");
Stack_Traverse(&stack,VisitInteger);
printf("\n");
while(!Stack_IsEmpty(&stack)){
Stack_Pop(&stack,&iData);
printf("%d\t",iData);
}
Stack_Free(&stack);
printf("\n\n");
char ch;
Stack_Init(&stack,sizeof(char));
for(i=0;i<j;++i){
ch=rand()%26+'A';
printf("%c\t",ch);
Stack_Push(&stack,&ch);
}
printf("\n");
Stack_Traverse(&stack,VisitChar);
printf("\n");
while(!Stack_IsEmpty(&stack)){
Stack_Pop(&stack,&ch);
printf("%c\t",ch);
}
Stack_Free(&stack);
printf("\n\n");
}
求纠错