这个计算器其实是我老师布置的一个c语言大作业,捉摸着搞了那么久的东西不能浪费了吧,于是我分享下我的代码和大概思路
给个关注点个赞,后续我会分享更多我们学生党的作业问题
白嫖党们先看代码,我就先上上全代码,干!
前言:
为了达到目的,首先自学了栈:按照先进后出的原则存储数据,先进入的数据被压入栈底,最后的数据在栈顶,需要读数据的时候从栈顶开始弹出数据。允许进行插入和删除操作的一端称为栈顶(top),另一端为栈底(bottom);栈底固定,而栈顶浮动;栈中元素个数为零时称为空栈。插入一般称为进栈(PUSH),删除则称为退栈(POP)。允许进行插入和删除操作的一端称为栈顶(top),另一端为栈底(bottom);栈底固定,而栈顶浮动;栈中元素个数为零时称为空栈。插入一般称为进栈(PUSH),删除则称为退栈(POP)。其次查阅多方面资料结合所学知识进行编写而成。
(1)InitStack(S)初始化:初始化一个新的栈。
(2)Empty(S)栈的非空判断:若栈S不空,则返回TRUE;否则,返回 FALSE。
(3)Push(S,x)入栈:在栈S的顶部插入元素x,若栈满,则返回 FALSE;否则,返回TRUE。
(4)Pop(S)出栈:若栈S不空,则返回栈顶元素,并从栈顶中删除该元 素;否则,返回空元素NULL。
(5)GetTop(S)取栈顶元素:若栈S不空,则返回栈顶元素;否则返回 空元素NULL。
(6)SetEmpty(S)置栈空操作:置栈S为空栈。
(7) 常以top = -1表示空栈。
进栈(PUSH)算法
①若TOP≥n时,则给出溢出信息,作出错处理(进栈前首先检查栈是否已满,满则溢出;不满则作②);
②置TOP=TOP+1(栈指针加1,指向进栈地址);
③S(TOP)=X,结束(X为新进栈的元素)
退栈(POP)算法
①若TOP≤0,则给出下溢信息,作出错处理(退栈前先检查是否已为空栈,空则下溢;不空则作②);
②X=S(TOP),(退栈后的元素赋给X):
③TOP=TOP-1,结束(栈指针减1,指向栈顶)
(1)函数功能介绍及介绍:能够实现连续的运算,混合运算,基本上可以等同于手机上计算器。仅限于加减乘除的四则运算。(强调运算时的括号必须是英文版本的,否则运行会出错。写表达式也可以加上“=”和不加不影响运行结果,最终还是以回车进行结束得到结果)。如果能在visualc++运行,稍微调整一下,可以利用自带的功能设置一个界面,这样就可以完成跟手机自带那种计算器相同了。
加法运算:1+2回车可得3,连续运算1+2+3+4+5回车可得15
减法运算:1-2回车为-1,连续运算5-1-2回车得2
乘法:23回车6,连续运算23*4回车24
除法运算24/4回车6,24/2/2回车6、
混合运算:(5+2)*2回车14
(一)软件环境:Devc++
我用的这个软件哈,个人感觉这里功能简单,特别容易上手。看图说话,是不是很简单嘛,又不复杂。

