三⾓函数算式的c语⾔表达式,能计算加减乘除表达式,求添加计算平⽅、三⾓函数的功能,...
能计算加减乘除表达式,求添加计算平⽅、三⾓函数的功能,在线等,急
各位C语⾔⼤神,求帮忙,现在代码能计算加减乘除表达式,但是不能计算平⽅和三⾓函数,求⼤神添加功能。
在线等~~~
// EX6_08.CPP
// A program to implement a calculator
#include // For input/output
#include // For the exit() function
#include // For the isdigit() function
#include // For the strcpy() function
void eatspaces(char * str);// Function to eliminate blanks
double expr(char * str);// Function evaluating an expression
double term(char * str, int * pindex); // Function analyzing a term
double number(char * str, int * pindex); // Function to recognize a number
char * extract(char * str, int * index); // Function to extract a substring
const int MAX = 80;// Maximum expression length including '\0'
int main(void)
{
char buffer[MAX];// Input area for expression to be evaluated
char c;
int i;
printf("Welcome to your friendly calculator.\n");
printf("Enter an expression, or an empty line to quit.\n");
for(;;)
{
i=0;
scanf("%c",&c); // Read an input line
while(c!='\n')
{
buffer[i++]=c;
scanf("%c",&c);
}
buffer[i]='\0';
eatspaces(buffer);// Remove blanks from input
if(!buffer[0])// Empty line ends calculator
return 0;
printf( "\t= %f\n\n",expr(buffer));// Output value of expression
}
}
// Function to eliminate blanks from a string
void eatspaces(char * str)
{
int i=0; // 'Copy to' index to string
int j=0; // 'Copy from' index to string
while((*(str+i) = *(str+j++)) != '\0') // Loop while character copied is not \0 if(*(str+i) != ' ') // Increment i as long as
i++; //character is not a blank
return;
}
// Function to evaluate an arithmetic expression
double expr(char * str)
{
double value = 0;// Store result here
int index = 0;// Keeps track of current character position
value = term(str, &index); // Get first term
for(;;)// Infinite loop, all exits inside
{
switch(*(str+index++)) // Choose action based on current character {
case '\0':// We're at the end of the string
return value;//so return what we have got
case '+':// + found so add in the
value += term(str, &index);//next term
break;
case '-':// - found so subtract
value -= term(str, &index);//the next term
break;
default: // If we reach here the string
printf("Arrrgh!*#!! There's an error.\n");
exit(1);
}
}
}
// Function to get the value of a term
double term(char * str, int * pindex)
{
double value = 0;// Somewhere to accumulate the result
value = number(str, pindex);// Get the first number in the term // Loop as long as we have a good operator
while((*(str+(*pindex))=='*')||(*(str+(*pindex))=='/'))
{
if(*(str+(*pindex))=='*')// If it's multiply,
{
++(*pindex);
value *= number(str, pindex);//multiply by next number
}
if(*(str+(*pindex))=='/')// If it's divide,
{
++(*pindex);
value /= number(str, pindex);//divide by next number
}
}
return value; // We've finished, so return what we've got
}
// Function to recognize a number in a string
double number(char * str, int * pindex)
{
double value = 0.0;// Store the resulting value
char * psubstr; // Pointer for substring
if(*(str + (*pindex)) == '(') // Start of parentheses
++(*pindex);
psubstr = extract(str, pindex); // Extract substring in brackets value = expr(psubstr); // Get the value of the substring return value; // Return substring value
}
while(isdigit(*(str+(*pindex))))// Loop accumulating leading digits value=10*value + (*(str+(*pindex)++) - 48);
// Not a digit when we get to here
if(*(str+(*pindex))!='.')// so check for decimal point
return value;// and if not, return value
double factor = 1.0;// Factor for decimal places
while(isdigit(*(str+(++(*pindex)))))// Loop as long as we have digits {
factor *= 0.1; // Decrease factor by factor of 10
value=value + (*(str+(*pindex))-48)*factor; // Add decimal place
}
return value; // On loop exit we are done
}
// Function to extract a substring between parentheses
// (requires string.h)
char * extract(char * str, int * pindex)
{
char buffer[MAX]; // Temporary space for substring
char * pstr = NULL; // Pointer to new string for return
int numL = 0; // Count of left parentheses found
int bufindex = *pindex; // Save starting value for index
do
{
buffer[(*pindex) - bufindex] = *(str + (*pindex));
switch(buffer[(*pindex) - bufindex])
{
case ')':
if(numL == 0)
buffer[(*pindex) - bufindex] = '\0'; // Replace ')' with '\0'
++(*pindex);
pstr = (char *) malloc((*pindex) - bufindex + 1);
if (!pstr)
{
printf("Memory allocation failed, program terminated.") ;
exit(1);
}
strcpy(pstr, buffer); // Copy substring to new memory
return pstr; // Return substring in new memory
}
else
numL--; // Reduce count of '(' to be matched
break;
case '(':
numL++; // Increase count of '(' to be // matched
break;
c++strcpy函数用法}
} while(*(str + (*pindex)++) != '\0'); // Loop - don't overrun end of string printf("Ran off the end of the expression, must be bad input.\n");
exit(1);
}
------解决思路----------------------
仅供参考:/*---------------------------------------
函数型计算器(VC++6.0,Win32 Console)程序由 yu_hua 于2007-07-27设计完成功能:
⽬前提供了10多个常⽤数学函数:
⑴正弦sin
⑵余弦cos
⑶正切tan
⑷开平⽅sqrt
⑸反正弦arcsin
⑹反余弦arccos
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论