简单计算器设计源代码
JavaFx-编写一个简单的计算器,实现加减乘除
JavaFx-编写⼀个简单的计算器,实现加减乘除0.题⽬描述编写⼀个简单的计算器,完成加、减、乘、除的功能1.源代码import javafx.application.Application;import javafx.stage.Stage;import yout.HBox;import yout.BorderPane;import bel;import javafx.scene.control.TextField;import javafx.scene.control.Button;import javafx.scene.Scene;import javafx.geometry.Insets;import javafx.geometry.Pos;public class Test2 extends Application {private TextField num1 = new TextField();private TextField num2 = new TextField();private TextField res = new TextField();private Button btAdd = new Button("Add");private Button btSub = new Button("Subtract");private Button btMul = new Button("Multiply");private Button btDiv = new Button("Divide");@Overridepublic void start(Stage primaryStage) {HBox top = new HBox();top.setAlignment(Pos.CENTER);top.setPadding(new Insets(5,10,5,10));top.setSpacing(10);top.getChildren().addAll(new Label("Number1"),num1,new Label("Number2"),num2,new Label("Result"),res);HBox bottom = new HBox();bottom.setAlignment(Pos.CENTER);bottom.setPadding(new Insets(5,10,5,10));bottom.setSpacing(10);bottom.getChildren().addAll(btAdd,btSub,btMul,btDiv);BorderPane pane = new BorderPane();pane.setTop(top);pane.setBottom(bottom);btAdd.setOnAction(e -> Add());btSub.setOnAction(e -> Sub());btMul.setOnAction(e -> Mul());btDiv.setOnAction(e -> Div());Scene scene = new Scene(pane, 900, 80);primaryStage.setResizable(false);primaryStage.setScene(scene);primaryStage.setTitle("Calculator");primaryStage.show();}private void Add() {double n1 = Double.parseDouble(num1.getText());double n2 = Double.parseDouble(num2.getText());res.setText(String.format("%.2f", n1+n2));}private void Sub() {double n1 = Double.parseDouble(num1.getText());double n2 = Double.parseDouble(num2.getText());res.setText(String.format("%.2f", n1-n2));}private void Mul() {double n1 = Double.parseDouble(num1.getText());double n2 = Double.parseDouble(num2.getText());res.setText(String.format("%.2f", n1*n2));}private void Div() {double n1 = Double.parseDouble(num1.getText());double n2 = Double.parseDouble(num2.getText());res.setText(String.format("%.2f", n1/n2));}public static void main(String[] args) {unch(args);}}2.运⾏结果3.技术总结 上半部分放⼊⼀个HBox中,下半部分放⼊⼀个HBox中。
C#编写简易计算器(附源代码)超详细
超详细一、因为计算器设计的控件太多,不便使用控制台应用程序完成,所以这里使用Windows窗体应用程序,并命名为Calc,如下图所示:二、向窗体中拖入需要的控件,如下图所示:(完成效果图)结果显示区(作者博客左边的文本框)是TextBox控件,并修改其name为txtShow ,按键0~9为Button控件,并将其name分别修改为btn_0、btn_1、btn_2、btn_3、btn_4、btn_5、btn_6、btn_7、btn_8、btn_9;按键【负数】的name值修改为btn_sign,按键【.】的name 修改为btn_dot,按键【+ - * /】的name值分别修改为btn_add、btn_sub、btn_mul、btn_div,按键【=】的name值修改为btn_equ,按键【倒数】的name值修改为btn_rev,按键【平方】的name值修改为btn_sqr,按键【开方】的name值修改为btn_sqrt。
右边的计算器图片空间是PictureBox,作者博客控件是LinkLabel,可以不添加,以上所有控件均可按照需求添加,只保留自己需要的按钮控件和textbox控件即可。
三、代码部分(含解释),采用switch多分支语句编写using System;using System.Drawing;using System.Collections;using ponentModel;using System.Windows.Forms;using System.Data;namespace Calc{///<summary>/// QQ:479340056 温柔一刀C#简易计算器的实现///</summary>public class CalcForm : System.Windows.Forms.Form{private System.Windows.Forms.Button btn_0;private System.Windows.Forms.Button btn_1;private System.Windows.Forms.Button btn_2;private System.Windows.Forms.Button btn_3;private System.Windows.Forms.Button btn_4;private System.Windows.Forms.Button btn_5;private System.Windows.Forms.Button btn_6;private System.Windows.Forms.Button btn_7;private System.Windows.Forms.Button btn_8;private System.Windows.Forms.Button btn_9;private System.Windows.Forms.Button btn_add;private System.Windows.Forms.Button btn_sub;private System.Windows.Forms.Button btn_mul;private System.Windows.Forms.Button btn_div;private System.Windows.Forms.Button btn_sqrt;private System.Windows.Forms.Button btn_sign;private System.Windows.Forms.Button btn_equ;private System.Windows.Forms.Button btn_dot;private System.Windows.Forms.Button btn_rev;private System.Windows.Forms.TextBox txtShow;private System.Windows.Forms.Button btn_sqr;private PictureBox pictureBox1;private LinkLabel linkLabel1;///<summary>///必需的设计器变量。
简易计算器程序源代码
简易计算器程序源代码下面是一个简易计算器程序的源代码,它可以执行基本的四则运算:```python#定义加法函数def add(x, y):return x + y#定义减法函数def subtract(x, y):return x - y#定义乘法函数def multiply(x, y):return x * y#定义除法函数def divide(x, y):if y == 0:return "除数不能为0"else:return x / y#显示菜单print("选择操作:")print("1. 相加")print("2. 相减")print("3. 相乘")print("4. 相除")#获取用户输入choice = input("输入你的选择(1/2/3/4): ")#获取用户输入的两个数字num1 = float(input("输入第一个数字: "))num2 = float(input("输入第二个数字: "))#根据用户选择执行相应操作if choice == '1':print(num1, "+", num2, "=", add(num1, num2))elif choice == '2':print(num1, "-", num2, "=", subtract(num1, num2)) elif choice == '3':print(num1, "*", num2, "=", multiply(num1, num2)) elif choice == '4':print(num1, "/", num2, "=", divide(num1, num2))else:print("请输入有效的选择")```运行这个程序,你将看到一个简易的计算器菜单。
C语言程序设计-第四章简单计算器小程序-分支程序设计
P1 逻辑运算符 P2
或
逻辑运算符 P2
课堂练习: 求下面逻辑表达式的值: 2>1 && 4 && 7<3+!0
值为0
等价于((2>1)&&4)&&(7<(3+(!
1 条件运算符和条件表达式 1.2 逻辑运算—逻辑运算的重要规则
a&&b:当a为0时,不管b为何值,结果为0
课堂练习:有下面定义的语句和逻辑表达式: int a=0,b=10,c=0,d=0 a && b && (c=a+10,d=100) 逻辑表达式执行后,a,b,c,d的值各为多少?
任务实现效果演示 例题源代码:switchcalculator.c
相关知识 1. switch语句 2. break语句
1 switch多分支选择结构
❖ 尽管用if…else if结构可以实现多分支,注但意当:分常支量较表多达时式,必程须序是结整构型较、复字杂符。型或枚举类型,不能是 实型表达式,每个case关键字后的常量表达式应互不相同
值为0 等价于i==(k>j),值为1
1 条件运算符和条件表达式 1.2 逻辑运算—逻辑运算符与逻辑表达式
&&(逻辑与)、||(逻辑或)、!(逻辑非)(单目)
逻辑表达式的一般形式:
优先级: 1.由低到高: ||→ && → !; 2.多种运算符一起: = →|| →&& →关系运算符 →算术运算符→ !。
【】 简单计算器小程序:由用户输入运算数和四则运算符(+、-、*、/),输出计算结果。
【】
可视化编程课程设计(含源码)计算器系统实现
设计题目:计算器系统实现1.分别用API与MFC编程来实现计算器的以下功能。
2.实现计算器的基本功能:连续数据的无优先级混合运算(加减乘除)3.可以实现其他附加功能:优先级运算,加入括号,加入其他函数运算功能等。
(不在要求范围之内)4.要求界面良好,功能完整。
一.基于MFC的简单计算器1.设计思路打开MFC应用操作界面,布局计算器界面,利用组框将计算器界面分为三个部分,一个是编辑输入,一个是数字界面,一个是功能键部分。
利用布局参考线对齐按钮,使界面美观。
然后就是对各个按钮进行属性设置,关联类设置,接着对各个按钮进行源代码编程。
最后,调试找出问题,解决问题,运行MFC成品计算器。
2.简单操作以及功能说明由于本人技术有限,所以本程序只能按照正确的计算运算顺序进行,该简单计算器能进行四则混合运算,除了加减乘除外,添加了一个括号,对于有些未知的错误,由于时间有限,并未来得及全面测试使用。
该简单计算器能实现四则运算,退格运算,清零运算,并且支持输出的结果保留给直接的下一个运算。
3.系统实现的各个模块1)编辑框模块由于编辑框模块需要连续输入字符串,所以在给编辑框建立类向导时,给编辑框定义的成员变量应该是字符串(String)变量。
编辑框模块用于运算算式的输入,以及结果的输出。
2)数字键模块数字键模块比较简单,该简单计算器数字键模块设置有0~9数字,还设置有小数点,以及正负数转换实现按钮。
数字键模块按钮在建立类向导时无需定义成员变量,但是需要定义按钮响应链接,用于实现点击按钮,在编辑框上显示点击按钮信息。
3)功能键模块功能键模块包括加减乘除基本按钮,以及一个输入错误是后能用于退格功能的退格键按钮,还有一个用于区别优先级运算的括号功能,当然有最重要的计算结果输入按钮,等于号按钮。
在编辑框附近还有一个功能键,就是清除功能键,用于清除编辑框,以便实现下一轮输入。
4.设计过程1)设计总流程图2)界面的设计3)建立的变量,控件的命名,对应的消息处理函数对应表ID CAPTION MessageHandler IDD_JISUANQI_DIALOG 简易计算器N/AIDC_NUM0 0 OnNum0IDC_NUM1 1 OnNum1IDC_NUM2 2 OnNum2IDC_NUM3 3 OnNum3IDC_NUM4 4 OnNum4IDC_NUM5 5 OnNum5IDC_NUM6 6 OnNum6IDC_NUM7 7 OnNum7IDC_NUM8 8 OnNum8IDC_NUM9 9 OnNum9IDC_OPER_ADD + OnOperAdd IDC_OPER_SUB - OnOperSub IDC_OPER_MULTI * OnOperMulti IDC_OPER_DIV / OnOperDiv IDC_ADD_SUB +/- OnAddSubIDC_POINT . OnPointIDC_EQUAL = OnEqualIDC_LBRACKET ( OnLbracket1)数字键模块void CJiSuanQiDlg::OnNum0(){if(calculated == TRUE) //已经按了等号,不让其再接受字符return;UpdateData(TRUE);//刷新编辑框界面calcutateString += "0";//存储输入的数字m_data += "0";//显示输入的数字UpdateData(FALSE);}2)功能键模块①加法功能键源程序void CJiSuanQiDlg::OnOperAdd(){if(calculated == TRUE)///其前一步按了= 号{calculated = FALSE;m_data = oldResult;double temp = atof(oldResult);//定义临时变量存储上一步结果if(temp < 0){calcutateString = "0" + oldResult;}else{calcutateString = oldResult;}calcutateString += "+";//存储做完该功能后的结果m_data += "+";//显示该步骤完成的结果UpdateData(FALSE);}else ///前一步不是={UpdateData(TRUE);calcutateString += "+";//直接存储此步操作m_data += "+";//直接显示此步操作UpdateData(FALSE);}}②退格功能键源程序void CJiSuanQiDlg::OnBackspeace(){if(calculated == FALSE){UpdateData(TRUE);int count = m_data.GetLength();if(count >= 1)//判断是否能执行退格操作{m_data = m_data.Left(count -1);count = calcutateString.GetLength();calcutateString = calcutateString.Left(count -1);UpdateData(FALSE);}}}③清除功能键源程序void CJiSuanQiDlg::OnClear(){// TODO: Add your control notification handler code herem_data = "";oldResult = "";calcutateString = "";calculated = FALSE;UpdateData(FALSE);}④括号功能键源程序void CJiSuanQiDlg::OnLbracket(){// TODO: Add your control notification handler code hereif(calculated == TRUE) ///已经按了等号,不让其再接受字符return;UpdateData(TRUE);calcutateString += "(";m_data += "(";UpdateData(FALSE);}⑤正负转换功能键源程序void CJiSuanQiDlg::OnAddSub(){// TODO: Add your control notification handler code hereif(calculated == FALSE){UpdateData(TRUE);calcutateString = calcutateString + "0" + "-";m_data += "-";UpdateData(FALSE);}}6.运行结果界面截图7.制作过程中所遇问题以及解决方法过程1)源代码编写错误通过查阅资料,请教同学,逐步一一解决。
计算器编程c语言
计算器编程 c语言用C语言设计计算器程序源代码#include <dos.h> /*DOS接口函数*/#include <math.h> /*数学函数的定义*/#include <conio.h> /*屏幕操作函数*/函数*/#include <stdio.h> /*I/O#include <stdlib.h> /*库函数*/变量长度参数表*/#include <stdarg.h> /*图形函数*/#include <graphics.h> /*字符串函数*/#include <string.h> /*字符操作函数*/#include <ctype.h> /*#define UP 0x48 /*光标上移键*/#define DOWN 0x50 /*光标下移键*/#define LEFT 0x4b /*光标左移键*/#define RIGHT 0x4d /*光标右移键*/#define ENTER 0x0d /*回车键*/void *rar; /*全局变量,保存光标图象*/使用调色板信息*/struct palettetype palette; /*int GraphDriver; /* 图形设备驱动*/int GraphMode; /* 图形模式值*/int ErrorCode; /* 错误代码*/int MaxColors; /* 可用颜色的最大数值*/int MaxX, MaxY; /* 屏幕的最大分辨率*/double AspectRatio; /* 屏幕的像素比*/void drawboder(void); /*画边框函数*/初始化函数*/void initialize(void); /*计算器计算函数*/void computer(void); /*改变文本样式函数*/ void changetextstyle(int font, int direction, int charsize); /*窗口函数*/void mwindow(char *header); /*/*获取特殊键函数*/int specialkey(void) ;设置箭头光标函数*//*int arrow();/*主函数*/int main(){设置系统进入图形模式 */initialize();/*运行计算器 */computer(); /*系统关闭图形模式返回文本模式*/closegraph();/*/*结束程序*/return(0);}/* 设置系统进入图形模式 */void initialize(void){int xasp, yasp; /* 用于读x和y方向纵横比*/GraphDriver = DETECT; /* 自动检测显示器*/initgraph( &GraphDriver, &GraphMode, "" );/*初始化图形系统*/ErrorCode = graphresult(); /*读初始化结果*/如果初始化时出现错误*/if( ErrorCode != grOk ) /*{printf("Graphics System Error: %s\n",显示错误代码*/grapherrormsg( ErrorCode ) ); /*退出*/exit( 1 ); /*}getpalette( &palette ); /* 读面板信息*/MaxColors = getmaxcolor() + 1; /* 读取颜色的最大值*/MaxX = getmaxx(); /* 读屏幕尺寸 */MaxY = getmaxy(); /* 读屏幕尺寸 */getaspectratio( &xasp, &yasp ); /* 拷贝纵横比到变量中*/计算纵横比值*/ AspectRatio = (double)xasp/(double)yasp;/*}/*计算器函数*/void computer(void){定义视口类型变量*/struct viewporttype vp; /*int color, height, width;int x, y,x0,y0, i, j,v,m,n,act,flag=1;操作数和计算结果变量*/float num1=0,num2=0,result; /*char cnum[5],str2[20]={""},c,temp[20]={""};定义字符串在按钮图形上显示的符号 char str1[]="1230.456+-789*/Qc=^%";/**/mwindow( "Calculator" ); /*显示主窗口 */设置灰颜色值*//*color = 7;getviewsettings( &vp ); /* 读取当前窗口的大小*/width=(vp.right+1)/10; /* 设置按钮宽度 */设置按钮高度 */height=(vp.bottom-10)/10 ; /*/*设置x的坐标值*/x = width /2;设置y的坐标值*/y = height/2; /*setfillstyle(SOLID_FILL, color+3);bar( x+width*2, y, x+7*width, y+height );/*画一个二维矩形条显示运算数和结果*/setcolor( color+3 ); /*设置淡绿颜色边框线*/rectangle( x+width*2, y, x+7*width, y+height );/*画一个矩形边框线*/设置颜色为红色*/setcolor(RED); /*输出字符串"0."*/outtextxy(x+3*width,y+height/2,"0."); /*/*设置x的坐标值*/x =2*width-width/2;设置y的坐标值*/y =2*height+height/2; /*画按钮*/for( j=0 ; j<4 ; ++j ) /*{for( i=0 ; i<5 ; ++i ){setfillstyle(SOLID_FILL, color);setcolor(RED);bar( x, y, x+width, y+height ); /*画一个矩形条*/rectangle( x, y, x+width, y+height );sprintf(str2,"%c",str1[j*5+i]);/*将字符保存到str2中*/outtextxy( x+(width/2), y+height/2, str2);移动列坐标*/x =x+width+ (width / 2) ;/*}y +=(height/2)*3; /* 移动行坐标*/x =2*width-width/2; /*复位列坐标*/}x0=2*width;y0=3*height;x=x0;y=y0;gotoxy(x,y); /*移动光标到x,y位置*/显示光标*/arrow(); /*putimage(x,y,rar,XOR_PUT);m=0;n=0;设置str2为空串*/strcpy(str2,""); /*当压下Alt+x键结束程序,否则执行下面的循环while((v=specialkey())!=45) /**/{当压下键不是回车时*/while((v=specialkey())!=ENTER) /*{putimage(x,y,rar,XOR_PUT); /*显示光标图象*/if(v==RIGHT) /*右移箭头时新位置计算*/if(x>=x0+6*width)如果右移,移到尾,则移动到最左边字符位置*//*{x=x0;m=0;}else{x=x+width+width/2;m++;否则,右移到下一个字符位置*/} /*if(v==LEFT) /*左移箭头时新位置计算*/if(x<=x0){x=x0+6*width;m=4;} /*如果移到头,再左移,则移动到最右边字符位置*/else{x=x-width-width/2;m--;} /*否则,左移到前一个字符位置*/if(v==UP) /*上移箭头时新位置计算*/if(y<=y0){y=y0+4*height+height/2;n=3;} /*如果移到头,再上移,则移动到最下边字符位置*/else{y=y-height-height/2;n--;} /*否则,移到上边一个字符位置*/if(v==DOWN) /*下移箭头时新位置计算*/if(y>=7*height){ y=y0;n=0;} /*如果移到尾,再下移,则移动到最上边字符位置*/else{y=y+height+height/2;n++;} /*否则,移到下边一个字符位置*/putimage(x,y,rar,XOR_PUT); /*在新的位置显示光标箭头*/ }将字符保存到变量c中*/c=str1[n*5+m]; /*判断是否是数字或小数点*/if(isdigit(c)||c=='.') /*{如果标志为-1,表明为负数*/if(flag==-1) /*{将负号连接到字符串中*/strcpy(str2,"-"); /*flag=1;} /*将标志值恢复为1*/将字符保存到字符串变量temp中*/ sprintf(temp,"%c",c); /*将temp中的字符串连接到str2中*/strcat(str2,temp); /*setfillstyle(SOLID_FILL,color+3);bar(2*width+width/2,height/2,15*width/2,3*height/2);显示字符串*/outtextxy(5*width,height,str2); /*}if(c=='+'){将第一个操作数转换为浮点数*/num1=atof(str2); /*将str2清空*/strcpy(str2,""); /*做计算加法标志值*/act=1; /*setfillstyle(SOLID_FILL,color+3);bar(2*width+width/2,height/2,15*width/2,3*height/2);显示字符串*/outtextxy(5*width,height,"0."); /*}if(c=='-'){如果str2为空,说明是负号,而不是减号*/ if(strcmp(str2,"")==0) /*设置负数标志*/flag=-1; /*else{将第二个操作数转换为浮点数*/num1=atof(str2); /*将str2清空*/strcpy(str2,""); /*act=2; /*做计算减法标志值*/setfillstyle(SOLID_FILL,color+3);画矩形*/ bar(2*width+width/2,height/2,15*width/2,3*height/2); /*显示字符串*/outtextxy(5*width,height,"0."); /*}}if(c=='*'){将第二个操作数转换为浮点数*/num1=atof(str2); /*strcpy(str2,""); /*将str2清空*/做计算乘法标志值*/act=3; /*setfillstyle(SOLID_FILL,color+3); bar(2*width+width/2,height/2,15*width /2,3*height/2);显示字符串*/outtextxy(5*width,height,"0."); /*}if(c=='/'){将第二个操作数转换为浮点数*/num1=atof(str2); /*strcpy(str2,""); /*将str2清空*/做计算除法标志值*/act=4; /*setfillstyle(SOLID_FILL,color+3);bar(2*width+width/2,height/2,15*width/2,3*height/2);outtextxy(5*width,height,"0."); /*显示字符串*/}if(c=='^'){将第二个操作数转换为浮点数*/num1=atof(str2); /*将str2清空*/strcpy(str2,""); /*做计算乘方标志值*/act=5; /*设置用淡绿色实体填充*/ setfillstyle(SOLID_FILL,color+3); /*画矩形*/ bar(2*width+width/2,height/2,15*width/2,3*height/2); /*显示字符串*/outtextxy(5*width,height,"0."); /*}if(c=='%'){将第二个操作数转换为浮点数*/num1=atof(str2); /*strcpy(str2,""); /*将str2清空*/做计算模运算乘方标志值*/act=6; /*setfillstyle(SOLID_FILL,color+3); /*设置用淡绿色实体填充*/画矩形*/ bar(2*width+width/2,height/2,15*width/2,3*height/2); /*显示字符串*/outtextxy(5*width,height,"0."); /*}if(c=='='){将第二个操作数转换为浮点数*/num2=atof(str2); /*根据运算符号计算*/switch(act) /*{case 1:result=num1+num2;break; /*做加法*/case 2:result=num1-num2;break; /*做减法*/case 3:result=num1*num2;break; /*做乘法*/case 4:result=num1/num2;break; /*做除法*/case 5:result=pow(num1,num2);break; /*做x的y次方*/case 6:result=fmod(num1,num2);break; /*做模运算*/ }设置用淡绿色实体填充*/ setfillstyle(SOLID_FILL,color+3); /*覆盖结果区*/ bar(2*width+width/2,height/2,15*width/2,3*height/2); /*将结果保存到temp中*/sprintf(temp,"%f",result); /*outtextxy(5*width,height,temp); /*显示结果*/}if(c=='c'){num1=0; /*将两个操作数复位0,符号标志为1*/num2=0;flag=1;strcpy(str2,""); /*将str2清空*/设置用淡绿色实体填充*/ setfillstyle(SOLID_FILL,color+3); /*覆盖结果区*/ bar(2*width+width/2,height/2,15*width/2,3*height/2); /*显示字符串*/outtextxy(5*width,height,"0."); /*}如果选择了q回车,结束计算程序*/if(c=='Q')exit(0); /*}putimage(x,y,rar,XOR_PUT); /*在退出之前消去光标箭头*/返回*/return; /*}/*窗口函数*/void mwindow( char *header ){int height;cleardevice(); /* 清除图形屏幕 */setcolor( MaxColors - 1 ); /* 设置当前颜色为白色*//* 设置视口大小 */ setviewport( 20, 20, MaxX/2, MaxY/2, 1 );height = textheight( "H" ); /* 读取基本文本大小 */settextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );/*设置文本样式*/settextjustify( CENTER_TEXT, TOP_TEXT );/*设置字符排列方式*/输出标题*/outtextxy( MaxX/4, 2, header ); /*setviewport( 20,20+height+4, MaxX/2+4, MaxY/2+20, 1 ); /*设置视口大小*/ 画边框*/drawboder(); /*}画边框*/void drawboder(void) /*{定义视口类型变量*/struct viewporttype vp; /*setcolor( MaxColors - 1 ); /*设置当前颜色为白色 */setlinestyle( SOLID_LINE, 0, NORM_WIDTH );/*设置画线方式*/将当前视口信息装入vp所指的结构中*/getviewsettings( &vp );/*画矩形边框*/rectangle( 0, 0, vp.right-vp.left, vp.bottom-vp.top ); /*}/*设计鼠标图形函数*/int arrow(){int size;定义多边形坐标*/int raw[]={4,4,4,8,6,8,14,16,16,16,8,6,8,4,4,4}; /*设置填充模式*/setfillstyle(SOLID_FILL,2); /*/*画出一光标箭头*/fillpoly(8,raw);测试图象大小*/size=imagesize(4,4,16,16); /*分配内存区域*/rar=malloc(size); /*存放光标箭头图象*/getimage(4,4,16,16,rar); /*putimage(4,4,rar,XOR_PUT); /*消去光标箭头图象*/return 0;}/*按键函数*/int specialkey(void){int key;等待键盘输入*/while(bioskey(1)==0); /*key=bioskey(0); /*键盘输入*/只取特殊键的扫描值,其余为0*/ key=key&0xff? key&0xff:key>>8; /*return(key); /*返回键值*/}。
java语言写的计算器源代码附解析
//把代码改成Calc.java编译运行,完整版计算器,最下面是计算器界面import java.awt.*;import java.awt.event.*;public class Calc extends WindowAdapter implements ActionListener, MouseListener{Color cMoveOut=new Color(240 ,240 ,240);Color cMoveIn =new Color( 255,255,55);//状态变量boolean clicked=true;//判断是否单击了小数点boolean clear=true;//判断是否单击了符号位double previous;//记录第一个操作数double next;//记录第二个操作数String fuhao;//记录符号位int first=1;//标记是否开始一次新的运算过程Frame f;Panel p1,p2;TextField tf;Font fnt;Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0;Button bDiv,bSqrt,bMulti,bMinus,bPercent,bPlus,bReciprocal,bEqual,bDot,bNegative,bBack;Button bPingFang , bDaoShu , bSin, bCos ,bTan;Button bC;GridLayout p2Layout;public void display(){f=new Frame("计算器");f.setSize(280,213);f.setLocation(200,200);f.setBackground(Color.LIGHT_GRAY);f.setResizable(false);p1=new Panel(new FlowLayout());tf=new TextField(35);tf.setText("0.");tf.setEditable(false);p1.add(tf);f.add(p1,BorderLayout.NORTH);p2Layout=new GridLayout(5,5,5,4);p2=new Panel(p2Layout);f.add(p2,BorderLayout.CENTER);fnt=new Font("Serief",Font.BOLD,20);b1=new Button("1");b1.setFont(fnt);b2=new Button("2");b2.setFont(fnt);b3=new Button("3");b3.setFont(fnt);b4=new Button("4");b4.setFont(fnt);b5=new Button("5");b5.setFont(fnt);b6=new Button("6");b6.setFont(fnt);b7=new Button("7");b7.setFont(fnt);b8=new Button("8");b8.setFont(fnt);b7.setFont(fnt);b9=new Button("9");b9.setFont(fnt);b0=new Button("0");b0.setFont(fnt);b9.setFont(fnt);bPingFang=new Button("^2"); bPingFang.setFont(fnt); bDaoShu=new Button("1/X"); bDaoShu.setFont(fnt);bSin=new Button("sin");bSin.setFont(fnt);bCos=new Button("cos"); bCos.setFont(fnt);bTan=new Button("tan"); bTan.setFont(fnt);b1.addActionListener(this);b2.addActionListener(this);b3.addActionListener(this);b4.addActionListener(this);b5.addActionListener(this);b6.addActionListener(this);b7.addActionListener(this);b8.addActionListener(this);b9.addActionListener(this);b0.addActionListener(this); bBack=new Button("Back");//退格bBack.setFont(fnt);bC=new Button("C");//清空bC.setFont(fnt);bDiv=new Button("/");bDiv.setFont(fnt);bSqrt=new Button("sqrt"); bSqrt.setFont(fnt);bMulti=new Button("*"); bMulti.setFont(fnt);bMinus=new Button("-"); bMinus.setFont(fnt);bPercent=new Button("%"); bPercent.setFont(fnt);bPlus=new Button("+"); bPlus.setFont(fnt);bEqual=new Button("="); bEqual.setFont(fnt);bDot=new Button(".");bDot.setFont(fnt);bNegative=new Button("+/-"); bNegative.setFont(fnt); bBack.addActionListener(this);bC.addActionListener(this); bDiv.addActionListener(this); bSqrt.addActionListener(this); bPingFang.addActionListener(this); bPercent.addActionListener(this); bDaoShu.addActionListener(this); bMulti.addActionListener(this); bPlus.addActionListener(this); bMinus.addActionListener(this); bMulti.addActionListener(this); bDiv.addActionListener(this); bSin.addActionListener(this); bCos.addActionListener(this); bTan.addActionListener(this); bEqual.addActionListener(this); bDot.addActionListener(this); bNegative.addActionListener(this); bSqrt.addMouseListener(this); bPingFang.addMouseListener(this); bPercent.addMouseListener(this); bC.addMouseListener(this); bBack.addMouseListener(this);b7.addMouseListener(this);b8.addMouseListener(this);b9.addMouseListener(this); bDiv.addMouseListener(this); bDaoShu.addMouseListener(this); b4.addMouseListener(this);b5.addMouseListener(this);b6.addMouseListener(this); bMulti.addMouseListener(this); bSin.addMouseListener(this);b1.addMouseListener(this);b2.addMouseListener(this);b3.addMouseListener(this); bMinus.addMouseListener(this); bCos.addMouseListener(this);b0.addMouseListener(this); bDot.addMouseListener(this); bEqual.addMouseListener(this); bPlus.addMouseListener(this); bTan.addMouseListener(this);p2.add(bSqrt);p2.add(bPingFang);p2.add(bPercent);p2.add(bC);p2.add(bBack);p2.add(b7);p2.add(b8);p2.add(b9);p2.add(bDiv);p2.add(bDaoShu);p2.add(b4);p2.add(b5);p2.add(b6);p2.add(bMulti);p2.add(bSin);p2.add(b1);p2.add(b2);p2.add(b3);p2.add(bMinus);p2.add(bCos);p2.add(b0);p2.add(bDot);p2.add(bEqual);p2.add(bPlus);p2.add(bTan);f.setV isible(true);f.addWindowListener(this);}public void actionPerformed(ActionEvent e) {if (first ==1)tf.setText("");first=0;Object temp=e.getSource();//退格if (temp== bBack){String s=tf.getText();tf.setText("");for (int i=0;i<s.length()-1;i++){char a=s.charAt(i);tf.setText(tf.getText()+a);}}//清零if (temp==bC){tf.setText("0.");clear=true;first=1;}if (temp==b0){//判断是否单击了符号位if (clear==false)tf.setText("");tf.setText(tf.getText()+"0");}if (temp==b1){if (clear==false)tf.setText("");tf.setText(tf.getText()+"1");clear=true;}if (temp==b2){if (clear==false)tf.setText("");tf.setText(tf.getText()+"2");clear=true;}if (temp==b3){if (clear==false)tf.setText("");tf.setText(tf.getText()+"3");clear=true;}if (temp==b4){if (clear==false)tf.setText("");tf.setText(tf.getText()+"4");clear=true;}if (temp==b5){if (clear==false)tf.setText("");tf.setText(tf.getText()+"5");clear=true;}if (temp==b6){if (clear==false)tf.setText("");tf.setText(tf.getText()+"6");clear=true;}if (temp==b7){if (clear==false)tf.setText("");tf.setText(tf.getText()+"7");clear=true;}if (temp==b8){if (clear==false)tf.setText("");tf.setText(tf.getText()+"8");clear=true;}if (temp==b9){if (clear==false)tf.setText("");tf.setText(tf.getText()+"9");clear=true;}//判断是否含有小数点if (temp == bDot){clicked=true;for (int i=0;i<tf.getText().length();i++)if ('.'==tf.getText().charAt(i)){clicked = false;break;}if (clicked == true){tf.setText(tf.getText()+".");clear=true;}}//bSqrtif(temp == bSqrt){tf.setText( Math.sqrt( Double.parseDouble(tf.getText())) + "" );clear=false;}//bPingFangif(temp == bPingFang ){tf.setText( Math.pow( Double.parseDouble(tf.getText() ), 2 ) + "" );clear=false;}//倒数if(temp == bDaoShu){tf.setText( 1 / Double.parseDouble(tf.getText()) + "" );clear =false;}//Sinif(temp == bSin){tf.setText( Math.sin( Math.toRadians( Double.parseDouble( tf.getText() ) ) ) + "" );clear =false;}//Cosif(temp == bCos){tf.setText( Math.cos( Math.toRadians( Double.parseDouble( tf.getText() ) ) ) + "" );clear =false;}//tanif(temp == bTan){tf.setText( Math.tan( Math.toRadians( Double.parseDouble( tf.getText() ) ) ) + "");clear =false;}//加法运算if (temp==bPlus){previous=Double.parseDouble(tf.getText());fuhao="+";clear=false;//System.out.print("+");}//减法运算if (temp == bMinus){previous=Double.parseDouble(tf.getText());fuhao="-";clear=false;}//乘法运算if (temp == bMulti){previous=Double.parseDouble(tf.getText());fuhao="*";clear=false;}//除法运算bDivif (temp == bDiv){previous=Double.parseDouble(tf.getText());fuhao="/";clear=false;}//求余数if(temp == bPercent){previous=Double.parseDouble( tf.getText());fuhao="%";clear=false;}//等于if (temp==bEqual){try{next = Double.parseDouble(tf.getText());tf.setText("");if (fuhao =="+")tf.setText(previous + next +"");if(fuhao == "-")tf.setText(previous - next +"");if(fuhao == "*")tf.setText(previous * next +"");if(fuhao == "/")tf.setText(previous / next +"");if(fuhao == "%")tf.setText(previous % next +"");clear=false;}catch(Exception ex){tf.setText("请检查输入的数据:" + ex.getMessage()) ;clear=false;}}}public void mouseEntered(MouseEvent e){Object temp= e.getSource();if(temp==bPingFang){//System.out.println( bBack.getBackground() );bPingFang.setBackground(cMoveIn);}if(temp==bSqrt){bSqrt.setBackground(cMoveIn);}if(temp==bPercent){bPercent.setBackground(cMoveIn);}if(temp==bC){bC.setBackground(cMoveIn);}if(temp==bBack){bBack.setBackground(cMoveIn);}if(temp==b7){b7.setBackground(cMoveIn);}if(temp==b8){b8.setBackground(cMoveIn);}if(temp==b9){b9.setBackground(cMoveIn);}if(temp==bDiv){bDiv.setBackground(cMoveIn);}if(temp==bDaoShu){bDaoShu.setBackground(cMoveIn); }if(temp==b4){b4.setBackground(cMoveIn);}if(temp==b5){b5.setBackground(cMoveIn);}if(temp==b6){b6.setBackground(cMoveIn);}if(temp==bMulti){bMulti.setBackground(cMoveIn); }if(temp==bSin){bSin.setBackground(cMoveIn);}if(temp==b1){b1.setBackground(cMoveIn);}if(temp==b2){b2.setBackground(cMoveIn);}if(temp==b3){b3.setBackground(cMoveIn);}if(temp==bMinus){bMinus.setBackground(cMoveIn); }if(temp==bCos){bCos.setBackground(cMoveIn);}if(temp==b0){b0.setBackground(cMoveIn);}if(temp==bDot){bDot.setBackground(cMoveIn);}if(temp==bEqual){bEqual.setBackground(cMoveIn);}if(temp==bPlus){bPlus.setBackground(cMoveIn);}if(temp==bTan){bTan.setBackground(cMoveIn);}}public void mouseExited(MouseEvent e) {Object temp= e.getSource();if(temp==bPingFang){//System.out.println( bBack.getBackground() );bPingFang.setBackground(cMoveOut);}if(temp==bSqrt){bSqrt.setBackground(cMoveOut);}if(temp==bPercent){bPercent.setBackground(cMoveOut);}if(temp==bC){bC.setBackground(cMoveOut);}if(temp==bBack){bBack.setBackground(cMoveOut);}if(temp==b7){b7.setBackground(cMoveOut);}if(temp==b8){b8.setBackground(cMoveOut);}if(temp==b9){b9.setBackground(cMoveOut);}if(temp==bDiv){bDiv.setBackground(cMoveOut);}if(temp==bDaoShu){bDaoShu.setBackground(cMoveOut); }if(temp==b4){b4.setBackground(cMoveOut);}if(temp==b5){b5.setBackground(cMoveOut);}if(temp==b6){b6.setBackground(cMoveOut);}if(temp==bMulti){bMulti.setBackground(cMoveOut); }if(temp==bSin){bSin.setBackground(cMoveOut);}if(temp==b1){b1.setBackground(cMoveOut);}if(temp==b2){b2.setBackground(cMoveOut);}if(temp==b3){b3.setBackground(cMoveOut);}if(temp==bMinus){bMinus.setBackground(cMoveOut); }if(temp==bCos){bCos.setBackground(cMoveOut);}if(temp==b0){b0.setBackground(cMoveOut);}if(temp==bDot){bDot.setBackground(cMoveOut);}if(temp==bEqual){bEqual.setBackground(cMoveOut);}if(temp==bPlus){bPlus.setBackground(cMoveOut);}if(temp==bTan){bTan.setBackground(cMoveOut);}}public static void main(String args[]){Calc frame=new Calc();frame.display();}public void windowClosing(WindowEvent e){System.exit(0);}public void mouseClicked(MouseEvent arg0) {}public void mousePressed(MouseEvent arg0) {}public void mouseReleased(MouseEvent arg0) { }}。
8086汇编语言实现简易计算器
8086汇编语⾔实现简易计算器8086汇编语⾔实现简易计算器本周看了⼀个很有意思的知识。
C语⾔的编译器最初是怎么来的?最初应该是由汇编语⾔实现⼀个简化版C语⾔,然后使⽤简化版的C语⾔进⾏多次迭代,功能更新,从⽽出现了如今强⼤的C语⾔。
本⼈找到了⼀个古⽼的课程设计,当时学汇编时候的⼀个⼩demo分享出来。
1.概述为了更深⼊地理解计算机⼯作原理以及CPU的功能和结构,掌握汇编语⾔的使⽤。
本⽂以简易计算器程序的汇编语⾔实现为主要任务,进⾏对程序的⼀些算法和汇编语⾔语法的论述。
计算器是最简单的计算⼯具,简单计算器具有加、减、乘、除四项运算功能。
想要⽤汇编语⾔实现简单的计算器,就必须通过对数据存储,寄存器的使⽤,加减乘除相关指令以及模块的调⽤等汇编语⾔知识进⾏运⽤,以实现⼀个基本功能完善,界⾯友好,操作简便易⾏的计算器。
⽤汇编语⾔实现简单计算器还涉及到输⼊输出模块的设计,加减乘除运算的判断以及退出程序的判断的设计。
通过对各种指令的合理使⽤,设计各个功能模块。
当实现各个程序模块后,通过程序的调⽤最终实现⼀个简单的计算器。
本⽂以⽤8086汇编语⾔实现简易计算器为⽬标,对程序的算法以及结构进⾏分析和解释。
汇编语⾔的语句类型有指令语句、伪指令语句和宏指令语句。
在实现简易计算器的过程中暂不需要宏指令语句,故对此语句不进⾏介绍。
计算器的实现需要使⽤输⼊输出,DOS系统的01H,02H,09H号调⽤可以完成所需功能。
由于简易计算器对结果没有很⾼的范围要求,故对四则运算只需考虑ADD,SUB,MUL,DIV等指令。
在计算器中,输⼊的是10进制数字,⽽在汇编语⾔中读⼊字符只能⼀位⼀位的读取,故需要使⽤MUL设置⼀个循环算法,将输⼊的数字以16进制形式放⼊寄存器中,⽽输出则是使⽤MOD设置⼀个循环算法,将16进制转化为10进制后处理为ASCII码进⾏输出。
2.程序算法结构简易计算器程序的任务主要有处理输⼊数据,选择运算⽅式进⾏计算,显⽰算式结果。
用c语言编写的计算器源代码
作品:科学计算器作者:欧宗龙编写环境:vc++6.0语言:c#include"stdafx.h"#include<stdio.h>#include<windows.h>#include<windowsx.h>#include"resource.h"#include"MainDlg.h"#include<math.h>#include<string.h>#definePI3.141593BOOLA_Op=FALSE;BOOLWINAPIMain_Proc(HWNDhWnd,UINTuMsg,WPARAMwParam,LPARAMlParam) {switch(uMsg){HANDLE_MSG(hWnd,WM_INITDIALOG,Main_OnInitDialog);HANDLE_MSG(hWnd,WM_COMMAND,Main_OnCommand);HANDLE_MSG(hWnd,WM_CLOSE,Main_OnClose);}returnFALSE;}BOOLMain_OnInitDialog(HWNDhwnd,HWNDhwndFocus,LPARAMlParam){returnTRUE;}voidTrimNumber(chara[])//判断并删除小数点后无用的零{for(unsignedi=0;i<strlen(a);i++){if(a[i]=='.'){for(unsignedj=strlen(a)-1;j>=i;j--){if(a[j]=='0'){a[j]='\0';}elseif(a[j]=='.'){a[j]='\0';}elsebreak;}}}}doubleOperate(charOperator,doublen1,doublen2)//判断符号,进行相应的运算{if(Operator=='0'){}if(Operator=='+'){n2+=n1;}if(Operator=='-'){n2=n1-n2;}if(Operator=='*'){n2*=n1;}if(Operator=='/'){n2=n1/n2;}if(Operator=='^'){n2=pow(n1,n2);}return n2;}////////////////////////////////////////////////voidIntBinary(chara[],intn){if(n>1)IntBinary(a,n/2);sprintf(a,"%s%i",a,n%2);}voiddecimal(chara[],doublem){if(m>0.000001){m=m*2;sprintf(a,"%s%d",a,(long)m);decimal(a,m-(long)m);}}voidBinary(chara[],doubleNum){charDecP[256]="";doublex,y;double*iptr=&y;x=modf(Num,iptr);decimal(DecP,x);IntBinary(a,(int)y);strcat(a,".");strcat(a,DecP);}////////////////////////////////////voidMain_OnCommand(HWNDhwnd,intid,HWNDhwndCtl,UINTcodeNotify) {staticDELTIMES=0;staticcharstr[256];staticcharOperator='0';staticdoubleRNum[3];switch(id){caseIDC_BUTTONN1://数字1{if(A_Op){SetDlgItemText(hwnd,IDC_EDIT,NULL);}GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));strcat(str,"1");SetDlgItemText(hwnd,IDC_EDIT,str);RNum[1]=atof(str);A_Op=FALSE;}break;caseIDC_BUTTONN2://数字2{if(A_Op){SetDlgItemText(hwnd,IDC_EDIT,NULL);}GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));strcat(str,"2");SetDlgItemText(hwnd,IDC_EDIT,str);RNum[1]=atof(str);A_Op=FALSE;}break;caseIDC_BUTTONN3://数字3{if(A_Op){SetDlgItemText(hwnd,IDC_EDIT,NULL);}GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));strcat(str,"3");SetDlgItemText(hwnd,IDC_EDIT,str);RNum[1]=atof(str);A_Op=FALSE;}break;caseIDC_BUTTONN4://数字4{if(A_Op){SetDlgItemText(hwnd,IDC_EDIT,NULL);}GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));strcat(str,"4");SetDlgItemText(hwnd,IDC_EDIT,str);RNum[1]=atof(str);A_Op=FALSE;}break;caseIDC_BUTTONN5://数字5{if(A_Op){SetDlgItemText(hwnd,IDC_EDIT,NULL);}GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));strcat(str,"5");SetDlgItemText(hwnd,IDC_EDIT,str);RNum[1]=atof(str);A_Op=FALSE;}break;caseIDC_BUTTONN6://数字6{if(A_Op){SetDlgItemText(hwnd,IDC_EDIT,NULL);}GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));strcat(str,"6");SetDlgItemText(hwnd,IDC_EDIT,str);RNum[1]=atof(str);A_Op=FALSE;}break;caseIDC_BUTTONN7://数字7{if(A_Op){SetDlgItemText(hwnd,IDC_EDIT,NULL);}GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));strcat(str,"7");SetDlgItemText(hwnd,IDC_EDIT,str);RNum[1]=atof(str);A_Op=FALSE;}break;caseIDC_BUTTONN8://数字8{if(A_Op){SetDlgItemText(hwnd,IDC_EDIT,NULL);}GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));strcat(str,"8");SetDlgItemText(hwnd,IDC_EDIT,str);RNum[1]=atof(str);A_Op=FALSE;}break;caseIDC_BUTTONN9://数字9{if(A_Op){SetDlgItemText(hwnd,IDC_EDIT,NULL);}GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));strcat(str,"9");SetDlgItemText(hwnd,IDC_EDIT,str);RNum[1]=atof(str);A_Op=FALSE;}break;caseIDC_BUTTONN0://数字0{if(A_Op){SetDlgItemText(hwnd,IDC_EDIT,NULL);}GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));strcat(str,"0");SetDlgItemText(hwnd,IDC_EDIT,str);RNum[1]=atof(str);A_Op=FALSE;}break;case{if(A_Op){SetDlgItemText(hwnd,IDC_EDIT,NULL);}GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));if(DELTIMES==0){strcat(str,".");}DELTIMES++;SetDlgItemText(hwnd,IDC_EDIT,str);A_Op=FALSE;}break;caseIDC_BUTTONADD://加法运算{RNum[1]=atof(str);RNum[0]=RNum[1];RNum[1]=RNum[2];RNum[2]=Operate(Operator,RNum[1],RNum[0]);sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);Operator='+';DELTIMES=0;A_Op=TRUE;}break;caseIDC_BUTTONSUB://减法运算{RNum[1]=atof(str);RNum[0]=RNum[1];RNum[1]=RNum[2];RNum[2]=Operate(Operator,RNum[1],RNum[0]);sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);DELTIMES=0;A_Op=TRUE;Operator='-';}break;caseIDC_BUTTONMUL://乘法运算{RNum[1]=atof(str);RNum[0]=RNum[1];RNum[1]=RNum[2];RNum[2]=Operate(Operator,RNum[1],RNum[0]);sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);Operator='*';DELTIMES=0;A_Op=TRUE;}break;caseIDC_BUTTONDIV://除法运算{RNum[1]=atof(str);RNum[0]=RNum[1];RNum[1]=RNum[2];RNum[2]=Operate(Operator,RNum[1],RNum[0]);sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);Operator='/';DELTIMES=0;A_Op=TRUE;}break;case{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[1]=atof(str);RNum[0]=RNum[1];RNum[1]=RNum[2];RNum[2]=Operate(Operator,RNum[1],RNum[0]);sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);Operator='^';DELTIMES=0;}break;caseIDC_BUTTONPI://圆周率PI,弧度{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));if(atof(str)!=0){RNum[2]=atof(str)*PI;sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);}else{sprintf(str,"%f",PI);SetDlgItemText(hwnd,IDC_EDIT,str);}A_Op=TRUE;}break;caseIDC_BUTTONSQRT://开根号{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[2]=sqrt(atof(str));sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);A_Op=TRUE;}break;caseIDC_BUTTONSIN://三角函数sin函数{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[2]=sin(atof(str));sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);A_Op=TRUE;}break;case{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[2]=cos(atof(str));sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);A_Op=TRUE;}break;case{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[2]=tan(atof(str));sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);A_Op=TRUE;}break;caseIDC_BUTTONSQ://平方{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[2]=atof(str)*atof(str);sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);A_Op=TRUE;}break;case{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[2]=atof(str)*atof(str)*atof(str);sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);A_Op=TRUE;}break;case{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[2]=exp(atof(str));sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);A_Op=TRUE;}break;case{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[2]=pow(10,atof(str));sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);A_Op=TRUE;}break;caseIDC_BUTTONLN://lnx{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[2]=log(atof(str));sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);A_Op=TRUE;}break;caseIDC_BUTTONLOG10://log10{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[2]=log10(atof(str));sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);A_Op=TRUE;}break;caseIDC_BUTTONBINARY://十进制转换为二进制{chara[256]="";GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[2]=atof(str);Binary(a,RNum[2]);strcpy(str,a);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);A_Op=TRUE;}break;case{DELTIMES=0;Operator='0';RNum[0]=RNum[1]=RNum[2]=0;memset(str,0,sizeof(str));SetDlgItemText(hwnd,IDC_EDIT,NULL);A_Op=FALSE;}break;case{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));inti=strlen(str);str[i-1]='\0';SetDlgItemText(hwnd,IDC_EDIT,str);}break;case{GetDlgItemText(hwnd,IDC_EDIT,str,sizeof(str));RNum[1]=atof(str);RNum[0]=RNum[1];RNum[1]=RNum[2];RNum[2]=Operate(Operator,RNum[1],RNum[0]);sprintf(str,"%f",RNum[2]);TrimNumber(str);SetDlgItemText(hwnd,IDC_EDIT,str);Operator='0';DELTIMES=0;}break;default:break;}}voidMain_OnClose(HWNDhwnd){EndDialog(hwnd,0);}本人拙作,如有不足之处请谅解。
北邮JAVA第三次作业科学计算器(附源代码)
北邮JAVA第三次作业科学计算器(附源代码)JAVA第三次作业选题:科学计算器一、设计思路(1)布局1.数字键盘有0~9和小数点2.符号键盘有+某÷,幂,开方,lg,ln3.功能按键有清屏(AC)和删除(Del)(2)计算器流程状态转移图设定的标志位和变量有:booleannumflag=fale;//标志:是否输入数字booleanopflag=fale;//标志:是否输入运算操作符Stringt某t=null;//显示到文本框里的字符串Stringnum_t某t=\;//当前操作数的字符串形式Stringop=\;//存放输入的运算操作符num1=0;//第一个操作数num2=0;//第二个操作数privateBigDecimalbig_num1=newBigDecimal(Double.toString(0)); //解决浮点数的精度丢失问题privateBigDecimalbig_num2=newBigDecimal(Double.toString(0));1.关于操作数操作数为输入数字时会改变的数,有num1和num2。
按下运算符和控制字符,会设定当前下在键入的操作数,由opflag控制,fale表示正在操作num1,true表示正在操作num2。
初始状态下的操作数为num1。
按下AC会恢复操作数为初始状态num1。
按Delete、开方、lg、ln不会改变当前的操作数。
其中后三者只对num1有效。
其他的运算符和控制符都会使操作数为num2。
2.计算器的状态框图输入第一个数,比如53numflag=true(num1)opflag=falenum_t某t=”53”op=””t某t=”53”初始状态numflag=faleopflag=falenum_t某t=””op=””t某t=nullnum1,num2=0按下一个运算符,比如+numflag=faleopflag=truenum_t某t=””op=”+”t某t=”num1+”其中按下按=显示结果,恢复初始状态numflag=true(num1)opflag=falenum_t某t=””op=””t某t=”reult”“=”后,结果存输入第二个数,比如66入numflag=true(num2)opflag=truenum_t某t=”66”op=””t某t=”num1+66”num1,继续输入数字会在num1的结果上处理。
JAVA计算器源代码
计算器源代码一、计算器源代码文件名:computer1.javaimport java.awt.*;import java.awt.event.*;public class computer1 extends Frame implements ActionListener{//声明窗口类并实现动作事件接口。
Button n0,n1,n2,n3,n4,n5,n6,n7,n8,n9;//声明数字按钮Button op,os,om,od,oe,oc;//声明操作按钮TextField tfd;//声明文本框String flg,rslt;//声明标志串、结果串Panel p1,p2,p3;//声明面板int i1,i2;float flt;computer1(){super("加减乘除计算器");n0 = new Button("0");//实现各按钮n1 = new Button("1");n2 = new Button("2");n3 = new Button("3");n4 = new Button("4");n5 = new Button("5");n6 = new Button("6");n7 = new Button("7");n8 = new Button("8");n9 = new Button("9");op = new Button("加");os = new Button("减");om = new Button("乘");od = new Button("除");oe = new Button("=");oc = new Button("c");tfd = new TextField(20);//实现文本框p1=new Panel();//实现各面板p2=new Panel();p3=new Panel();setLayout(new FlowLayout());//布局设计,用于安排按钮位置p1.add(n0);//将各数字按钮放入p1中p1.add(n1);p1.add(n2);p1.add(n3);p1.add(n4);p1.add(n5);p1.add(n6);p1.add(n7);p1.add(n8);p1.add(n9);p2.add(op);//将各操作按钮放入p2、p3中p2.add(os);p2.add(om);p2.add(od);p3.add(oe);p3.add(oc);setLayout(new BorderLayout());//布局设计,用于安排面板位置add("North",tfd);add("West",p1);add("Center",p2);add("East",p3);n0.addActionListener(this);//注册监听器到各按钮n1.addActionListener(this);n2.addActionListener(this);n3.addActionListener(this);n4.addActionListener(this);n5.addActionListener(this);n6.addActionListener(this);n7.addActionListener(this);n8.addActionListener(this);n9.addActionListener(this);op.addActionListener(this);os.addActionListener(this);om.addActionListener(this);od.addActionListener(this);oe.addActionListener(this);oc.addActionListener(this);addWindowListener(new closeWin());setSize(600,100);//确定窗口的尺寸setVisible(true);}public static void main (String args[]){new computer1();}public void actionPerformed(ActionEvent e){//处理鼠标事件的方法try{//异常处理if(e.getSource()==n0)//按数字键时tfd.setText(tfd.getText()+"0");if(e.getSource()==n1)tfd.setText(tfd.getText()+"1");if(e.getSource()==n2)tfd.setText(tfd.getText()+"2");if(e.getSource()==n3)tfd.setText(tfd.getText()+"3");if(e.getSource()==n4)tfd.setText(tfd.getText()+"4");if(e.getSource()==n5)tfd.setText(tfd.getText()+"5");if(e.getSource()==n6)tfd.setText(tfd.getText()+"6");if(e.getSource()==n7)tfd.setText(tfd.getText()+"7");if(e.getSource()==n8)tfd.setText(tfd.getText()+"8");if(e.getSource()==n9)tfd.setText(tfd.getText()+"9");if(e.getSource()==op){//按加号键时i1 = Integer.parseInt(tfd.getText());tfd.setText("");flg = "op";}if(e.getSource()==os){//按减号键时i1 = Integer.parseInt(tfd.getText());tfd.setText("");flg = "os";}if(e.getSource()==om){//按乘号键时i1 = Integer.parseInt(tfd.getText());tfd.setText("");flg = "om";}if(e.getSource()==od){//按除号键时i1 = Integer.parseInt(tfd.getText());tfd.setText("");flg = "od";}if(e.getSource()==oe){//按等号键时i2 = Integer.parseInt(tfd.getText());if(flg=="op"){rslt=Integer.toString(i1+i2);}if(flg=="os"){rslt=Integer.toString(i1-i2);}if(flg=="om"){rslt=Integer.toString(i1*i2);}if(flg=="od"){//除法需做小数处理flt=((float)i1)/((float)i2);rslt=Float.toString(flt);}tfd.setText(rslt);}if(e.getSource()==oc){//按清除键时tfd.setText("");flg = "";}}catch(Exception ex){}//扑捉到异常,但不进行处理}}class closeWin extends WindowAdapter{ //关闭窗口public void windowClosing(WindowEvent e){Frame frm=(Frame)(e.getSource());frm.dispose();System.exit(0);}}二、计算器界面三、修改后计算器界面。
计算器Android版源码
100%运行通过这是自己编写的,通过eclipse运行简易计算器Android版(源码)这是自己整理得,欢迎大家下载说明:每完成一次计算后,需先clear 清除记忆,当然也可以连算XML布局<?xml version="1.0"encoding="utf-8"?><LinearLayout xmlns:android=android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"><TableLayoutandroid:id="@+id/tableLayout1"android:layout_width="match_parent"android:layout_height="wrap_content"android:collapseColumns="4"><TableRowandroid:id="@+id/tableRow_et"android:layout_width="fill_parent"android:layout_height="fill_parent"><EditTextandroid:id="@+id/et"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_span="4"android:focusable="false"android:singleLine="true"android:inputType="text"android:gravity="right"></EditText></TableRow><TableRowandroid:id="@+id/tableRow1"android:layout_width="fill_parent"android:layout_height="fill_parent"><Buttonandroid:id="@+id/bt_7"android:layout_width="40px"android:layout_height="40px"android:text="7"/><Buttonandroid:id="@+id/bt_8"android:layout_width="40px"android:layout_height="40px"android:text="8"/><Buttonandroid:id="@+id/bt_9"android:layout_width="40px"android:layout_height="40px"android:text="9"/><Buttonandroid:id="@+id/bt_back"android:layout_width="40px"android:layout_height="40px"android:text="back"/></TableRow><TableRowandroid:id="@+id/tableRow2"android:layout_width="fill_parent"android:layout_height="fill_parent"> <Buttonandroid:id="@+id/bt_4"android:layout_width="40px"android:layout_height="40px"android:text="4"/><Buttonandroid:id="@+id/bt_5"android:layout_width="40px"android:layout_height="40px"android:text="5"/><Buttonandroid:id="@+id/bt_6"android:layout_width="40px"android:layout_height="40px"android:text="6"/><Buttonandroid:id="@+id/bt_divide"android:layout_width="40px"android:layout_height="40px"android:text="/"/></TableRow><TableRowandroid:id="@+id/tableRow3"android:layout_width="fill_parent"android:layout_height="fill_parent"> <Buttonandroid:id="@+id/bt_1"android:layout_width="40px"android:layout_height="40px"android:text="1"/><Buttonandroid:id="@+id/bt_2"android:layout_width="40px"android:layout_height="40px"android:text="2"/><Buttonandroid:id="@+id/bt_3"android:layout_width="40px"android:layout_height="40px"android:text="3"/><Buttonandroid:id="@+id/bt_multiply"android:layout_width="40px"android:layout_height="40px"android:text="*"/></TableRow><TableRowandroid:id="@+id/tableRow4"android:layout_width="fill_parent"android:layout_height="fill_parent"> <Buttonandroid:id="@+id/bt_0"android:layout_width="50px"android:layout_height="40px"android:text="0"/><Buttonandroid:id="@+id/bt_point"android:layout_width="50px"android:layout_height="40px"android:text="."/><Buttonandroid:id="@+id/bt_add"android:layout_width="50px"android:layout_height="40px"android:text="+"/><Buttonandroid:id="@+id/bt_sub"android:layout_width="50px"android:layout_height="40px"android:text="-"/></TableRow><TableRowandroid:id="@+id/tableRow4"android:layout_width="fill_parent"android:layout_height="fill_parent"><Buttonandroid:id="@+id/bt_equal"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_span="3"android:text="="/><Buttonandroid:id="@+id/bt_clear"android:layout_width="50px"android:layout_height="40px"android:text="clear"/></TableRow></TableLayout></LinearLayout>源代码:package android.sdk;importimportimportimportimportimportimportpublic class Android_calculatorActivity extends Activity {private Button bt_1;private Button bt_2;private Button bt_3;private Button bt_4;private Button bt_5;private Button bt_6;private Button bt_7;private Button bt_8;private Button bt_9;private Button bt_0;private Button bt_add;private Button bt_sub; // 减private Button bt_multiply; // 乘private Button bt_divide; // 除private Button bt_back;private Button bt_equal; // 等于private Button bt_point; // 点private Button bt_clear; // 清除private EditText et_play; // 显示private String str_oper = "+"; // 运算符private StringBuffer str_display = new StringBuffer();; // 显示private String str_result; // 结果显示private double num1;private double num2;private boolean flag = true; // 小数点个数开关控制;private boolean b_sub, b_mul, b_div; // 运算符开关控制@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(yout.main);bt_0 = (Button) findViewById(R.id.bt_0);bt_1 = (Button) findViewById(R.id.bt_1);bt_2 = (Button) findViewById(R.id.bt_2);bt_3 = (Button) findViewById(R.id.bt_3);bt_4 = (Button) findViewById(R.id.bt_4);bt_5 = (Button) findViewById(R.id.bt_5);bt_6 = (Button) findViewById(R.id.bt_6);bt_7 = (Button) findViewById(R.id.bt_7);bt_8 = (Button) findViewById(R.id.bt_8);bt_9 = (Button) findViewById(R.id.bt_9);bt_add = (Button) findViewById(R.id.bt_add);bt_sub = (Button) findViewById(R.id.bt_sub);bt_multiply = (Button) findViewById(R.id.bt_multiply);bt_divide = (Button) findViewById(R.id.bt_divide);bt_back = (Button) findViewById(R.id.bt_back);bt_equal = (Button) findViewById(R.id.bt_equal);bt_point = (Button) findViewById(R.id.bt_point);bt_clear = (Button) findViewById(R.id.bt_clear);et_play = (EditText) findViewById(R.id.et);et_play.setText("0.0");bt_0.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){}});bt_1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v){str_display.append("1");et_play.setText(str_display.toString());}});bt_2.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v){str_display.append("2");et_play.setText(str_display.toString());}});bt_3.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v){str_display.append("3");et_play.setText(str_display.toString());}});bt_4.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v){str_display.append("4");et_play.setText(str_display.toString());}});bt_5.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v){}});bt_6.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){str_display.append("6");et_play.setText(str_display.toString());}});bt_7.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){str_display.append("7");et_play.setText(str_display.toString());}});bt_8.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){str_display.append("8");et_play.setText(str_display.toString());}});bt_9.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){str_display.append("9");et_play.setText(str_display.toString());}});bt_point.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v){if (flag){str_display.append(".");flag = false;}}});bt_back.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){if (str_display.length() != 0){str_display.deleteCharAt(str_display.length() - 1);et_play.setText(str_display.toString());}}});bt_add.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){str_oper = "+";if (!(str_display.toString() == "")){num1 += Double.parseDouble(str_display.toString());str_display = new StringBuffer("");}if (!(str_result == null)){num1 = Double.parseDouble(str_result);str_result = null;}et_play.setText(String.valueOf(num1));flag = true;}});bt_sub.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){str_oper = "-";if (!b_sub && !(str_display.toString() == "")){num1 = Double.parseDouble(str_display.toString());et_play.setText(String.valueOf(num1));str_display = new StringBuffer("");b_sub = true;} else{if (!(str_display.toString() == "")){num1 -= Double.parseDouble(str_display.toString());str_display = new StringBuffer("");}if (!(str_result == null)){num1 = Double.parseDouble(str_result);str_result = null;}et_play.setText(String.valueOf(num1));}flag = true;}});bt_multiply.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){str_oper = "*";if (!b_mul && !(str_display.toString() == "")){num1 = Double.parseDouble(str_display.toString());et_play.setText(String.valueOf(num1));str_display = new StringBuffer("");b_mul = true;} else{if (!(str_display.toString() == "")){num1 *= Double.parseDouble(str_display.toString());str_display = new StringBuffer("");}if (!(str_result == null)){num1 = Double.parseDouble(str_result);str_result = null;}et_play.setText(String.valueOf(num1));}flag = true;}});bt_divide.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){str_oper = "/";if (!b_div && !(str_display.toString() == "")){num1 = Double.parseDouble(str_display.toString());et_play.setText(String.valueOf(num1));str_display = new StringBuffer("");b_div = true;} else{if (!(str_display.toString() == "")){if (Double.parseDouble(str_display.toString()) == 0){Toast.makeText(Android_calculatorActivity.this,"除数不能为0!", Toast.LENGTH_LONG).show();} else{num1 /= Double.parseDouble(str_display.toString());str_display = new StringBuffer("");}}if (!(str_result == null)){num1 = Double.parseDouble(str_result);str_result = null;}et_play.setText(String.valueOf(num1));}flag = true;}});bt_clear.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){str_oper = "+";str_display = new StringBuffer("");str_result = null;num1 = 0;num2 = 0;flag = true;b_sub = false;b_mul = false;b_div = false;et_play.setText("0.0");}});bt_equal.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){if (str_oper.equals("+")){num2 = Double.parseDouble(str_display.toString());str_result = String.valueOf((num1 + num2));et_play.setText(str_result);str_display = new StringBuffer("");}if (str_oper.equals("-")){num2 = Double.parseDouble(str_display.toString());str_result = String.valueOf((num1 - num2));et_play.setText(str_result);str_display = new StringBuffer("");}if (str_oper.equals("*")){num2 = Double.parseDouble(str_display.toString());str_result = String.valueOf((num1 * num2));et_play.setText(str_result);str_display = new StringBuffer("");}if (str_oper.equals("/")){num2 = Double.parseDouble(str_display.toString());if (!(num2 == 0)){str_result = String.valueOf((num1 / num2));et_play.setText(str_result);} else{Toast.makeText(Android_calculatorActivity.this,"除数不能为0!", Toast.LENGTH_LONG).show();}str_display = new StringBuffer("");}}});}。
科学计算器源代码
科学计算器源代码以下是一个简单的科学计算器的源代码,包含了基本的数学运算、三角函数、指数和对数运算、阶乘和组合运算等功能。
```#include <iostream>#include <cmath>using namespace std;//计算器类class ScientificCalculatorpublic://基本运算double add(double a, double b)return a + b;}double subtract(double a, double b)return a - b;}double multiply(double a, double b)return a * b;}double divide(double a, double b)if (b != 0)return a / b;} elsecout << "Error: Division by zero!" << endl; return 0;}}//三角函数运算double sine(double angle)return sin(angle);}double cosine(double angle)return cos(angle);}double tangent(double angle)return tan(angle);}//指数和对数运算double exponent(double x) return exp(x);}double logarithm(double x) return log10(x);}//阶乘运算double factorial(int n)if (n == 0)return 1;} elseint result = 1;for (int i = 1; i <= n; i++) result *= i;}return result;}}//组合运算if (n >= r)return factorial(n) / (factorial(r) * factorial(n - r));} elsecout << "Error: Invalid input!" << endl;return 0;}}};//主函数int maiScientificCalculator calculator;//进行一些简单的计算cout << "Addition: " << calculator.add(5, 3) << endl;cout << "Subtraction: " << calculator.subtract(5, 3) << endl;cout << "Multiplication: " << calculator.multiply(5, 3) << endl;cout << "Division: " << calculator.divide(5, 3) << endl;//三角函数运算cout << "Sine: " << calculator.sine(0.5) << endl;cout << "Cosine: " << calculator.cosine(0.5) << endl;cout << "Tangent: " << calculator.tangent(0.5) << endl;//指数和对数运算cout << "Exponent: " << calculator.exponent(2) << endl;cout << "Logarithm: " << calculator.logarithm(100) << endl;//阶乘和组合运算cout << "Factorial: " << calculator.factorial(5) << endl;return 0;```这个科学计算器的源代码使用了面向对象的编程思想,将不同的数学运算封装成类的成员函数。
C语言计算器程序源代码
C语⾔计算器程序源代码//strcmp(s1,s2) 当s1⼤于s2时,返回1 ,s1⼩于s2时,返回-1,相等时,返回0 #include "stdio.h"#include "ctype.h"#include "string.h"#include "math.h"#define MAX 256#define STACK_SIZE 128#define WORD_LEN 8#define POP 1#define PUSH 0#define ERR -1#define END 2#define OPER 0#define NUM 1#define WORD 2#define ADD 1#define SUB 2#define MUL 3#define DIV 4#define POW 5#define FAC 6#define BRA_L 7#define BRA_R 8#define SIN 9#define COS 10#define TAN 11#define CTG 12#define LG 13 //以10为底的常⽤对数//#define LN 14//#define LOG 15//⾏标为当前操作符代号,列标为栈顶元素代号//2表⽰计算结束,0表⽰当前操作符进栈,1表⽰栈顶操作符出栈// \0 + - * / ^ ! ( ) sin cos tg ctg lgint Priority[14][14]={2, 1, 1, 1, 1, 1, 1,-1,-1, 1, 1, 1, 1, 1, /* \0 */0, 1, 1, 1, 1, 1, 1, 0,-1, 1, 1, 1, 1, 1, /* + */0, 1, 1, 1, 1, 1, 1, 0,-1, 1, 1, 1, 1, 1, /* - */0, 0, 0, 1, 1, 1, 1, 0,-1, 1, 1, 1, 1, 1, /* * */0, 0, 0, 1, 1, 1, 1, 0,-1, 1, 1, 1, 1, 1, /* / */0, 0, 0, 1, 1, 1, 1, 0,-1, 0, 0, 0, 0, 0, /* ^ */0, 0, 0, 0, 0, 0, 1, 0,-1, 0, 0, 0, 0, 0, /* ! */0, 0, 0, 0, 0, 0,-1, 0,-1, 0, 0, 0, 0, 0, /* ( */-1,1, 1, 1, 1, 1, 1, 1,-1, 1, 1, 1, 1, 1, /* ) */0, 0, 0, 0, 0, 0,-1, 0,-1, 0, 0, 0, 0, 0, /* sin */0, 0, 0, 0, 0, 0,-1, 0,-1, 0, 0, 0, 0, 0, /* cos */0, 0, 0, 0, 0, 0,-1, 0,-1, 0, 0, 0, 0, 0, /* tg */0, 0, 0, 0, 0, 0,-1, 0,-1, 0, 0, 0, 0, 0, /* ctg */0, 0, 0, 0, 0, 0,-1, 0,-1, 0, 0, 0, 0, 0}; /* lg */char KeyWord[36][WORD_LEN+1]={"sin", //前12个为函数,多余的⽤于扩展"cos","tan","tg","ctg","lg","","","","","","","","","","help", // 后⾯为命令,多余的为扩展"version","set","digit", //精度,⼩数点后的位数"color","radian", //弧度"degree", //⾓度"file","clr","clear","window", //窗⼝模式"fullscr", //全屏模式"","","","","","","","",""};int OperCode(char c){int code;switch(c){case '\0':code=0;break;case '+':code=1;break;case '-':code=2;break;case '*':code=3;break;case '/':code=4;break;case '^':code=5;break;case '!':code=6;break;case '(':code=7;break;case ')':code=8;break;case 's':code=9;break; //sincase 'c':code=10;break; //coscase 't':code=11;break; //tgcase 'C':code=12;break; //ctgcase 'l':code=13;break; //log default:code=-1;break;};return code;}int WordCode(char* word){int i;for(i=0;i<25;i++)if(strcmp(KeyWord[i],word)==0) break;if(i>=25)return -1;else}void help(){printf("显⽰帮助信息!\n");return;}void version(){printf("显⽰版本信息!\n");return;}void Err(int errcode,int position,char *p){printf("\n ERR:%d Position:%d %s",errcode,position,p); return;}double long factorial(int i){if(i==1 || i==0)return(1.0);elsereturn(i*factorial(i-1));}main(){char Expression[MAX+1];int Operator[STACK_SIZE];int OperStackTop;double long Number[STACK_SIZE];int NumStackTop;double long NumList[STACK_SIZE];int NumCursor,NumListSize;int OperList[STACK_SIZE];int OperCursor,OperListSize;int WordList[STACK_SIZE];int WordCursor,WordListSize;int Index[MAX+1];int IndexCursor,IndexSize;char Word[WORD_LEN+1];double long num,num1,num2,weight,tempnum;int Oper;int isDecimal,isErr,isNumber,isEnd;char CurrentOper;int i,j,k,m,n;char ch;num=0.0;num2=0.0;tempnum=0.0;Oper=-1;while(1){for(i=0;i<=MAX;i++) //表达式初始化,中间表索引初始化{Expression[i]='\0';Index[i]=-1;}for(i=0;i{Operator[i]='0';Number[i]=0.0;NumList[i]=0.0;OperList[i]=-1;WordList[i]=-1;}NumStackTop=-1; //栈顶指针初始化OperStackTop=0; //操作符栈压⼊\0Operator[OperStackTop]=OperCode('\0');NumCursor=0; //各种中间表指针初始化,各种中间表的长度初始化NumListSize=0;OperCursor=0;OperListSize=0; //操作符表中先写⼊第⼀个操作符'\0'WordCursor=0;WordListSize=0;IndexCursor=0;IndexSize=0;// Index[0]=OPER;printf("Cal>"); //初始化完成,输出提⽰符i=0;while((ch=getchar())!='\n'){if(i>MAX) /*输⼊超长,则出错*/{Err(0,i,"输⼊的表达式长度超过规定值!\n");isErr=1;break;}if(isupper(ch))ch=tolower(ch);Expression[i]=ch;i++;}if(isErr==1){isErr=0;continue;}if(strlen(Expression)==0) //直接回车continue;if(strcmp("end",Expression)==0 ||strcmp("exit",Expression)==0 || strcmp("quit",Expr ession)==0)break;//⼀下代码为编译预处理,主要处理负号,并检查括号是否配对k=0;for(i=0;Expression[i]!='\0';i++){if((i==0&&Expression[i]=='-') || (i>0&&Expression[i]=='-'&&Expression[i-1]=='(')){for(j=strlen(Expression);j>i;j--)Expression[j]=Expression[j-1];Expression[i]='0';}if(Expression[i]=='(') //检查括号k++;if(Expression[i]==')')k--;}if(k>0) //如果括号不配对{Err(1,-1,"缺少右括号 )\n");continue;}if(k<0){Err(1,-1,"缺少左括号 (\n");continue;}//编译预处理结束i=0; //词法分析while(1){if(Expression[i]=='\0'){OperList[OperListSize]=OperCode(Expression[i]);OperListSize++;Index[IndexSize]=OPER;IndexSize++;// printf("IndexSize=%d,Index[IndexSize]=%d,Expression[i]=%c\n",IndexSize,Index[IndexSize],Expression[i]); break;}isDecimal=0;isNumber=0;while(isdigit(Expression[i])||Expression[i]=='.') //读取数字{isNumber=1;if(Expression[i]=='.'){if((i<(MAX-1) && !isdigit(Expression[i+1])) || (i+1)==MAX) //不正确的⼩数点位置{Err(2,i,"⼩数点位置不正确!\n");isErr=1;isNumber=0;i++;break;}isDecimal=1;weight=0.1;i++;continue;}if(isDecimal==0)num=num*10.0+(double long)(Expression[i]-'0');{num=num+(double long)(Expression[i]-'0')*weight;weight=weight*0.1;}i++;} //数字读完if(isErr==1)break;if(isNumber==1) //如果刚才成功读取了数字,则数字⼊栈{NumList[NumListSize]=num;NumListSize++;isNumber=0;num=0.0;Index[IndexSize]=NUM;// printf("IndexSize=%d,Index[IndexSize]=%d\n",IndexSize,Index[IndexSize]); IndexSize++;}for(k=0;k<=WORD_LEN;k++)Word[k]='\0';j=0;while(isalpha(Expression[i])){if(j>=WORD_LEN) //超过长度仍然未匹配,则出错{Err(3,i,"单词长度超过规定值/未定义的单词:");printf("%s\n",Word);isErr=1;break;}Word[j]=Expression[i];j++;// printf("WORD:%s\n",Word);if(WordCode(Word)==-1) //匹配不成功{if(!isalpha(Expression[i+1]))//匹配不成功,但是下⼀个字符已经不是字母,{Err(4,i,"未定义的单词:"); //则出错,并跳出循环printf("%s\n",Word);isErr=1;break;} //匹配不成功且还能继续读取字符,则继续读取下⼀个字母i++;continue;}else //匹配成功,则单词⼊表,读取下⼀个字符{switch(WordCode(Word)){case 0:ch='s';break;case 1:ch='c';break;case 2:case 3:ch='t';break;case 4:ch='C';case 5:ch='l';break;default:ch='\0';WordList[WordListSize]=WordCode(Word);WordListSize++;Index[IndexSize]=WORD;IndexSize++;break;};if(ch!='\0'){OperList[OperListSize]=OperCode(ch);OperListSize++;Index[IndexSize]=OPER;IndexSize++;}i++;break;}} //单词读完if(isErr==1)break;if(Expression[i]==' ')i++;if(!isdigit(Expression[i]) && !isalpha(Expression[i]) && Expression[i]!='\0') {if(OperCode(Expression[i])==-1){isErr=1;Err(5,i,"未定义的操作符:");printf("%c\n",Expression[i]);break;}else{OperList[OperListSize]=OperCode(Expression[i]);OperListSize++;Index[IndexSize]=OPER;IndexSize++;i++;}} //操作符读完if(isErr==1)break;} //词法分析结束if(isErr==1){isErr=0;continue;}/* for(k=0;kprintf("NumList[%d]=%f\n",k,NumList[k]);for(k=0;kprintf("OperList[%d]=%d\n",k,OperList[k]);for(k=0;kprintf("WordList[%d]=%d\n",k,WordList[k]);for(k=0;kprintf("Index[%d]=%d\n",k,Index[k]);printf("\n\n IndexCursor=%d IndexSize=%d,\n",IndexCursor,IndexSize);printf("OperStacktop=%d,Operator[OperStackTop]=%d,NumStackTop=%d\n",OperStackTop,Operator[OperStackTop],NumStackTop); // continue;*/isEnd=0;IndexCursor=0;while(1){if(Index[IndexCursor]==NUM){if(NumCursor<0 || NumListSize<0){Err(10,-1,"索引列表与操作数列表信息不匹配\n");isErr=1;break;}NumStackTop++;Number[NumStackTop]=NumList[NumCursor];NumCursor++;IndexCursor++;continue;} //数字处理if(Index[IndexCursor]==OPER){m=OperList[OperCursor];n=Operator[OperStackTop];switch(Priority[m][n]){case ERR:Err(20,IndexCursor,"不可预见的错误!\n");isErr=1;break;case PUSH:OperStackTop++;Operator[OperStackTop]=m;OperCursor++;IndexCursor++;break;case END:isEnd=1;break;case POP:Oper=Operator[OperStackTop];OperStackTop--;switch(Oper){case BRA_L: IndexCursor++;OperCursor++;break;case ADD:if(NumStackTop>=1){num2=Number[NumStackTop];NumStackTop--;num1=Number[NumStackTop];NumStackTop--;tempnum=num1+num2;NumStackTop++;Number[NumStackTop]=tempnum;num1=0.0;num2=0.0;tempnum=0.0;}else{Err(11,IndexCursor,"加法运算缺少操作数!\n");isErr=1;}break;case SUB:if(NumStackTop>=1){num2=Number[NumStackTop];NumStackTop--;num1=Number[NumStackTop];NumStackTop--;tempnum=num1-num2;NumStackTop++;Number[NumStackTop]=tempnum;num1=0.0;num2=0.0;tempnum=0.0;}else{Err(12,IndexCursor,"减法运算缺少操作数!\n");isErr=1;}break;case MUL:if(NumStackTop>=1){num2=Number[NumStackTop];NumStackTop--;num1=Number[NumStackTop];NumStackTop--;tempnum=num1*num2;NumStackTop++;Number[NumStackTop]=tempnum;num1=0.0;num2=0.0;tempnum=0.0;}else{Err(13,IndexCursor,"乘法运算缺少操作数!\n");isErr=1;}break;case DIV:if(NumStackTop>=1){num2=Number[NumStackTop];NumStackTop--;if(num2==0.0){Err(14,IndexCursor,"除数为 0 ,不能进⾏除法运算!\n"); isErr=1;break;}num1=Number[NumStackTop];NumStackTop--;tempnum=num1/num2;NumStackTop++;Number[NumStackTop]=tempnum;num1=0.0;num2=0.0;tempnum=0.0;}else{Err(15,IndexCursor,"除法运算缺少操作数!\n"); isErr=1;}break;case POW:if(NumStackTop>=1){num2=Number[NumStackTop]; NumStackTop--;num1=Number[NumStackTop]; NumStackTop--;tempnum=pow(num1,num2); NumStackTop++;Number[NumStackTop]=tempnum;num1=0.0;num2=0.0;tempnum=0.0;}else{Err(16,IndexCursor,"乘⽅运算缺少操作数!\n"); isErr=1;}break;case FAC:if(NumStackTop>=0){num2=Number[NumStackTop]; NumStackTop--;tempnum=factorial(num2); NumStackTop++;Number[NumStackTop]=tempnum;num2=0.0;tempnum=0.0;}else{Err(17,IndexCursor,"阶乘运算缺少操作数!\n"); isErr=1;}break;case SIN:if(NumStackTop>=0){num2=Number[NumStackTop]; NumStackTop--;tempnum=sin(num2);NumStackTop++;Number[NumStackTop]=tempnum;num2=0.0;tempnum=0.0;}{Err(18,IndexCursor,"正弦函数缺少参数!\n"); isErr=1;}break;case COS:if(NumStackTop>=0){num2=Number[NumStackTop]; NumStackTop--;tempnum=cos(num2);NumStackTop++;Number[NumStackTop]=tempnum;num2=0.0;tempnum=0.0;}else{Err(19,IndexCursor,"余弦函数缺少参数!\n"); isErr=1;}break;case TAN:if(NumStackTop>=0){num2=Number[NumStackTop]; NumStackTop--;tempnum=tan(num2);NumStackTop++;Number[NumStackTop]=tempnum;num2=0.0;tempnum=0.0;}else{Err(20,IndexCursor,"正切函数缺少参数!\n");isErr=1;}break;case CTG:if(NumStackTop>=0){num2=Number[NumStackTop]; NumStackTop--;tempnum=1.0/tan(num2); NumStackTop++;Number[NumStackTop]=tempnum;num2=0.0;tempnum=0.0;}else{Err(21,IndexCursor,"余切函数缺少参数!\n"); isErr=1;}break;/* case LN:if(NumStackTop>=0)num2=Number[NumStackTop]; NumStackTop--;if(num2<=0.0){Err(20,IndexCursor,"⾃然对数函数真数:"); printf(" %f ⼩于0!\n",num2);isErr=1;break;}tempnum=log(num2);NumStackTop++;Number[NumStackTop]=tempnum;num2=0.0;tempnum=0.0;}else{Err(17,IndexCursor,"⾃然对数函数缺少参数!\n"); isErr=1;}break; */case LG:if(NumStackTop>=0){num2=Number[NumStackTop]; NumStackTop--;if(num2<=0.0){Err(23,IndexCursor,"常⽤对数函数真数:"); printf(" %f ⼩于0!\n",num2);isErr=1;break;}tempnum=log10(num2);NumStackTop++;Number[NumStackTop]=tempnum;num2=0.0;tempnum=0.0;}else{Err(22,IndexCursor,"常⽤对数函数缺少参数!\n"); isErr=1;}break;default:Err(100,IndexCursor,"运算符(代码:"); printf(" %d )暂不⽀持!\n",Oper);isErr=1;break;/*#define SIN 9#define COS 10#define TAN 11#define CTG 12#define LOG 13 */}; //switch 语句结束break;};//switchif(isErr==1 || isEnd==1)break;continue;} //运算符处理if(Index[IndexCursor]==WORD){printf(" 单词尚未处理!\n");break;}//在这⾥处理单词}//核⼼计算结束if(isErr==1){isErr=0;continue;}if(NumStackTop>0){Err(1000,-1,"多余的操作数:");printf("%f!\n",Number[NumStackTop]);}elseif(NumStackTop==0 && isEnd==1){if(fabs(Number[NumStackTop])>1e20) printf(" %.20e\n",Number[NumStackTop]); elseprintf(" %f\n",Number[NumStackTop]); isEnd=0;}}//主循环结束}//主函数结束。
数独计算器源码(java语言)
/*这是本人最近做的一个数独计算器程序。
1.该程序由java编写。
2.该程序具有界面3.把以下代码复制到记事本上保存为java文件。
4.javac Test8.java ,然后再java Test8,你懂的。
5.再图形中输入已知的数字,点击确定,图形中就会出现解。
*/import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.ArrayList;import java.util.Iterator;import java.util.TreeSet;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JTextField;class Myframe extends JFrame{public static Object obj=new Object();public final static JTextField[][] filed=new JTextField[9][9]; public Myframe(){for(int a=0;a<9;a++){for(int b=0;b<9;b++){filed[a][b]=new JTextField();filed[a][b].setText("");}}JPanel jpan=new JPanel();jpan.setLayout(new GridLayout(9,9));for(int a=8;a>-1;a--){for(int b=0;b<9;b++){jpan.add(filed[b][a]);}}add(jpan,BorderLayout.CENTER);JPanel jpb=new JPanel();JButton button=new JButton("确定");jpb.add(button);button.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){synchronized (obj) {for(int a=0;a<9;a++){for(int b3=0;b3<9;b3++){int pp=0;if(!(filed[a][b3].getText().trim().equals(""))){pp=Integer.parseInt(filed[a][b3].getText().trim());Jieda.b[a][b3]=pp;}}}}synchronized (obj) {new Thread(new Jieda()).start();}}});add(jpb,BorderLayout.SOUTH);}}public class Test8{public static void main(String[] args) {Myframe myf=new Myframe();myf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);myf.setTitle("数独");myf.setSize(500,500);myf.setVisible(true);}}class Jieda implements Runnable{public static boolean[][] boo=new boolean[9][9];public static int shanghang=0;public static int shanglie=0;public static int[][] b=new int[9][9];public static void Huigui(boolean[][] boo1,int hang,int lie){int s=lie*9+hang;s--;int shang=s/9;int yushu=s%9;if(boo1[yushu][shang]){Huigui(boo1,yushu,shang);}else{shanghang=yushu;shanglie=shang;}}public static void arrayAdd(ArrayList<Integer> array,TreeSet<Integer> tree){for(int z=1;z<10;z++){boolean flag3=true;Iterator<Integer> it=tree.iterator();while(it.hasNext()){//10int b=it.next().intValue();if(z==b){flag3=false;break;}}if(flag3){array.add(new Integer(z));}flag3=true;}}public static ArrayList<Integer> Maybe(int hang,int lie){ ArrayList<Integer> array=new ArrayList<Integer>();TreeSet<Integer> tree=new TreeSet<Integer>();if(0<=hang&&hang<=2&&0<=lie&&lie<=2){for(int a=0;a<9;a++){if(a!=lie&&b[hang][a]!=0){tree.add(new Integer(b[hang][a]));}}for(int b1=0;b1<9;b1++){if(b1!=hang&&b[b1][lie]!=0){tree.add(new Integer(b[b1][lie]));}}for(int a2=0;a2<3;a2++){for(int b4=0;b4<3;b4++){if((!(a2==hang&&b4==lie))&&b[a2][b4]!=0){tree.add(new Integer(b[a2][b4]));}}}arrayAdd(array,tree);}else if(0<=hang&&hang<=2&&3<=lie&&lie<=5){for(int a=0;a<9;a++){if(a!=lie&&b[hang][a]!=0){tree.add(new Integer(b[hang][a]));}for(int b1=0;b1<9;b1++){if(b1!=hang&&b[b1][lie]!=0){tree.add(new Integer(b[b1][lie]));}}for(int a2=0;a2<3;a2++){for(int b4=3;b4<6;b4++){if((!(a2==hang&&b4==lie))&&b[a2][b4]!=0){tree.add(new Integer(b[a2][b4]));}}}arrayAdd(array,tree);}else if(0<=hang&&hang<=2&&6<=lie&&lie<=8){ for(int a=0;a<9;a++){if(a!=lie&&b[hang][a]!=0){tree.add(new Integer(b[hang][a]));}}for(int b1=0;b1<9;b1++){if(b1!=hang&&b[b1][lie]!=0){tree.add(new Integer(b[b1][lie]));}}for(int a2=0;a2<3;a2++){for(int b4=6;b4<9;b4++){if((!(a2==hang&&b4==lie))&&b[a2][b4]!=0){tree.add(new Integer(b[a2][b4]));}}}arrayAdd(array,tree);}else if(3<=hang&&hang<=5&&0<=lie&&lie<=2){ for(int a=0;a<9;a++){if(a!=lie&&b[hang][a]!=0){tree.add(new Integer(b[hang][a]));}}for(int b1=0;b1<9;b1++){if(b1!=hang&&b[b1][lie]!=0){tree.add(new Integer(b[b1][lie]));}for(int a2=3;a2<6;a2++){for(int b4=0;b4<3;b4++){if((!(a2==hang&&b4==lie))&&b[a2][b4]!=0){tree.add(new Integer(b[a2][b4]));}}}arrayAdd(array,tree);}else if(3<=hang&&hang<=5&&3<=lie&&lie<=5){ for(int a=0;a<9;a++){if(a!=lie&&b[hang][a]!=0){tree.add(new Integer(b[hang][a]));}}for(int b1=0;b1<9;b1++){if(b1!=hang&&b[b1][lie]!=0){tree.add(new Integer(b[b1][lie]));}}for(int a2=3;a2<6;a2++){for(int b4=3;b4<6;b4++){if((!(a2==hang&&b4==lie))&&b[a2][b4]!=0){tree.add(new Integer(b[a2][b4]));}}}arrayAdd(array,tree);}else if(3<=hang&&hang<=5&&6<=lie&&lie<=8){ for(int a=0;a<9;a++){if(a!=lie&&b[hang][a]!=0){tree.add(new Integer(b[hang][a]));}}for(int b1=0;b1<9;b1++){if(b1!=hang&&b[b1][lie]!=0){tree.add(new Integer(b[b1][lie]));}}for(int a2=3;a2<6;a2++){for(int b4=6;b4<9;b4++){if((!(a2==hang&&b4==lie))&&b[a2][b4]!=0){tree.add(new Integer(b[a2][b4]));}}}arrayAdd(array,tree);}else if(6<=hang&&hang<=8&&0<=lie&&lie<=2){ for(int a=0;a<9;a++){if(a!=lie&&b[hang][a]!=0){tree.add(new Integer(b[hang][a]));}}for(int b1=0;b1<9;b1++){if(b1!=hang&&b[b1][lie]!=0){tree.add(new Integer(b[b1][lie]));}}for(int a2=6;a2<9;a2++){for(int b4=0;b4<3;b4++){if((!(a2==hang&&b4==lie))&&b[a2][b4]!=0){tree.add(new Integer(b[a2][b4]));}}}arrayAdd(array,tree);}else if(6<=hang&&hang<=8&&3<=lie&&lie<=5){ for(int a=0;a<9;a++){if(a!=lie&&b[hang][a]!=0){tree.add(new Integer(b[hang][a]));}}for(int b1=0;b1<9;b1++){if(b1!=hang&&b[b1][lie]!=0){tree.add(new Integer(b[b1][lie]));}}for(int a2=6;a2<9;a2++){for(int b4=3;b4<6;b4++){if((!(a2==hang&&b4==lie))&&b[a2][b4]!=0){tree.add(new Integer(b[a2][b4]));}}}arrayAdd(array,tree);}else if(6<=hang&&hang<=8&&6<=lie&&lie<=8){ for(int a=0;a<9;a++){if(a!=lie&&b[hang][a]!=0){tree.add(new Integer(b[hang][a]));}}for(int b1=0;b1<9;b1++){if(b1!=hang&&b[b1][lie]!=0){tree.add(new Integer(b[b1][lie]));}}for(int a2=6;a2<9;a2++){for(int b4=6;b4<9;b4++){if((!(a2==hang&&b4==lie))&&b[a2][b4]!=0){tree.add(new Integer(b[a2][b4]));}}}arrayAdd(array,tree);}return array;}public void run() {for(int a=0;a<9;a++){for(int b1=0;b1<9;b1++){if(b[a][b1]!=0){boo[a][b1]=true;}else{boo[a][b1]=false;}}}boolean flag=true;ArrayList<Integer>[][] yinyong=new ArrayList[9][9]; int hang=0;int lie=0;while(lie<9){if(flag==true){hang=0;}while(hang<9){if(b[hang][lie]==0){if(flag){ArrayList<Integer> list=Maybe(hang,lie);yinyong[hang][lie]=list;}if(yinyong[hang][lie].isEmpty()){Huigui(boo,hang,lie);hang=shanghang;lie=shanglie;b[hang][lie]=0;lie--;flag=false;break;}//if(list.isEmpty())else{b[hang][lie]=yinyong[hang][lie].get(0);yinyong[hang][lie].remove(0);flag=true;boolean r=true;for(int a1=0;a1<9;a1++){for(int b1=0;b1<9;b1++){if(r==false){break;}if(b[a1][b1]==0){r=false;}}}if(r){for(int a1=0;a1<9;a1++){for(int b1=0;b1<9;b1++){System.out.print("b["+a1+"]["+b1+"]"+b[a1][b1]+","); Myframe.filed[a1][b1].setText(b[a1][b1]+"");}}}}}//if(int[hang][lie]==0)else{flag=true;}hang++;}lie++;}}}。
android计算器源代码
界面设计<LinearLayoutxmlns:android="/apk/res/android"xmlns:tools="/tools"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical"tools:context=".MainActivity"><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="2"android:orientation="horizontal"><EditTextandroid:id="@+id/led"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_margin="3dip"android:background="#ccc"android:gravity="top|right"android:text=""android:textColor="#000"android:textSize="12pt"/></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1"android:orientation="horizontal"><Buttonandroid:id="@+id/time"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:text="time"android:textSize="10pt"/><Buttonandroid:id="@+id/pow"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:text="pow"android:textSize="10pt"/><Buttonandroid:id="@+id/sqrt"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:text="sqrt"android:textSize="10pt"/><Buttonandroid:id="@+id/clear"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:text="clear"android:textSize="10pt"/></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1"android:orientation="horizontal"><Buttonandroid:id="@+id/seven"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:text="7"android:textSize="12pt"/><Buttonandroid:id="@+id/eight"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:text="8"android:textSize="12pt"/><Buttonandroid:id="@+id/nine"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:text="9"android:textSize="12pt"/><Buttonandroid:id="@+id/division"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:text="÷"android:textSize="12pt"/></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1"android:orientation="horizontal"><Buttonandroid:id="@+id/four"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:text="4"android:textSize="12pt"/><Buttonandroid:id="@+id/five"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:text="5"android:textSize="12pt"/><Buttonandroid:id="@+id/six"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:text="6"android:textSize="12pt"/><Buttonandroid:id="@+id/mult"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:text="*"android:textSize="12pt"/></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1"android:orientation="horizontal"><Buttonandroid:id="@+id/one"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:text="1"android:textSize="12pt"/><Buttonandroid:id="@+id/two"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:text="2"android:textSize="12pt"/><Buttonandroid:id="@+id/three"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:text="3"android:textSize="12pt"/><Buttonandroid:id="@+id/sub"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:text="-"android:textSize="12pt"/></LinearLayout>Java源代码package youtclss;import java.util.Date;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener; import android.widget.Button;import android.widget.EditText;public class MainActivity extends Activity { private Button one;private Button two;private Button three;private Button four;private Button five;private Button six;private Button seven;private Button eight;private Button nine;private Button zero;private Button dot;private Button add;private Button sub;private Button mult;private Button division;private Button amount;private Button clear;private Button time;private Button sqrt;private Button pow;private EditText led;String num1 = "";String num2 = "";double num3 = 0;String rezult = "";String sign = null;int mark = 0;boolean flag = true;boolean dotmark = true;protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);setContentView(yout.main);zero = (Button) findViewById(R.id.zero);one = (Button) findViewById(R.id.one);two = (Button) findViewById(R.id.two);three = (Button) findViewById(R.id.three);four = (Button) findViewById(R.id.four);five = (Button) findViewById(R.id.five);six = (Button) findViewById(R.id.six);seven = (Button) findViewById(R.id.seven);eight = (Button) findViewById(R.id.eight);nine = (Button) findViewById(R.id.nine);dot = (Button) findViewById(R.id.dot);add = (Button) findViewById(R.id.add);sub = (Button) findViewById(R.id.sub);mult = (Button) findViewById(R.id.mult);division = (Button) findViewById(R.id.division); clear = (Button) findViewById(R.id.clear); amount = (Button) findViewById(R.id.amount); time = (Button) findViewById(R.id.time);pow = (Button) findViewById(R.id.pow);sqrt = (Button) findViewById(R.id.sqrt);led = (EditText) findViewById(R.id.led);zero.setOnClickListener(new OnClickListener() { public void onClick(View v) {if (mark == 0) {num1 = num1 + "0";led.setText(num1);} else if (mark == 1) {num2 = num2 + "0";led.setText(num1 + sign + num2);} else {num1 = String.valueOf(num3);num2 = num2 + "0";led.setText(num1 + sign + num2);}}});one.setOnClickListener(new OnClickListener() { public void onClick(View v) {if (mark == 0) {num1 = num1 + "1";led.setText(num1);} else if (mark == 1) {num2 = num2 + "1";led.setText(num1 + sign + num2);} else {num1 = String.valueOf(num3);num2 = num2 + "1";led.setText(num1 + sign + num2);}}});two.setOnClickListener(new OnClickListener() { public void onClick(View v) {if (mark == 0) {num1 = num1 + "2";led.setText(num1);} else if (mark == 1) {num2 = num2 + "2";led.setText(num1 + sign + num2);} else {num1 = String.valueOf(num3);num2 = num2 + "2";led.setText(num1 + sign + num2);}}});three.setOnClickListener(new OnClickListener() { public void onClick(View v) {if (mark == 0) {num1 = num1 + "3";led.setText(num1);} else if (mark == 1) {num2 = num2 + "3";led.setText(num1 + sign + num2);} else {num1 = String.valueOf(num3);num2 = num2 + "3";led.setText(num1 + sign + num2);}}});four.setOnClickListener(new OnClickListener() { public void onClick(View v) {if (mark == 0) {num1 = num1 + "4";led.setText(num1);} else if (mark == 1) {num2 = num2 + "4";led.setText(num1 + sign + num2);} else {num1 = String.valueOf(num3);num2 = num2 + "4";led.setText(num1 + sign + num2);}}});five.setOnClickListener(new OnClickListener() { public void onClick(View v) {if (mark == 0) {num1 = num1 + "5";led.setText(num1);} else if (mark == 1) {num2 = num2 + "5";led.setText(num1 + sign + num2);} else {num1 = String.valueOf(num3);num2 = num2 + "5";led.setText(num1 + sign + num2);}}});six.setOnClickListener(new OnClickListener() { public void onClick(View v) {if (mark == 0) {num1 = num1 + "6";led.setText(num1);} else if (mark == 1) {num2 = num2 + "6";led.setText(num1 + sign + num2);} else {num1 = String.valueOf(num3);num2 = num2 + "6";led.setText(num1 + sign + num2);}}});seven.setOnClickListener(new OnClickListener() { public void onClick(View v) {if (mark == 0) {num1 = num1 + "7";led.setText(num1);} else if (mark == 1) {num2 = num2 + "7";led.setText(num1 + sign + num2);} else {num1 = String.valueOf(num3);num2 = num2 + "7";led.setText(num1 + sign + num2);}}});eight.setOnClickListener(new OnClickListener() { public void onClick(View v) {if (mark == 0) {num1 = num1 + "8";led.setText(num1);} else if (mark == 1) {num2 = num2 + "8";led.setText(num1 + sign + num2);} else {num1 = String.valueOf(num3);num2 = num2 + "8";led.setText(num1 + sign + num2);}}});nine.setOnClickListener(new OnClickListener() { public void onClick(View v) {if (mark == 0) {num1 = num1 + "9";led.setText(num1);} else if (mark == 1) {num2 = num2 + "9";led.setText(num1 + sign + num2);} else {num1 = String.valueOf(num3);num2 = num2 + "9";led.setText(num1 + sign + num2);}}});add.setOnClickListener(new OnClickListener() { public void onClick(View v) {if (flag) {sign = "+";mark++;flag = false;dotmark = true;}}});sub.setOnClickListener(new OnClickListener() { public void onClick(View v) {if (flag) {sign = "-";mark++;flag = false;dotmark = true;}}});mult.setOnClickListener(new OnClickListener() { public void onClick(View v) {if (flag) {sign = "*";mark++;flag = false;dotmark = true;}}});division.setOnClickListener(new OnClickListener() {public void onClick(View v) {if (flag) {sign = "÷";mark++;flag = false;dotmark = true;}}});pow.setOnClickListener(new OnClickListener() {public void onClick(View v) {if (flag) {sign = "^";mark++;flag = false;dotmark = true;}}});dot.setOnClickListener(new OnClickListener() {public void onClick(View v) {if (dotmark) {if (mark == 0) {num1 = num1 + ".";led.setText(num1);} else if (mark == 1) {num2 = num2 + ".";led.setText(num1 + sign + num2);} else {num1 = String.valueOf(num3);num2 = num2 + ".";led.setText(num1 + sign + num2);}dotmark = false;}}});amount.setOnClickListener(new OnClickListener() { // 等于时发生的运算public void onClick(View v) {flag = true;dotmark = true;if (sign.equals("+")) {double x = Double.parseDouble(num1);double y = Double.parseDouble(num2);num3 = x + y;rezult = String.valueOf(num3);led.setText(num1 + sign + num2 + "=" + rezult);num2 = "";} else if (sign.equals("-")) {double x = Double.parseDouble(num1);double y = Double.parseDouble(num2);num3 = x - y;rezult = String.valueOf(num3);led.setText(num1 + sign + num2 + "=" + rezult);num2 = "";} else if (sign.equals("*")) {double x = Double.parseDouble(num1);double y = Double.parseDouble(num2);num3 = x * y;rezult = String.valueOf(num3);led.setText(num1 + sign + num2 + "=" + rezult);num2 = "";} else if (sign.equals("÷")) {double x = Double.parseDouble(num1);double y = Double.parseDouble(num2);num3 = x / y;rezult = String.valueOf(num3);led.setText(num1 + sign + num2 +"=" + rezult);num2 = "";} else if (sign.equals("^")) {double x = Double.parseDouble(num1);double y = Double.parseDouble(num2);num3 = Math.pow(x, y);rezult = String.valueOf(num3);led.setText(num1 + sign + num2 + "=" + rezult);num2 = "";}}});clear.setOnClickListener(new OnClickListener() {public void onClick(View v) {num1 = "";num2 = "";num3 = 0;mark = 0;sign = "";led.setText("0");flag = true;dotmark = true;}});sqrt.setOnClickListener(new OnClickListener() {public void onClick(View v) {if (mark == 0 && !num1.equals("") &&num2.equals("")) {double parent =Double.parseDouble(num1);double root = Math.sqrt(parent);rezult = String.valueOf(root);led.setText(rezult);num1 = "";}}});time.setOnClickListener(new OnClickListener() { public void onClick(View v) {Date date = new Date();led.setText(date.toLocaleString());}});}。
MSP430单片机课程设计-简单计算器设计
MSP430 单片机课程设计
unsigned char disbuff[8] = {20,20,0,0,0,0,0,0};//初始数码管 unsigned char getkey(void); unsigned int qiushu(); void clear(void); void zhuanhuan(); void delay(unsigned int j) { for(uint i=0;i<j;i++); } unsigned int qiushu() //转化成相应的十进制整数 {unsigned int y; y=disbuff[2]*100000+disbuff[3]*10000+disbuff[4]*1000+disbuff[5]*100 +disbuff[6]*10+disbuff[7]; return y; } void clear(void) {unsigned int a; for(a=7;a>1;a--) disbuff[a]=0; } //清除屏幕
实验二:猜数字游戏设计
一、目的
利用单片机芯片 MSP430x14x、四位八段共阴数码管,设计一个猜数字游戏, 并将游戏结果显示在数码管上。
二、功能
要求该设计能实现如下的功能:游戏开始后,先在程序内随即设定四个 0-9 之间的不同数,作为游戏数答案。然后用户给出 4 个数,该软件会将您提交的数 与它自动产生的数进行比较,结果在八段数码管上显示成"kAsB"。k 代表位置正 确数字也正确的字数,s 代表数字正确但位置不正确的字数。当 k=4 且 s=0 时, 则比较正确显示“GOOD” ,进行下一次比较。
4
MSP430 单片机课程设计
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Button btn0,btn1,btn2,btn3,btn4,btn5;
Button btn6,btn7,btn8,btn9;
EditText et=null;
Button btnC,btnadd,btnsub,btnmul,btndiv,btnequal,btnpoint;
<Button
android:id="@+id/btn_seven_main"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:textSize="40sp"
android:text="/"/>
</LinearLayout>
</LinearLayout>
源代码:
package com.example.num;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
android:text="-"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layoutid:orientation="horizontal" >
android:text="1"/>
<Button
android:id="@+id/btn_two_main"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:src="@drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
android:layout_weight="1"
android:gravity="center"
android:textSize="40sp"
android:text="0"/>
<Button
android:id="@+id/btn_point_main"
android:layout_width="0dp"
<Button
android:id="@+id/btn_one_main"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:textSize="40sp"
界面设计&算法实现
界面设计:
<LinearLayout xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="C"/>
<ImageView
android:id="@+id/iv_logo_main"
android:layout_width="match_parent"
android:layout_height="60dp"
android:scaleX="4"
android:scaleY="2"
android:textSize="40sp"
android:text="8"/>
<Button
android:id="@+id/btn_nine_main"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:layout_weight="1"
android:gravity="center"
android:textSize="40sp"
android:text="*"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:textSize="40sp"
android:text="="/>
<Button
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:textSize="40sp"
android:text="6"/>
<Button
android:id="@+id/btn_sub_main"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:textSize="40sp"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center"
android:textSize="40sp"
android:text="5"/>
<Button
android:id="@+id/btn_six_main"
android:id="@+id/btn_div_main"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:textSize="40sp"
android:gravity="center"
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener {
android:gravity="center"
android:textSize="40sp"
android:text="3"/>
<Button
android:id="@+id/btn_add_main"
android:layout_width="0dp"
android:layout_height="60dp"
double num1=0,num2=0,result=0;
int n=0;
boolean Boolean1=false,Boolean=false;