(二)设计方案
根据自学所得栈进行数据和符号的存储再输出,先设立单独的数据栈符号栈,我们以top=-1为标准,判断其是否为空栈,当然也用到了学过的struct来构建栈,先把字符存进去再说,在里面我们要进行运算,然后再拿出来给我们看。于是我们就要四则运算,用switch四个case把加减乘除表达出来,这能进行简单的运算吧,那混合运算还带括号咋办,于是要用到判断优先级,加减,乘除分别同级,先左括号读到右括号我们要停止,这样就可以进行混合的运算了。后面我们经过调用前面设的函数想办法怎么把它输出来,我们就是要用到入栈顶什么的最后出栈,用个free(str)释放下内存打印出来得到结果。
(3)函数功能:
用到了第八章内容结构结合自学内容构造栈,switch表达式来判断优先级,主要用到的为自学的栈push进栈,pop出栈,top=-1划分是否为空字符,在前言写很清楚了。
(四)全代码:
#include
#include
/*数据栈*/
struct shuju
//struct结构体构建栈
{
int data
[100];
int top
;
};
/*符号栈*/
struct fuhao
{
char symbol
[100];
int top
;
};
void InitOperateNum(struct shuju
*StackNum
) //数据栈非空
{
StackNum
->top
= -1;
}
void InitOperateSymbol(struct fuhao
*StackSymbol
) //符号栈非空
{
StackSymbol
->top
= -1;
}
/*存入数据栈*/
void Inshuju(struct shuju
*StackNum
, int num
)
{
StackNum
->top
++;
StackNum
->data
[StackNum
->top
] = num
;
}
/*存入符号栈*/
void Infuhao(struct fuhao
*StackSymbol
, char ch
)
{
StackSymbol
->top
++;
StackSymbol
->symbol
[StackSymbol
->top
] = ch
;
}
/*读取数据栈*/
int Randshuju(struct shuju
*StackNum
)
{
return StackNum
->data
[StackNum
->top
];
}
/*读取符号栈*/
char Randfuhao(struct fuhao
*StackSymbol
)
{
return StackSymbol
->symbol
[StackSymbol
->top
];
}
/*从数据栈取出数据*/
int Putshuju(struct shuju
*StackNum
)
{
int x
;
x
= StackNum
->data
[StackNum
->top
];
StackNum
->top
--;
return x
;
}
/*从符号栈取出符号*/
char Putfuhao(struct fuhao
*StackSymbol
)
{
char c
;
c
= StackSymbol
->symbol
[StackSymbol
->top
];
StackSymbol
->top
--;
return c
;
}
/*符号优先级判断*/
int judge(char ch
) {
if(ch
== '(')
{
return 1;
}
if(ch
== '+' || ch
== '-') {
return 2;
}
else if(ch
== '*' || ch
== '/') {
return 3;
}
else if(ch
== ')') {
return 4;
}
}
/*四则运算*/
int Math(int v1
, int v2
, char c
)
{
int sum
;
switch(c
) {
case '+' : {
sum
= v1
+ v2
;
break;
}
case '-' : {
sum
= v1
- v2
;
break;
}
case '*' : {
sum
= v1
* v2
;
break;
}
case '/' : {
sum
= v1
/ v2
;
break;
}
}
return sum
;
}
int main()
{
struct shuju data
;
struct fuhao symbol
;
InitOperateNum(&data
); //调用数据
InitOperateSymbol(&symbol
); //调用符号
int i
, t
, sum
, v1
, v2
;
char c
;
i
= t
= sum
= 0;
char v
[100] = {0};
char *str
= (char *)malloc(sizeof(char)*200);
while((c
= getchar()) != '\n') //非空字符
{
str
[i
] = c
;
i
++;
}
str
[i
] = '\0';
for(i
= 0; str
[i
] != '\0'; i
++) {
if(i
== 0 && str
[i
] == '-') {
v
[t
++] = str
[i
];
}
else if(str
[i
] == '(' && str
[i
+1] == '-') {
i
++;
v
[t
++] = str
[i
++];
while(str
[i
] >= '0' && str
[i
]
v
[t
] = 0;
t
--;
}
if(str
[i
] != ')') {
i
--;
Infuhao(&symbol
, '(');
}
}
else if(str
[i
] >= '0' && str
[i
]
v
[t
] = str
[i
];
t
++;
i
++;
}
Inshuju(&data
, atoi(v
));
while(t
> 0) {
v
[t
] = 0;
t
--;
}
i
--;
}
else {
if(symbol
.top
== -1)
{ //如果符号栈没有元素,直接把符号放入符号栈
Infuhao(&symbol
, str
[i
]);
}
else if(judge(str
[i
]) == 1) { //如果此符号是'(',直接放入符号栈
Infuhao(&symbol
, str
[i
]);
}
else if(judge(str
[i
]) == 2) { //如果此符号是'+'或'-',判断与栈顶符号是优先级
if(judge(Randfuhao(&symbol
)) == 1) { //如果栈顶符号是'(',放入符号栈
Infuhao(&symbol
, str
[i
]);
}
else if(judge(Randfuhao(&symbol
)) == 2) { //如果栈顶符号是'+'或'-',则出栈运算
while(symbol
.top
>= 0 && data
.top
>= 1) { //循环出栈
v2
= Putshuju(&data
);
v1
= Putshuju(&data
);
sum
= Math(v1
, v2
, Putfuhao(&symbol
));
Inshuju(&data
, sum
); //将运算结果压入数据栈
}
Infuhao(&symbol
, str
[i
]); //新符号进栈
}
else if(judge(Randfuhao(&symbol
)) == 3) { //如果栈顶符号是'*'或'/',则进符号栈
while(symbol
.top
>= 0 && data
.top
>= 1) { //循环出栈
v2
= Putshuju(&data
);
v1
= Putshuju(&data
);
sum
= Math(v1
, v2
, Putfuhao(&symbol
));
Inshuju(&data
, sum
); //将运算结果压入数据栈
}
Infuhao(&symbol
, str
[i
]); //新符号进栈
}
/*栈顶符号不可能是')',故不做判断*/
}
else if(judge(str
[i
]) == 3) { //如果此符号是'*'或'/',则判断与栈顶符号是优先级
if(judge(Randfuhao(&symbol
)) == 1) { //如果栈顶符号是'(',放入符号栈
Infuhao(&symbol
, str
[i
]);
}
else if(judge(Randfuhao(&symbol
)) == 2) { //如果栈顶符号是'+'或'-',则进符号栈
Infuhao(&symbol
, str
[i
]); //新符号进栈
}
else if(judge(Randfuhao(&symbol
)) == 3) { //如果栈顶符号是'*'或'/',则出栈运算
while(symbol
.top
>= 0 && data
.top
>= 1) { //循环出栈
v2
= Putshuju(&data
);
v1
= Putshuju(&data
);
sum
= Math(v1
, v2
, Putfuhao(&symbol
));
Inshuju(&data
, sum
); //将运算结果压入数据栈
}
Infuhao(&symbol
, str
[i
]); //新符号进栈
}
}
else if(judge(str
[i
]) == 4) { // 如果此符号是')',则出栈运算直到遇到'('
do { //循环出栈直到遇到'('
v2
= Putshuju(&data
);
v1
= Putshuju(&data
);
sum
= Math(v1
, v2
, Putfuhao(&symbol
));
Inshuju(&data
, sum
); //将运算结果压入数据栈
}while(judge(Randfuhao(&symbol
)) != 1);
Putfuhao(&symbol
); //括号内运算结束后使'('出栈
}
}
}
free(str
); //释放内存空间
while(symbol
.top
!= -1) {
v2
= Putshuju(&data
);
v1
= Putshuju(&data
);
sum
= Math(v1
, v2
, Putfuhao(&symbol
));
Inshuju(&data
, sum
);
}
printf("%d", data
.data
[0]);
return 0;
}
我们来看看演示结果:



