本文目錄一覽:
C語言計算棧
#include stdio.h
double readnumber(char a[],int *i)//將數字字元轉變成相應的數
{
double x=0.0;
int k=0;
while(a[*i]=’0’a[*i]=’9′)
{
x=x*10+a[*i]-‘0’;
(*i)++;
}
if(a[*i]==’.’)
{
(*i)++;
while(a[*i]=’0’a[*i]=’9′)
{
x=x*10+a[*i]-‘0’;
(*i)++;
k++;
}
}
while(k!=0)
{
x=x/10.0;
k=k-1;
}
return x;
}
double yunsuan(char a[])//求一個後綴表達式的值
{
double obst[100],b,c;//操作數棧
int top=0,i=0;
while(a[i]!=’\0′)
{
if(a[i]=’0’a[i]=’9′)
obst[top++]=readnumber(a,i);
else if(a[i]==’ ‘) i++;
else if(a[i]==’+’)
{
b=obst[–top];
c=obst[–top];
obst[top++]=b+c;
i++;
}
else if(a[i]==’-‘)
{
b=obst[–top];
c=obst[–top];
obst[top++]=c-b;
i++;
}
else if(a[i]==’*’)
{
b=obst[–top];
c=obst[–top];
obst[top++]=b*c;
i++;
}
else if(a[i]==’/’)
{
b=obst[–top];
c=obst[–top];
obst[top++]=c/b;
i++;
}
}
return obst[0];
}
int pd(char op)//判斷一個字元是不是運算符
{
switch(op)
{
case ‘+’:
case ‘-‘:
case ‘*’:
case ‘/’:return 1;
default :return 0;
}
}
int priority(char op)//求運算符的優先順序
{
switch(op)
{
case ‘\0’:return -1;
case ‘(‘:return 0;
case ‘+’:
case ‘-‘:return 1;
case ‘*’:
case ‘/’:return 2;
default:return -1;
}
}
void charge(char a[],char b[])//將中綴表達式轉換等價的後綴表達式
{
int i=0,j=0;
char opst[100];
int top,t;
top=0;
opst[top]=’\0′;
top++;
while(a[i]!=’\0′)
{
if(a[i]=’0’a[i]=’9’||a[i]==’.’)
b[j++]=a[i];//遇到數字和小數點直接寫入後綴表達式
else if(a[i]=='(‘)//遇到左括弧進入操作符棧
{
opst[top]=a[i];
top++;
}
else if(a[i]==’)’)
{
t=top-1;
while(opst[t]!='(‘)
{//'(‘之前出棧
b[j++]=opst[–top];
t=top-1;
}
top–;
}
else if(pd(a[i]))//’+’,’-‘,’*’,’/’
{
b[j++]=’ ‘;//用空格分開兩個操作數
while(priority(opst[top-1])=priority(a[i]))
b[j++]=opst[–top];
opst[top]=a[i];
top++;
}
i++;
}
while(top) b[j++]=opst[–top];
}
int main()
{
char a[100],b[100];
double jieguo;
printf(“\n\t請輸入算術表達式:”);
scanf(“%s”,a);
charge(a,b);
jieguo=yunsuan(b);
printf(“\t表達式運算的結果為:%lf”,jieguo);
return 0;
}
棧的操作演算法c語言實現
#includeiostream
using namespace std;
#define STACK_INIT_SIZE 100
#define STACKINCREASE 1ypedef struct{
int *base;
int *top;
int stacksize;
}SqStack;
int InitStack (SqStack S)//構造空棧
{
S.base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
if(!S.base)exit(OVERFLOW);//存儲分配失敗
S.top=S.base;
return 0;
}
int Push(SqStack S,int e)//插入新的元素為新的棧頂元素
{
if(S.top-S.base=S.stacksize)//z棧滿,追加存儲空間
{
S.base=(int *)realloc(S.base,(S.stacksize+STACKINCREASE)*sizeof(int));
if(!S.base)exit(OVERFLOW);//存儲失敗
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREASE;
}
*S.top++ =e;//認為是先賦值再加加?
return 0;
}
void Insert(SqStack S,int e)
{
int *p;
int temp;
p=S.base;
if(S.top-S.base=S.stacksize)//z棧滿,追加存儲空間
{
S.base=(int *)realloc(S.base,(S.stacksize+STACKINCREASE)*sizeof(int));
if(!S.base)exit(OVERFLOW);//存儲失敗
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREASE;
}
while(*p=e)
p++;
while(pS.top)
{
temp=*p;
*p=e;
e=temp;
p++;
}
}0
void Pop(SqStack S)
{
if(S.base==S.top)
cout”該棧是空棧”endl;
while(S.baseS.top)
{
cout*++S.base” “;
}
}
void main()
{
SqStack S;
int j,i;
InitStack(S);
for(i=1;i=5;i++)
{
cout”請輸入”i”個棧內元素”endl;
cinj;
Push(S,j);
}
Pop(S);
cout”請輸入你要插入的元素”endl;
cinj;
Insert(S,j);
cout”輸出棧內元素”endl;
Pop(S);
}
c語言 C語言 雙向棧 演算法
#include stdio.h
#include stdlib.h
#include conio.h
#define MaxSize 1024
typedef struct stack {
int data[MaxSize];
int lp; // 左指針
int rp; // 右指針
}*STACK;
/*
棧指針總是指向待壓入位置。初始時,(0#棧)左指針lp指向data[0],即lp == 0;
(1#棧)右指針rp指向data[MaxSize – 1],即rp == MaxSize – 1。當lp == rp時,
還有一個存儲單元可用,所以棧滿的條件是lp rp。
*/
STACK InitStack() { // 置空雙向棧
STACK S = (STACK)malloc(sizeof(struct stack));
S-lp = 0;
S-rp = MaxSize – 1;
return S;
}
int Full(STACK S) { // 判斷是否滿了
return S-lp S-rp;
}
int Empty(STACK S, int number) { // 判斷number棧是否為空
if(number 0 || number 1) {
printf(“不存在 #%d 棧。\n”,number);
return 1;
}
if(number) return (S-rp MaxSize – 1); // 右棧
return S-lp 1; // 左棧
}
int Push(STACK S,int number,int e) { // 入棧
if(number 0 || number 1) {
printf(“不存在 #%d 棧。\n”,number);
return 0;
}
if(Full(S)) {
printf(“棧已滿。\n”);
return 0;
}
if(number) S-data[S-rp–] = e;
else S-data[S-lp++] = e;
return 1;
}
int Pop(STACK S,int number,int *e) { // 出棧
if(number 0 || number 1) {
printf(“不存在 #%d 棧。\n”,number);
return 0;
}
if(Empty(S,number)) {
printf(“#%d 空棧已空。\n”,number);
return 0;
}
if(number == 0) *e = S-data[–S-lp];
else *e = S-data[++S-rp];
return 1;
}
原創文章,作者:ZUEP,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/147419.html