c语言堆栈实现
A. c语言中如何用堆栈实现个位数的四则运算(不带括号也可)
#include<iostream>
#include<stdio.h>
#include<math.h>
#define MaxSize 100
using namespace std;
//下面两个函数不是我发明的,数据结构上有
void trans(char*exp,char postexp[])//此函数是将中缀表达式转后缀表达式,利用栈
{
char *temp=exp;
for(;*temp!='\0';temp++){if(*temp=='a')*temp='1';}
struct
{
char data[MaxSize];
int top;
}op;
int i=0;
op.top=-1;
while(*exp!='\0')
{
switch(*exp)
{
case '(':
op.top++;op.data[op.top]=*exp;
exp++;
break;
case ')':
while(op.data[op.top]!='(')
{
postexp[i++]=op.data[op.top];
op.top--;
}
op.top--;
exp++;
break;
case '+':
case '-':
while(op.top!=-1&&op.data[op.top]!='(')
{
postexp[i++]=op.data[op.top];
op.top--;
}
op.top++;op.data[op.top]=*exp;
exp++;
break;
case '*':
case '/':
while(op.data[op.top]=='*'||op.data[op.top]=='/'||op.data[op.top]=='^')
{
postexp[i++]=op.data[op.top];
op.top--;
}
op.top++;op.data[op.top]=*exp;
exp++;
break;
case '^':
while(op.data[op.top]=='^')
{
postexp[i++]=op.data[op.top];
op.top--;
}
op.top++;op.data[op.top]=*exp;
exp++;
break;
case ' '://这里考虑空格了
exp++;
break;
default:
while(*exp>='0'&&*exp<='9')
{
postexp[i++]=*exp;
exp++;
}
postexp[i++]='#';
}
}
while(op.top!=-1)
{
postexp[i++]=op.data[op.top];
op.top--;
}
postexp[i]='\0';
}
float compvalue(char *postexp)//此函数利用栈对后缀表达式求值
{
struct
{
float data[MaxSize];
int top;
}st;
float d,a,b,c;
st.top=-1;
while(*postexp!='\0')
{
switch(*postexp)
{
case '+':
a=st.data[st.top];
st.top--;
b=st.data[st.top];
st.top--;
c=a+b;
st.top++;
st.data[st.top]=c;
break;
case '-':
a=st.data[st.top];
st.top--;
b=st.data[st.top];
st.top--;
c=b-a;
st.top++;
st.data[st.top]=c;
break;
case '*':
a=st.data[st.top];
st.top--;
b=st.data[st.top];
st.top--;
c=a*b;
st.top++;
st.data[st.top]=c;
break;
case '/':
a=st.data[st.top];
st.top--;
b=st.data[st.top];
st.top--;
c=b/a;
st.top++;
st.data[st.top]=c;
break;
case '^':
a=st.data[st.top];
st.top--;
b=st.data[st.top];
st.top--;
c=pow(b,a);
st.top++;
st.data[st.top]=c;
break;
default:
d=0;
while(*postexp>='0'&&*postexp<='9')
{
d=10*d+*postexp-'0';
postexp++;
}
st.top++;
st.data[st.top]=d;
break;
}
postexp++;
}
return(st.data[st.top]);
}
int main()
{
char exp[50],postexp[50],output[50];
int temp,n;
char *X=output,*Y=exp;//临时指针
memset(output,0,50);//这句将全部数组元素置为零
memset(exp,0,50);
cout<<"input:"<<endl;
cin.getline(exp,50);
trans(exp,postexp);
temp=compvalue(postexp);
cin>>n;
fflush(stdin);
for(int i=0;i<n;i++)
{
memset(exp,0,50);
cin.getline(exp,50);
trans(exp,postexp);
if(temp==compvalue(postexp)){*X='A'+i;X++;}//通过指针X来逐个向output元素赋值
}
cout<<"output:"<<endl;
cout<<output<<endl;
return 0;
}
B. 用堆栈来实现四则运算.(C语言)
here it is,1个半小时才搞定的,好辛苦,呵呵:
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
double var[1024];
char opt[1024];
double* sp1 = var;
char* sp2 = opt;
#define PUSH(s, v) (*s++ = v)
#define POP(s) (*--s)
#define EMPTY1() (sp1 == var)
#define EMPTY2() (sp2 == opt)
#define TOP(s) (s[-1])
#define isopt1(c) (c == '+' || c == '-')
#define isopt2(c) (c == '*' || c == '/')
#define isopt(c) (isopt1(c) || isopt2(c))
#define isbkt(c) (c == '(')
double docal(double lhs, double rhs, char op)
{
switch(op) {
case '+':return lhs + rhs;
case '-':return lhs - rhs;
case '*':return lhs * rhs;
}
if(op == '/' && rhs == 0) {
printf("divided by zero encountered.\n");
exit(0);
}
return lhs / rhs;
}
void eval1(char* expr)
{
double t1, t2;
while(sp1 - var > 1 && !EMPTY2() && TOP(sp2) != '(') {
t2 = POP(sp1);
t1 = POP(sp1);
PUSH(sp1, docal(t1, t2, POP(sp2)));
}
if(expr)
PUSH(sp2, *expr);
}
void eval2(char* expr)
{
double t1, t2;
while(TOP(sp2) != '(') {
t2 = POP(sp1);
t1 = POP(sp1);
PUSH(sp1, docal(t1, t2, POP(sp2)));
}
POP(sp2);
}
double parse(char* expr)
{
double val;
while(*expr) {
if(isdigit(*expr)) {
sscanf(expr, "%lf", &val);
PUSH(sp1, val);
while(isdigit(*expr) || *expr == '.')
++expr;
--expr;
} else if(isopt(*expr)) {
if(EMPTY2() || (isopt1(TOP(sp2)) && !isopt1(*expr)) || isbkt(TOP(sp2)))
PUSH(sp2, *expr);
else if(isopt(TOP(sp2)))
eval1(expr);
} else if(*expr == '(') {
PUSH(sp2, *expr);
}
C. c语言怎么用堆栈实现多项式的加法运算
//已经帮忙调整,对比下代码吧,就知道是什么问题了
typedefstructPolyNode*Polynomial;
structPolyNode{
intcoef;
intexpon;
Polynomiallink;
};
PolynomialReadPoly();
PolynomialPolyAdd(PolynomialP1,PolynomialP2);
voidAttach(intc,inte,Polynomial*pRear);
intCompare(intp1,intp2);//未定义函数,自己写实现
intmain()
{
PolynomialP1,P2,PP,PS;
P1=ReadPoly();
P2=ReadPoly();
// PP=Mult(P1,P2);
// PrintPoly(PP);
PS=PolyAdd(P1,P2);
// PrintPoly(PS);
return0;
}
PolynomialReadPoly()
{
PolynomialP,Rear,t;
intc,e,N;
scanf("%d",&N);
P=(Polynomial)malloc(sizeof(structPolyNode));
P->link=NULL;
Rear=P;
while(N--){
scanf("%d%d",&c,&e);
Attach(c,e,&Rear);
}
t=P;P=P->link;
free(t);
//retrunP;//return写错
returnP;
}
voidAttach(intc,inte,Polynomial*pRear)
{
PolynomialP;
P=(Polynomial)malloc(sizeof(structPolyNode));
P->coef=c;
P->expon=e;
P->link=NULL;
(*pRear)->link=P;
*pRear=P;
}
PolynomialPolyAdd(PolynomialP1,PolynomialP2)
{
Polynomialfront,rear,temp;
intsum;
rear=(Polynomial)malloc(sizeof(structPolyNode));
front=rear;
while(P1&&P2);
switch(Compare(P1->expon,P2->expon)){//这里要用指针->
case1:
Attach(P1->coef,P1->expon,&rear);//这里要用指针->
P1=P1->link;
break;
case-1:
Attach(P2->coef,P2->expon,&rear);//这里要用指针->
P2=P2->link;
break;
case0:
sum=P1->coef+P2->coef;//这里要用指针->
if(sum)Attach(sum,P1->expon,&rear);//这里要用指针->
P1=P1->link;
P2=P2->link;
break;
}
for(;P1;P1=P1->link)Attach(P1->coef,P1->expon,&rear);
for(;P2;P2=P2->link)Attach(P2->coef,P2->expon,&rear);
rear->link=NULL;
temp=front;
front=front->link;
free(temp);
returnfront;
}
D. C语言,栈的实现~
你写的太复杂,这个拿去用吧
// Stack node
struct Node
{
int data ;
Node* next ;
Node(int d, Node* p):data(d), next(p){}
};
class Stack
{
public:
Stack():top(NULL){}
void Push(int d)
{
top = new Node(d, top) ;
}
int Pop()
{
Node* temp = top ;
top = top->next ;
return temp->data ;
}
bool Empty()
{
return top == NULL ;
}
private:
Node* top ;
};
E. C语言问题,利用堆栈实现四则运算,谢谢高手帮我编写出来
我就喜欢害人:)
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#defineERR-1
#defineMAX100/*定义堆栈的大小*/
intstack[MAX];/*用一维数组定义堆栈*/
inttop=0;/*定义堆栈指示*/
intpush(inti)/*存储运算数,入栈操作*/
{
if(top<MAX)
{
stack[++top]=i;/*堆栈仍有空间,栈顶指示上移一个位置*/
return0;
}
else
{
printf("Thestackisfull");
returnERR;
}
}
intpop()/*取出运算数,出栈操作*/
{
intvar;/*定义待返回的栈顶元素*/
if(top!=NULL)/*堆栈中仍有元素*/
{
var=stack[top--];/*堆栈指示下移一个位置*/
returnvar;/*返回栈顶元素*/
}
else
printf("Thestackisempty!\n");
returnERR;
}
voidmain()
{
intm,n;
charl;
inta,b,c;
intk;
do{
printf("\tAriothmaticOperatesimulator\n");/*给出提示信息*/
printf("\n\tPleaseinputfirstnumber:");/*输入第一个运算数*/
scanf("%d",&m);
push(m);/*第一个运算数入栈*/
printf("\n\tPleaseinputsecondnumber:");/*输入第二个运算数*/
scanf("%d",&n);
push(n);/*第二个运算数入栈*/
printf("\n\tChooseoperator(+/-/*//):");
l=getche();/*输入运算符*/
switch(l)/*判断运算符,转而执行相应代码*/
{
case'+':
b=pop();
a=pop();
c=a+b;
printf("\n\n\tTheresultis%d\n",c);
printf("\n");
break;
case'-':
b=pop();
a=pop();
c=a-b;
printf("\n\n\tTheresultis%d\n",c);
printf("\n");
break;
case'*':
b=pop();
a=pop();
c=a*b;
printf("\n\n\tTheresultis%d\n",c);
printf("\n");
break;
case'/':
b=pop();
a=pop();
c=a/b;
printf("\n\n\tTheresultis%d\n",c);
printf("\n");
break;
}
printf("\tContinue?(y/n):");/*提示用户是否结束程序*/
l=getche();
if(l=='n')
exit(0);
}while(1);
}
F. 用C语言代码来编写含汉诺塔问题,利用堆栈来实现.求代码
算法思想
对于汉诺塔问题,当只移动一个圆盘时,直接将圆盘从 A 针移动到 C 针。若移动的圆盘为 n(n>1),则分成几步走:把 (n-1) 个圆盘从 A 针移动到 B 针(借助 C 针);A 针上的最后一个圆盘移动到 C 针;B 针上的 (n-1) 个圆盘移动到 C 针(借助 A 针)。每做一遍,移动的圆盘少一个,逐次递减,最后当 n 为 1 时,完成整个移动过程。
因此,解决汉诺塔问题可设计一个递归函数,利用递归实现圆盘的整个移动过程,问题的解决过程是对实际操作的模拟。
程序代码
#include <stdio.h>
int main()
{
int hanoi(int,char,char,char);
int n,counter;
printf("Input the number of diskes:");
scanf("%d",&n);
printf("\n");
counter=hanoi(n,'A','B','C');
return 0;
}
int hanoi(int n,char x,char y,char z)
{
int move(char,int,char);
if(n==1)
move(x,1,z);
else
{
hanoi(n-1,x,z,y);
move(x,n,z);
hanoi(n-1,y,x,z);
}
return 0;
}
int move(char getone,int n,char putone)
{
static int k=1;
printf("%2d:%3d # %c---%c\n",k,n,getone,putone);
if(k++%3==0)
printf("\n");
return 0;
}
G. 怎么用C语言编写堆栈并能进行四则运算
3个文件,按您的要求
头文件H
#IFNDEF _XXXX_H
#定义_XXXX_H
#定义错误-1
#定义MAX 100 / *定义堆栈的大小* /
静态int堆栈[MAX]; / *定义一个一维数组堆栈* /
静态int top = 0的; / *自定义堆栈指令* /
诠释推(int i)的;
的pop();
#ENDIF / * _XXXX_H * /
功能文件CPP
#包括“xxxx.h”的
诠释推(I)/ *内存操作数栈操作* /
{
??(顶部<MAX)
???
????栈[+ +顶部] = I / *栈,仍然有空间的堆栈顶部的表示移动一个位置* /
????返回0;
??}
??其他
???
????printf的(“堆栈满了”);
????返回错误;
??}
}
POP()/ *删除的操作数,并弹出操作* /
{
??VAR; / *定义要返回的顶级元素* /
??如果(top! = NULL)/ *仍然堆放元素* /
???
????VAR =栈[ - ] / *堆栈指针向下移动一个位置* /
????返回VAR / *返回的顶部堆栈元素* /
??}
??其他
????printf的(“堆栈是空的!\ n”);
??返回错误;
}
主文件CPP
#包括<stdio.h>
#包括<conio.h>
#包括<stdlib.h>
#包括“xxxx.h”的
无效的主要()
{
??INT M,N;
??字符L;
??整数A,B,C;
??整数K;
??{
????输出(“\ n \ n \ t单击以下提示输入你需要的信息\ n”);
????输出(“\ n \问你输入第二个捣弄数字”);
????scanf的(“%d”,&M);
????推(M)/ *第一个操作数堆栈* /
????输出(“\ n \问你输入第二个捣弄数字”);
????scanf的(“%d”,&N);
????推(N); / *第二个操作数堆栈* /
????输出(“\ n \你不想算术运算(+ / - / * / /):”);
????L = getche(); / *输入操作符* /
????开关(L)/ *确定的运营商,依次执行代码* /
????
??????'+':
????????= pop()方法;
????????= pop()方法;
????????C = A + B;
????????输出(“\ n \ n \ t计算公式为:%d条\ n”,c);
????????输出(“\ n”);
????????;
??????情况下,' - ':
????????= pop()方法;
????????= pop()方法;
????????C = A-B;
????????输出(“\ n \ n \ t计算公式为:%d条\ n”,c);
????????输出(“\ n”);
????????;
??????情况下,'*':
????????= pop()方法;
????????= pop()方法;
????????C = A * B;
????????输出(“\ n \ n \ t计算公式为:%d条\ n”,c);
????????输出(“\ n”);
????????;
??????情况下,'/':
????????= pop()方法;
????????= pop()方法;
????????C = A / B;
????????输出(“\ n \ n \ t计算公式为:%d条\ n”,c);
????????输出(“\ n”);
????????;
????}
????输出(“\ t要继续输入计算吗?(Y / N):”); / *提示用户是否在程序结束* /
????L = getche();
????(L =='N')
??????退出(0);
??}(1);
}
H. 栈的c语言实现基本操作
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#definestack_init_size20
#defineincreasesize10;
typedefintElemType;
typedefstructnode{
ElemType*base;
ElemType*top;
intsize;
}stack;
voidcreat(stack*s)
{
s->base=(ElemType*)malloc(sizeof(ElemType));
if(!s->base)
{
exit(0);
}
s->base=s->top;
s->size=stack_init_size;
}
voidpush(stack*s,ElemTypee)
{
if(s->top-s->base>=s->size)
{
s->base=(ElemType*)realloc(s->base,(s->size+increasesize)*sizeof(ElemType));
if(!s->base)
{
exit(0);
}
s->top=s->base+s->size;
s->size+=increasesize;
}
*(s->top)=e;
s->top++;
}
voidpop(stack*s,ElemType*e)
{
if(s->top==s->base)
{
return;
}
*e=*--(s->top);
}
intstacklen(stacks)
{
return(s.top-s.base);
}
intmain()
{
stacks;
intn;
ElemTypee1,e2,d;
inti=1,j=1;
push(&s,i);
push(&s,j);
scanf("%d",&n);
while(n--)
{
pop(&s,&e1);
pop(&s,&e2);
d=e1+e2;
push(&s,e2);
push(&s,d);
}
pop(&s,&d);
printf("%d",d);
return0;
}
I. c语言堆栈编程实现问题
修改完成:
#define MAXLEN 1000
#define EMPTY -1
#define FULL (MAXLEN-1)
#include <stdio.h>
typedef enum boolean
{
false, true
} boolean;
typedef struct stack
{
char s[MAXLEN];
int top;
} stack;
void reset(stack *stk)
{
stk->top = EMPTY;
}
void push(char c, stack *stk)
{
stk->top++;
stk->s[stk->top] = c;
}
char pop(stack *stk)
{
return (stk->s[stk->top--]);
}
void top(const stack *stk)
{
stk->s[stk->top];
}
boolean empty(const stack *stk)
{
return((boolean)(stk->top==EMPTY));
}
boolean full(const stack *stk)
{
return((boolean)(stk->top==FULL));
}
int main(void)
{
char str[] = "my name is laura";
int i;
stack s;
reset(&s);
printf("in the string: %s\n", str);
for (i = 0; str[i] != '\0'; ++i)
if(!full(&s))
push(str[i],&s);
printf("from the stack: ");
while (!empty(&s))
putchar(pop(&s));
putchar('\n');
return 0;
}
J. C语言实现堆栈
做个简单的例子,其他功能可以自己完善~ typedef struct
{
char *mem ;
int size ;
int poz ;
} Stack ;
Stack *CreateStack(int size)
{
Stack *stack ;
stack = (Stack *)malloc(sizeof (Stack)) ;
if (!stack) return 0 ;
stack->mem = (char *)malloc(size) ;
if (!stack->mem)
{
free(stack) ;
return 0 ;
}
stack->size = size ;
stack->poz = size - 1 ;
return stack ;
}
int ReleaseStack(Stack *stack)
{
if (!stack) return 0 ;
if (stack->mem)
free(stack->mem) ;
stack->mem = 0 ;
stack->size = 0 ;
stack->poz = -1 ;
return 0 ;
}
int Push(Stack *stack, void *souce, int size)
{
int p ;
if (!stack) return 0 ;
if (!stack->mem) return 0 ;
if (stack->poz + 1 < size) return 0 ;
stack->poz -= size ;
for (p = 0 ; p < size ; p++)
stack->mem[stack->poz + p] = ((char *)souce)[p] ;
return 1 ;
}
int Pop(Stack *stack, void *target, int size)
{
int p ;
if (!stack) return 0 ;
if (!stack->mem) return 0 ;
if (stack->poz + 1 == stack->size) return 0 ;
for (p = 0 ; p < size ; p++)
((char *)target)[p] = stack->mem[stack->poz + p] ;
stack->poz += size ;
return 1 ;
}
int PushInt(Stack *stack, int source)
{
return Push(stack, &source, sizeof (int)) ;
}
int PopInt(Stack *stack, int *target)
{
return Pop(stack, target, sizeof (int)) ;
}
void main( )
{
int i ;
Stack *stack = CreateStack(100) ;
for (i = 0 ; i < 10 ; i++)
PushInt(stack, i) ;
for (i = 0 ; i < 10 ; i++)
{
int n ;
PopInt(stack, &n) ;
printf("%d\n", n) ;
}
getch( ) ;
}