当前位置:网站首页>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);
}
边栏推荐
- 研发团队资源成本优化实践
- Server coding bug
- 106. 如何提高 SAP UI5 应用路由 url 的可读性
- Can newly graduated European college students get an offer from a major Internet company in the United States?
- Tutoriel PowerPoint, comment enregistrer une présentation sous forme de vidéo dans Powerpoint?
- Libuv Library - Design Overview (Chinese version)
- Flink SQL knows why (VIII): the wonderful way to parse Flink SQL tumble window
- MySQL functions and related cases and exercises
- mysql更新时条件为一查询
- Error running 'application' in idea running: the solution of command line is too long
猜你喜欢
rxjs Observable filter Operator 的实现原理介绍
JSP and filter
[quantitative trading] permanent portfolio, turtle trading rules reading, back testing and discussion
MySQL functions and related cases and exercises
刚毕业的欧洲大学生,就能拿到美国互联网大厂 Offer?
Detailed explanation of multithreading
8 Queen question
KEIL5出现中文字体乱码的解决方法
SQL Injection (GET/Select)
掌握Cypress命令行选项,是真正掌握Cypress的基础
随机推荐
Mobile phones and computers can be used, whole people, spoof code connections, "won't you Baidu for a while" teach you to use Baidu
常见的几种最优化方法Matlab原理和深度分析
PowerPoint 教程,如何在 PowerPoint 中將演示文稿另存為視頻?
Flink SQL knows why (13): is it difficult to join streams? (next)
json序列化时案例总结
The principle of human voice transformer
Setting up Oracle datagurd environment
栈应用(平衡符)
untiy世界边缘的物体阴影闪动,靠近远点的物体阴影正常
Error handling when adding files to SVN:.... \conf\svnserve conf:12: Option expected
Complete deep neural network CNN training with tensorflow to complete picture recognition case 2
35道MySQL面试必问题图解,这样也太好理解了吧
SwiftUI 开发经验之作为一名程序员需要掌握的五个最有力的原则
Flutter动态化 | Fair 2.5.0 新版本特性
Start signing up CCF C ³- [email protected] chianxin: Perspective of Russian Ukrainian cyber war - Security confrontation and sanctions g
ThreadPoolExecutor realizes multi-threaded concurrency and obtains the return value (elegant and concise way)
用户和组命令练习
【电脑插入U盘或者内存卡显示无法格式化FAT32如何解决】
Comprehensive evaluation of double chain notes remnote: fast input, PDF reading, interval repetition / memory
When we are doing flow batch integration, what are we doing?