好了,诸如类似不再演示了,代码讲解我觉得注释就够了,不关注点个赞吗?
(5)升级版代码
补充更新,看到很多人都觉得上面的代码太简单,为此再更新一个复杂的代码,可以执行科学计算,复杂四则运算:


完整代码:运行即可成功
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "windows.h"
#include "math.h"
#include "conio.h"
double stringToDouble(char a
[1000])
{
int i
=0;
char *p
=(char*)malloc(1000*sizeof(char));
int top
=0;
double sum
=0;
double mul
=1;
while(a
[i
]!='\0'&&a
[i
]!='.')
{
p
[top
]=a
[i
];
top
++;
i
++;
}
while(top
!=0)
{
top
--;
sum
=sum
+(p
[top
]-48)*mul
;
mul
=mul
*10;
}
if(a
[i
]=='.')
{
i
++;
mul
=0.1;
while(a
[i
]!='\0')
{
sum
=sum
+(a
[i
]-48)*mul
;
mul
=mul
/10;
i
++;
}
}
return sum
;
}
struct expre
{
double num
;
char sign
;
int ifNum
;//标志位,1表示数字,0表示符号
int end
;//标志位,标志结构体数据末尾
}*expre1
,*expre2
;
struct numstack
{
double *base
;
}numstack1
;//计算后缀表达式所需的数据栈
struct signstack
{
char *base
;
}signstack1
;//符号栈
void structExpreRead(struct expre expre1
[1000])
{
int i
=0;
while(expre1
[i
].end
!=1)
{
if(expre1
[i
].ifNum
==1)
{
printf("%f ",expre1
[i
].num
);
i
++;
}
else
{
printf("%c ",expre1
[i
].sign
);
i
++;
}
}
}
//四则运算
double compute(double a
,double b
,char sign
)
{
if(sign
=='+')
return a
+b
;
if(sign
=='-')
return b
-a
;
if(sign
=='*')
return a
*b
;
if(sign
=='/')
return b
/a
;
}
//符号优先级
int signnum(char x
)
{
if(x
=='(')
return 1;
if(x
=='+'||x
=='-')
return 2;
if(x
=='*'||x
=='/')
return 3;
}
void stringCopy(char *a
,char *b
,int start
,int length
)
{
int i
;
int j
=0;
for(i
=start
;i
int strRead
=0,strWrite
=0;
int strStart
;
int length
=0;
double strCuttenFloat
;
char strCutten
[1000];
int i
=0;//表达式遍历索引
int j
=0;//后缀表达式索引
int signtop
=-1;//符号栈顶指针
int signtop1
=-1;//数据栈顶指针
int s
=0;
int k
=0;//后缀表达式遍历
double a
,b
;//四则运算参数
expre1
=(struct expre
*)malloc(strlen(exp
)*sizeof(struct expre
));
while(exp
[strRead
]!='\0')
{
//如果是数字,存进结构体中的num
if(exp
[strRead
]>='0'&&exp
[strRead
]
strRead
++;
length
++;
}
//截取数字字符串,并转换成数字
strncpy(strCutten
,exp
+strStart
,length
);
strCutten
[length
]='\0';
strCuttenFloat
=stringToDouble(strCutten
);
expre1
[strWrite
].num
=strCuttenFloat
;
expre1
[strWrite
].ifNum
=1;
expre1
[strWrite
].end
=0;
expre1
[strWrite
].sign
='o';
strWrite
++;
length
=0;
}
//如果是符号,存进结构体中的sign
else
{
expre1
[strWrite
].sign
=exp
[strRead
];
expre1
[strWrite
].ifNum
=0;
expre1
[strWrite
].end
=0;
expre1
[strWrite
].num
=0;
strRead
++;
strWrite
++;
}
}
expre1
[strWrite
].end
=1;
expre1
[strWrite
].ifNum
=1;
expre1
[strWrite
].sign
='o';
expre1
[strWrite
].num
=0;
/*下面将表达式转换成后缀表达式*/
//符号栈初始化
signstack1
.base
=(char*)malloc(strlen(exp
)*sizeof(char));
//后缀表达式初始化
expre2
=(struct expre
*)malloc(strlen(exp
)*sizeof(struct expre
));
while(expre1
[i
].end
!=1)
{
if(expre1
[i
].ifNum
==1)
{
expre2
[j
].num
=expre1
[i
].num
;
expre2
[j
].ifNum
=1;
j
++;
i
++;
}
else if(expre1
[i
].sign
=='(')
{
signtop
++;
signstack1
.base
[signtop
]=expre1
[i
].sign
;
i
++;
}
else if(signnum(expre1
[i
].sign
)==2)
{
if(signtop
==-1)
{
signtop
++;
signstack1
.base
[signtop
]=expre1
[i
].sign
;
i
++;
}
else
{
while(signnum(signstack1
.base
[signtop
])==2||signnum(signstack1
.base
[signtop
])==3)
{
expre2
[j
].sign
=signstack1
.base
[signtop
];
expre2
[j
].ifNum
=0;
j
++;
signtop
--;
if(signtop
==-1)
break;
}
signtop
++;
signstack1
.base
[signtop
]=expre1
[i
].sign
;
i
++;
}
}
else if(expre1
[i
].sign
=='*'||expre1
[i
].sign
=='/')
{
if(signtop
==-1)
{
signtop
++;
signstack1
.base
[signtop
]=expre1
[i
].sign
;
i
++;
}
else
{
while(signnum(signstack1
.base
[signtop
]==3))
{
expre2
[j
].sign
=signstack1
.base
[signtop
];
expre2
[j
].ifNum
=0;
j
++;
signtop
--;
if(signtop
==-1)
break;
}
signtop
++;
signstack1
.base
[signtop
]=expre1
[i
].sign
;
i
++;
}
}
else
{
if(expre1
[i
].sign
==')')
{
while(signstack1
.base
[signtop
]!='(')
{
expre2
[j
].sign
=signstack1
.base
[signtop
];
expre2
[j
].ifNum
=0;
j
++;
signtop
--;
}
i
++;
signtop
--;
}
else if(expre1
[i
].sign
=='s')
{
}
else
{
printf("输入表达式格式不规范!\n");
Sleep(2000);
exit(1);
}
}
}
while(signtop
!=-1)
{
expre2
[j
].sign
=signstack1
.base
[signtop
];
expre2
[j
].ifNum
=0;
j
++;
signtop
--;
}
expre2
[j
].end
=1;
numstack1
.base
=(double*)malloc(100*sizeof(double));
if(numstack1
.base
==NULL)
{
printf("\n动态内存分配失败\n");
Sleep(2000);
exit(1);
}
while(expre2
[k
].end
!=1)
{
if(expre2
[k
].ifNum
==1)/*数字直接入栈*/
{
signtop1
++;
numstack1
.base
[signtop1
]=expre2
[k
].num
;
k
++;
}
else/*如果是符号*/
{
a
=numstack1
.base
[signtop1
];
signtop1
--;
b
=numstack1
.base
[signtop1
];
numstack1
.base
[signtop1
]=compute(a
,b
,expre2
[k
].sign
);
k
++;
}
}
return numstack1
.base
[signtop1
];
}
//结构体用来分别存储表达式中的数字和符号
//表达式转换成标准的中缀表达式
//操作符分为单目和双目
/*
*s:sin;x:arcsin
*c:cos;d:arccos
*t:tan;g:arctan
*q:平方根
*l:lg
*e:ln
*/
//检测表达式是否需要化简
int ifNeedSimplify(char *expre
)
{
int i
=0;
while(expre
[i
]!='\0')
{
if(expre
[i
]!='+'&&expre
[i
]!='-'&&expre
[i
]!='*'&&expre
[i
]!='/'&&expre
[i
]!='('&&expre
[i
]!=')')
return 1;
i
++;
}
return 0;
}
void HideCursor() /*隐藏光标*/
{
CONSOLE_CURSOR_INFO cursor_info
= {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE
), &cursor_info
);
}
void gotoxy(int x
,int y
)
{
COORD c
;
c
.X
=x
-1;
c
.Y
=y
-1;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE
),c
);
}
void setxy(int x
, int y
)/*列,行*/
{ COORD coord
= {x
, y
};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE
), coord
);
}
void Horizontalline(int x1
,int y1
,int length
)
{
int i
;
setxy(x1
,y1
);
for(i
=0;i
putchar('|');
y1
++;
setxy(x1
,y1
);
}
}
void initialSet()
{
Horizontalline(0,4,18);
Horizontalline(0,8,18);
Horizontalline(0,12,18);
Horizontalline(0,16,18);
Verticalline(0,4,14);
Verticalline(8,4,14);
Verticalline(16,4,14);
Verticalline(24,4,14);
Verticalline(32,4,14);
setxy(3,6);
printf("sin:s");
setxy(1,10);
printf("arcsin:x");
setxy(3,14);
printf("sqrt:q");
setxy(11,6);
printf("cos:c");
setxy(10,10);
printf("arccos:d");
setxy(11,14);
printf("+/-");
setxy(19,6);
printf("tan:t");
setxy(19,10);
printf("atan:g");
setxy(19,14);
printf("*/");
setxy(27,6);
printf("lg:l");
setxy(27,10);
printf("ln:e");
setxy(27,14);
printf("( )");
setxy(0,0);
}
void clearIn()
{
setxy(0,1);
printf(" ");
setxy(2,0);
printf(" ");
setxy(0,0);
}
void clearOut()
{
setxy(0,20);
printf(" ");
}
int main()
{
while(1){
char expre
[1000];
int i
=0,j
=1;
int startI
;
char str1
[1000],numStr
[1000],str2
[1000];//将expre分成三个部分
int bracketsNum
=1;
double sinValue
,cosValue
,tanValue
,qValue
,logValue
,lnValue
,arcsinValue
,arccosValue
,arctanValue
;
char numChar
[1000];
int s
=0;
char pause
;
initialSet();
printf("请输入表达式:\n");
gets(expre
);
while(expre
[i
]!='\0')//缺陷:sin里面不能实现函数的嵌套。sin函数必须带括号.
{
if(expre
[i
]=='s')
{
startI
=i
;
//截取expre的第一个片段
stringCopy(str1
,expre
,0,startI
);
//读取sin函数中的表达式
i
=i
+2;
while(bracketsNum
!=0)
{
if(expre
[i
]=='(')
bracketsNum
++;
if(expre
[i
]==')')
bracketsNum
--;
i
++;
}
bracketsNum
=1;
//截取expre的第二个片段
stringCopy(numStr
,expre
,startI
+2,i
-startI
-3);
//数字片段进行计算
sinValue
=sin(com(numStr
));
gcvt(sinValue
, i
-startI
-2, numChar
);
//截取expre的第三个片段
stringCopy(str2
,expre
,i
,strlen(expre
)-3);
//三个片段重新拼接成expre
strcat(str1
,numChar
);
strcat(str1
,str2
);
strcpy(expre
,str1
);
}
else if(expre
[i
]=='c')
{
startI
=i
;
//截取expre的第一个片段
stringCopy(str1
,expre
,0,startI
);
//读取sin函数中的表达式
i
=i
+2;
while(bracketsNum
!=0)
{
if(expre
[i
]=='(')
bracketsNum
++;
if(expre
[i
]==')')
bracketsNum
--;
i
++;
}
bracketsNum
=1;
//截取expre的第二个片段
stringCopy(numStr
,expre
,startI
+2,i
-startI
-3);
//数字片段进行计算
cosValue
=cos(com(numStr
));
gcvt(cosValue
, i
-startI
-2, numChar
);
//截取expre的第三个片段
stringCopy(str2
,expre
,i
,strlen(expre
)-3);
//三个片段重新拼接成expre
strcat(str1
,numChar
);
strcat(str1
,str2
);
strcpy(expre
,str1
);
}
else if(expre
[i
]=='t')
{
startI
=i
;
//截取expre的第一个片段
stringCopy(str1
,expre
,0,startI
);
//读取sin函数中的表达式
i
=i
+2;
while(bracketsNum
!=0)
{
if(expre
[i
]=='(')
bracketsNum
++;
if(expre
[i
]==')')
bracketsNum
--;
i
++;
}
bracketsNum
=1;
//截取expre的第二个片段
stringCopy(numStr
,expre
,startI
+2,i
-startI
-3);
//数字片段进行计算
tanValue
=tan(com(numStr
));
gcvt(tanValue
, i
-startI
-2, numChar
);
//截取expre的第三个片段
stringCopy(str2
,expre
,i
,strlen(expre
)-3);
//三个片段重新拼接成expre
strcat(str1
,numChar
);
strcat(str1
,str2
);
strcpy(expre
,str1
);
}
else if(expre
[i
]=='q')
{
startI
=i
;
//截取expre的第一个片段
stringCopy(str1
,expre
,0,startI
);
//读取sin函数中的表达式
i
=i
+2;
while(bracketsNum
!=0)
{
if(expre
[i
]=='(')
bracketsNum
++;
if(expre
[i
]==')')
bracketsNum
--;
i
++;
}
bracketsNum
=1;
//截取expre的第二个片段
stringCopy(numStr
,expre
,startI
+2,i
-startI
-3);
//数字片段进行计算
qValue
=sqrt(com(numStr
));
gcvt(qValue
, i
-startI
-2, numChar
);
//截取expre的第三个片段
stringCopy(str2
,expre
,i
,strlen(expre
)-3);
//三个片段重新拼接成expre
strcat(str1
,numChar
);
strcat(str1
,str2
);
strcpy(expre
,str1
);
}
else if(expre
[i
]=='l')
{
startI
=i
;
//截取expre的第一个片段
stringCopy(str1
,expre
,0,startI
);
//读取sin函数中的表达式
i
=i
+2;
while(bracketsNum
!=0)
{
if(expre
[i
]=='(')
bracketsNum
++;
if(expre
[i
]==')')
bracketsNum
--;
i
++;
}
bracketsNum
=1;
//截取expre的第二个片段
stringCopy(numStr
,expre
,startI
+2,i
-startI
-3);
//数字片段进行计算
logValue
=log10(com(numStr
));
gcvt(logValue
, i
-startI
-2, numChar
);
//截取expre的第三个片段
stringCopy(str2
,expre
,i
,strlen(expre
)-3);
//三个片段重新拼接成expre
strcat(str1
,numChar
);
strcat(str1
,str2
);
strcpy(expre
,str1
);
}
else if(expre
[i
]=='e')
{
startI
=i
;
//截取expre的第一个片段
stringCopy(str1
,expre
,0,startI
);
//读取sin函数中的表达式
i
=i
+2;
while(bracketsNum
!=0)
{
if(expre
[i
]=='(')
bracketsNum
++;
if(expre
[i
]==')')
bracketsNum
--;
i
++;
}
bracketsNum
=1;
//截取expre的第二个片段
stringCopy(numStr
,expre
,startI
+2,i
-startI
-3);
//数字片段进行计算
lnValue
=log(com(numStr
));
gcvt(lnValue
, i
-startI
-2, numChar
);
//截取expre的第三个片段
stringCopy(str2
,expre
,i
,strlen(expre
)-3);
//三个片段重新拼接成expre
strcat(str1
,numChar
);
strcat(str1
,str2
);
strcpy(expre
,str1
);
}
else if(expre
[i
]=='x')
{
startI
=i
;
//截取expre的第一个片段
stringCopy(str1
,expre
,0,startI
);
//读取sin函数中的表达式
i
=i
+2;
while(bracketsNum
!=0)
{
if(expre
[i
]=='(')
bracketsNum
++;
if(expre
[i
]==')')
bracketsNum
--;
i
++;
}
bracketsNum
=1;
//截取expre的第二个片段
stringCopy(numStr
,expre
,startI
+2,i
-startI
-3);
//数字片段进行计算
arcsinValue
=asin(com(numStr
));
gcvt(arcsinValue
, i
-startI
-2, numChar
);
//截取expre的第三个片段
stringCopy(str2
,expre
,i
,strlen(expre
)-3);
//三个片段重新拼接成expre
strcat(str1
,numChar
);
strcat(str1
,str2
);
strcpy(expre
,str1
);
}
else if(expre
[i
]=='d')
{
startI
=i
;
//截取expre的第一个片段
stringCopy(str1
,expre
,0,startI
);
//读取sin函数中的表达式
i
=i
+2;
while(bracketsNum
!=0)
{
if(expre
[i
]=='(')
bracketsNum
++;
if(expre
[i
]==')')
bracketsNum
--;
i
++;
}
bracketsNum
=1;
//截取expre的第二个片段
stringCopy(numStr
,expre
,startI
+2,i
-startI
-3);
//数字片段进行计算
arccosValue
=acos(com(numStr
));
gcvt(arccosValue
, i
-startI
-2, numChar
);
//截取expre的第三个片段
stringCopy(str2
,expre
,i
,strlen(expre
)-3);
//三个片段重新拼接成expre
strcat(str1
,numChar
);
strcat(str1
,str2
);
strcpy(expre
,str1
);
}
else if(expre
[i
]=='g')
{
startI
=i
;
//截取expre的第一个片段
stringCopy(str1
,expre
,0,startI
);
//读取sin函数中的表达式
i
=i
+2;
while(bracketsNum
!=0)
{
if(expre
[i
]=='(')
bracketsNum
++;
if(expre
[i
]==')')
bracketsNum
--;
i
++;
}
bracketsNum
=1;
//截取expre的第二个片段
stringCopy(numStr
,expre
,startI
+2,i
-startI
-3);
//数字片段进行计算
arctanValue
=atan(com(numStr
));
gcvt(arctanValue
, i
-startI
-2, numChar
);
//截取expre的第三个片段
stringCopy(str2
,expre
,i
,strlen(expre
)-3);
//三个片段重新拼接成expre
strcat(str1
,numChar
);
strcat(str1
,str2
);
strcpy(expre
,str1
);
}
else
{
i
++;
}
}
setxy(0,20);
printf("%f\n",com(expre
));
pause
=getchar();
clearOut();
setxy(0,0);
clearIn();
}
system("pause");
}
欢迎你加群与我一起交流:813269919