相关知识 为了完成本关任务,你需要掌握:1.栈的基本特性,2.实现一个简单的顺序栈。
-
栈的基本特性 栈的基本特性是先进后出。
-
实现一个简单的顺序栈 本关要求完成一个顺序栈SqStack的基本运算及其实现,各函数详细功能请参见代码目录中的sqstack.h头文件,基本功能定义如下:
define MaxSize 1000 //顺序栈最大容量
typedef char ElemType; //栈中元素类型 //声明顺序栈类型 typedef struct { ElemType data[MaxSize]; int top; //栈顶指针 } SqStack; //初始化顺序栈 void InitStack(SqStack *&s); //判断栈空否 bool StackEmpty(SqStack *s); //进栈 bool Push(SqStack *&s, ElemType e); //出栈 bool Pop(SqStack *&s, ElemType &e); //取栈顶元素 bool GetTop(SqStack s, ElemType &e); 编程要求 任务一: 根据提示,在右侧编辑器补充完成sqstack.cpp文件中有关顺序栈的功能函数。 任务二: 根据提示,在右侧编辑器补充完成step5.cpp文件中的函数bool is_valid(char str)的代码,该函数的功能为判断传入的字符串str中包含的括号是否匹配,如果匹配函数返回true,否则返回false。
测试说明 平台会对你编写的代码进行测试:
测试输入: sin(10+20) 预期输出: yes
测试输入: {}{ 预期输出: no
测试输入: [ , { ( , . ) [,,,,,] } , , ] 预期输出: yes
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MaxSize 1000
typedef char ElemType;
//声明顺序栈类型
typedef struct
{
ElemType data[MaxSize];
int top; //栈顶指针
} SqStack;
//初始化顺序栈
void InitStack(SqStack *&s)
{
//请在下面编写代码
/**************************************Begin**************************************/
/***************************************End**************************************/
}
//销毁顺序栈
void DestroyStack(SqStack *&s)
{
//请在下面编写代码
/**************************************Begin**************************************/
/***************************************End**************************************/
}
//判断栈空否
bool StackEmpty(SqStack *s)
{
//请在下面编写代码
/**************************************Begin**************************************/
/***************************************End**************************************/
}
//进栈
bool Push(SqStack *&s, ElemType e)
{
//请在下面编写代码
/**************************************Begin**************************************/
/***************************************End**************************************/
}
//出栈
bool Pop(SqStack *&s, ElemType &e)
{
//请在下面编写代码
/**************************************Begin**************************************/
/***************************************End**************************************/
}
//取栈顶元素
bool GetTop(SqStack *s, ElemType &e)
{
//请在下面编写代码
/**************************************Begin**************************************/
/***************************************End**************************************/
}
/**
* 判断给出的括号表达式是否匹配:匹配返回true;不匹配返回false
*/
bool is_valid(char* str)
{
//请在下面编写代码
/*************************Begin*********************/
/**************************End**********************/
}
int main(int argc, char const *argv[])
{
char exp[1000];
fgets(exp, 1000, stdin);
bool flag = is_valid(exp);
if (flag)printf("yes\n");
else printf("no\n");
return 0;
}