我正在尝试添加具有sin,pow和exp函数功能的能力到我的计算器。 我正在进行分段错误"当我尝试编译时。 我认为问题出在< void trigFunction(char s [])'功能
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#define MAXOP 100
#define NUMBER '0'
#define TRIG 1 /**/
#define TRUE 1
#define FALSE 0
int getop(char []);
void push(double);
double pop(void);
void swap(void);
void trigFunction(char s[]);
main()
{
int type;
double op2;
char s[MAXOP];
printf("\nEnter numbers below in the following format\nwith a space between each:\n");
printf("\tnum1 num2 operator\n");
printf("You may use the operators:\n");
printf("plus(+), minus(!), divide(/), multiply(*),\npercentage(%), swap(~), sine(S), exp(E), and pow(P)\n");
while((type = getop(s)) != EOF)
{
switch(type)
{
/*other cases I have taken out to make code shorter*/
case TRIG: /*case for sin, exp, pow functions*/
trigFunction(s);
break;
case '\n':
printf("\t%.8g\n", pop());/*prints out soultion*/
break;
default:
printf("error: unknown command %s\n", s);
break;
}
}
return 0;
}
void trigFunction(char s[])
{
double op2;
if(0 == strcmp(s, 'S'))/*strcmp() function compares the string pointed to by s1 to the string pointed to by s2.*/
push(sin(pop()));/*pushes the sine of pop() onto the stack*/
else if(0 == strcmp(s, "E"))
push(exp(pop()));
else if(0 == strcmp(s, "P"))
{
op2 = pop();
push(pow(pop(), op2));
}
else
printf("I don't know what you mean by %s, but I may \nself destruct if you don't enter \na correct operator\n", s);
}
#define MAXVAL 100
int sp = 0;
double val[MAXVAL];
void push (double f)
{
if(sp < MAXVAL)
{
val[sp++] = f;
}
else
{
printf("error: stack full, cant push %g\n",f);
}
}
double pop(void)
{
if(sp > 0)
{
return val[--sp];
}
else
{
printf("error: stack empty \n");
return 0.0;
}
}
int getch(void);
void ungetch(int);
int getop(char s[])
{
int i = 0, c, next;
while ((s[0] = c = getch()) == ' ' || c == '\t')/*skips white space*/
;
s[1] = '\0';
if(isalpha(c))
{
i = 0;
while(isalpha(s[i++] = c ))
c = getch();
s[i - 1] = '\0';
if(c != EOF)
/*ungetch(c);*/
return TRIG;
}
if(!isdigit(c) && c != '.' && c != '-')/* not number. may contain minus*/
return c;
if(c == '-')
{
next = getch();
if(!isdigit(next) && next != '.')
{
ungetch(next);/*puts next char in buffer*/
return c;
}
c = next;
}
else
c = getch();
while(isdigit(s[++i] = c))
c = getch();
if(c == '.') /* Collect fraction part */
while(isdigit(s[++i] = c = getch()))
;
s[i] = '\0';
if(c != EOF)
ungetch(c);/*puts c in buffer*/
return NUMBER;
}
#define BUFSIZE 100
char buf[BUFSIZE];
int bufp = 0;
int getch(void)
{
return(bufp > 0) ? buf[--bufp] : getchar();
}
void ungetch(int c)
{
if(bufp >= BUFSIZE)
{
printf("unGetch: too many characters\n");
}
else
{
buf[bufp++] = c;
}
}
答案 0 :(得分:0)
void trigFunction(char s[])
{
double op2;
if(0 == strcmp(s, 'S'))/*strcmp() function compares the string pointed to by s1 to the string pointed to by s2.*/
push(sin(pop()));/*pushes the sine of pop() onto the stack*/
else if(0 == strcmp(s, "E"))
push(exp(pop()));
else if(0 == strcmp(s, "P"))
{
op2 = pop();
push(pow(pop(), op2));
}
else
printf("I don't know what you mean by %s, but I may \nself destruct if you don't enter \na correct operator\n", s);
}
好吧,我们来看看:
if(0 == strcmp(s, 'S'))
应该是
if(0 == strcmp(s, "S"))
当您将单个char
传递给期望char *
的函数时...
这应该可以修复您的代码。
你可以进一步简化它。在所有情况下,你只看第一个角色,所以你可以这样做:
if (s[0] = 'S')
并将char
与char
进行比较,但如果您想稍后展开,则无法提供帮助,例如将as
用于arcsine
。
旁注:
有趣的是知道主要的三个trig函数是 sin
,exp
和pow
。 我多年来一直在做错 sin
,cos
和tan
。
(我认为它们只是占位符)