当前位置:网站首页>栈应用(平衡符)
栈应用(平衡符)
2022-07-03 12:58:00 【fitpolo】
说明:是基于前一章,C语言实现栈的基本功能。
栈代码
symbol_match.h
#ifndef __SYMBOL_MATCH_C__
#define __SYMBOL_MATCH_C__
extern int scanner(const char *code);
#endif
symbol_match.c
#include "symbol_match.h"
#include "link_stack.h"
static int is_left(char c)
{
int ret = 0;
switch(c)
{
case '<':
case '(':
case '[':
case '{':
case '\'':
case '\"':
ret = 1;
break;
default:
ret = 0;
break;
}
return ret;
}
static int is_right(char c)
{
int ret = 0;
switch(c)
{
case '>':
case ')':
case ']':
case '}':
case '\'':
case '\"':
ret = 1;
break;
default:
ret = 0;
break;
}
return ret;
}
static int match(char left, char right)
{
int ret = 0;
switch(left)
{
case '<':
ret = (right == '>');
break;
case '(':
ret = (right == ')');
break;
case '[':
ret = (right == ']');
break;
case '{':
ret = (right == '}');
break;
case '\'':
ret = (right == '\'');
break;
case '\"':
ret = (right == '\"');
break;
default:
ret = 0;
break;
}
return ret;
}
int scanner(const char *code)
{
int ret = 0;
int i=0;
char *c;
link_stack *head;
head = link_stack_create();
while(code[i] != '\0')
{
// printf("%c",code[i]);
if ( is_left(code[i]))
{
link_stack_push(head,(void*)&code[i]);
}
if ( is_right(code[i]))
{
c = link_stack_pop(head);
if (c==NULL || !match(c[0],code[i]))
{
printf("%c does not match!\n", code[i]);
ret = 0;
break;
}
}
i++;
}
if ( 0==link_stack_size(head) && code[i]=='\0')
{
printf("Succeed!\n");
ret = 1;
} else
{
printf("Invalid code!\n");
ret = 0;
}
link_stack_destroy(head);
return ret;
}
测试代码
#include "stdio.h"
#include "stdint.h"
#include "symbol_match.h"
int main(void)
{
//#define BUF_SIZE 10
// int i;
// int buf[BUF_SIZE];
// int *int32_tmp;
//const char* code = "#include <stdio.h> int main() { int a[5][5]; int (*p)[4]; p = a[0]; printf(\"%d\\n\", &p[3][3] - &a[3][3]); return 0; }";
const char* code = "{}[][]asdggggggggg<>()hhhhhhhh[][iiiiiiiiiiiii]";
printf("hello\n");
scanner(code);
}
边栏推荐
- The 35 required questions in MySQL interview are illustrated, which is too easy to understand
- Today's sleep quality record 77 points
- Flink SQL knows why (13): is it difficult to join streams? (next)
- Flink SQL knows why (12): is it difficult to join streams? (top)
- 已解决(机器学习中查看数据信息报错)AttributeError: target_names
- 显卡缺货终于到头了:4000多块可得3070Ti,比原价便宜2000块拿下3090Ti
- The R language GT package and gtextras package gracefully and beautifully display tabular data: nflreadr package and gt of gtextras package_ plt_ The winloss function visualizes the win / loss values
- 双链笔记 RemNote 综合评测:快速输入、PDF 阅读、间隔重复/记忆
- User and group command exercises
- MySQL constraints
猜你喜欢
stm32和电机开发(从mcu到架构设计)
今日睡眠质量记录77分
【历史上的今天】7 月 3 日:人体工程学标准法案;消费电子领域先驱诞生;育碧发布 Uplay
My creation anniversary: the fifth anniversary
用户和组命令练习
JSP and filter
Road construction issues
MySQL installation, uninstallation, initial password setting and general commands of Linux
Kivy tutorial how to automatically load kV files
使用Tensorflow进行完整的深度神经网络CNN训练完成图片识别案例2
随机推荐
Setting up Oracle datagurd environment
【R】 [density clustering, hierarchical clustering, expectation maximization clustering]
MySQL functions and related cases and exercises
User and group command exercises
Sword finger offer 11 Rotate the minimum number of the array
Kivy教程之 盒子布局 BoxLayout将子项排列在垂直或水平框中(教程含源码)
Smbms project
双链笔记 RemNote 综合评测:快速输入、PDF 阅读、间隔重复/记忆
使用tensorflow进行完整的DNN深度神经网络CNN训练完成图片识别案例
Flick SQL knows why (10): everyone uses accumulate window to calculate cumulative indicators
网上开户哪家证券公司佣金最低,我要开户,网上客户经理开户安全吗
106. How to improve the readability of SAP ui5 application routing URL
Spark实战1:单节点本地模式搭建Spark运行环境
PowerPoint 教程,如何在 PowerPoint 中将演示文稿另存为视频?
Flink SQL knows why (7): haven't you even seen the ETL and group AGG scenarios that are most suitable for Flink SQL?
rxjs Observable filter Operator 的实现原理介绍
Setting up remote links to MySQL on Linux
Resolved (error in viewing data information in machine learning) attributeerror: target_ names
Flink SQL knows why (19): the transformation between table and datastream (with source code)
Task5: multi type emotion analysis