简单计算器可视化程序编程

合集下载

VB简易计算器代码

VB简易计算器代码

VB简易计算器代码以下是一个简单的VB计算器代码:```Public Class Form1Dim firstNum As Double ' 第一个数字Dim secondNum As Double ' 第二个数字Dim operation As Integer ' 1-加法,2-减法,3-乘法,4-除法Private Sub Button0_Click(sender As Object, e As EventArgs) Handles Button0.ClickTextBoxResult.Text = TextBoxResult.Text & "0"End SubPrivate Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.ClickTextBoxResult.Text = TextBoxResult.Text & "1"End SubPrivate Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.ClickTextBoxResult.Text = TextBoxResult.Text & "2"End SubHandles Button3.ClickTextBoxResult.Text = TextBoxResult.Text & "3"End SubPrivate Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.ClickTextBoxResult.Text = TextBoxResult.Text & "4"End SubPrivate Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.ClickTextBoxResult.Text = TextBoxResult.Text & "5"End SubPrivate Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.ClickTextBoxResult.Text = TextBoxResult.Text & "6"End SubPrivate Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.ClickTextBoxResult.Text = TextBoxResult.Text & "7"End SubHandles Button8.ClickTextBoxResult.Text = TextBoxResult.Text & "8"End SubPrivate Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.ClickTextBoxResult.Text = TextBoxResult.Text & "9"End SubPrivate Sub ButtonDot_Click(sender As Object, e As EventArgs) Handles ButtonDot.ClickIf Not TextBoxResult.Text.Contains(".") ThenTextBoxResult.Text = TextBoxResult.Text & "."End IfEnd SubPrivate Sub ButtonAdd_Click(sender As Object, e As EventArgs) Handles ButtonAdd.ClickfirstNum = Double.Parse(TextBoxResult.Text)TextBoxResult.Text = ""operation = 1End SubPrivate Sub ButtonSubtract_Click(sender As Object, e As EventArgs) Handles ButtonSubtract.ClickfirstNum = Double.Parse(TextBoxResult.Text)TextBoxResult.Text = ""operation = 2End SubPrivate Sub ButtonMultiply_Click(sender As Object, e As EventArgs) Handles ButtonMultiply.ClickfirstNum = Double.Parse(TextBoxResult.Text)TextBoxResult.Text = ""operation = 3End SubPrivate Sub ButtonDivide_Click(sender As Object, e As EventArgs) Handles ButtonDivide.ClickfirstNum = Double.Parse(TextBoxResult.Text)TextBoxResult.Text = ""operation = 4End SubPrivate Sub ButtonClear_Click(sender As Object, e As EventArgs) Handles ButtonClear.ClickTextBoxResult.Text = ""End SubPrivate Sub ButtonEquals_Click(sender As Object, e As EventArgs) Handles ButtonEquals.ClickDim result As DoublesecondNum = Double.Parse(TextBoxResult.Text)Select Case operationCase 1result = firstNum + secondNumCase 2result = firstNum - secondNumCase 3result = firstNum * secondNumCase 4result = firstNum / secondNumEnd SelectTextBoxResult.Text = result.ToStringEnd SubEnd Class```这个计算器包括数字按钮0-9、小数点按钮、加法、减法、乘法、除法和等于按钮。

java swing实现简易计算器

java swing实现简易计算器

简易计算器【案例介绍】1.案例描述本案例要求利用Java Swing 图形组件开发一个可以进行简单的四则运算的图形化计算器。

2.运行结果运行结果【案例目标】●学会分析“简易计算器”程序实现的逻辑思路。

●能够独立完成“简易计算器”程序的源代码编写、编译及运行。

●掌握Java Swing界面编程的应用。

●了解计算器逻辑运算实现。

【案例分析】要制作一个计算器,首先要知道它由哪些部分组成,如下图所示:一个简单的图形化界面计算器,需要由界面组件、组件的时间监听器和具体的事件处理逻辑组成。

实现一个简易图形化界面计算器可分以下几个步骤:1.UI组件创建和初始化:包括窗体的创建,显示计算结果文本框、清除按钮、数字按钮、运算符等按钮的初始化。

2.在窗体中添加UI组件:包括放置数字键及运算符面板、放置清除框面板。

3.布局结束之后,就是计算器的难点:编写事件处理程序。

4.按键的响应实现。

5.计算逻辑的实现。

