20210414

回顾| 无穷级数(三)| 数据结构-栈与队列(六)| 田静语法C2-S2

Table of Contents

回顾

恋词复习

无穷级数(三)

傅里叶系数与傅里叶级数

收敛定理(狄利克雷)

周期为2Π的函数的展开

[0,Π]上展为正弦或展为余弦

周期为2l的函数的展开

常考题型与例题

不在这里展示。

数据结构-栈与队列(六)

用栈求中缀表达式值

int getPriority(char op) //判断优先级大小
{
if(op == '+' || op == '-')
return 0;
else
return 1;
}
int calSub(float opand1, char op, float opand2, float &result)
{
if (op == '+') result = opand1 + opand2;
if (op == '-') result = opand1 - opand2;
if (op == '*') result = opand1 * opand2;
if (op == '/')
{
if(fabs(opand2) < MIN)
{
return 0;
}
else
{
result = opand1/opand2;
}
}
return 1;
}
float calInfix(char exp[])
{
float s1[maxSize]; int top = -1;
char s2[maxSize]; int top = -1;
int i = 0;
while(exp[i] != '\0')
{
if('0' <= exp[i] && exp[i] <= 'q')
{
s1[++top1] = exp[i] - '0';
++i;
}
else if(exp[i] = '(')
{
s2[++top2] = '(';
++i;
}
else if(exp[i] == +'
exp[i] == '-'
exp[i] == '*'
exp[i] == '/')
{
if (top2 == -1 || s2[top2] == '(' || getPriority(exp[i]) > getPriority(s2[top2]))
{
s2[++top2] == exp[i];
++i;
}
else
{
int flag = calStackTopTwo(s1,top1,s2,top2);
if(flag ==0)
return 0;
}
}
else if(exp[i] == ')')
{
while(s2[top2] != '(')
{
int flag = calStackTopTwo(s1,top1,s2,top2);
if(flag == 0)
return 0;
}
--top2;
++i;
}
}
while (top2 != -1)
{
int flag = calStackTopTwo(s1,top1,s2,top2);
if(flag == 0)
return 0;
}
return s1[top1];
}
int calStackTopTwo(float s[i], int &top1,char s2[], int &top2)
{
float opnd1,opnd2,result;
char op;
int flag;
opnd2 = s1[top--];
opnd1 = s1[top--];
op = s2[top2--];
flag = calSub(opnd1,op,opnd2,result);
if(flag == 0 )
std::cout <<"ERROR"<<std::endl;
s1[++top1] = result;
return flag;
}

用栈求后缀表达式值

自己画图感受一下过程把.....

float calPostFix(char exp[])
{
float s[maxSize];int top = -1;
int i = 0;
while(exp[i] != '\0')
{
if('0' <= exp[i] && exp[i] <= '9')
s[++top] = exp[i]-'0';
else if(exp[i] == +'
exp[i] == '-'
exp[i] == '*'
exp[i] == '/')
{
float opnd1,opnd2,result;
char op;
int flag;
opnd2 = s[top--];
opnd1 = s[top--];
op = exp[i];
flag = calSub(opnd1,op,opnd2,result);
if(flag == 0){
std::cout<<"ERROR"<<std::endl;
break;
}
s[++top] = result;
}
++i;
return s[top];
}
}

用栈求前缀表达式值

float calPreFix(char exp[], int len)
{
float s[maxSize];int top = -1;
for(int i = len-1; i >= 0 ; --i)
{
if('0' <= exp[i] && exp[i] <= '9')
s[++top] = exp[i]-'0';
else
{
float opnd1,opnd2,result;
char op;
int flag;
opnd2 = s[top--];
opnd1 = s[top--];
op = exp[i];
flag = calSub(opnd1,op,opnd2,result);
if(flag == 0){
std::cout<<"ERROR"<<std::endl;
break;
}
s[++top] = result;
}
}
return s[top];
}