惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog
Know Your Adversary
Know Your Adversary
博客园 - 叶小钗
雷峰网
雷峰网
大猫的无限游戏
大猫的无限游戏
M
MIT News - Artificial intelligence
量子位
A
About on SuperTechFans
The Register - Security
The Register - Security
F
Fortinet All Blogs
Microsoft Azure Blog
Microsoft Azure Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
IT之家
IT之家
博客园 - 聂微东
Blog — PlanetScale
Blog — PlanetScale
Hugging Face - Blog
Hugging Face - Blog
J
Java Code Geeks
有赞技术团队
有赞技术团队
阮一峰的网络日志
阮一峰的网络日志
云风的 BLOG
云风的 BLOG
人人都是产品经理
人人都是产品经理
Hacker News: Ask HN
Hacker News: Ask HN
T
The Exploit Database - CXSecurity.com
Vercel News
Vercel News
Stack Overflow Blog
Stack Overflow Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园 - 司徒正美
NISL@THU
NISL@THU
V2EX - 技术
V2EX - 技术
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Schneier on Security
Schneier on Security
博客园 - 三生石上(FineUI控件)
T
The Blog of Author Tim Ferriss
AWS News Blog
AWS News Blog
The GitHub Blog
The GitHub Blog
C
Cisco Blogs
T
Tenable Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Cyber Attacks, Cyber Crime and Cyber Security
V
Vulnerabilities – Threatpost
美团技术团队
L
LangChain Blog
Google DeepMind News
Google DeepMind News
腾讯CDC
P
Privacy International News Feed
Spread Privacy
Spread Privacy
D
DataBreaches.Net
Engineering at Meta
Engineering at Meta
S
Security @ Cisco Blogs

珒陶

LC电路分析入门 - 珒陶 Windows下OpenCV交叉编译到ARM-Linux - 珒陶 本博客基本完成迁移 - 珒陶 微软在GitHub合并了我提交的PR! - 珒陶 使用VSCode编辑Keil μVision5项目,并用Makefile自动化编译和下载 - 珒陶 Murmurer 现已上架Google Play! - 珒陶 MindCanvas 思维导图现已开源! - 珒陶 本站已更新 - 珒陶 本站已恢复 - 珒陶
C语言实现中缀表达式计算 - 珒陶
珒陶 珒陶 · 2022-04-05 · via 珒陶

  关于中缀表达式的计算,网上已经有许多代码了,但大多为了学习而专门定义了栈,且不支持识别除数为0等错误。刚好最近需要,所以写了一种尽量精炼的中缀表达式计算方法,在此记录一下。

#include "stdio.h"
#include "stdlib.h"

typedef enum
{
    NO_ERROR,               // 成功
    DIVISION_BY_ZERO,       // 除数为0
    INVALID_EXPRESSION,     // 表达式无效
} CalcErrorType;

// 计算从*buffer开始的表达式
// 利用lastResult和*result参数模拟栈
CalcErrorType getResult(char **buffer, float lastResult, float *result)
{
    float num;
    char op;
    CalcErrorType error;

    // 判断表达式结束
    if (**buffer == '\0' || **buffer == ')')
    {
        *result = lastResult;
        return NO_ERROR;
    }

    // 尝试读一个操作符,默认加号(如果当前buffer指向数字)
    op = (**buffer == '-' || **buffer == '*' || **buffer == '/') ? *((*buffer)++) : '+';

    // 读一个数
    if (*((*buffer)++) == '(')
    {
        if ((error = getResult(buffer, 0, &num)) != NO_ERROR) return error;
        (*buffer)++;
    }
    else
        num = strtof(--(*buffer), buffer);

    switch (op)
    {
        case '-':
            num = -num;// 把减法当成加一个负数,所以此处不能break
        case '+':
            if ((error = getResult(buffer, num, &num)) != NO_ERROR) return error;
            *result = lastResult + num;
            return NO_ERROR;
        case '*':
            return getResult(buffer, lastResult * num, result);
        case '/':
            return num == 0 ? DIVISION_BY_ZERO : getResult(buffer, lastResult / num, result);
        default:
            return INVALID_EXPRESSION;
    }
}

int main()
{
    char buffer[100], *p1, **p2;
    CalcErrorType error;
    float result;

    while (1)
    {
        p1 = buffer;
        p2 = &p1;
        printf("expression: ");
        scanf("%s", buffer);

        if ((error = getResult(p2, 0, &result)) == NO_ERROR)
            printf("result: %f\n", result);
        else
            printf("error: %d\n", error);
    }

    return 0;
}

  运行效果:
捕获.PNG

Q.E.D.