当前位置:网站首页>栈应用(平衡符)
栈应用(平衡符)
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);
}
边栏推荐
- Multi table query of MySQL - multi table relationship and related exercises
- php:&nbsp; The document cannot be displayed in Chinese
- Smbms project
- JSP and filter
- Comprehensive evaluation of double chain notes remnote: fast input, PDF reading, interval repetition / memory
- Flink SQL knows why (VIII): the wonderful way to parse Flink SQL tumble window
- 2022-02-14 analysis of the startup and request processing process of the incluxdb cluster Coordinator
- Kivy教程之 如何自动载入kv文件
- Asp.Net Core1.1版本没了project.json,这样来生成跨平台包
- Box layout of Kivy tutorial BoxLayout arranges sub items in vertical or horizontal boxes (tutorial includes source code)
猜你喜欢

Annotation and reflection

Libuv库 - 设计概述(中文版)

Setting up remote links to MySQL on Linux

研发团队资源成本优化实践

Logseq 评测:优点、缺点、评价、学习教程

Mysql database basic operation - regular expression

File uploading and email sending

已解决TypeError: Argument ‘parser‘ has incorrect type (expected lxml.etree._BaseParser, got type)

When we are doing flow batch integration, what are we doing?

User and group command exercises
随机推荐
Finite State Machine FSM
双链笔记 RemNote 综合评测:快速输入、PDF 阅读、间隔重复/记忆
Flink SQL knows why (7): haven't you even seen the ETL and group AGG scenarios that are most suitable for Flink SQL?
2022-02-13 plan for next week
Asp.Net Core1.1版本没了project.json,这样来生成跨平台包
Tutoriel PowerPoint, comment enregistrer une présentation sous forme de vidéo dans Powerpoint?
网上开户哪家证券公司佣金最低,我要开户,网上客户经理开户安全吗
Task6: using transformer for emotion analysis
Start signing up CCF C ³- [email protected] chianxin: Perspective of Russian Ukrainian cyber war - Security confrontation and sanctions g
Sword finger offer 14- ii Cut rope II
PowerPoint 教程,如何在 PowerPoint 中将演示文稿另存为视频?
Swiftui development experience: the five most powerful principles that a programmer needs to master
The difference between session and cookie
Logseq evaluation: advantages, disadvantages, evaluation, learning tutorial
CVPR 2022 | 美团技术团队精选6篇优秀论文解读
Can newly graduated European college students get an offer from a major Internet company in the United States?
In the promotion season, how to reduce the preparation time of defense materials by 50% and adjust the mentality (personal experience summary)
实现CNN图像的识别和训练通过tensorflow框架对cifar10数据集等方法的处理
阿南的疑惑
CVPR 2022 image restoration paper