当前位置:网站首页>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);
}
边栏推荐
- Flink SQL knows why (16): dlink, a powerful tool for developing enterprises with Flink SQL
- KEIL5出现中文字体乱码的解决方法
- R language uses the data function to obtain the sample datasets available in the current R environment: obtain all the sample datasets in the datasets package, obtain the datasets of all packages, and
- When updating mysql, the condition is a query
- 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
- 掌握Cypress命令行选项,是真正掌握Cypress的基础
- Anan's doubts
- 人身变声器的原理
- Flink SQL knows why (XI): weight removal is not only count distinct, but also powerful duplication
- [redis] cache warm-up, cache avalanche and cache breakdown
猜你喜欢

PowerPoint 教程,如何在 PowerPoint 中将演示文稿另存为视频?

双向链表(我们只需要关注插入和删除函数)

2022-02-14 incluxdb cluster write data writetoshard parsing

rxjs Observable filter Operator 的实现原理介绍

Multi table query of MySQL - multi table relationship and related exercises

regular expression

Internet of things completion -- (stm32f407 connects to cloud platform detection data)

Smbms project

The 35 required questions in MySQL interview are illustrated, which is too easy to understand

【历史上的今天】7 月 3 日:人体工程学标准法案;消费电子领域先驱诞生;育碧发布 Uplay
随机推荐
双链笔记 RemNote 综合评测:快速输入、PDF 阅读、间隔重复/记忆
Asp.Net Core1.1版本没了project.json,这样来生成跨平台包
Libuv库 - 设计概述(中文版)
STM32 and motor development (from MCU to architecture design)
8 Queen question
父亲和篮球
MapReduce实现矩阵乘法–实现代码
Students who do not understand the code can also send their own token, which is easy to learn BSC
Kivy tutorial how to automatically load kV files
Flink SQL knows why (VIII): the wonderful way to parse Flink SQL tumble window
DQL basic query
Internet of things completion -- (stm32f407 connects to cloud platform detection data)
Server coding bug
已解决(机器学习中查看数据信息报错)AttributeError: target_names
The difference between session and cookie
Anan's doubts
Logseq evaluation: advantages, disadvantages, evaluation, learning tutorial
mysql更新时条件为一查询
Servlet
Comprehensive evaluation of double chain notes remnote: fast input, PDF reading, interval repetition / memory