6.注册监听器【案例实现】Calculator.java1 package chapter1102;2 import java.awt.*;3 import java.awt.event.*;4 import javax.swing.*;5 import java.util.Vector;6 import java.math.BigDecimal;7 public class Calculator {8 // 操作数1,为了程序的安全,初值一定设置,这里我们设置为0。

9 String str1 = "0";10 // 操作数211 String str2 = "0";12 // 运算符13 String signal = "+";14 // 运算结果15 String result = "";16 // 以下k1至k5为状态开关17 // 开关1用于选择输入方向,将要写入str1或str218 // 为 1 时写入 str1,为 2 时写入 str219 int k1 = 1;20 // 开关 2 用于记录符号键的次数21 // 如果 k2>1 说明进行的是 2+3-9+8 这样的多符号运算22 int k2 = 1;23 // 开关3用于标识 str1 是否可以被清 024 // 等于 1 时可以,不等于1时不能被清025 int k3 = 1;26 // 开关4用于标识 str2 是否可以被清 027 // 等于 1 时可以,不等于1时不能被清028 int k4 = 1;29 // 开关5用于控制小数点可否被录入30 // 等于1时可以,不为1时,输入的小数点被丢掉31 int k5 = 1;32 // store的作用类似于寄存器,用于记录是否连续按下符号键33 JButton store;34 //vt 存储之前输入的运算符。

简易计算器代码

简易计算器代码
connect(button[9],SIGNAL(clicked()),this,SLOT(setOper()));
button[10] = new QPushButton(buttontext[10],this,buttontext[10]); // “—”
button[10]->setFixedSize(BUTTONWIDTH,BUTTONHEIGHT);
edit->setText(tr("%1").arg(0)); //这个函数可获取文本编辑框中光标的位置,并显示在状态栏中
//setText(tr(“%1行%2列”).arg(rowNum).arg(colNum));
initialize(); //调用初始化函数
}
void CWidget::initialize()
cwidget.cpp
#include "cwidget.h"
#define KEY_CLR "CLR"
#define KEY_ADD "+"
#define KEY_SUB "-"
#define KEY_MUL "*"
#define KEY_DIV "/"
#define KEY_EQ "="
#define KEY_0 "0"
{
delete edit;
delete *button;
delete mainLayout;
delete topLayout;
delete bottomLayout;
}
void CWidget::calculate() //响应按键“=”,调用计算功能

简单计算器可视化程序编程

简单计算器可视化程序编程

简单计算器一、基本功能描绘二进制、八进制、十进制及十六进制数的加、减、乘、除、乘方、取模等简单计算科学计算函数,包括( 反 ) 正弦、 ( 反) 余弦、 ( 反 ) 正切、 ( 反 ) 余切、开方、指数等函数运行以角度、弧度两种方式实现上述部分函数具备历史计算的记忆功能对不正确的表达式能指出其错误原因二、流程图输入数据子函数编写框关系变量m_num=0键入一个数N可否为正数?Ym_num m_num=10*m_num+i=10*m_num-i可否为小数?Y复原 m_numN依照小数点后位数将键入的数除以 n 次 10 累加到 m_numY连续键入数字?N用 UpdateData(FALSE) 刷新显示图 1 输入数据子函数流程图图 2三、设计步骤翻开 Microsoft Visual C++ 6.0 , 在文件中点击新建,在弹出框内选择MFC AppWizard[exe] 工程,输入工程名Calculter及其所在位置,点击确定,如图3所示。

将弹出这样,图 3 新建 MFC AppWizard[exe] 工程MFC AppWizard-step 1对话框,选择基本对话框,点击达成,如图图 4 基本对话框MFC AppWizard 就成立了一个鉴于对话窗口的程序框架,如图图 5 新建的对话框4 所示。

5 所示。

四、界面设计1、创立控件在图 5 所示的 Resoure View 选项卡中翻开Dialog 资源组,双击IDD_CALCULATOR_DIALOG,在右边的窗口中显示出待编写的对话框。

开始摆放控件,包括编写框和按钮的创立。

按钮的创立1)在图 5 中 Controls 的“编写框”按钮上单击鼠标左键,在对话框编写窗口上合适的地点按下鼠标左键并拖动鼠标画出一个大小合适的编写框。

在编写框上单击鼠标右键,在弹出的快捷莱单中选择属性选项,此时弹出Edit 属性对话框,如图 6 所示,在该对话框中输入ID 属性。

简易计算器小程序使用微信小程序开发工具和JavaScript开发

简易计算器小程序使用微信小程序开发工具和JavaScript开发

简易计算器小程序使用微信小程序开发工具和JavaScript开发微信小程序是一种基于微信平台的轻量级应用程序,可以在微信中使用。

它具有开发简便、使用方便、功能丰富等特点,在各行各业都得到了广泛的应用。

本文将介绍如何使用微信小程序开发工具和JavaScript开发一个简易计算器小程序。

一、准备工作在开始开发之前,我们需要准备一些基本的工具和环境。

首先,下载并安装微信小程序开发工具,该工具提供了一个可视化的界面,方便我们进行小程序的开发和调试。

其次,我们需要熟悉JavaScript语言的基本语法和特性,因为小程序的逻辑部分主要使用JavaScript来实现。

二、创建小程序项目打开微信小程序开发工具,点击新建项目,填写项目名称、项目目录和AppID等信息。

点击确定后,工具会自动生成一个基本的小程序项目结构。

三、编写界面在小程序项目的根目录下,找到和页面相关的文件夹。

新建一个名为"calculator"的文件夹,用于存放计算器页面的相关文件。

在该文件夹下新建三个文件:calculator.wxml、calculator.wxss、calculator.js,分别用于编写页面的结构、样式和逻辑。

在calculator.wxml文件中,我们可以使用一些基本的标签来构建计算器的界面,例如view、text、button等。

使用这些标签,我们可以创建一个包含数字键盘和运算符键的界面。

在calculator.wxss文件中,我们可以设置各个标签的样式,使界面看起来更加美观。

可以设置按钮的背景色、边框样式等。

