Chinaunix首页 | 论坛 | 博客
  • 博客访问: 111525
  • 博文数量: 106
  • 博客积分: 2025
  • 博客等级: 大尉
  • 技术积分: 1165
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-06 12:51
文章分类

全部博文(106)

文章存档

2012年(106)

我的朋友

分类: C/C++

2012-05-07 17:28:35

栈的应用举例

一、目的:

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

二、要求:

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

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

三、实验内容

1、设计程序。

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

四、实验报告要求

写出程序和实验结果。

1.

#include
#include
#include
#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{
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
#include
#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;iProcess(&numstack,&opstack,s[i]);
printf("The result is %f",numstack.array[numstack.top-1]);
}

阅读(292) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~