设计一个C语言程序实现计算器功能包括加减乘除、小数、括号、取余

时间: 2023-12-01 17:03:57 浏览: 31
以下是一个简单的C语言程序实现计算器功能: ```c #include <stdio.h> #include <stdlib.h> #include <ctype.h> #define MAX_EXPR_LEN 100 // 定义运算符栈结构体 typedef struct { char data[MAX_EXPR_LEN]; int top; } OperatorStack; // 定义操作数栈结构体 typedef struct { double data[MAX_EXPR_LEN]; int top; } OperandStack; // 初始化运算符栈 void initOperatorStack(OperatorStack *stack) { stack->top = -1; } // 初始化操作数栈 void initOperandStack(OperandStack *stack) { stack->top = -1; } // 判断是否为数字 int isNumber(char c) { return isdigit(c) || c == '.'; } // 判断是否为运算符 int isOperator(char c) { return c == '+' || c == '-' || c == '*' || c == '/' || c == '%' || c == '(' || c == ')'; } // 获取运算符优先级 int getPriority(char c) { switch (c) { case '+': case '-': return 1; case '*': case '/': case '%': return 2; default: return 0; } } // 将中缀表达式转换为后缀表达式 void infixToPostfix(char *infixExpr, char *postfixExpr) { OperatorStack operatorStack; initOperatorStack(&operatorStack); int i = 0, j = 0; while (infixExpr[i]) { if (isNumber(infixExpr[i])) { while (isNumber(infixExpr[i])) { postfixExpr[j++] = infixExpr[i++]; } postfixExpr[j++] = ' '; } else if (isOperator(infixExpr[i])) { if (infixExpr[i] == '(') { operatorStack.data[++operatorStack.top] = infixExpr[i++]; } else if (infixExpr[i] == ')') { while (operatorStack.data[operatorStack.top] != '(') { postfixExpr[j++] = operatorStack.data[operatorStack.top--]; postfixExpr[j++] = ' '; } operatorStack.top--; i++; } else { while (operatorStack.top != -1 && operatorStack.data[operatorStack.top] != '(' && getPriority(operatorStack.data[operatorStack.top]) >= getPriority(infixExpr[i])) { postfixExpr[j++] = operatorStack.data[operatorStack.top--]; postfixExpr[j++] = ' '; } operatorStack.data[++operatorStack.top] = infixExpr[i++]; } } else { printf("Invalid character: %c\n", infixExpr[i]); exit(1); } } while (operatorStack.top != -1) { postfixExpr[j++] = operatorStack.data[operatorStack.top--]; postfixExpr[j++] = ' '; } postfixExpr[j] = '\0'; } // 计算后缀表达式 double calculatePostfix(char *postfixExpr) { OperandStack operandStack; initOperandStack(&operandStack); int i = 0; while (postfixExpr[i]) { if (isNumber(postfixExpr[i])) { char *start = postfixExpr + i; while (isNumber(postfixExpr[i])) { i++; } double num = atof(start); operandStack.data[++operandStack.top] = num; } else if (isOperator(postfixExpr[i])) { double b = operandStack.data[operandStack.top--]; double a = operandStack.data[operandStack.top--]; switch (postfixExpr[i]) { case '+': operandStack.data[++operandStack.top] = a + b; break; case '-': operandStack.data[++operandStack.top] = a - b; break; case '*': operandStack.data[++operandStack.top] = a * b; break; case '/': operandStack.data[++operandStack.top] = a / b; break; case '%': operandStack.data[++operandStack.top] = (int)a % (int)b; break; } i++; } else { printf("Invalid character: %c\n", postfixExpr[i]); exit(1); } } return operandStack.data[operandStack.top]; } int main() { char infixExpr[MAX_EXPR_LEN], postfixExpr[MAX_EXPR_LEN]; printf("Enter the infix expression: "); fgets(infixExpr, MAX_EXPR_LEN, stdin); infixToPostfix(infixExpr, postfixExpr); printf("The postfix expression is: %s\n", postfixExpr); printf("The result is: %lf\n", calculatePostfix(postfixExpr)); return 0; } ``` 使用方法:在终端中运行程序,输入中缀表达式,程序将自动计算并输出结果。例如,输入`(2.5+3)*4-5%2`,程序将输出: ``` Enter the infix expression: (2.5+3)*4-5%2 The postfix expression is: 2.5 3 + 4 * 5 2 % - The result is: 16.000000 ```

相关推荐

最新推荐

recommend-type

C语言程序设计实现区号查询系统C语言程序设计实现

用C语言编程的区号查询系统,要求实现区号查询系统中,添加新记录、删除记录、显示记录信息、按城市查找信息和退出系统等功能。 ①录入有关城市的名称和区号。 ②显示所有城市的信息。 ③通过输入城市名称查找对应...
recommend-type

C语言程序设计实现学生成绩记录簿

通过编写c语言《学生成绩记录簿系统...3)C语言程序设计的主要任务是要求学生遵循软件开发过程的基本规范,运用结构化程序设计的方法,按照课程设计的题目要求,分析、设计、编写、调试和测试C语言程序及编写设计报告。
recommend-type

C语言:一元多项式加减法运算(链表 附答案).docx

C语言链表的入门题,里面提供了两种思路供参考,用链表来实现一元多项式的加减法,并按照一定规律输出。也是练习链表和排序算法的一道小实验,初学链表的小伙伴可以参考参考噢
recommend-type

C语言程序设计+研究生复试+求职+面试题

包括C语言程序设计基本概念及各章节问题回答。可供研究生复试或相关专业岗位面试使用。如: 简述C语⾔采取了哪些措施提⾼执⾏效率; ⼆维数组在物理上以及逻辑上的数组维度理解; 隐式类型转换的四种情况; 结构体对...
recommend-type

C语言程序设计实现门禁控制系统

系统将ID卡技术、计算机控制技术与电子门锁有机结合,用ID卡替代钥匙,配合计算机实现智能化门禁控制和管理,有效的解决了传统门锁的使用繁琐和无法信息记录等不足,利用数据控制器采集的数据实现数字化管理可为内部...
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

解释minorization-maximization (MM) algorithm,并给出matlab代码编写的例子

Minorization-maximization (MM) algorithm是一种常用的优化算法,用于求解非凸问题或含有约束的优化问题。该算法的基本思想是通过构造一个凸下界函数来逼近原问题,然后通过求解凸下界函数的最优解来逼近原问题的最优解。具体步骤如下: 1. 初始化参数 $\theta_0$,设 $k=0$; 2. 构造一个凸下界函数 $Q(\theta|\theta_k)$,使其满足 $Q(\theta_k|\theta_k)=f(\theta_k)$; 3. 求解 $Q(\theta|\theta_k)$ 的最优值 $\theta_{k+1}=\arg\min_\theta Q(
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。