四、编写逻辑在calculator.js文件中,我们需要编写计算器的逻辑代码。

首先,我们需要定义一些变量来保存计算器的状态。

比如,一个变量用于保存用户输入的表达式,一个变量用于保存计算结果等。

然后,我们可以通过监听用户的点击事件来获取用户输入的数字和运算符,并更新表达式变量的值。

接下来,我们需要编写一个计算函数,用于根据用户输入的表达式计算结果。

matlab-GUI-设计(简易科学计算器)

matlab-GUI-设计(简易科学计算器)

MATLAB大作业班级:姓名:学号:计算器➢题目本题目通过MATLAB的gui程序设计较为简单,在gui设计中主要用到三种控件,文本编辑框(edit text),静态文本框(Static text),命令按钮(push button)。

然后在通过各个按钮的回调函数,实现简单的计算功能。

➢1、功能介绍(1)具有友好的用户图形界面。

实现十进制数的加、减、乘、除、乘方、开方等简单计算。

(2)具有科学计算函数,包括(反)正弦、(反)余弦、(反)正切、(反)余切、开方、指数等函数运行。

(注:三角函数计算的是弧度而不是角度)。

(3)有清除键,能清除操作。

➢2、功能实现程序由两个部分组成:MATLAB代码(.m文件)和GUI图形(.fig)。

程序使用的流程:直接利用图形界面中的按键键入所需数值、运算符等即可得出结果。

备注:软件版本:MATLAB 2011b首先用MATLAB GUI功能,在绘制一个静态文本框和一个文本编辑框,以及33个命令按钮,调整好各控件大小、颜色,整体布局如图所示:(附录中有相关属性修改介绍)然后通过双击各个按钮来改写其属性,在m文件中编写其回调函数,最后在运行调试。

2.1 各功能界面设计GUI设计界面:注:底部边框用(Panel)工具添加,有两种设计顺序。

(1、先加底部边框,再在底部边框上画功能键。

2、先画功能键,布好局,画底框,全选功能键拖动到底框上。

)2.2 各功能模块实现(可根据需要增减功能键)算法设计:1. 数字键设计:0—9以及小数点函数都一样,只是参数不同:例如:按键‘1’响应:global jjtextString = get(handles.text1,'String');if(strcmp(textString,'0.')==1)&(jj==0)set(handles.text1,'String','1') ;elsetextString =strcat(textString,'1');set(handles.text1,'String',textString)endjj=0;2. 四则运算函数:‘+’功能响应:textString = get(handles.text1,'String');textString =strcat(textString,'+');set(handles.text1,'String',textString)‘-’功能响应:textString = get(handles.text1,'String');textString =strcat(textString,'-');set(handles.text1,'String',textString)‘×’功能响应:textString = get(handles.text1,'String');textString =strcat(textString,'*');set(handles.text1,'String',textString)‘÷’功能响应:textString = get(handles.text1,'String');textString =strcat(textString,'/');set(handles.text1,'String',textString)3. 科学计算函数:例如:‘sin’功能响应:textString = get(handles.text1,'String');if(strcmp(textString,'0.')==1)set(handles.text1,'String','0.') ;elsea = strread(textString, '%f');a=sin(a);set(handles.text1,'String',a)end4. 退格键(DEL):通过取屏幕值,计算出其字符长度,然后取其前N-1项的值来实现退格:global jjtextString = get(handles.text1,'String');if(strcmp(textString,'0.')==1)&(jj==0)set(handles.text1,'String','0.') ;elsess=char(textString);l=length(textString);textString=ss(1:l-1);set(handles.text1,'String',textString)endjj=0;5. 清屏键函数(AC):set(handles.text1,'String','0.') ;2.3 各模块程序添加方法选中一个需添加程序的功能键,右击,View Callbacks,Callback,出现如下图所示界面。

可视化计算器c语言代码

可视化计算器c语言代码

可视化计算器c语言代码好的,这是一个简易计算器的可视化界面的示例代码,使用`Graphics.h`库实现:```c#include <graphics.h>#include <conio.h>#include <stdio.h>int main() {int gd = DETECT, gm;initgraph(&gd, &gm, "");int x, y, a, b;char c;// 设置文本和背景颜色为白色setcolor(WHITE);// 设置文本方向为水平方向settextstyle(DEFAULT_FONT, HORIZ_DIR, 2);// 在屏幕上输出提示信息和输入框outtextxy(100, 100, "Enter the first number: ");scanf("%d", &a);outtextxy(100, 150, "Enter the second number: ");scanf("%d", &b);setcolor(YELLOW);outtextxy(100, 200, "Choose an operation (+, -, *, /): ");c = getch();// 根据用户选择的操作计算结果switch (c) {case '+':x = a + b;break;case '-':x = a - b;break;case '*':x = a * b;break;case '/':x = a / b;break;default:printf("Invalid operation.\n");return 0;}// 设置文本颜色为绿色setcolor(GREEN);outtextxy(100, 250, "The result is: ");printf("%d\n", x);getch();// 关闭图形界面closegraph();return 0;}```上述代码使用`initgraph()`函数初始化图形界面,在屏幕上显示了几个提示信息和输入框,然后根据用户选择的操作计算结果并输出。

