当前位置:网站首页>Stack application (balancer)
Stack application (balancer)
2022-07-03 13:33:00 【fitpolo】
explain : Is based on the previous chapter ,C Language realizes the basic functions of stack .
Stack code
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;
}
Test code
#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);
}
边栏推荐
- Universal dividend source code, supports the dividend of any B on the BSC
- Multi table query of MySQL - multi table relationship and related exercises
- Flink SQL knows why (13): is it difficult to join streams? (next)
- The shortage of graphics cards finally came to an end: 3070ti for more than 4000 yuan, 2000 yuan cheaper than the original price, and 3090ti
- User and group command exercises
- 网上开户哪家证券公司佣金最低,我要开户,网上客户经理开户安全吗
- CVPR 2022 | 美团技术团队精选6篇优秀论文解读
- mysql中的字段问题
- Mobile phones and computers can be used, whole people, spoof code connections, "won't you Baidu for a while" teach you to use Baidu
- 服务器硬盘冷迁移后网卡无法启动问题
猜你喜欢
[email protected]奇安信:透视俄乌网络战 —— 网络空间基础设施面临的安全对抗与制裁博弈..."/>
开始报名丨CCF C³[email protected]奇安信:透视俄乌网络战 —— 网络空间基础设施面临的安全对抗与制裁博弈...
MySQL_ JDBC
PowerPoint 教程,如何在 PowerPoint 中將演示文稿另存為視頻?
Internet of things completion -- (stm32f407 connects to cloud platform detection data)
Flink SQL knows why (17): Zeppelin, a sharp tool for developing Flink SQL
Setting up remote links to MySQL on Linux
Flink SQL knows why (VIII): the wonderful way to parse Flink SQL tumble window
CVPR 2022 | 美团技术团队精选6篇优秀论文解读
Mycms we media mall v3.4.1 release, user manual update
(first) the most complete way to become God of Flink SQL in history (full text 180000 words, 138 cases, 42 pictures)
随机推荐
Flink SQL knows why (17): Zeppelin, a sharp tool for developing Flink SQL
PowerPoint 教程,如何在 PowerPoint 中将演示文稿另存为视频?
35道MySQL面试必问题图解,这样也太好理解了吧
Disruptor -- a high concurrency and high performance queue framework for processing tens of millions of levels
Introduction to the implementation principle of rxjs observable filter operator
Shell timing script, starting from 0, CSV format data is regularly imported into PostgreSQL database shell script example
今日睡眠质量记录77分
Unity EmbeddedBrowser浏览器插件事件通讯
Kivy tutorial how to load kV file design interface by string (tutorial includes source code)
Logback 日志框架
106. 如何提高 SAP UI5 应用路由 url 的可读性
AI scores 81 in high scores. Netizens: AI model can't avoid "internal examination"!
CVPR 2022 | 美团技术团队精选6篇优秀论文解读
Universal dividend source code, supports the dividend of any B on the BSC
MySQL installation, uninstallation, initial password setting and general commands of Linux
Setting up remote links to MySQL on Linux
显卡缺货终于到头了:4000多块可得3070Ti,比原价便宜2000块拿下3090Ti
Tencent cloud tdsql database delivery and operation and maintenance Junior Engineer - some questions of Tencent cloud cloudlite certification (TCA) examination
MapReduce implements matrix multiplication - implementation code
实现CNN图像的识别和训练通过tensorflow框架对cifar10数据集等方法的处理