当前位置:网站首页>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);
}
边栏推荐
- Logseq evaluation: advantages, disadvantages, evaluation, learning tutorial
- MySQL functions and related cases and exercises
- Task5: multi type emotion analysis
- Multi table query of MySQL - multi table relationship and related exercises
- 父亲和篮球
- Red Hat Satellite 6:更好地管理服务器和云
- 今日睡眠质量记录77分
- Server coding bug
- Comprehensive evaluation of double chain notes remnote: fast input, PDF reading, interval repetition / memory
- 静态链表(数组的下标代替指针)
猜你喜欢

Mycms we media mall v3.4.1 release, user manual update

常见的几种最优化方法Matlab原理和深度分析

Flink SQL knows why (XV): changed the source code and realized a batch lookup join (with source code attached)

Detailed explanation of multithreading

stm32和电机开发(从mcu到架构设计)

STM32 and motor development (from MCU to architecture design)

mysql更新时条件为一查询

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

2022-02-14 incluxdb cluster write data writetoshard parsing

Several common optimization methods matlab principle and depth analysis
随机推荐
Setting up remote links to MySQL on Linux
The network card fails to start after the cold migration of the server hard disk
Spark实战1:单节点本地模式搭建Spark运行环境
MapReduce implements matrix multiplication - implementation code
Red hat satellite 6: better management of servers and clouds
User and group command exercises
Will Huawei be the next one to fall
Flink SQL knows why (XIV): the way to optimize the performance of dimension table join (Part 1) with source code
Libuv库 - 设计概述(中文版)
Father and basketball
stm32和电机开发(从mcu到架构设计)
Annotation and reflection
Universal dividend source code, supports the dividend of any B on the BSC
Spark practice 1: build spark operation environment in single node local mode
Fabric. JS three methods of changing pictures (including changing pictures in the group and caching)
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
In the promotion season, how to reduce the preparation time of defense materials by 50% and adjust the mentality (personal experience summary)
The principle of human voice transformer
PostgreSQL installation
Kivy教程之 如何自动载入kv文件