栈的应用举例

栈的应用举例

一、目的:

掌握栈的表示,实现及其针对栈的各种操作进行具体的应用。

 

二、要求:

1、建立一个顺序栈,实现括号配对,判断一个表达式中括号配对是否合法。

2、当用户输入一个合法的表达式后,能够返回正确的结果。能够计算的运算符包括:加、减、乘、除、括号;能够计算的数要求在实数范围内。对于异常表达式给出错误提示。(要求使用静态栈数据结构。)

 

三、实验内容

1、设计程序。

2、调试程序,并设计输入数据。

 

四、实验报告要求

写出程序和实验结果。

1.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define INITSIZE 10000
typedef struct
{
char *base;
int top;
int stacksize;
}sqstack;

void InitStack(sqstack *S);
void Print(char expression[]);
void Choose(int choice,char expression[]);
void InputForm(char expression[]);
void JudgeForm(char expression[]);
int Match(char x,char y);
void Push(sqstack *S,char x);
void Pop(sqstack *S);

int main(void)
{

char expression[INITSIZE]={' '};
Print(expression);
while(true)
{
   printf("按enter键继续...");
   getchar();
   getchar();
   system("cls");
   Print(expression);
}
return 0;
}
void InitStack(sqstack *S)
{
S->top=-1;
S->base=(char *)malloc(INITSIZE*sizeof(char));
S->stacksize=INITSIZE;
}

void Print(char expression[])
{
int choice;
printf("Made By 首都经济贸易大学信息学院\n");
printf("---------------------\n");
printf("使用说明:本程序可以判别一个数学表达式中所有括号的配对是否合法.\n");
printf("---------------------\n");
printf("1.输入一个带括号的数学表达式.\n");
printf("2.判断当前表达式是否合法.\n");
printf("3.按其它任意键退出.\n");
printf("---------------------\n");
printf("请选择你要的操作:");
scanf("%d",&choice);
Choose(choice,expression);
}
void Choose(int choice,char expression[])
{
switch(choice)
{
case 1:
   InputForm(expression);
   break;
case 2:
   JudgeForm(expression);
   break;
default:
   exit(0);
}
}
void InputForm(char expression[])
{
printf("请输入一个数学表达式:\n");
scanf("%s",expression);
printf("输入成功!\n");
}
void JudgeForm(char expression[])
{
printf("当前表达式:");
printf("%s",expression);
sqstack p;
sqstack *S;
S=&p;
InitStack(S);
int flag=1;
int length;
int i;
length=strlen(expression);
for(i=0;i<length;i++)
{
   if(expression[i]=='('||expression[i]=='['||expression[i]=='{')
   {
    Push(S,expression[i]);
   }
   else if(expression[i]==')'||expression[i]==']'||expression[i]=='}')
   {
    if(Match(expression[i],S->base[S->top]))
    {
     Pop(S);
    }
    else
    {
     flag=0;
     break;
    }
   }
}
if(flag)
{
   printf("合法!表达式中括号均匹配!\n");
}
else
{
   printf("非法!表达式中存在不匹配的括号\n");
}
}

int Match(char x,char y)
{
if(y=='('&&x==')')return true;
else if(y=='['&&x==']')return true;
else if(y=='{'&&x=='}')return true;
else return false;
}
void Push(sqstack *S,char x)
{
S->top++;
S->base[S->top]=x;
}
void Pop(sqstack *S)
{
S->top--;
}

 

 

 

2.

#define N 50
#include <ctype.h>
#include <string.h>
#include "stdio.h"
#include "stdlib.h"
typedef struct{
 int top;
 double array[N];
}NumStack;//数字栈
typedef struct{
 int top;
 char array[N];
}OpStack;//操作符栈
int Cint(char mychar){
 return (mychar-48);
}
void PushNum(NumStack *numstack,double num){
 numstack->top++;
 numstack->array[numstack->top-1]=num;
}
void PopNum(NumStack *numstack,double *num){
 *num=numstack->array[numstack->top-1];
 numstack->top--;
}
void PushOp(OpStack *opstack,char op){
 opstack->top++;
 opstack->array[opstack->top-1]=op;
}
void PopOp(OpStack *opstack,char *op){
 *op=opstack->array[opstack->top-1];
 opstack->top--;
}
double Calc(double a,double b,char c){
 double result;
 switch(c){
  case '+':result=a+b;break;
  case '-':result=a-b;break;
  case '*':result=a*b;break;
  case '/':result=a/b;break;
 }
 return result;
}
char Priority(char y,char x){
  char priority='<';
  switch(x){
   case '+':
   case '-':if(y=='(' || y=='#')priority='>';break;
   case '*':
   case '/':if(y=='(' || y=='#'|| y=='+' || y=='-')priority='>';break;
   case '(':priority='>';break;
   case ')':if(y=='(')priority='=';break;
   case '#':if(y=='#')priority='=';break;
   default:priority='E';
  }
  return priority;
}
void Process(NumStack *numstack,OpStack *opstack,char x){
 double a,b;char c;
 static double tempnum=0.00000000;static int len=10;static int dot=0,flags=0;
 if(isdigit(x) || x=='.'){
  if(x=='.')dot=1;
  else{
   if(dot==0)
    tempnum=tempnum*10+Cint(x);
   else{
    tempnum=tempnum+(double)Cint(x)/len;
    len*=10;
   }
  }
 }
 else{
  if(flags==0 && x!='('){PushNum(numstack,tempnum);tempnum=0.00000000;len=10;dot=0;}
  switch(Priority(opstack->array[opstack->top-1],x)){
   case '>':PushOp(opstack,x);flags=0;break;
   case '<':
     PopOp(opstack,&c);
     PopNum(numstack,&b);
     PopNum(numstack,&a);
     PushNum(numstack,Calc(a,b,c));flags=1;
     Process(numstack,opstack,x);break;
   case '=':PopOp(opstack,&c);flags=1;break;
   default:printf("Wrong Express!");
    exit(0);
  }
 }
}
main(){
 NumStack numstack;OpStack opstack;char s[N];int i=0;
 numstack.top=0;opstack.top=0;
 PushOp(&opstack,'#');
 printf("\nEnter your expression and end it with #:");scanf("%s",s);
 for(i=0;i<strlen(s);i++)
 Process(&numstack,&opstack,s[i]);
 printf("The result is %f",numstack.array[numstack.top-1]);
       }

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值