可视化编程应用VisualBasic6.0课程设计

可视化编程应用VisualBasic6.0课程设计

可视化编程应用VisualBasic6.0课程设计简介可视化编程是一种使用图形化界面设计程序的方式,它可以帮助程序员更快速、更直观地完成程序开发。

VisualBasic6.0是一款经典的可视化编程工具,它可以让开发者使用图形化界面完成各种小型应用程序的设计与开发。

本文将介绍如何利用VisualBasic6.0进行可视化编程的课程设计。

设计思路本次课程设计的主题是设计一个简单的计算器程序,要求用户可以输入两个数值,然后选择加、减、乘、除四种运算方式,最终输出计算结果。

为了实现这个程序,我们需要在VisualBasic6.0的窗体设计器中进行一些界面设计和代码编写。

界面设计首先,我们需要在VisualBasic6.0中创建一个新的窗体,然后在窗体上添加各种控件,包括文本框、标签、按钮等。

具体地,我们需要添加两个文本框用于输入数值,四个按钮用于进行四种运算操作,一个标签用于显示计算结果calculatorcalculator代码编写接下来,我们需要编写VisualBasic6.0代码,实现上述界面的实际功能。

具体地,我们需要在按钮的点击事件中添加代码,实现对应的加、减、乘、除四种运算操作。

代码如下:Private Sub cmdAdd_Click()' 加法运算Dim num1 As IntegerDim num2 As Integernum1 = Val(txtNum1.Text)num2 = Val(txtNum2.Text)lblResult.Caption = num1 + num2 End SubPrivate Sub cmdSub_Click()' 减法运算Dim num1 As IntegerDim num2 As Integernum1 = Val(txtNum1.Text)num2 = Val(txtNum2.Text)lblResult.Caption = num1 - num2 End SubPrivate Sub cmdMul_Click()' 乘法运算Dim num1 As IntegerDim num2 As Integernum1 = Val(txtNum1.Text)num2 = Val(txtNum2.Text)lblResult.Caption = num1 * num2 End SubPrivate Sub cmdDiv_Click()' 除法运算Dim num1 As IntegerDim num2 As Integernum1 = Val(txtNum1.Text)num2 = Val(txtNum2.Text)lblResult.Caption = num1 / num2End Sub代码逻辑比较简单,就是读取文本框中的数值,进行相应运算,最后将结果展示在标签中。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

简单计算器一、基本功能描述二进制、八进制、十进制及十六进制数的加、减、乘、除、乘方、取模等简单计算科学计算函数,包括(反)正弦、(反)余弦、(反)正切、(反)余切、开方、指数等函数运行以角度、弧度两种方式实现上述部分函数具备历史计算的记忆功能对不正确的表达式能指出其错误原因二、流程图输入数据子函数图1 输入数据子函数流程图图2三、设计步骤打开Microsoft Visual C++ 6.0,在文件中点击新建,在弹出框内选择MFC AppWizard[exe]工程,输入工程名Calculter及其所在位置,点击确定,如图3所示。

图3 新建MFC AppWizard[exe]工程将弹出MFC AppWizard-step 1对话框,选择基本对话框,点击完成,如图4所示。

图4 基本对话框这样,MFC AppWizard就建立了一个基于对话窗口的程序框架,如图5所示。

图5 新建的对话框四、界面设计1、创建控件在图5所示的Resoure View选项卡中打开Dialog资源组,双击IDD_CALCULATOR_DIALOG,在右边的窗口中显示出待编辑的对话框。

开始摆放控件,包括编辑框和按钮的创建。

按钮的创建1)在图5中Controls的“编辑框”按钮上单击鼠标左键,在对话框编辑窗口上合适的位置按下鼠标左键并拖动鼠标画出一个大小合适的编辑框。

在编辑框上单击鼠标右键,在弹出的快捷莱单中选择属性选项,此时弹出Edit属性对话框,如图6所示,在该对话框中输入ID属性。

图6 Edit属性对话框2)在图3中Controls的“Button”按钮上单击鼠标左键,在对话框上的合适的位置上按下鼠标左键并拖动鼠标画出一个大小合适的下压式按钮。

在按钮上单击鼠标右键,在弹出的快捷菜单中选择属性选项,此时也弹出Push Button属性对话框,如图7所示,在该对话框中输入控件的ID值和标题属性。

图7 Push Button属性按照上面的操作过程编辑其他按钮和编辑框等对象的属性。

表1 各按钮和编辑框等对象的属性完成后界面如图8所示。

