资源简介
四则表达式求真值表,支持与或非蕴含等 直接就应该可以用
代码片段和文件信息
# include “stdio.h“
# include “ctype.h“
# include “string.h“
# include “stdlib.h“
# include “math.h“
# define MAXSIZE 100
typedef char elemtype;
typedef struct{
elemtype data[MAXSIZE];
int top;
}sqstack;
void init_sqsqstack(sqstack*S){
S->top = -1;
}
int empty_sqstack(sqstack*S){
if (S->top == -1)
return 1;
else
return 0;
}
void push_sqstack(sqstack*S elemtype x){
if (S->top == MAXSIZE - 1)
{
printf(“出现错误!“); return;
}
else
S->data[++(S->top)] = x;
}
void pop_sqstack(sqstack *S elemtype *x){
if (S->top == -1)
{
printf(“出现错误!“); return;
}
else{
*x = S->data[S->top];
S->top--;
}
}
void top_sqstack(sqstack*S elemtype*x){
if (S->top == -1)
{
printf(“出现错误!“); return;
}
else
*x = S->data[S->top];
}
int pre(char op){
switch (op){
case‘>‘:
case‘=‘:return 1; break;
case‘|‘:return 2; break;
/*‘>’‘=’“|”“&”“~”优先级依次升高*/
case‘&‘:return 3; break;
case‘~‘:return 4; break;
case‘(‘:
case‘#‘:
default:return 0; break;
}
}
void transform(char suffix[] char exp[]){
sqstack S;
char ch;
int i = 0 j = 0;
init_sqsqstack(&S);
push_sqstack(&S ‘#‘);
while (exp[i] != ‘\0‘){
ch = exp[i];
switch (ch){
case‘(‘:push_sqstack(&S exp[i]); i++; break;
case‘)‘:while (S.data[S.top] != ‘(‘){
pop_sqstack(&S &ch); suffix[j++] = ch;
}
pop_sqstack(&S &ch);
i++;
break;
case‘|‘:
case‘&‘:
case‘~‘:
case‘=‘:
case‘>‘:
while (pre(S.data[S.top]) >= pre(exp[i])){
pop_sqstack(&S &ch);
suffix[j++] = ch;
}
push_sqstack(&S exp[i]);
i++;
break;
default:while (isdigit(exp[i])){
suffix[j++] = exp[i];
i++;
}
}
}
while (S.data[S.top] != ‘#‘){
pop_sqstack(&S &ch); suffix[j++] = ch;
}
suffix[j] = ‘\0‘;
}
int calculate_exp(char exp[]){
sqstack S;
int i = 0;
char x x1 x2;
init_sqsqstack(&S);
while (exp[i] != ‘\0‘){
switch (exp[i]) {
case‘|‘:
pop_sqstack(&S &x2);
pop_sqstack(&S &x1);
x = (x1 - ‘0‘) || (x2 - ‘0‘);
push_sqstack(&S (x + ‘0‘));
i++; break;
//跳出寻找下一次
case‘=‘:
pop_sqstack(&S &x2);
pop_sqstack(&S &x1);
x = (x1 == x2);
push_sqstack(&S (x + ‘0‘));
i++; break;
case‘>‘:
pop_sqstack(&S &x2);
pop_sqstack(&S &x1);
x = !(x1 - ‘0‘) || (x2 - ‘0‘);
push_sqstack(&S (x + ‘0‘));
i++; break;
case‘&‘:
pop
- 上一篇:51单片机C语言电流电压测量代码
- 下一篇:51单片机GPS程序
相关资源
- MFC小学生速算练习系统VS2017
- C语言下用单链表实现一元多项式的四
- 自动生成算式的四则运算机器
- c++用栈实现四则运算
- c++实现四则运算器源码 支持括号
- c语言实现的大数四则运算程序
- C语言 实现离散数学真值表
- c语言实现离散的真值表判断、并交差
- 基于VS2010的C++小学生四则算数测试系
- C语言课程设计报告-长整数四则运算
- 整数小数四则运算计算器(C语言版用
- 信息安全原理大数四则运算及DH算法
- 离散数学c++编程 传递闭包 公式的真值
- VC++中运算表达式字符串解析求值四则
- c语言离散数学程序设计,求真值表,
- c++/c长整数四则运算
- 抽象数据类型:有理数四则运算
- 四则运算C++实现
- Qt之加减乘除四则运算-支持负数
- 小学生四则运算c++
- 数据结构实习1.4 双向循环链表实现长
- c语言,实现带括号的四则运算的程序
- 数据结构C语言运用栈实现的四则运算
评论
共有 条评论