/*输入一个个位数的数学表达式,如6+3*(9-5)/2-8,并将运行程序后将结果显示在屏幕上。*/为什么输出不了结果,哪出错了?帮忙看看,谢谢啦
#include<stdio.h>
#include<malloc.h>
typedef char type;
typedef struct node
{
type *base;
type *top;
}sqstack;
void initstack(sqstack *s)
{
s->base=(type *)malloc(sizeof(type));
s->top=s->base;
}
type push(sqstack *s,type x)
{
if(s->base==s->top)
*s->base=x;
else *s->top=x;
s->top=(type *)malloc(sizeof(type));
return x;
}
type gettop(sqstack *s,type x)
{
if(s->base==s->top)
return 0;
else x=*(--s->top);
return x;
}
type pop(sqstack *s,type x)
{
if(s->base==s->top)return 0;
else x=*(--s->top);
return x;
}
int firstvalue(char ch)
{
int k;
switch(ch)
{
case '+':k=0;break;
case '-':k=1;break;
case '*':k=2;break;
case '/':k=3;break;
case '(':k=4;break;
case ')':k=5;break;
case '#':k=6;break;
}
return k;
}
type sign(char ch1,char ch2)
{
int i,j;
char a[7][7]={
{'>','>','<','<','<','>','>'},
{'>','>','<','<','<','>','>'},
{'>','>','>','>','<','>','>'},
{'>','>','>','>','<','>','>'},
{'<','<','<','<','<','=',' '},
{'>','>','>','>',' ','>','>'},
{'<','<','<','<','<',' ','='}
};
i=firstvalue(ch1);
j=firstvalue(ch2);//调用函数两次,返回两个函数值
return a;
}
type count(char a,char b,char c)
{
int l;
switch(c)
{
case '+':l=(a-'0')+(b-'0');break;
case '-':l=(a-'0')-(b-'0');break;
case '*':l=(a-'0')*(b-'0');break;
case '/':l=(a-'0')/(b-'0');break;
}
return(l+'0');
}
void main()//主函数
{
char m,n;
char c,a,d,b,e='z';
sqstack s1,s2;
initstack(&s1);push(&s1,'#');
initstack(&s2);
printf("请输入表达式,以#结尾\n");
c=getchar();
while(c!='#'||gettop(&s1,e)!='#')
{
if(c<='9'&&c>='0')
{
push(&s2,c);
c=getchar();
}
else
switch(sign(gettop(&s1,e),c))
{
case'<':push(&s1,c);c=getchar();break;
case'=':pop(&s1,e);c=getchar();break;
case'>':d=pop(&s1,e);a=pop(&s2,e);b=pop(&s2,e);n=count(b,a,d);push(&s2,n);break;
}
}
m=gettop(&s2,e);
printf("结果为:%d\n",m-'0');
} c sizeof
[解决办法]
int firstvalue(char ch)
{
int k = 0;
switch(ch)
{
case '+':k=0;break;
case '-':k=1;break;
case '*':k=2;break;
case '/':k=3;break;
case '(':k=4;break;
case ')':k=5;break;
case '#':k=6;break;
default : printf("error");
}
return k;
}
type count(char a,char b,char c)
{
int l=0;
switch(c)
{
case '+':l=(a-'0')+(b-'0');break;
case '-':l=(a-'0')-(b-'0');break;
case '*':l=(a-'0')*(b-'0');break;
case '/':l=(a-'0')/(b-'0');break;
default : printf("error");
}
return(l+'0');
}
/* 在BC31下编译 或VC6.0*/
/* compile under Borland C++ 3.1 or Visual C++ 6.0*/
/*#include "stdafx.h"*/
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "conio.h"
#define TRUE 1
#define FALSE 0
#define STACK_INIT_SIZE 100/*存储空间初始分配量*/
#define STACKINCREMENT 20/*存储空间分配增量*/
typedef struct
{
int *pBase;/*在构造之前和销毁之后,base的值为NULL*/
int *pTop;/*栈顶指针*/
int StackSize;/*当前已分配的存储空间,以元素为单位*/
}Stack;
typedef int BOOLEAN;
char Operator[8]="+-*/()#";/*合法的操作符存储在字符串中*/
char Optr;/*操作符*/
int Opnd=-1;/*操作符*/
int Result;/*操作结果*/
/*算符间的优先关系*/
char PriorityTable[7][7]=
{
{'>','>','<','<','<','>','>'},
{'>','>','<','<','<','>','>'},
{'>','>','>','>','<','>','>'},
{'>','>','>','>','<','>','>'},
{'<','<','<','<','<','=','o'},
{'>','>','>','>','o','>','>'},
{'<','<','<','<','<','o','='},
};
//数据对象的操作方法
//构造一个空栈,如果返回值为0,则表示初始化失败
Stack InitStack()/*这是个效率低的方法*/
{
Stack S;
S.pBase=(int*)malloc(STACK_INIT_SIZE*sizeof(int));
if(!S.pBase)
{/*内存分配失败*/
printf("内存分配失败,程序中止运行\n");
exit(-1);
}
else
{
S.pTop=S.pBase;
S.StackSize=STACK_INIT_SIZE;
}
return S;
}
//销毁栈S,S不再存在
void DestoryStack(Stack *S)
{
if(S->pBase)
{
free(S->pBase);
S->pTop=S->pBase=NULL;
}
}
//若栈不空,则用e返回S的栈顶元素
//注:由于应用的特殊,可以不检查栈是否为空
int GetTop(Stack S)
{
return *(S.pTop-1);
}
//插入元素e为新的栈顶元素,如果成功则返回1,否则返回0
int Push(Stack *S,int e)
{
if(S->pTop-S->pBase==S->StackSize)
{//栈满,追加存储空间
S->pBase=(int*)realloc(S->pBase,S->StackSize+STACKINCREMENT*sizeof(int));
if(!S->pBase)
return 0;//存储分配失败
S->pTop=S->pBase+S->StackSize;
S->StackSize+=STACKINCREMENT;
}
*(S->pTop++)=e;
return 1;
}
int Pop(Stack *S,int *e)
{//若栈不空,则删除S的栈顶元素,用e 返回其值,并返回1;否则返回0
if(S->pTop==S->pBase)
return 0;
*e=*--(S->pTop);
return 1;
}
//主函数及其它函数的实现
//比较两个数学符号operator_1,operator_2的计算优先权,在算符优先关系表中查找相应的关系并返回'<','=',或'>'
char CheckPriority(char operator_1,char operator_2)
{
int i,j;//用来查询算符间优先关系表的下标
//char *ptr;
i=strchr(Operator,operator_1)-Operator;//找到传入操作符在字符串Operators中的相对位置
j=strchr(Operator,operator_2)-Operator;
//返回算符优先关系表中相应值
return PriorityTable[i][j];
}
BOOLEAN IsOperator(char ch)
{//判断一个字符是否为打操作符
if(strchr(Operator,ch))
return TRUE;
else
return FALSE;
}
//从键盘获得输入
void GetInput(void)
{
char Buffer[20];//键盘输入缓冲区,用来处理输入多位数的情况
char ch;//存放键盘输入
int index;//存放Buffer的下标
index=0;
ch=getch();//从键盘读入一个字符
while(ch!=13&&!IsOperator(ch))
{//如果输入的字符是回车符或是操作符,循环结束
if(ch>='0'&&ch<='9')
{//将字符回显到屏幕
printf("%c",ch);
Buffer[index]=ch;
index++;
}
ch=getch();
}
if(ch==13)
Optr='#';//输入的表达式以回车符结束
else
{
Optr=ch;
printf("%c",ch);
}
if(index>0)
{
Buffer[index]='\0';
Opnd=atoi((Buffer));
}
else
Opnd=-1;//程序不支持输入负数,当Opnd为负数时,表示输入的字符为操作符
}
//计算形如a+b之类的表达式,theta为操作符,a,b为操作数
int Calc(int a,char theta,int b)
{
switch(theta)
{
case '+':
return a+b;
case '-':
return a-b;
case '*':
return a*b;
default:
if(b==0)//除数为零的情况
{
printf("除数不能为");
return 0;//返回0用以显示
}
else
return a/b;
}
}
/*表达式求值*/
BOOLEAN EvaluateExpression()
{
int temp;//临时变量
char theta;//存放操作符的变量
int itheta;//存放出栈的操作符的变量add by me
int a,b;//存放表达式运算时的中间值
int topOpnd;//栈顶操作数
char topOptr;//栈顶操作符
Stack OPTR=InitStack();//操作符栈
Stack OPND=InitStack();//操作数栈
if(!Push(&OPTR,'#'))//操作符栈中的第一个为#字符
return FALSE;
GetInput();//从键盘获得输入
while(Optr!='#'
[解决办法]
GetTop(OPTR)!='#')
{//如果Optr>=0,表示有操作数输入
if(Opnd>=0)Push(&OPND,Opnd);
switch(CheckPriority(GetTop(OPTR),Optr))
{
case '<'://栈顶元素优先权低
if(!Push(&OPTR,Optr))return FALSE;
GetInput();
break;
case '='://脱括号并接收键盘输入
Pop(&OPTR,&temp);GetInput();
break;
case '>'://退栈并将运算结果入栈
//先用itheta得到操作符在赋给theta
Pop(&OPTR,&itheta);
Pop(&OPND,&b);
Pop(&OPND,&a);
theta = (char)( itheta );
Push(&OPND,Calc(a,itheta,b));
Opnd=-1;
break;
}
}
//本算法中,当输入只有一个操作数然后就输入回车符时,
//OPND.pTop==OPND.pBase
//如果OPND.pTop==OPND.pBase并且Opnd<0,则说明用户
//未输入任何操作和操作符而直接输入[回车],程序直接
//退出运行
if(OPND.pTop==OPND.pBase&&Opnd<0)
{
printf("\n\n感谢使用!\n");
exit(1);
}
else if(OPND.pTop==OPND.pBase)
Result=Opnd;
else
{
Result=GetTop(OPND);
DestoryStack(&OPND);
DestoryStack(&OPTR);
}
return TRUE;
}
void Message(void)
{
printf("\n四则运算表达式求值演示\n");
printf("-------------------------------\n");
printf("使用方法:请从键盘上直接输入表达式,以回车键结束.如45*(12-2)[回车]\n");
printf("注0:不输入任何数而直接按[回车]键,将退出程序.\n");
printf("注1:本程序暂时不接受除数字键及四则运算符之外的任何其它键盘输入.\n");
printf("注2:本程序暂时只能处理正确的表达式,不支持输入负数.\n");
printf("-------------------------------\n\n");
}
void main(void)
{
int i;//用来一些说明性信息
Message();
for(i=1;;i++)
{
printf("表达式%d:",i);
if(EvaluateExpression())
printf("=%d\n",Result);
else
printf("计算中遇到错误\n");
}
}
[解决办法]
栈的实现有问题,基本的先搞好测试过没问题,再去写应用...
[解决办法]
仅供参考
/*---------------------------------------
函数型计算器(VC++6.0,Win32 Console)程序由 yu_hua 于2007-07-27设计完成
功能:
目前提供了10多个常用数学函数:
⑴正弦sin
⑵余弦cos
⑶正切tan
⑷开平方sqrt
⑸反正弦arcsin
⑹反余弦arccos
⑺反正切arctan
⑻常用对数lg
⑼自然对数ln
⑽e指数exp
⑾乘幂函数∧
用法:
如果要求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键入字母时不区分大小写,以方便使用。
----------------------------------------*/
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <stdio.h>
#include <string.h>
#include <windows.h>
using namespace std;
const char Tab=0x9;
const int DIGIT=1;
const int MAXLEN=16384;
char s[MAXLEN],*endss;
int pcs=15;
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;
}
return x;
}
double calc(char *expr,char **addr) {
static int deep; //递归深度
static char *fname[]={ "sin","cos","tan","sqrt","arcsin","arccos","arctan","lg","ln","exp",NULL};
double ST[10]={0.0}; //数字栈
char op[10]={'+'}; //运算符栈
char c,*rexp,*pp,*pf;
int ist=1,iop=1,last,i;
if (!deep) {
pp=pf=expr;
do {
c = *pp++;
if (c!=' '&& c!=Tab)
*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) {
cout<< "π左侧遇)" <<endl;exit(1);
}
ST[ist++]=3.14159265358979323846264338328;
ST[ist-1]=fun(ST[ist-1],op,&iop);
pp += 2;
last = DIGIT;
if (!strnicmp(pp,"Pi",2)) {
cout<< "两个π相连" <<endl;exit(2);
}
if (*pp=='(') {
cout<< "π右侧遇(" <<endl;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 {
cout<< "陌生函数名" <<endl;exit(4);
}
}
} else if (c=='+'
[解决办法]
c=='-'
[解决办法]
c=='*'
[解决办法]
c=='/'
[解决办法]
c=='^') {
char cc;
if (last != DIGIT) {
cout<< "运算符粘连" <<endl;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] = pow(ST[ist-1],ST[ist]);break;
}
} while (iop);
op[iop++] = c;
} else if (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] = pow(ST[ist-1],ST[ist]);break;
}
}
} else {
cc = op[iop-1];
if (cc=='^') {
cout<< "乘幂符连用" <<endl;exit(6);
}
op[iop++] = c;
}
last = !DIGIT;
} else {
if (last == DIGIT) {
cout<< "两数字粘连" <<endl;exit(7);
}
ST[ist++]=strtod(pp,&rexp);
ST[ist-1]=fun(ST[ist-1],op,&iop);
if (pp == rexp) {
cout<< "非法字符" <<endl;exit(8);
}
pp = rexp;
last = DIGIT;
if (*pp == '('
[解决办法]
isalpha(*pp)) {
op[iop++]='*';
last = !DIGIT;
c = op[--iop];
goto operate ;
}
}
}
*addr=pp;
if (iop>=ist) {
cout<< "表达式有误" <<endl;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] = pow(ST[ist-1],ST[ist]);break;
}
}
return ST[0];
}
int main(int argc,char **argv) {
if (argc<=1) {
if (GetConsoleOutputCP()!=936) system("chcp 936>NUL");//中文代码页
cout << "计算函数表达式的值。"<<endl<<"支持(),+,-,*,/,^,Pi,sin,cos,tan,sqrt,arcsin,arccos,arctan,lg,ln,exp"<<endl;
while (1) {
cout << "请输入表达式:";
gets(s);
if (s[0]==0) break;//
cout << s <<"=";
cout << setprecision(15) << calc(s,&endss) << endl;
}
} else if (argc==2 && 0==strcmp(argv[1],"/?")) {
if (GetConsoleOutputCP()!=936) system("chcp 936>NUL");//中文代码页
cout << "计算由≥1个命令行参数给出的函数表达式的值。最后一个参数是.0~.15表示将计算结果保留小数0~15位"<<endl<<"支持(),+,-,*,/,^^,Pi,sin,cos,tan,sqrt,arcsin,arccos,arctan,lg,ln,exp"<<endl;
} else {
strncpy(s,argv[1],MAXLEN-1);s[MAXLEN-1]=0;
int a;
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位
printf("%.*lf\n",pcs,calc(s,&endss));
} else {
strncat(s,argv[a],MAXLEN-1);
printf("%.15lg\n",calc(s,&endss));
}
}
return 0;
}