📄 jishuqi.cpp
字号:
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "Header.h"
#include "jishuqi.h"
/* 第一部分:验证输入的合法性 */
/******************************************************************/
/* 栈初始化 */
/* 函数名:init_sequence_stack() */
/******************************************************************/
void init_sequence_stack(sequence_stack *st)
{ st->top=0;}
/******************************************************************/
/* 判断栈是否为空 */
/* 函数名:is_empty_sequence_stack() */
/******************************************************************/
int is_empty_sequence_stack(sequence_stack st)
{return (st.top?0:1);
}
/******************************************************************/
/* 栈的插入操作 */
/* 函数名:push() */
/******************************************************************/
void push(sequence_stack *st,char x)
{if(st->top==maxsize)
{printf("\n The sequence stack id full!"); exit(1);}
st->a[st->top]=x;
st->top++;
}
/******************************************************************/
/* 取得栈顶节点值 */
/* 函数名:get_top() */
/******************************************************************/
char get_top(sequence_stack st)
{if(is_empty_sequence_stack (st))
{printf("\n The stack is empty!"); exit(1);}
else
return st.a[st.top-1];
}
/******************************************************************/
/* 栈的删除操作 */
/* 函数名:pop() */
/******************************************************************/
void pop(sequence_stack *st)
{if(st->top==0)
{printf("\n The sequence stack is empty!"); exit(1);}
st->top--;
}
/******************************************************************/
/* 判断表达式括号是否匹配 */
/* 函数名:match_Kuohao() */
/******************************************************************/
int match_kuohao(char c[])
{int i=0;
sequence_stack s;
init_sequence_stack(&s);
while(c[i]!='\0')
{
switch(c[i])
{case'{':
case'[':
case'(':push(&s,c[i]); break;
case'}':if(!is_empty_sequence_stack(s)&&get_top(s)=='{')
{pop(&s); break;}
else return 0;
case']':if(!is_empty_sequence_stack(s)&&get_top(s)=='[')
{pop(&s); break;}
else return 0;
case')':if(!is_empty_sequence_stack(s)&&get_top(s)=='(')
{pop(&s); break;}
else return 0;
}
i++;
}
return(is_empty_sequence_stack(s));
}
/******************************************************************/
/* 判断部分运算符 */
/* 函数名:isoperate() */
/******************************************************************/
int is_operate(char cha)
{
switch(cha)
{case'+':
case'-':
case'*':
case'/':
case',':
case'^':return 1;
default:return 0;
}
}
/******************************************************************/
/* 检验表达式是否正确 */
/* 函数名:jianyan() */
/******************************************************************/
int jianyan(char h[])
{int i=0,k;
i=0;
while(h[i]!='\0')
{if(h[i]==' ')
return Error2;
if(is_operate(h[i]))
{k=i-1;
if(h[i]!='-'&&(i==0||h[k]=='('||h[k]=='['||h[k]=='{'))
return Error3;
}
if(is_operate(h[i])&&is_operate(h[++i]))
return Error3;
if(is_operate(h[i])&&h[++i]=='!')
return Error3;
if((h[i]=='s'||h[i]=='S'||h[i]=='a'||h[i]=='A'||h[i]=='c'||h[i]=='C')&&h[++i]!='(')
return Error3;
if(h[i]=='!'&&(h[++i]=='s'|| h[i]=='S'||h[i]=='a'||h[i]=='A'||h[i]=='c'||h[i]=='C'))
return Error3;
i++;
}
h[i]='\0';
return R_OK;
}
/* 第二部分:转换为后缀表达式 */
/******************************************************************/
/* 将大中括号转换为小括号,负号改为'~'号 */
/* 函数名:zhuanhuan() */
/******************************************************************/
void zhuanhuan(char e[] )
{int i=0,k;
while(e[i]!='\0')
{if(e[i]=='{'||e[i]=='[')
e[i]='(';
else if(e[i]=='}'||e[i]==']')
e[i]=')';
else if(e[i]=='-') /*处理负数的‘-’号*/
{k=i-1;
if (i==0||e[k]=='('||e[k]=='['||e[k]=='{')
e[i]='~';
}
i++;
}
e[i]='\0';
}
/******************************************************************/
/* 求运算符的优先级 */
/* 函数名:priority() */
/******************************************************************/
int priority(char op)
{switch(op)
{case'\0':return -1;
case'(':return 0;
case',':return 1;
case'+':
case'-':
case'~':return 2;
case'*':
case'/':return 3;
case'^':
case'!':return 4;
case'A':
case'a':
case'C':
case'c':
case'S':
case's':return 5;
default:return -1;
}
}
/******************************************************************/
/* 判断一个字符是否为运算符 */
/* 函数名:is_operation() */
/******************************************************************/
int is_operation(char op)
{switch(op)
{case'+':
case'-':
case'*':
case'/':
case'A':
case'a':
case'C':
case'c':
case'S':
case's':
case',':
case'!':
case'^':
case'~':return 1;
default:return 0;
}
}
/******************************************************************/
/* 将一个中缀表达式,转换为后缀表达式 */
/* 函数名:postfix() */
/******************************************************************/
void postfix(char e[],char f[])
{int i=0,j=0;
char opst[100];
int top,t;
zhuanhuan(e);
top=0;
opst[top]='\0'; top++;
while(e[i]!='\0')
{if((e[i]>='0'&&e[i]<='9')||e[i]=='.')
f[j++]=e[i]; /*遇到数字和小数点直接写入后缀表达式*/
else if(e[i]=='(') /*遇到左括号进入操作符栈*/
{opst[top]=e[i];
top++;
}
else if(e[i]==')')
/*遇到右括号将其对应的左括号后的操作符全部写入后缀表达式*/
{ t=top-1;
while(opst[t]!='(')
{ f[j++]=opst[--top];
t=top-1;
}
top--;
}
else if(is_operation(e[i]))
{f[j++]=' '; /*用空格分开两个操作符*/
while(priority(opst[top-1])>=priority(e[i]))
f[j++]=opst[--top];
opst[top]=e[i];top++; /*当前元素进栈*/
}
i++;
}
while(top) f[j++]=opst[--top];
i=0;
while(f[i]!='\0') /*将后缀表达式中的','转换为' '*/
{ if(f[i]==',')
f[i]=' ';
i++;
}
f[i]='\0';
}
/* 第三部分:计算后缀表达式 */
/*****************************************************/
/* 求排列 */
/* 函数名:pailie() */
/*****************************************************/
double pailie(double m,double n)
{ double i,j;
double sum=1.0;
if(!m||!n) return 1;
else
for(i=m,j=1;j<=n&&i>=1;i--)
{sum=sum*i;j++;}
return sum;
}
/*****************************************************/
/* 求组合 */
/* 函数名:zuhe() */
/*****************************************************/
double zuhe(double m,double n)
{ double sum=1.0;
double add,i;
if(!m||!n) return 1;
else
for(i=n;i>=1;i--)
sum=sum*i;
add=pailie(m,n);
return(add/sum);
}
/*****************************************************/
/* 求阶乘 */
/* 函数名:fact() */
/*****************************************************/
double fact(double n)
{ double i;
double fac;
fac=1.0;
for(i=1.0;i<=n;++i)
fac=i*fac;
return(fac);
}
/******************************************************/
/* 将数字字符串转变成数 */
/* 函数名:readnumber() */
/******************************************************/
float readnumber(char f[],int *i)
{float x=0.0;
int k=0;
while(f[*i]>='0'&&f[*i]<='9') /*处理整数部分*/
{x=x*10+(f[*i]-'0');
(*i)++;
}
if(f[*i]=='.')
{(*i)++;
while(f[*i]>='0'&&f[*i]<='9') /*处理小数部分*/
{x=x*10+(f[*i]-'0');
(*i)++;
k++;
}
}
while(k!=0)
{ x=x/10;
k=k-1;
}
return(x);
}
/**************************************************************/
/* 求一个后缀表达式的值 */
/* 函数名:evalpost() */
/**************************************************************/
int evalpost(char f[],double *p_vaule)
{double obst[100];
int top=0;
int i=0,k1,k2;
double x1,x2;
while(f[i]!='\0')
{if(f[i]>='0'&&f[i]<='9')
{obst[top]=readnumber(f,&i); top++;}
else if(f[i]==' ') i++;
else if(f[i]=='+')
{x2=obst[--top];
x1=obst[--top];
if(top == -1)
return Error8;
obst[top]=x1+x2; top++;
i++;
}
else if(f[i]=='-')
{x2=obst[--top];
x1=obst[--top];
if(top == -1)
return Error8;
obst[top]=x1-x2; top++;
i++;
}
else if(f[i]=='~') /*处理负数*/
{x2=obst[--top];
obst[top]=(-1)*x2;
top++; i++;
}
else if(f[i]=='*')
{x2=obst[--top];
x1=obst[--top];
if(top == -1)
return Error8;
obst[top]=x1*x2;
top++;
i++;
}
else if(f[i]=='/')
{x2=obst[--top];
x1=obst[--top];
if(top == -1)
return Error8;
obst[top]=x1/x2; top++;
i++;
}
else if(f[i]=='!')
{x2=obst[--top];
k2=(int)x2;
if(x2<0)
return Error6;
if(k2!=x2)
return Error7;
obst[top]=fact(x2);
top++;
i++;
}
else if(f[i]=='a'||f[i]=='A')
{
x2=obst[--top];
x1=obst[--top];
k2=(int)x2;
k1=(int)x1;
if(top == -1)
return Error8;
if(x1<=0||x2<0||x2>x1)
return Error5;
if(k2!=x2||k1!=x1)
return Error7;
obst[top]=pailie(x1,x2);
top++;
i++;
}
else if(f[i]=='c'||f[i]=='C')
{x2=obst[--top];
x1=obst[--top];
k2=(int)x2;
k1=(int)x1;
if(top == -1)
return Error8;
if(x1<=0||x2<0||x2>x1)
return Error5;
if(k2!=x2||k1!=x1)
return Error7;
obst[top]=zuhe(x1,x2);
top++;i++;
}
else if(f[i]=='s'||f[i]=='S')
{x2=obst[--top];
if(x2<0)
return Error4;
obst[top]=sqrt(x2);
top++;
i++;
}
else if(f[i]=='^')
{x2=obst[--top];
x1=obst[--top];
if(top == -1)
return Error8;
obst[top]=pow(x1,x2);
top++;
i++;
}
}
*p_vaule=obst[0];
return R_OK;
}
/* 第四部分:主函数 */
int mainfunc(char *a,double *p_vaule)
{char f[100];
int i=0, error = 0;
if(!match_kuohao(a))
{ error= Error1;
return error;
}
if(error = jianyan(a), error)
{
return error;
}
postfix(a,f);
error=evalpost(f,p_vaule);
if(error!=0)
return error;
return R_OK;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -