C语言!!!!

C语言!!!!刚了解到栈 不知道咋做 有点懵
题目是修改下面的栈示例使它存储字符而不是整数。接下来,增加main函数,用来要求用户输入一串圆括号或花括号,然后指出它们之间的嵌套是否正确:
Enter parenteses and/or braces:((){}{()})
Parenteses/braces are nested properly
提示:读入左圆括号或左花括号时,把它们像字符一样压入栈中。当读入右圆括号或右花括号时,把栈顶的项弹出,并且检查弹出项是否是匹配的圆括号或花括号。(如果不是,那么圆括号或花括号嵌套不正确。)当程序读入换行符时,检查栈是否为空。如果为空,那么圆括号或花括号匹配;如果栈不为空(或者如果曾经调用过stack_underflow函数),那么圆括号或花括号不匹配。如果调用stack_underflow函数,程序显示信息Stack overflow,并且立刻终止。
下面是栈示例:
#include <stdbool.h>
#define STACK_SIZE 100

int contents[STACK_SIZE];
int top = 0;

void make_empty (void)
{
top = 0;
}
bool is_empty (void)
{
return top == 0;
}
bool is_full (void)
{
return top == STACK_SIZE;
}
void push (int i)
{
is (is_full))
stack_overflow();
else
contents[top++] = i;
}
int pop (void)
{
if (is_empty ())
stack_underflow ();
else
return contents[--top];
}

第1个回答  2020-04-15
一楼正确。
第二道题的A
如果操作的数是常量比如1,就不能用++,--对其进行操作了。
如果定义了一些不能变的数也就不能用了。
第2个回答  2018-04-10
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define STACK_SIZE 100
char contents[STACK_SIZE];
int top = 0;
void make_empty(void)
{
top = 0;
}
bool is_empty(void)
{
return top == 0;
}
bool is_full(void)
{
return top == STACK_SIZE;
}
void stack_overflow(void)
{
printf("Stack overflow");
exit(1);
}
char stack_underflow(void)
{
printf("Stack underflow");
return 0;
}
void push(char i)
{
if(is_full())
stack_overflow();
else
contents[top++] = i;
}
char pop(void)
{
if (is_empty())
return stack_underflow();
else
return contents[--top];
}
int main()
{
char ch;
while (true) {
ch = getchar();
if (ch == '(' || ch == '{') {
push(ch);
}
else if (ch == ')') {
if (pop() != '(') {
printf("Parenteses/braces are not nested properly! ");
break;
}
}
else if (ch == '}') {
if (pop() != '{') {
printf("Parenteses/braces are not nested properly! ");
break;
}
}
else if (ch == '\n') {
if (is_empty()) {
printf("Parenteses/braces are nested properly! ");
}
else{
printf("Parenteses/braces are not nested properly! ");

}
break;
}
}
return 0;
}本回答被提问者和网友采纳
第3个回答  2020-01-13
相似回答