代码:

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h> typedef enum{
TT_NUMBER,
TT_ADD,
TT_SUB,
TT_MUL,
TT_DIV,
TT_EOL
}TokenType; typedef struct{
TokenType type;
char text[];
}Token; static int pos=;
static char* line; void getToken(Token *token){
char arr[];
int index=;
arr[index]='\0'; while(line[pos]!='\0'){
if(line[pos]=='+'){
if(strlen(arr)>){
strcpy(token->text,arr);
index=;
arr[index]='\0';
token->type=TT_NUMBER; return;
} token->text[]='+';
token->text[]='\0';
token->type=TT_ADD;
pos++;
return;
}else if(line[pos]=='-'){
if(strlen(arr)>){
strcpy(token->text,arr);
index=;
arr[index]='\0';
token->type=TT_NUMBER;
return;
} token->text[]='-';
token->text[]='\0';
token->type=TT_SUB;
pos++;
return;
}else if(line[pos]=='*'){
if(strlen(arr)>){
strcpy(token->text,arr);
index=;
arr[index]='\0';
token->type=TT_NUMBER;
return;
} token->text[]='*';
token->text[]='\0';
token->type=TT_MUL;
pos++;
return;
}else if(line[pos]=='/'){
if(strlen(arr)>){
strcpy(token->text,arr);
index=;
arr[index]='\0';
token->type=TT_NUMBER;
return;
} token->text[]='/';
token->text[]='\0';
token->type=TT_DIV;
pos++;
return;
}else if(line[pos]=='\n'){
if(strlen(arr)>){
strcpy(token->text,arr);
index=;
arr[index]='\0';
token->type=TT_NUMBER;
return;
} token->text[]='\0';
token->type=TT_EOL;
pos++;
return;
}else{
arr[index]=line[pos];
index++;
arr[index]='\0';
pos++;
}
} if(strlen(arr)>){
strcpy(token->text,arr);
index=;
arr[index]='\0';
token->type=TT_NUMBER;
return;
}
} char* getTokenTypeDesc(Token *token){
char* arr;
arr = (char *)malloc(); if(token->type==){
strcpy(arr,"Num");
}else if(token->type==){
strcpy(arr,"Add");
}else if(token->type==){
strcpy(arr,"Sub");
}else if(token->type==){
strcpy(arr,"Mul");
}else if(token->type==){
strcpy(arr,"Div");
} return arr;
} void parse(){
Token token;
for(;;){
getToken(&token); if(token.type==TT_EOL){
break;
}else{
printf("%d %s %s\n",token.type, getTokenTypeDesc(&token) ,token.text);
}
}
} int _tmain(int argc, _TCHAR* argv[])
{
char buf[]; while(fgets(buf,,stdin)!=NULL){
pos=;
line=buf;
parse();
}
return ;
}

运行结果:

*+/-+
Num
Mul *
Num
Add +
Num
Div /
Num
Sub -
Num
Add +
Num
+
Num
Add +
Num

--2020年6月6日--

