#include <stdio.h>
#include <string.h>
#include <math.h>
#define MaxSize 100
typedef struct CharStack //字符栈
{
char data[MaxSize];
int top;
}cStack;
typedef struct DoubleStack //数据栈
{
int data[MaxSize];
int top;
}dStack;
int Isop(char ); //当前扫描元素优先级
int Inop(char ); //栈顶元素优先级
void Initc(cStack *); //初始化字符栈
int Pushc(cStack *,char); //字符栈压栈
char Gettopc(cStack *); //返回栈顶元素
char Popc(cStack *); //出栈
void Initd(dStack *); //初始化数据栈
int Pushd(dStack *,int); //数据压栈
int Popd(dStack *); //出栈
void Trans(char*s1,char*s2); //转化为后缀表达式
int Calculate(char *s2); //后缀表达式求值
int main()
{
char s1[MaxSize]; //用于存储前缀表达式
char s2[MaxSize]; //用于存储转换后的表达式
//printf("请输入表达式:");
scanf("%s",s1);
Trans(s1,s2); //处理字符串,并转化为后缀表达式,存放在s2中
printf("表达式值: %d",Calculate(s2)); //后缀表达式求值
return 0;
}
//初始化
void Initc(cStack *s1)
{
s1->top=-1;
}
//字符栈压栈
int Pushc(cStack *c1,char op)
{
if(c1->top<MaxSize)
{
c1->data[++c1->top]=op;
return 1;
}
else return 0;
}
//GET栈顶元素
char Gettopc(cStack *c1)
{
return c1->data[c1->top];
}
//字符栈出栈
char Popc(cStack *c1)
{
return c1->data[c1->top--];
}
//初始化数据栈
void Initd(dStack *d1)
{
d1->top=-1;
}
//数据栈压栈
int Pushd(dStack *d1,int data)
{
if(d1->top<MaxSize)
{
d1->data[++d1->top]=data;
printf("%d\n",data);
return 1;
}
else return 0;
}
//数据栈出栈
int Popd(dStack *d1)
{
return d1->data[d1->top--];
}
int Isop(char op) //当前扫描运算符优先级
{
switch(op)
{
case '(': return 6;
case '+': case '-': return 2;
case '*': case '/': case '^': return 4;
}
return 0;
}
int Inop(char op) //当前扫描运算符优先级
{
switch(op)
{
case '(': return 1;
case '+': case '-': return 3;
case '*': case '/':case '^': return 5;
}
return 0;
}
void Trans(char *s1,char *s2)
{
int i=0;
int j=0;
int flag1=-1; //flag1为0表示上次输出为数字,flag1为1表示上次输出为字符
int flag2=-1; //flag2为0表示上次扫描为数字,flag为1表示上次扫描为运算符,用于区分数字后加空格
cStack st1; //暂放运算符
Initc(&st1);
while(s1[i]!='\0') //处理负数
{
if(s1[0]=='-') //第一位数字为负数时
{
j=strlen(s1);
while(j>0)
{
s1[j+5]=s1[j];
j--;
}
s1[j++]='(';
s1[j++]='0';
s1[j++]='-';
s1[j++]='1';
s1[j++]=')';
s1[j]='*';
}
if(s1[i]=='('&&s1[i+1]=='-') //非第一位负数时
{
j=strlen(s1);
while(j>i+1)
{
s1[j+5]=s1[j];
j--;
}
s1[j++]='(';
s1[j++]='0';
s1[j++]='-';
s1[j++]='1';
s1[j++]=')';
s1[j]='*';
i=i+5;
}
i++;
}
i=0;
j=0;
while(s1[i]!='\0')
{
if(flag1==0&&flag2==1) //若上次的输出为数字,上次循环扫描为字符,则表示该数字串结束,则在数字后加空格区分
{
s2[j++]=' ';
flag1=1;
}
if(s1[i]>='0'&&s1[i]<='9'||s1[i]=='.')
{
s2[j++]=s1[i];
flag2=0;
flag1=0;
}
else if(s1[i]=='+'||s1[i]=='-'||s1[i]=='*'||s1[i]=='/'||s1[i]=='('||s1[i]=='^')
{
flag2=1;
if(st1.top<0||Isop(s1[i])>Inop(Gettopc(&st1)))
{
Pushc(&st1,s1[i]);
}
else
{
while(st1.top>=0&&Isop(s1[i])<Inop(Gettopc(&st1))) //当前扫描字符优先级不断与栈顶字符优先级比较,当前字符小于栈顶字符时退栈并输出
{
s2[j++]=Popc(&st1);
flag1=1;
}
if(st1.top<0||Isop(s1[i])>Inop(Gettopc(&st1))) //当前字符优先级大于栈顶优先级或栈空时当前字符压入字符栈内
{
Pushc(&st1,s1[i]);
}
}
}
else if(s1[i]==')')
{
flag2=1;
if(Gettopc(&st1)!='(') //若括号仅包含数字则没有输出运算符
{
flag1=1;
}
while(Gettopc(&st1)!='(')
{
s2[j++]=Popc(&st1);
}
Popc(&st1); //将'('出栈
}
i++;
}
while(st1.top>=0) //将栈内剩余的运算符依次退栈输出
{
s2[j++]=Popc(&st1);
}
s2[j]='\0';
}
//表达式求值
int Calculate(char *s1)
{
int i=0;
int flag; //char类型转换为double类型数据标记
int data1,data2;
int sum;
dStack ds1;
Initd(&ds1);
while(s1[i]!='\0')
{
if(s1[i]=='+'||s1[i]=='-'||s1[i]=='*'||s1[i]=='/' || s1[i]=='^') //若为运算符获取栈顶两个元素进行计算
{
data1=Popd(&ds1);
data2=Popd(&ds1);
if(s1[i]=='+') Pushd(&ds1,data2+data1);
else if(s1[i]=='-') Pushd(&ds1,data2-data1);
else if(s1[i]=='*') Pushd(&ds1,data2*data1);
else if(s1[i]=='/') Pushd(&ds1,data2/data1);
else if(s1[i]=='^') Pushd(&ds1,(int)pow((double)data2,data1));
}
else //为数据时转化为double类型压栈
{
flag=0; //初始化为0为整数部分标记,1为小数部分标记
sum=0;
int divider=1;
while(s1[i]!=' '&&s1[i]!='+'&&s1[i]!='-'&&s1[i]!='*'&&s1[i]!='/' && s1[i]!='^')
{
if(s1[i]=='.') //若有小数点,进入小数转化模式
{
flag=1;
i++;
continue;
}
if(flag==0)
{
sum=sum*10+(int)(s1[i]-'0');
}
else
{
divider=divider*10;
sum=sum+((int)(s1[i]-'0'))/divider;
}
i++;
}
if(s1[i]=='+'||s1[i]=='-'||s1[i]=='*'||s1[i]=='/' || s1[i]=='^') i--; //转化成功一个数据,若下个字符为运算符,则i--,回到当前运算的数据位置
Pushd(&ds1,sum);
}
i++; //i++准备下一个字符的转换
}
return Popd(&ds1);
}
仅供参考:
#pragma warning(disable:4996)
/*---------------------------------------
函数型计算器(VC++6.0,Win32 Console)
功能:
目前提供了10多个常用数学函数:
⑴正弦sin
⑵余弦cos
⑶正切tan
⑷开平方sqrt
⑸反正弦arcsin
⑹反余弦arccos
⑺反正切arctan
⑻常用对数lg
⑼自然对数ln
⑽e指数exp
⑾乘幂函数^
⑿向上取整ceil
⒀向下取整floor
⒁四舍五入取整round
⒂取符号sign
⒃取绝对值abs
用法:
如果要求2的32次幂,可以打入2^32<回车>
如果要求30度角的正切可键入tan(Pi/6)<回车>
注意不能打入:tan(30)<Enter>
如果要求1.23弧度的正弦,有几种方法都有效:
sin(1.23)<Enter>
sin 1.23 <Enter>
sin1.23 <Enter>
如果验证正余弦的平方和公式,可打入sin(1.23)^2+cos(1.23)^2 <Enter>或sin1.23^2+cos1.23^2 <Enter>
此外两函数表达式连在一起,自动理解为相乘如:sin1.23cos0.77+cos1.23sin0.77就等价于sin(1.23)*cos(0.77)+cos(1.23)*sin(0.77)
当然你还可以依据三角变换,再用sin(1.23+0.77)也即sin2验证一下。
本计算器充分考虑了运算符的优先级因此诸如:2+3*4^2 实际上相当于:2+(3*(4*4))
另外函数名前面如果是数字,那么自动认为二者相乘.
同理,如果某数的右侧是左括号,则自动认为该数与括弧项之间隐含一乘号。
如:3sin1.2^2+5cos2.1^2 相当于3*sin2(1.2)+5*cos2(2.1)
又如:4(3-2(sqrt5-1)+ln2)+lg5 相当于4*(3-2*(√5 -1)+loge(2))+log10(5)
此外,本计算器提供了圆周率Pi键入字母时不区分大小写,以方便使用。
16进制整数以0x或0X开头。
----------------------------------------*/
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <locale.h>
#include <windows.h>
const char Tab = 0x9;
const int DIGIT = 1;
#define MAXLEN 16384
char s[MAXLEN], *endss;
int pcs = 15;
FILE *fp;
double sign(double dVal) {
if (dVal>0.0) return 1.0;
else if (dVal<0.0) return -1.0;
else return 0.0;
}
double round(double dVal, short iPlaces) {//iPlaces>=0
char s[30];
double dRetval;
sprintf(s, "%.*lf", iPlaces, dVal);
sscanf(s, "%lf", &dRetval);
return (dRetval);
}
double fun(double x, char op[], int *iop) {
while (op[*iop - 1]<32) //本行使得函数嵌套调用时不必加括号,如 arc sin(sin(1.234)) 只需键入arc sin sin 1.234<Enter>
switch (op[*iop - 1]) {
case 7: x = sin(x); (*iop)--; break;
case 8: x = cos(x); (*iop)--; break;
case 9: x = tan(x); (*iop)--; break;
case 10: x = sqrt(x); (*iop)--; break;
case 11: x = asin(x); (*iop)--; break;
case 12: x = acos(x); (*iop)--; break;
case 13: x = atan(x); (*iop)--; break;
case 14: x = log10(x); (*iop)--; break;
case 15: x = log(x); (*iop)--; break;
case 16: x = exp(x); (*iop)--; break;
case 17: x = ceil(x); (*iop)--; break;
case 18: x = floor(x); (*iop)--; break;
case 19: x = round(x,0);(*iop)--; break;
case 20: x = sign(x); (*iop)--; break;
case 21: x = fabs(x); (*iop)--; break;
}
return x;
}
double calc(char *expr, char **addr) {
static int deep; //递归深度
static char *fname[] = { "sin","cos","tan","sqrt","arcsin","arccos","arctan","lg","ln","exp","ceil","floor","round","sign","abs",NULL };
double ST[10] = { 0.0 }; //数字栈
char op[10] = { '+' }; //运算符栈
char c, *rexp, *pp, *pf;
int ist = 1, iop = 1, last, i, n;
__int64 i64;
if (!deep) {
pp = pf = expr;
do {
c = *pp++;
if (c != ' '&& c != Tab && c != ',')//跳过半角逗号(通常作为千分位分割符)
*pf++ = c;
} while (c != '\0');
}
pp = expr;
if ((c = *pp) == '-' || c == '+') {
op[0] = c;
pp++;
}
last = !DIGIT;
while ((c = *pp) != '\0') {
if (c == '(') {//左圆括弧
deep++;
ST[ist++] = calc(++pp, addr);
deep--;
ST[ist - 1] = fun(ST[ist - 1], op, &iop);
pp = *addr;
last = DIGIT;
if (*pp == '(' || isalpha(*pp) && strnicmp(pp, "Pi", 2)) {//目的是:当右圆括弧的右恻为左圆括弧或函数名字时,默认其为乘法
op[iop++] = '*';
last = !DIGIT;
c = op[--iop];
goto operate;
}
}
else if (c == ')') {//右圆括弧
pp++;
break;
}
else if (isalpha(c)) {
if (!strnicmp(pp, "Pi", 2)) {
if (last == DIGIT) {
fprintf(fp,"π左侧遇)\n"); if (fp!=stdout) fclose(fp); exit(1);
}
ST[ist++] = 3.14159265358979323846264338328;
ST[ist - 1] = fun(ST[ist - 1], op, &iop);
pp += 2;
last = DIGIT;
if (!strnicmp(pp, "Pi", 2)) {
fprintf(fp,"两个π相连\n"); if (fp!=stdout) fclose(fp); exit(2);
}
if (*pp == '(') {
fprintf(fp,"π右侧遇(\n"); if (fp!=stdout) fclose(fp); exit(3);
}
}
else {
for (i = 0; (pf = fname[i]) != NULL; i++)
if (!strnicmp(pp, pf, strlen(pf))) break;
if (pf != NULL) {
op[iop++] = 07 + i;
pp += strlen(pf);
}
else {
fprintf(fp,"陌生函数名\n"); if (fp!=stdout) fclose(fp); exit(4);
}
}
}
else if (c == '+' || c == '-' || c == '*' || c == '/' || c == '%' || c == '^') {
char cc;
if (last != DIGIT) {
fprintf(fp,"运算符粘连\n"); if (fp!=stdout) fclose(fp); exit(5);
}
pp++;
if (c == '+' || c == '-') {
do {
cc = op[--iop];
--ist;
switch (cc) {
case '+': ST[ist - 1] += ST[ist]; break;
case '-': ST[ist - 1] -= ST[ist]; break;
case '*': ST[ist - 1] *= ST[ist]; break;
case '/': ST[ist - 1] /= ST[ist]; break;
case '%': ST[ist - 1] = fmod(ST[ist - 1], ST[ist]); break;
case '^': ST[ist - 1] = pow(ST[ist - 1], ST[ist]); break;
}
} while (iop);
op[iop++] = c;
}
else if (c == '*' || c == '/' || c == '%') {
operate: cc = op[iop - 1];
if (cc == '+' || cc == '-') {
op[iop++] = c;
}
else {
--ist;
op[iop - 1] = c;
switch (cc) {
case '*': ST[ist - 1] *= ST[ist]; break;
case '/': ST[ist - 1] /= ST[ist]; break;
case '%': ST[ist - 1] = fmod(ST[ist - 1], ST[ist]); break;
case '^': ST[ist - 1] = pow(ST[ist - 1], ST[ist]); break;
}
}
}
else {
cc = op[iop - 1];
if (cc == '^') {
fprintf(fp,"乘幂符连用\n"); if (fp!=stdout) fclose(fp); exit(6);
}
op[iop++] = c;
}
last = !DIGIT;
}
else {
if (last == DIGIT) {
fprintf(fp,"两数字粘连\n"); if (fp!=stdout) fclose(fp); exit(7);
}
if (pp[0] == '0' && (pp[1] == 'x' || pp[1] == 'X')) {
sscanf(pp + 2, "%I64x%n", &i64, &n);
rexp = pp + 2 + n;
ST[ist++] = (double)i64;
}
else ST[ist++] = strtod(pp, &rexp);
ST[ist - 1] = fun(ST[ist - 1], op, &iop);
if (pp == rexp) {
fprintf(fp,"非法字符\n"); if (fp!=stdout) fclose(fp); exit(8);
}
pp = rexp;
last = DIGIT;
if (*pp == '(' || isalpha(*pp)) {
op[iop++] = '*';
last = !DIGIT;
c = op[--iop];
goto operate;
}
}
}
*addr = pp;
if (iop >= ist) {
fprintf(fp,"表达式有误\n"); if (fp!=stdout) fclose(fp); exit(9);
}
while (iop) {
--ist;
switch (op[--iop]) {
case '+': ST[ist - 1] += ST[ist]; break;
case '-': ST[ist - 1] -= ST[ist]; break;
case '*': ST[ist - 1] *= ST[ist]; break;
case '/': ST[ist - 1] /= ST[ist]; break;
case '%': ST[ist - 1] = fmod(ST[ist - 1], ST[ist]); break;
case '^': ST[ist - 1] = pow(ST[ist - 1], ST[ist]); break;
}
}
return ST[0];
}
void x2star() {//将两边不是字母且左边不是非数字或串开头紧跟0的x替换为*,目的是支持用x代替*,且和0x开头的16进制数不冲突
int i,L;
L=strlen(s);
for (i=1;i<L;i++) {
if (s[i]=='x' && !isalpha(s[i-1]) && !isalpha(s[i+1])) {
if (!(
(i==1 && s[0]=='0')
|| (i>1 && s[i-1]=='0' && !isdigit(s[i-2]))
)) s[i]='*';
}
}
}
int main(int argc, char **argv) {
int a;
setlocale( LC_ALL,"chs");
if (argc<2) {
//if (GetConsoleOutputCP() != 936) system("chcp 936>NUL");//中文代码页
printf("计算函数表达式的值。\n支持(),+,-,*,x,/,%%,^,Pi,sin,cos,tan,sqrt,arcsin,arccos,arctan,lg,ln,exp,ceil,floor,round,sign,abs\n");
while (1) {
printf("请输入表达式:");
fgets(s,MAXLEN,stdin);
if ('\n' == s[strlen(s)-1]) s[strlen(s) - 1] = 0;
if (s[0] == 0) break;//
x2star();printf("%s=%.15lg\n",s,calc(s, &endss));
}
return 0;
}
if (argc == 2 && 0 == strcmp(argv[1], "/?")) {
//if (GetConsoleOutputCP() != 936) system("chcp 936>NUL");//中文代码页
printf(
"计算由≥1个命令行参数给出的函数表达式的值。\n"
"最后一个参数是.0~.15表示将计算结果小数点后保留0~15位\n"
"最后一个参数是g1~g15表示将计算结果保留有效数字1~15位\n"
"最后一个参数是e0~e15表示将计算结果用科学计数法表示,且小数点后保留0~15位\n"
"最后一个参数是x表示将计算结果以16进制正整数格式输出\n"
"支持(),+,-,*,x,/,%%,^^,Pi,sin,cos,tan,sqrt,arcsin,arccos,arctan,lg,ln,exp,ceil,floor,round,sign,abs\n"
"16进制整数以0x或0X开头\n"
"忽略表达式中的半角逗号(通常作为千分位分割符)\n"
"如果第一个参数是/f且c:\\jsresult.txt可写,就将所有输出重定向到该文件\n"
);
return 0;
}
if (argc>2 && 0 == strcmp(argv[1], "/f")) {
fp=fopen("c:\\jsresult.txt","w");
if (NULL==fp) fp=stdout;
for (a=2;a<argc;a++) argv[a-1]=argv[a];
argc--;
} else {
fp=stdout;
}
strncpy(s, argv[1], MAXLEN - 1); s[MAXLEN - 1] = 0;
if (argc>2) {
for (a = 2; a<argc - 1; a++) strncat(s, argv[a], MAXLEN - 1);//将空格间隔的各参数连接到s
if (1 == sscanf(argv[a], ".%d", &pcs) && 0 <= pcs && pcs <= 15) {//最后一个参数是.0~.15表示将计算结果保留小数0~15位
x2star();fprintf(fp,"%.*lf\n", pcs, calc(s, &endss));
} else if (1 == sscanf(argv[a], "g%d", &pcs) && 1 <= pcs && pcs <= 15) {//最后一个参数是g1~g15表示将计算结果保留有效数字1~15位
x2star();fprintf(fp,"%.*lg\n", pcs, calc(s, &endss));
} else if (1 == sscanf(argv[a], "e%d", &pcs) && 0 <= pcs && pcs <= 15) {//最后一个参数是e0~e15表示将计算结果用科学计数法表示,且小数点后保留0~15位
x2star();fprintf(fp,"%.*le\n", pcs, calc(s, &endss));
} else if (argv[a][0] == 'x' || argv[a][0] == 'X') {//最后一个参数是x表示将计算结果以16进制正整数格式输出
x2star();fprintf(fp,"0x%016I64x\n", (__int64)calc(s, &endss));
} else {
strncat(s, argv[a], MAXLEN - 1);
x2star();fprintf(fp,"%.15lg\n", calc(s, &endss));
}
} else {
x2star();fprintf(fp,"%.15lg\n", calc(s, &endss));
}
if (fp!=stdout) fclose(fp);
return 0;
}