能计算加减乘除表达式,求添加计算平方、三角函数的功能,在线等,急

杜小牙 杜小牙 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
各位C语言大神,求帮忙,现在代码能计算加减乘除表达式,但是不能计算平方和三角函数,求大神添加功能。
在线等~~~

// EX6_08.CPP
// A program to implement a calculator

#include <stdio.h> // For input/output
#include <stdlib.h> // For the exit() function
#include <ctype.h> // For the isdigit() function
#include <string.h> // 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;
}
} 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);
}

...全文

相关推荐

MFC简易计算器(含加减乘除,平方,立方,开方,三角函数)

有加减乘除,平方,立方,开方,三角函数。但这只是我的作业,仅仅只能看看哦,希望有所启发哦~~~

jingqiong

基于Java Swing的科学计算器与进制转换实战项目解析

在Java桌面应用开发中,Swing是经典且稳定的GUI工具包,它为初学者提供了理解事件驱动编程与界面布局的最佳实践入口。表达式求值作为计算器程序的核心,通常采用中缀转后缀(逆波兰式)算法,利用栈结构处理运算符优先级,这是计算机科学中基础且重要的思想。进制转换则基于短除法与按权展开原理,通过以十进制为桥接,可轻松实现二、八、十、十六进制间的任意互转。这类项目不仅覆盖面向对象设计、异常处理与集合框架等Java高频知识点,还能锻炼代码封装与模块划分能力。无论是课程设计、面试项目展示,还是日常开发中快速完成数据换

元宿six

【雕爷学编程】Arduino 手册之算术运算符赋值 =与取余 %

如果两个操作数的类型不同,会发生隐式类型转换,通常是将较低精度的类型转换为较高精度的类型,例如字符型转换为整型。如果结果的类型与被赋值的变量的类型不同,也会发生隐式类型转换,通常是将较高精度的类型转换为较低精度的类型,例如整型转换为字符型。Arduino算术运算符取余%是一种用于求两个整数相除后的余数的符号,它可以用来判断一个数是否能被另一个数整除,或者获取一个数的某一位或某几位。Arduino算术运算符赋值=是一种将等号右边的值或表达式存储到等号左边的变量中的操作,它可以用来初始化或修改变量的值。

驴友花雕

VC++实现的计算器,用MFC实现界面操作,实现加减乘除,平方,求根号

VC++实现计算器,用MFC实现界面操作,

xiaolongmei

计算器小程序 实现计算器加减乘除 开方 平方功能

用MFC做的计算器小程序,可以实现加减乘除 开方 平方功能

jxtaoran1234567
评论 7
收藏 已收藏
分享
返回顶部
杜小牙
C语言 70,039 成员 243,237 内容