C:把算术表达式分成Token的更多相关文章

  1. 利用栈实现算术表达式求值(Java语言描述)

    利用栈实现算术表达式求值(Java语言描述) 算术表达式求值是栈的典型应用,自己写栈,实现Java栈算术表达式求值,涉及栈,编译原理方面的知识.声明:部分代码参考自茫茫大海的专栏. 链栈的实现: pa ...

  2. OpenJudge计算概论-简单算术表达式求值

    /*===================================== 简单算术表达式求值 总时间限制: 1000ms 内存限制: 65536kB 描述 2位正整数的简单算术运算(只考虑整数运 ...

  3. 【算法】E.W.Dijkstra算术表达式求值

    算术表达式求值 我们要学习的一个栈的用例同时也是展示泛型的应用的一个经典例子,就是用来计算算术表达式的值,例如 ( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) ) 如果将4乘以5,把3 ...

  4. page80-栈用例-算术表达式求值

    表达式由括号, 运算符和操作数(数字)组成.我们根据以下4中情况从左到右逐个将这些实体送入栈处理. (1)将操作数压入操作数栈: (2)将运算符压入运算符栈: (3)忽略左括号: (4)在遇到右括号时 ...

  5. SDUT2484算术表达式的转换

    http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2484&cid=1182 题目描述 小明在学习了数据结构之后,突然想起了以前没有解决的算术 ...

  6. Project Euler 93:Arithmetic expressions 算术表达式

    Arithmetic expressions By using each of the digits from the set, {1, 2, 3, 4}, exactly once, and mak ...

  7. poj3295解题报告(构造、算术表达式运算)

    POJ 3952,题目链接http://poj.org/problem?id=3295 题意: 输入由p.q.r.s.t.K.A.N.C.E共10个字母组成的逻辑表达式, 其中p.q.r.s.t的值为 ...

  8. C++算术运算符与算术表达式

    基本的算术运算符 在本章中主要介绍算术运算符与算术表达式,赋值运算符与赋值表达式,逗号运算符与逗号表达式,其他运算符将在以后各章中陆续介绍. 常见算数运算符 运算符 说明 举例 + 加法运算符,或正值 ...

  9. Openjudge-NOI题库-简单算术表达式求值

    题目描述 Description 两位正整数的简单算术运算(只考虑整数运算),算术运算为: +,加法运算:-,减法运算:*,乘法运算:/,整除运算:%,取余运算. 算术表达式的格式为(运算符前后可能有 ...

随机推荐

  1. 总结笔记 | 深度学习之Pytorch入门教程

    笔记作者:王博Kings 目录 一.整体学习的建议 1.1 如何成为Pytorch大神? 1.2 如何读Github代码? 1.3 代码能力太弱怎么办? 二.Pytorch与TensorFlow概述 ...

  2. Vue 函数式组件 functional

    函数式组件 无状态 无法实例化 内部没有任何生命周期处理函数 轻量,渲染性能高,适合只依赖于外部数据传递而变化的组件(展示组件,无逻辑和状态修改) 在template标签里标明functional 只 ...

  3. 01从DataGrid中导入到Excel

    01网络上有很多导出数据到Excel的方法,我在网上找到了一种比较简单实用的方法(参考了网友的方法) string fileName = ""; Microsoft.Win32.S ...

  4. Android开发学习进程0.18 SharePreference的使用 AIDL

    SharePreference SharePreference是一种持久化存储手段,使用场景很多,如第一次打开时加载的用户协议等.适合小数据单进程的应用.将数据以键值对的形式存储在XML中. 使用方式 ...

  5. 机器学习:支持向量机(SVM)

    SVM,称为支持向量机,曾经一度是应用最广泛的模型,它有很好的数学基础和理论基础,但是它的数学基础却比以前讲过的那些学习模型复杂很多,我一直认为它是最难推导,比神经网络的BP算法还要难懂,要想完全懂这 ...

  6. github Repository not found 解决办法

    git pull的时候遇到下面的报错. remote: Repository not found fatal: repository 'https://github.com/MyRepo/projec ...

  7. 【全解】Eclipse添加Spring项目插件

    1.Eclipse打开window-preference-InstallNewSoftware 2.先点Manage,取消掉The Eclipse Project Updates 3.选择Add . ...

  8. gpio模拟i2c驱动

    前段时间做项目,需要gpio模拟i2c通信,最后参考了一些资料,然后编写了一个程序.现在发出来,以免以后忘记,也为一些需要的朋友提供参考.不喜勿喷哈. /* 说明:该程序是基于atmel公司的sama ...

  9. StructuredStreaming简单的例子(NewAPI)

    StructuredStreaming简单的例子(NewAPI)(wordCount) package com.briup.streaming.structed import org.apache.l ...

  10. Navicat12 for Mysql激活

    1      下载 注册机和Navicat网盘下载地址 链接:https://pan.baidu.com/s/1AFpQIlHCXVHc8OuBZ9PAlA  提取码:xvi2 2      安装 2 ...