图8 计算器界面五、计算器源程序代码// Calculation.cpp: implementation of the CCalculation class.//////////////////////////////////////////////////////////////////////#include "stdafx.h"#include "Calculator.h"#include "Calculation.h"#include "math.h"#ifdef _DEBUG#undef THIS_FILEstatic char THIS_FILE[]=__FILE__;#define new DEBUG_NEW#endif#define FUNCNUM 15 //函数个数#define CONSTNUM 11 //常数个数#define PI 3.14159//////////////////////////////////////////////////////////////////////// Construction/Destruction//////////////////////////////////////////////////////////////////////CCalculation::CCalculation(){//初始化函数和常数m_bDegree=0;m_nOutputFlag=0;opt[0]="e^";opt[1]="ch";opt[2]="sqr";opt[3]="sh";opt[5]="cos";opt[6]="sin";opt[7]="tan";opt[8]="ln";opt[9]="abs";opt[10]="at";opt[11]="as";opt[12]="ac";opt[13]="th";opt[14]="cot";opt2[0]='^';opt2[1]='/';opt2[2]='*';opt2[3]='%';opt2[4]='+';opt2[5]='-';m_strConValue[1]="9.80665"; m_strConName[1]="GG";m_strConValue[2]="(8.988*10^9)"; m_strConName[2]="EF";m_strConValue[3]="0.02241383"; m_strConName[3]="VM";m_strConValue[4]="(1.380662*10^(-23))"; m_strConName[4]="BC";m_strConValue[5]="8.3144"; m_strConName[5]="MG";m_strConValue[6]="(6.6720*10^(-11))"; m_strConName[6]="GR";m_strConValue[7]="(6.022045*10^23)"; m_strConName[7]="NA";}CCalculation::~CCalculation(){}bool CCalculation::IsDigital(CString str){while(str.GetAt(0)=='-') str.Delete(0);for(int i=0;i<FUNCNUM;i++) if(str.Find(opt[i])!=-1) return 0;for(i=0;i<6;i++) if(str.Find(opt2[i])!=-1) return 0;return 1;}CString CCalculation::NtoS(double d){int decimal, sign;char *buffer;buffer = _ecvt( d, 16, &decimal, &sign );CString str=buffer;if(decimal>=0 && decimal<=16) str.Insert(decimal,".");else if(decimal>16){for(int i=str.GetLength();i<decimal;i++) str+="0";else{for(int i=0;i<-decimal;i++) str.Insert(0,"0");str.Insert(0,".");}if(sign==1) str.Insert(0,"-");return str;}double CCalculation::StoN(CString str){char *stopstring;double x;x = strtod( str.GetBuffer(0), &stopstring );m_strTmp=stopstring;return x;}CString CCalculation::TwoE(CString strExp){double x2,y,x1=StoN(strExp);strExp=m_strTmp;char op=strExp.GetAt(0);strExp.Delete(0);x2=StoN(strExp);if(m_strTmp!="") return "ERROR_"+m_strTmp+"未知符号_";if(op=='+') {y=x1+x2;}else if(op=='*') {y=x1*x2;}else if(op=='-') {y=x1-x2;}else if(op=='^') {y=pow(x1,x2);}else if(op=='/') {y=x1/x2;}else if(op=='%') {y=fmod(x1,x2);}else return "ERROR_"+m_strTmp+"未知运算符_";return NtoS(y);}CString CCalculation::SingleE(CString op,double dx){if(op=="ln"){return NtoS(log(dx));}else if(op=="log"){return NtoS(log10(dx));}return NtoS(sqrt(dx));}else if(op=="e^"){return NtoS(exp(dx));}else if(op=="abs"){return NtoS(fabs(dx));}else if(op=="ac"){return NtoS(acos(dx)); }else if(op=="as"){return NtoS(asin(dx));}else if(op=="at"){return NtoS(atan(dx));}if(m_bDegree) dx=dx*PI/180; if(op=="tan"){return NtoS(tan(dx));}else if(op=="sin"){return NtoS(sin(dx));}else if(op=="cos"){return NtoS(cos(dx));}else if(op=="cot"){return NtoS(1/tan(dx)); }else if(op=="sh"){return NtoS(sinh(dx));}return NtoS(cosh(dx));}else if(op=="th"){return NtoS(sinh(dx)/cosh(dx));}else if(op=="exp"){return NtoS(pow(10,dx));}return "ERROR"+op+"_未知函数_";}int CCalculation::LocateLBra(CString strExp){int len=strExp.GetLength();int pos=-1;for(int i=0;i<len;i++){if(pos>=0 && strExp.GetAt(i)==')') break;if(strExp.GetAt(i)=='('){pos=i;}}return pos;}void CCalculation::DelBracket(CString *strExp){int pos=LocateLBra(*strExp);int i,len=strExp->GetLength();if(pos==-1) {*strExp="ERROR_无效表达式_";return;}for(i=pos;i<len;i++) if(strExp->GetAt(i)==')') break;CString str;str=strExp->Mid(pos+1,i-pos-1);MultiE(&str);strExp->Delete(pos,i-pos+1);strExp->Insert(pos,str);}void CCalculation::MultiE(CString *strExp){if(strExp->IsEmpty()) *strExp="ERROR_函数表达式为空_";if(IsDigital(*strExp)) {return;}while(1)/*处理所有的一元运算*/for(int i=0;i<FUNCNUM;i++){int pos=strExp->Find(opt[i]);if(pos!=-1){CString str=strExp->Right(strExp->GetLength()-pos-opt[i].GetLength());double dx=StoN(str);if(m_strTmp==str) {*strExp="ERROR"+str+"_无法识别的函数_";return;}strExp->Delete(pos,strExp->GetLength()-pos);*strExp+=SingleE(opt[i],dx)+m_strTmp;MinusMinus(strExp);if(pos>=1){char ch=strExp->GetAt(pos-1);if(ch>=48 && ch<=57){*strExp="ERROR_缺少二元运算符_";return;}}break;}}if(i==FUNCNUM) break;}//按运算符优先级处理二元运算int pos=-1;while(1){pos=strExp->Find('^');if(pos==-1) break;else Calcu(strExp,pos);}while(1){pos=strExp->Find('/');if(pos==-1) break;else Calcu(strExp,pos);}while(1){pos=strExp->Find('*');if(pos==-1) break;else Calcu(strExp,pos);}while(1){if(pos==-1) break;else Calcu(strExp,pos);}pos=0;if(strExp->GetAt(0)=='-' ||strExp->GetAt(0)=='+') strExp->Insert(0,"0"); while(1){int tmpos=strExp->Right(strExp->GetLength()-pos).Find('-');if(tmpos==-1) break;else pos+=tmpos;if(pos==0 && strExp->GetAt(pos+1)=='-' || pos>0){if(strExp->GetAt(pos+1)=='+'){strExp->Delete(pos+1);pos=0;continue;}/*********处理连减(如:----1)的情况***********/int nCount=0;while(1){if(++pos>0){if(strExp->GetAt(pos)!='-') break;else nCount++;}else break;}if(nCount>0){strExp->Delete(pos-nCount-1,nCount+1);if(nCount%2==0) strExp->Insert(pos-nCount-1,"-");else if(pos-nCount-1>0) strExp->Insert(pos-nCount-1,"+");pos=0;continue;}else pos--;/***********************************************/if(pos>0 && strExp->GetAt(pos-1)=='+'){pos++;continue;}Calcu(strExp,pos);}else pos++;}pos=0;while(1){pos=strExp->Find('+');// int tmpos=strExp->Right(strExp->GetLength()-pos).Find('+');if(pos==-1) break;// else pos+=tmpos;if(pos==0 && strExp->GetAt(pos+1)=='+' || pos>0){/*********处理连加(如:++++1)的情况***********/int nCount=0;while(1){if(++pos>0){if(strExp->GetAt(pos)!='+') break;else nCount++;}else break;}if(nCount>0){strExp->Delete(pos-nCount-1,nCount+1);strExp->Insert(pos-nCount-1,"+");pos=0;continue;}else pos--;/***********************************************/Calcu(strExp,pos);}else pos++;}}void CCalculation::Calcu(CString *strExp,int pos){char ch;for(int j=pos-1;j>=0;j--){ch=strExp->GetAt(j);if(ch=='+' ||ch=='-' ||ch=='*'||ch=='/' ||ch=='%' ||ch=='^' ){if(j==0 && ch!='-') {*strExp="ERROR_缺少参数_";return;}if(j==0 && ch=='-') j=-1;//防止把负号误判为减号else if(j>0 && ch=='-' && !IsDigital(strExp->Mid(j-1,1))) j--;break;}}for(int k=pos+1;k<strExp->GetLength();k++){ch=strExp->GetAt(k);if(ch=='+' ||ch=='-' ||ch=='*'||ch=='/' ||ch=='%' ||ch=='^' ){if(ch=='-' && k>pos+1) break;if(ch=='-' && k>0 && IsDigital(strExp->Mid(k+1,1)) ) k++;else break;}}CString strExp2=strExp->Mid(j+1,k-j-1);*strExp=strExp->Left(j+1)+TwoE(strExp2)+strExp->Right(strExp->GetLength()-k);if(strExp->Find("#IN")!=-1) {*strExp="ERROR_结果有溢出或值域越界_";return;} if(!SynRes(strExp)) {*strExp="ERROR_缺少运算符_";return;}MinusMinus(strExp);}CString CCalculation::MainPro(CString strExp){if(strExp.IsEmpty()) return "表达式不能为空";Macro(&strExp);strExp.MakeLower(); //表达式全部小写/**********给表达式加上保护括号************/strExp.Insert(0,"(");strExp+=")";/******************************************/int pos=strExp.Find(" ");int n=BraCheck(strExp);CString str;str.Format("%d",abs(n));if(n==1) strExp+=")";else if(n==-1) strExp.Insert(0,"(");else if(n>0) return "缺少"+str+"个右括号";else if(n<0) return "缺少"+str+"个左括号";while(pos!=-1) //去掉表达式中的空格符{strExp.Delete(pos);pos=strExp.Find(" ");}Oct2Dec(&strExp); //将表达式中的八进制数转换成十进制Hex2Dec(&strExp); //将表达式中的十六进制数转换成十进制Bin2Dec(&strExp); //将表达式中的二进制数转换成十进制while(!IsDigital(strExp)){DelBracket(&strExp);if(!SynRes(&strExp)) return strExp;}if(!SynRes(&strExp)) return strExp;else return ModiResult(strExp);}void CCalculation::Macro(CString *strExp){int pos;for(int i=0;i<CONSTNUM;i++){pos=strExp->Find(m_strConName[i]);while(pos!=-1){strExp->Delete(pos,m_strConName[i].GetLength());strExp->Insert(pos,m_strConValue[i]);if(pos>=1){char ch=strExp->GetAt(pos-1);if(ch>=48 && ch<=57 || ch==41){*strExp="缺少二元运算符";return;}}pos=strExp->Find(m_strConName[i]);}}}CString CCalculation::ModiResult(CString strRes){if(strRes.Find("#IN")!=-1) return "结果有溢出或值域越界";/*****************去掉保护括号**********************/if(strRes.GetAt(0)=='(') strRes.Delete(0);if(strRes.GetAt(strRes.GetLength()-1)==')') strRes.Delete(strRes.GetLength()-1);/***************************************************/int pos=strRes.Find(".");CString str="";if(pos!=-1){if(pos==0) strRes="0"+strRes;else if(strRes.GetAt(0)=='-' && strRes.GetAt(1)=='.') strRes.Insert(1,"0");}if(pos>16){strRes.Delete(pos);strRes.Insert(1,".");str.Format("%d",pos-1);str=" E"+str;}pos=strRes.Find(".");if(pos==0 || pos==1 && strRes.GetAt(0)=='0'){for(int i=pos+1;i<strRes.GetLength();i++){if(strRes.GetAt(i)!='0') break;}if(i>4){str.Format(" E-%d",i-2);strRes.Delete(pos,i-1);strRes.Insert(1,".");}}strRes=strRes.Left(pos+16)+str;//截取小数点后16位return strRes;}bool CCalculation::SynRes(CString *strExp){int pos=strExp->Find("ERROR");if(pos!=-1){pos=strExp->ReverseFind('_');strExp->Delete(pos,strExp->GetLength()-pos);pos=strExp->ReverseFind('_');strExp->Delete(0,pos+1);return 0;}return 1;}void CCalculation::MinusMinus(CString *strExp){int pos=strExp->Find("--");if(pos!=-1){strExp->Delete(pos,2);strExp->Insert(pos,"+");if(strExp->GetAt(0)=='+') strExp->Delete(0);}}int CCalculation::BraCheck(CString str){int lb=0,rb=0,len=str.GetLength();for(int i=0;i<len;i++){if(str.GetAt(i)=='(') lb++;else if(str.GetAt(i)==')') rb++;}return lb-rb;}void CCalculation::Oct2Dec(CString *strExp){int len,i,index,pos=strExp->Find("xo");CString strTmp,strDF;char ch;double dx;while(pos!=-1){dx=0;strTmp="";strDF="";strExp->Delete(pos,2);for(i=pos-1;i>=0;i--){ch=strExp->GetAt(i);if(ch==56 || ch==57 || ch>=97 && ch<=102){*strExp="ERROR_八进制数越界_";return;}if(ch>=48 && ch<=55 ||ch==46){strTmp.Insert(0,strExp->Mid(i,1));strExp->Delete(i);}else break;}if(i==pos-1) {*strExp="ERROR_缺少二元运算符_";return;}index=i;pos=strTmp.Find(".");if(pos!=-1){strDF=strTmp.Right(strTmp.GetLength()-pos-1);strTmp.Delete(pos,strTmp.GetLength()-pos);}strTmp.MakeReverse();len=strTmp.GetLength();for(i=0;i<len;i++){ch=strTmp.GetAt(i);dx+=(ch-48)*pow(8,i);}len=strDF.GetLength();for(i=0;i<len;i++){ch=strDF.GetAt(i);dx+=(ch-48)*pow(8,-i-1);}strTmp=NtoS(dx);strExp->Insert(index+1,strTmp);pos=strExp->Find("xo");}}void CCalculation::Hex2Dec(CString *strExp){int len,i,index,pos=strExp->Find("xh");CString strTmp,strDF;char ch;double dx;while(pos!=-1){dx=0;strTmp="";strDF="";strExp->Delete(pos,2);for(i=pos-1;i>=0;i--){ch=strExp->GetAt(i);if(ch>=48 && ch<=57 || ch>=97 && ch<=102 ||ch==46){strTmp.Insert(0,strExp->Mid(i,1));strExp->Delete(i);}else break;}if(i==pos-1) {*strExp="ERROR_缺少二元运算符_";return;}index=i;pos=0;for(i=0;i<strTmp.GetLength();i++){if(strTmp.GetAt(i)=='.') pos++;if(pos>1) {*strExp="ERROR_缺少二元运算符_";return;} }pos=strTmp.Find(".");if(pos!=-1){strDF=strTmp.Right(strTmp.GetLength()-pos-1);strTmp.Delete(pos,strTmp.GetLength()-pos);}strTmp.MakeReverse();len=strTmp.GetLength();for(i=0;i<len;i++){ch=strTmp.GetAt(i);if(ch>=48 && ch<=57)//该数在0~9之间{dx+=(ch-48)*pow(16,i);}else if(ch>=97 && ch<=102)//该数在a~f之间{dx+=(ch-87)*pow(16,i);}}len=strDF.GetLength();for(i=0;i<len;i++){ch=strDF.GetAt(i);if(ch>=48 && ch<=57)//该数在0~9之间{dx+=(ch-48)*pow(16,-i-1);}else if(ch>=97 && ch<=102)//该数在a~f之间{dx+=(ch-87)*pow(16,-i-1);}}strTmp=NtoS(dx);strExp->Insert(index+1,strTmp);pos=strExp->Find("xh");}}void CCalculation::Bin2Dec(CString *strExp){int len,i,index,pos=strExp->Find("xb");CString strTmp,strDF;char ch;double dx;while(pos!=-1){dx=0;strTmp="";strDF="";strExp->Delete(pos,2);for(i=pos-1;i>=0;i--){ch=strExp->GetAt(i);if(ch>=50 && ch<=57 || ch>=97 && ch<=102){*strExp="ERROR_二进制数越界_";return;}if(ch=='0' || ch=='1' ||ch==46){strTmp.Insert(0,strExp->Mid(i,1));strExp->Delete(i);}else break;}if(i==pos-1) {*strExp="ERROR_缺少二元运算符_";return;}index=i;pos=strTmp.Find(".");if(pos!=-1){strDF=strTmp.Right(strTmp.GetLength()-pos-1);strTmp.Delete(pos,strTmp.GetLength()-pos);}strTmp.MakeReverse();len=strTmp.GetLength();for(i=0;i<len;i++){ch=strTmp.GetAt(i);dx+=(ch-48)*pow(2,i);}len=strDF.GetLength();for(i=0;i<len;i++){ch=strDF.GetAt(i);dx+=(ch-48)*pow(2,-i-1);}strTmp=NtoS(dx);strExp->Insert(index+1,strTmp);pos=strExp->Find("xb");}}void CCalculation::Dec2Hex(CString *strExp/*strExp须为数字*/) {bool bMinus=0;if(strExp->GetAt(0)=='-'){bMinus=1;strExp->Delete(0);}int pos=strExp->Find('.');CString str,strDec;int nDecInt;double dDec;if(pos!=-1){strDec=strExp->Left(pos);nDecInt=atoi(strDec.GetBuffer(0));strDec=strExp->Right(strExp->GetLength()-pos); }else{nDecInt=atoi(strExp->GetBuffer(0));}strExp->Empty();while(nDecInt!=0){int nTmp=nDecInt%16;if(nTmp==10) str="a";else if(nTmp==11) str="b";else if(nTmp==12) str="c";else if(nTmp==13) str="d";else if(nTmp==14) str="e";else if(nTmp==15) str="f";else str.Format("%d",nTmp);nDecInt/=16;strExp->Insert(0,str);}*strExp+=".";if(pos!=-1){dDec=StoN(strDec);int nCount=0;while(dDec!=0){dDec*=16;int nTmp=dDec;if(nTmp==10) str="a";else if(nTmp==11) str="b";else if(nTmp==12) str="c";else if(nTmp==13) str="d";else if(nTmp==14) str="e";else if(nTmp==15) str="f";else str.Format("%d",nTmp);*strExp+=str.Left(pos);dDec-=nTmp;if(++nCount==17) break;}}if(bMinus) strExp->Insert(0,"-");if(strExp->Find("-1")!=-1 && bMinus!=1) *strExp="太大无法表示"; }void CCalculation::Dec2Oct(CString *strExp){bool bMinus=0;if(strExp->GetAt(0)=='-'){bMinus=1;strExp->Delete(0);}int pos=strExp->Find('.');CString str,strDec;int nDecInt;double dDec;if(pos!=-1){strDec=strExp->Left(pos);nDecInt=atoi(strDec.GetBuffer(0));strDec=strExp->Right(strExp->GetLength()-pos);}else{nDecInt=atoi(strExp->GetBuffer(0));}strExp->Empty();while(nDecInt!=0){int nTmp=nDecInt%8;str.Format("%d",nTmp);nDecInt/=8;strExp->Insert(0,str);}*strExp+=".";if(pos!=-1){dDec=StoN(strDec);int nCount=0;while(dDec!=0){dDec*=8;int nTmp=dDec;str.Format("%d",nTmp);*strExp+=str.Left(pos);dDec-=nTmp;if(++nCount==17) break;}}if(bMinus) strExp->Insert(0,"-");}void CCalculation::Dec2Bin(CString *strExp){bool bMinus=0;if(strExp->GetAt(0)=='-'){bMinus=1;strExp->Delete(0);}int pos=strExp->Find('.');CString str,strDec;_int64 nDecInt;double dDec;if(pos!=-1){strDec=strExp->Left(pos);{*strExp="太大无法转换";return;}nDecInt=atoi(strDec.GetBuffer(0));strDec=strExp->Right(strExp->GetLength()-pos);}else{{*strExp="太大无法转换";return;}nDecInt=atoi(strExp->GetBuffer(0));}strExp->Empty();while(nDecInt!=0){_int64 nTmp=nDecInt%2;str.Format("%d",nTmp);nDecInt/=2;strExp->Insert(0,str);}*strExp+=".";if(pos!=-1){dDec=StoN(strDec);int nCount=0;while(dDec!=0){dDec*=2;int nTmp=dDec;str.Format("%d",nTmp);*strExp+=str.Left(pos);dDec-=nTmp;if(++nCount==17) break;}}if(bMinus) strExp->Insert(0,"-");}六、结论与心得体会七测试结果1.测试+ - * / PI运算2.测试sin cos tan cot3. 20的二进制八进制十六进制二进制八进制十六进制4.历史表达式5.错误判断。

相关文档
最新文档