C++逻辑表达式运算
题目
C++逻辑表达式运算
逻辑表达式运算
由大写英文字母和符号~、 *、+、()组成逻辑表达式,其中三个符号分别表示逻辑非、与、或运算,英文字母表示变量,变量有两种可能的取值,FALSE(0)或TRUE(1),、*、+、括号()可改变表达式的运算次序,且可以嵌套.编一个程序计算逻辑表达式的值.
【输入格式】
输入为若干行
第一行字符串s(1
答案
#include
using namespace std;
#define MAXSIZE 100
#define CHARNUM 26
//定义栈结构体
typedef struct _stack
{
char data[MAXSIZE];
int top;
} stack;
//将输入逻辑表达式字符串转换为逆波兰式的字符串
void translate(const char *p,char *pout)
{
stack op;
op.top=-1;
int i=0;
while (*p!='\0')
{
switch (*p)
{
case '(':
op.data[++op.top]=*p;
*p++;
break;
case ')':
while (op.data[op.top]!='(')
{
pout[i++]=op.data[op.top];
op.top--;
}
op.top--;
*p++;
break;
case ':
op.data[++op.top]=*p;
*p++;
break;
case '*':
case '+':
while ((op.top!=-1)&&(op.data[op.top]!='('))
{
pout[i++]=op.data[op.top];
op.top--;
}
op.data[++op.top]=*p;
*p++;
break;
case ' ':
break;
default:
pout[i++]=*p;
*p++;
}
}
while (op.top!=-1)
{
pout[i++]=op.data[op.top];
op.top--;
}
pout[i]='\0';
}
//逆波兰式的求解
bool calculator(const char* str)
{
stack st;
st.top=-1;
char ch0,ch1,c;
while (*str!='\0')
{
switch (*str)
{
case '0':
case '1':
st.data[++st.top]=*str;
break;
case ':
ch0=st.data[st.top--];
c=!(ch0-'0')+'0';
st.data[++st.top]=c;
break;
case '+':
ch0=st.data[st.top--];
ch1=st.data[st.top--];
c=((ch0-'0')||(ch1-'0'))+'0';
st.data[++st.top]=c;
break;
case '*':
ch0=st.data[st.top--];
ch1=st.data[st.top--];
c=((ch0-'0')&&(ch1-'1'))+'0';
st.data[++st.top]=c;
break;
default:
break;
}
*str++;
}
return st.data[st.top]-'0';
}
int main()
{
char str[MAXSIZE]={0};
int chartab[CHARNUM]={0};
fstream infile;
fstream outfile;
int sum;
//打开输入和输出文件
infile.open("stackc.in",fstream::in);
outfile.open("stackc.out",fstream::out);
//获取第一行字符串
infile.getline(str,MAXSIZE);
//获取第二行整数
sum=infile.get()-'0';
infile.get();
//得到各个变量的值
for (int i=0;i
举一反三
已知函数f(x)=x,g(x)=alnx,a∈R.若曲线y=f(x)与曲线y=g(x)相交,且在交点处有相同的切线,求a的值和该切线方程.
我想写一篇关于奥巴马的演讲的文章,写哪一篇好呢?为什么好
最新试题
- Always keep the faith Stand by TVXQ
- He wants to mske Chinese style music for the whole world to hear.
- p(x0,y0)是函数y=lnx-1与y=-x(x>0)的图像的交点,则lnx0^2+2x0=
- 某校到离校28千米的国家森林公园春游,先坐车,车速为36千米一小时,下车后以4千米一小时的速度步到达目的地,共花了1个小时,问步行了多长时间?
- 英语作文《We should protect to earth our home》初二的,80词
- 葡萄牙语语法分析
- 在等比数列an中,已知a1=-2,a(n+1)=Sn,求an,Sn n+1是下标
- 描写歌舞的句子!
- DT862-4型三相四线有功电表,型号为3*15(60)A,60r/kw·h,3×220/380V.这样的电表怎么读数
- 30+2y/20+2x=2/3,求x/y
热门考点
- 求函数z=y/√x²+y²的全微分dz
- 初一整式加减题
- 设A,B为n阶非零矩阵,且AB=0,则A,B的秩分别为都小于n,我只明白A或B的其中一个小于n.
- Very few people are ____ in the streets after 8:00 pm in this small town.
- 一位老寿星在2000年过第25个生日,已知他8岁时过第一个生日,他是何时生的?
- 1雁塔水泥厂去年上半年完成年生产计划的55%,下半年完成生产计划的60%,全年超产1500吨,这个厂去年计划
- 鼓励人们积极向上的诗句有哪些
- 为什么电风扇的叶片转动就会有风?
- 为了研究电阻跟哪些因素有关,实验中必须( ) A.保持一个因素不变,改变其它因素,比较电阻中电流的大小 B.同时变化多个因素,保持其它因素不变,比较每次电阻中电流的大小 C.一
- 染色体中的蛋白质是怎么跟Dna结合在一起的?