3个回答
展开全部
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#define UNKNOWN_CHAR 2
#define DIVISOR_IS_ZERO 3
#define FALSE 0
#define TRUE 1
/*定义结构,主要用于GetString函数,可以不管它*/
typedef struct string
{char str;
struct string *next;
}string;
/*函数声明*/
/*可以无限接受字符,输入一个字符就给你个空间,返回一个字符串首地址 . */
/*这个函数可以看成普通的scanf()*/
char *GetString();
/*从字符串str第*rank个字符开始分析直到遇到 ')' or ']' or '}' or '\0' . */
/*有错误就把错误代号装在*errorNum中,返回从中计算出的值*/
double GetASResultOf(char * str, int *rank, int *errorNum);
/*从字符串str第*rank个字符开始分析直到遇到不符合要求的字符,返回这个单位值.*/
/*如:str="12+sin(30)+[12+cos(60)]"
GetUnitageOf(str,0,errorNum)=12
GetUnitageOf(str,3,errorNum)=0.5
GetUnitageOf(str,11,errorNum)=12.5 */
double GetUnitageOf(char * str, int *rank, int *errorNum);
int main()
{
/*定义变量*/
double value;
int rank=0;
int errorNum=TRUE;
char * str;
/*接收字符*/
printf("counter v1.0\n");
printf("Made by PT\n");
printf("Date 2005-07-16\n\n");
printf("Please input your arithmetic:\n");
str=GetString();
/*调用处理函数,*/
value = GetASResultOf(str, &rank, &errorNum);
/*错误处理(没完善)*/
if (errorNum==TRUE) printf("The result is: %lf\n",value, rank);
else if (errorNum==UNKNOWN_CHAR) printf("char error!\n");
else if (errorNum==DIVISOR_IS_ZERO) printf("Divisor could not be 0\n");
else printf("Other error\n");
getch();
return 0;
}
/*0==[=========>无限动态字符接受函数*/
char *GetString()
{
char *CharToString(string * sHead, int arraySize);
int arraySize=0;
string *s;
string *sHead;
s = (string *)malloc(sizeof(string));
sHead = s;
s->str = getchar();
arraySize++;
while((s->str)!='\n')
{
arraySize++;
s->next=(string *)malloc(sizeof(string));
s=s->next;
s->str=getchar();
}
return CharToString(sHead, arraySize);
}
char *CharToString(string * sHead, int arraySize)
{
char *strArray;
string *s=sHead;
int i=0;
strArray = (char*)malloc(arraySize*sizeof(char));
while(i<arraySize)
{
if (s->str=='=' || s->str=='\n') break;
if (s->str!=32) strArray[i++]=s->str;
s=s->next;
}
strArray[i]='\0';
return strArray;
}
/*0==[=========> 得到一个单位的值*/
double GetUnitageOf(char * str, int *rank, int *errorNum)
{
double CharToNum(char * str, int *rank);
double value;
/*如果是数字或负号就返回这个数字*/
if ((str[*rank]>=48 && str[*rank]<=57) || (str[*rank]=='-'))
{if (str[*rank]=='-') {(*rank)++;value = (-1)*GetUnitageOf(str, rank,errorNum);}
else {value = CharToNum(str, rank);}}
/*如果是括号就用GetASResultOf取得括号里面的值并返回*/
else if (str[*rank]=='(' ||
str[*rank]=='[' ||
str[*rank]=='{' )
{(*rank)++;
value = GetASResultOf(str, rank, errorNum);}
/*返回sin(x)*/
else if (str[*rank]=='s' &&
str[(*rank)+1]=='i' &&
str[(*rank)+2]=='n')
{ (*rank)=(*rank)+3;
value = GetUnitageOf(str, rank, errorNum);
value = sin(value);
}
/*返回cos(x)*/
else if (str[*rank]=='c' &&
str[(*rank)+1]=='o' &&
str[(*rank)+2]=='s')
{*rank=*rank+3;
value = cos(GetUnitageOf(str, rank, errorNum));}
/*返回tan(x)*/
else if (str[*rank]=='t' &&
str[(*rank)+1]=='a' &&
str[(*rank)+2]=='n')
{*rank=*rank+3;
value = tan(GetUnitageOf(str, rank, errorNum));}
/*还可以加一些的,懒得加了*/
/*defult:处理错误*/
else {*errorNum = UNKNOWN_CHAR;value = 0;}
/*检查errorNum*/
if (*errorNum==TRUE){return value;}
else return FALSE;
}
/*0==[=========>这个是把字符转换成数字的函数,只在GetUnitageOf()里用*/
/*数字不能超过15个,其余的将被抹去.*/
double CharToNum(char * str, int *rank)
{
int j=0;
double value=0;
while((str[*rank]>=48 && str[*rank]<=57) || (str[*rank]==46))
{
if (str[*rank]!='.')
{if ((j++)<=15) {value = value*10+(str[*rank]-48);}}
else
{if (j<=15)
{
for ((*rank)++,j=1; (str[*rank]>=48 && str[*rank]<=57 && str[*rank]!='\0'); (*rank)++, j++)
{value = value+(str[*rank]-48)*pow(0.1,j);}
return value;
}
}
(*rank)++;
}
return value;
}
/*0==[=========>这个函数写的太复杂了,实在不好作解释,反正是在GetPDResultOf
函数和GetUnitageOf函数之间转啊转的*/
/*简单地说就是把str中第一个加减号前的式子算出来放在value变量中.再用函数
GetPDResultOf算出直到二个加减号前的式子然后与value相加或相减放在
变量value中,以此类推,直到遇上 ')' or ']' or '}' or '\0' */
double GetASResultOf(char * str, int *rank, int *errorNum)
{
double GetPDResultOf(char * str, int *rank, int *errorNum);
double value;
double dwIf_Zero;
/*遇上 ')' or ']' or '}' or '\0' 就返回*/
while (str[*rank]!=')' &&
str[*rank]!=']' &&
str[*rank]!='}' &&
str[*rank]!='\0')
{
if ((str[*rank]>=48 && str[*rank]<=57) ||
str[*rank]=='s' ||
str[*rank]=='c' ||
str[*rank]=='t')
{
if ((str[*rank-1]==')' ||
str[*rank-1]==']' ||
str[*rank-1]=='}') &&
*rank!=0)
{value = value*GetUnitageOf(str, rank, errorNum);}
else
{value = GetUnitageOf(str, rank, errorNum);}
}
else if (str[*rank]=='+')
{if (str[*rank-1]!= '(' &&
str[*rank-1]!= '[' &&
str[*rank-1]!= '{' && (*rank)!=0 )
{(*rank)++; value = value + GetPDResultOf(str, rank, errorNum);}
else {*errorNum=UNKNOWN_CHAR;return 0;}}
else if (str[*rank]=='-')
{if (str[*rank-1]!= '(' &&
str[*rank-1]!= '[' &&
str[*rank-1]!= '{' && (*rank)!=0 )
{(*rank)++; value = value - GetPDResultOf(str, rank, errorNum);}
else {value = GetUnitageOf(str, rank, errorNum);}}
else if (str[*rank]=='*')
{if (str[*rank-1]!= '(' &&
str[*rank-1]!= '[' &&
str[*rank-1]!= '{' && (*rank)!=0 )
{(*rank)++; value = value * GetUnitageOf(str, rank, errorNum);}
else {*errorNum=UNKNOWN_CHAR;return 0;}}
else if (str[*rank]=='/')
{if (str[*rank-1]!= '(' &&
str[*rank-1]!= '[' &&
str[*rank-1]!= '{' && (*rank)!=0 )
{(*rank)++;
dwIf_Zero=GetUnitageOf(str, rank, errorNum);
if (dwIf_Zero==0) {*errorNum=DIVISOR_IS_ZERO;return 0;}
else value = value /dwIf_Zero;}
else {*errorNum=UNKNOWN_CHAR;return 0;}}
else if (str[*rank]== '(' ||
str[*rank]== '[' ||
str[*rank]== '{' )
{if (str[*rank-1]!= '(' &&
str[*rank-1]!= '[' &&
str[*rank-1]!= '{' && (*rank)!=0 )
{(*rank)++; value = value*GetASResultOf(str, rank, errorNum);}
else {value = GetUnitageOf(str, rank, errorNum);}}
else {*errorNum=UNKNOWN_CHAR;return FALSE;}
if (*errorNum!=TRUE) return FALSE;
}
(*rank)++;
return value;
}
double GetPDResultOf(char * str, int *rank, int *errorNum)
{
double value;
double dwIf_Zero;
value = GetUnitageOf(str, rank, errorNum);
if (str[*rank]=='\0') return value;
while (str[*rank]!='+' &&
str[*rank]!='-' &&
str[*rank]!=')' &&
str[*rank]!=']' &&
str[*rank]!='}')
{
if (str[*rank]=='*')
value = value * GetUnitageOf(str, rank, errorNum);
else if (str[*rank]=='/')
{
dwIf_Zero= GetUnitageOf(str, rank, errorNum);
if (dwIf_Zero=0) {*errorNum=DIVISOR_IS_ZERO;return 0;}
else value = value /dwIf_Zero;
}
else if (str[*rank]=='(' ||
str[*rank]=='[' ||
str[*rank]=='{')
{(*rank)++;
value = value * GetUnitageOf(str, rank, errorNum);}
else {*errorNum=UNKNOWN_CHAR;return FALSE;}
if (*errorNum!=TRUE) return FALSE;
}
return value;
}
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#define UNKNOWN_CHAR 2
#define DIVISOR_IS_ZERO 3
#define FALSE 0
#define TRUE 1
/*定义结构,主要用于GetString函数,可以不管它*/
typedef struct string
{char str;
struct string *next;
}string;
/*函数声明*/
/*可以无限接受字符,输入一个字符就给你个空间,返回一个字符串首地址 . */
/*这个函数可以看成普通的scanf()*/
char *GetString();
/*从字符串str第*rank个字符开始分析直到遇到 ')' or ']' or '}' or '\0' . */
/*有错误就把错误代号装在*errorNum中,返回从中计算出的值*/
double GetASResultOf(char * str, int *rank, int *errorNum);
/*从字符串str第*rank个字符开始分析直到遇到不符合要求的字符,返回这个单位值.*/
/*如:str="12+sin(30)+[12+cos(60)]"
GetUnitageOf(str,0,errorNum)=12
GetUnitageOf(str,3,errorNum)=0.5
GetUnitageOf(str,11,errorNum)=12.5 */
double GetUnitageOf(char * str, int *rank, int *errorNum);
int main()
{
/*定义变量*/
double value;
int rank=0;
int errorNum=TRUE;
char * str;
/*接收字符*/
printf("counter v1.0\n");
printf("Made by PT\n");
printf("Date 2005-07-16\n\n");
printf("Please input your arithmetic:\n");
str=GetString();
/*调用处理函数,*/
value = GetASResultOf(str, &rank, &errorNum);
/*错误处理(没完善)*/
if (errorNum==TRUE) printf("The result is: %lf\n",value, rank);
else if (errorNum==UNKNOWN_CHAR) printf("char error!\n");
else if (errorNum==DIVISOR_IS_ZERO) printf("Divisor could not be 0\n");
else printf("Other error\n");
getch();
return 0;
}
/*0==[=========>无限动态字符接受函数*/
char *GetString()
{
char *CharToString(string * sHead, int arraySize);
int arraySize=0;
string *s;
string *sHead;
s = (string *)malloc(sizeof(string));
sHead = s;
s->str = getchar();
arraySize++;
while((s->str)!='\n')
{
arraySize++;
s->next=(string *)malloc(sizeof(string));
s=s->next;
s->str=getchar();
}
return CharToString(sHead, arraySize);
}
char *CharToString(string * sHead, int arraySize)
{
char *strArray;
string *s=sHead;
int i=0;
strArray = (char*)malloc(arraySize*sizeof(char));
while(i<arraySize)
{
if (s->str=='=' || s->str=='\n') break;
if (s->str!=32) strArray[i++]=s->str;
s=s->next;
}
strArray[i]='\0';
return strArray;
}
/*0==[=========> 得到一个单位的值*/
double GetUnitageOf(char * str, int *rank, int *errorNum)
{
double CharToNum(char * str, int *rank);
double value;
/*如果是数字或负号就返回这个数字*/
if ((str[*rank]>=48 && str[*rank]<=57) || (str[*rank]=='-'))
{if (str[*rank]=='-') {(*rank)++;value = (-1)*GetUnitageOf(str, rank,errorNum);}
else {value = CharToNum(str, rank);}}
/*如果是括号就用GetASResultOf取得括号里面的值并返回*/
else if (str[*rank]=='(' ||
str[*rank]=='[' ||
str[*rank]=='{' )
{(*rank)++;
value = GetASResultOf(str, rank, errorNum);}
/*返回sin(x)*/
else if (str[*rank]=='s' &&
str[(*rank)+1]=='i' &&
str[(*rank)+2]=='n')
{ (*rank)=(*rank)+3;
value = GetUnitageOf(str, rank, errorNum);
value = sin(value);
}
/*返回cos(x)*/
else if (str[*rank]=='c' &&
str[(*rank)+1]=='o' &&
str[(*rank)+2]=='s')
{*rank=*rank+3;
value = cos(GetUnitageOf(str, rank, errorNum));}
/*返回tan(x)*/
else if (str[*rank]=='t' &&
str[(*rank)+1]=='a' &&
str[(*rank)+2]=='n')
{*rank=*rank+3;
value = tan(GetUnitageOf(str, rank, errorNum));}
/*还可以加一些的,懒得加了*/
/*defult:处理错误*/
else {*errorNum = UNKNOWN_CHAR;value = 0;}
/*检查errorNum*/
if (*errorNum==TRUE){return value;}
else return FALSE;
}
/*0==[=========>这个是把字符转换成数字的函数,只在GetUnitageOf()里用*/
/*数字不能超过15个,其余的将被抹去.*/
double CharToNum(char * str, int *rank)
{
int j=0;
double value=0;
while((str[*rank]>=48 && str[*rank]<=57) || (str[*rank]==46))
{
if (str[*rank]!='.')
{if ((j++)<=15) {value = value*10+(str[*rank]-48);}}
else
{if (j<=15)
{
for ((*rank)++,j=1; (str[*rank]>=48 && str[*rank]<=57 && str[*rank]!='\0'); (*rank)++, j++)
{value = value+(str[*rank]-48)*pow(0.1,j);}
return value;
}
}
(*rank)++;
}
return value;
}
/*0==[=========>这个函数写的太复杂了,实在不好作解释,反正是在GetPDResultOf
函数和GetUnitageOf函数之间转啊转的*/
/*简单地说就是把str中第一个加减号前的式子算出来放在value变量中.再用函数
GetPDResultOf算出直到二个加减号前的式子然后与value相加或相减放在
变量value中,以此类推,直到遇上 ')' or ']' or '}' or '\0' */
double GetASResultOf(char * str, int *rank, int *errorNum)
{
double GetPDResultOf(char * str, int *rank, int *errorNum);
double value;
double dwIf_Zero;
/*遇上 ')' or ']' or '}' or '\0' 就返回*/
while (str[*rank]!=')' &&
str[*rank]!=']' &&
str[*rank]!='}' &&
str[*rank]!='\0')
{
if ((str[*rank]>=48 && str[*rank]<=57) ||
str[*rank]=='s' ||
str[*rank]=='c' ||
str[*rank]=='t')
{
if ((str[*rank-1]==')' ||
str[*rank-1]==']' ||
str[*rank-1]=='}') &&
*rank!=0)
{value = value*GetUnitageOf(str, rank, errorNum);}
else
{value = GetUnitageOf(str, rank, errorNum);}
}
else if (str[*rank]=='+')
{if (str[*rank-1]!= '(' &&
str[*rank-1]!= '[' &&
str[*rank-1]!= '{' && (*rank)!=0 )
{(*rank)++; value = value + GetPDResultOf(str, rank, errorNum);}
else {*errorNum=UNKNOWN_CHAR;return 0;}}
else if (str[*rank]=='-')
{if (str[*rank-1]!= '(' &&
str[*rank-1]!= '[' &&
str[*rank-1]!= '{' && (*rank)!=0 )
{(*rank)++; value = value - GetPDResultOf(str, rank, errorNum);}
else {value = GetUnitageOf(str, rank, errorNum);}}
else if (str[*rank]=='*')
{if (str[*rank-1]!= '(' &&
str[*rank-1]!= '[' &&
str[*rank-1]!= '{' && (*rank)!=0 )
{(*rank)++; value = value * GetUnitageOf(str, rank, errorNum);}
else {*errorNum=UNKNOWN_CHAR;return 0;}}
else if (str[*rank]=='/')
{if (str[*rank-1]!= '(' &&
str[*rank-1]!= '[' &&
str[*rank-1]!= '{' && (*rank)!=0 )
{(*rank)++;
dwIf_Zero=GetUnitageOf(str, rank, errorNum);
if (dwIf_Zero==0) {*errorNum=DIVISOR_IS_ZERO;return 0;}
else value = value /dwIf_Zero;}
else {*errorNum=UNKNOWN_CHAR;return 0;}}
else if (str[*rank]== '(' ||
str[*rank]== '[' ||
str[*rank]== '{' )
{if (str[*rank-1]!= '(' &&
str[*rank-1]!= '[' &&
str[*rank-1]!= '{' && (*rank)!=0 )
{(*rank)++; value = value*GetASResultOf(str, rank, errorNum);}
else {value = GetUnitageOf(str, rank, errorNum);}}
else {*errorNum=UNKNOWN_CHAR;return FALSE;}
if (*errorNum!=TRUE) return FALSE;
}
(*rank)++;
return value;
}
double GetPDResultOf(char * str, int *rank, int *errorNum)
{
double value;
double dwIf_Zero;
value = GetUnitageOf(str, rank, errorNum);
if (str[*rank]=='\0') return value;
while (str[*rank]!='+' &&
str[*rank]!='-' &&
str[*rank]!=')' &&
str[*rank]!=']' &&
str[*rank]!='}')
{
if (str[*rank]=='*')
value = value * GetUnitageOf(str, rank, errorNum);
else if (str[*rank]=='/')
{
dwIf_Zero= GetUnitageOf(str, rank, errorNum);
if (dwIf_Zero=0) {*errorNum=DIVISOR_IS_ZERO;return 0;}
else value = value /dwIf_Zero;
}
else if (str[*rank]=='(' ||
str[*rank]=='[' ||
str[*rank]=='{')
{(*rank)++;
value = value * GetUnitageOf(str, rank, errorNum);}
else {*errorNum=UNKNOWN_CHAR;return FALSE;}
if (*errorNum!=TRUE) return FALSE;
}
return value;
}
展开全部
package as;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class JiSuanQi implements ActionListener{
/**
* 程序功能: 计算器
*
* Bug: 1)小数点:设一个Boolean成员变量,在点击后不可用。。。在"+-/*"中设为可用。
*/
//step1 Define Container
private JFrame fra = new JFrame("计算器");
private Panel panel = new Panel();
private float sum,sum1,sum2;
private char zifu;
private boolean ii=true;
Container con = fra.getContentPane();
JMenuBar bar = new JMenuBar();
JMenu JMenu1 = new JMenu("编辑");
JMenu JMenu2 = new JMenu("查看");
JMenu JMenu3= new JMenu("帮助");
JMenuItem JMenuitem= new JMenuItem("帮助111");
JMenuItem JMenuitem1= new JMenuItem("帮助111");
//step3 Define Component
TextField text = new TextField();
JButton bu1 = new JButton("1");
JButton bu2 = new JButton("2");
JButton bu3 = new JButton("3");
JButton bu4 = new JButton("4");
JButton bu5 = new JButton("5");
JButton bu6 = new JButton("6");
JButton bu7 = new JButton("7");
JButton bu8 = new JButton("8");
JButton bu9 = new JButton("9");
JButton bu0 = new JButton("0");
JButton bujia = new JButton("+");
JButton bujian = new JButton("-");
JButton bucheng = new JButton("*");
JButton buchu = new JButton("/");
JButton budian = new JButton(".");
JButton bufuhao = new JButton("+/-");
JButton budenghao = new JButton("=");
JButton buqingling = new JButton("C");
public void m()
{
//step2 Define Layout
con.setLayout(new BorderLayout());
panel.setLayout(new GridLayout(5,4,4,4));
buqingling.setForeground(Color.RED);
//step5 把组件(subassembly)放在Panel中
panel.add(bu1);
panel.add(bu2);
panel.add(bu3);
panel.add(bujia);
panel.add(bu4);
panel.add(bu5);
panel.add(bu6);
panel.add(bujian);
panel.add(bu7);
panel.add(bu8);
panel.add(bu9);
panel.add(bucheng);
panel.add(bu0);
panel.add(bufuhao);
panel.add(budian);
panel.add(buchu);
panel.add(buqingling);
panel.add(budenghao);
/*JMenu1.add("复制");
JMenu1.add("粘贴");
JMenu2.add("标准型");
JMenu2.add("科学型");
JMenu2.addSeparator();
JMenu2.add("数学分组");
//JMenu3.add("帮助");*/
JMenu1.add(JMenuitem1);
bar.add(JMenu1);
bar.add(JMenu2);
JMenu3.add(JMenuitem);
bar.add(JMenu3);
fra.setJMenuBar(bar);
//把Panel放到Fream中
con.add(panel,BorderLayout.CENTER);
con.add(text,BorderLayout.NORTH);
//对 JJButton 进行事件处理
bu1.addActionListener(this);
bu2.addActionListener(this);
bu3.addActionListener(this);
bu4.addActionListener(this);
bu5.addActionListener(this);
bu6.addActionListener(this);
bu7.addActionListener(this);
bu8.addActionListener(this);
bu9.addActionListener(this);
bu0.addActionListener(this);
bujia.addActionListener(this);
bujian.addActionListener(this);
bucheng.addActionListener(this);
buchu.addActionListener(this);
budian.addActionListener(this);
bufuhao.addActionListener(this);
budenghao.addActionListener(this);
buqingling.addActionListener(this);
//show windows size and window
fra.setSize(230,200);
fra.setResizable(false);//屏蔽最大化
fra.setVisible(true);//显示Frame
//关闭windows窗口
fra.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
public void actionPerformed(ActionEvent e)
{
String str=e.getActionCommand();//获取用户点击按扭上的文字是什么。
String shuzi="0123456789";
String yunsuan="+-*/";
if(shuzi.indexOf(str)>=0)//判断返回的数值是否大于0
{
text.setText(text.getText()+str);
//ii=true;
}
else if(yunsuan.indexOf(str)>=0)
{
sum1=Float.parseFloat(text.getText());
text.setText("");
zifu=yunsuan.charAt(yunsuan.indexOf(str));
ii=true;
}
else if(str.equals(".") && ii==true)
{
text.setText(text.getText()+str);
ii=false;
}
else if(str.equals("="))
{
sum2=Float.parseFloat(text.getText());
switch(zifu)
{
case '+':
sum=sum1+sum2;
text.setText(""+sum);
break;
case '-':sum=sum1-sum2;
text.setText(""+sum);
break;
case '*':sum=sum1*sum2;
text.setText(""+sum);
break;
case '/':sum=sum1/sum2;
text.setText(""+sum);
break;
}
}
else if(str.equals("C"))
{
text.setText("");
sum=0;
sum1=0;
sum2=0;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
JiSuanQi ji = new JiSuanQi();
ji.m();
}
}
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class JiSuanQi implements ActionListener{
/**
* 程序功能: 计算器
*
* Bug: 1)小数点:设一个Boolean成员变量,在点击后不可用。。。在"+-/*"中设为可用。
*/
//step1 Define Container
private JFrame fra = new JFrame("计算器");
private Panel panel = new Panel();
private float sum,sum1,sum2;
private char zifu;
private boolean ii=true;
Container con = fra.getContentPane();
JMenuBar bar = new JMenuBar();
JMenu JMenu1 = new JMenu("编辑");
JMenu JMenu2 = new JMenu("查看");
JMenu JMenu3= new JMenu("帮助");
JMenuItem JMenuitem= new JMenuItem("帮助111");
JMenuItem JMenuitem1= new JMenuItem("帮助111");
//step3 Define Component
TextField text = new TextField();
JButton bu1 = new JButton("1");
JButton bu2 = new JButton("2");
JButton bu3 = new JButton("3");
JButton bu4 = new JButton("4");
JButton bu5 = new JButton("5");
JButton bu6 = new JButton("6");
JButton bu7 = new JButton("7");
JButton bu8 = new JButton("8");
JButton bu9 = new JButton("9");
JButton bu0 = new JButton("0");
JButton bujia = new JButton("+");
JButton bujian = new JButton("-");
JButton bucheng = new JButton("*");
JButton buchu = new JButton("/");
JButton budian = new JButton(".");
JButton bufuhao = new JButton("+/-");
JButton budenghao = new JButton("=");
JButton buqingling = new JButton("C");
public void m()
{
//step2 Define Layout
con.setLayout(new BorderLayout());
panel.setLayout(new GridLayout(5,4,4,4));
buqingling.setForeground(Color.RED);
//step5 把组件(subassembly)放在Panel中
panel.add(bu1);
panel.add(bu2);
panel.add(bu3);
panel.add(bujia);
panel.add(bu4);
panel.add(bu5);
panel.add(bu6);
panel.add(bujian);
panel.add(bu7);
panel.add(bu8);
panel.add(bu9);
panel.add(bucheng);
panel.add(bu0);
panel.add(bufuhao);
panel.add(budian);
panel.add(buchu);
panel.add(buqingling);
panel.add(budenghao);
/*JMenu1.add("复制");
JMenu1.add("粘贴");
JMenu2.add("标准型");
JMenu2.add("科学型");
JMenu2.addSeparator();
JMenu2.add("数学分组");
//JMenu3.add("帮助");*/
JMenu1.add(JMenuitem1);
bar.add(JMenu1);
bar.add(JMenu2);
JMenu3.add(JMenuitem);
bar.add(JMenu3);
fra.setJMenuBar(bar);
//把Panel放到Fream中
con.add(panel,BorderLayout.CENTER);
con.add(text,BorderLayout.NORTH);
//对 JJButton 进行事件处理
bu1.addActionListener(this);
bu2.addActionListener(this);
bu3.addActionListener(this);
bu4.addActionListener(this);
bu5.addActionListener(this);
bu6.addActionListener(this);
bu7.addActionListener(this);
bu8.addActionListener(this);
bu9.addActionListener(this);
bu0.addActionListener(this);
bujia.addActionListener(this);
bujian.addActionListener(this);
bucheng.addActionListener(this);
buchu.addActionListener(this);
budian.addActionListener(this);
bufuhao.addActionListener(this);
budenghao.addActionListener(this);
buqingling.addActionListener(this);
//show windows size and window
fra.setSize(230,200);
fra.setResizable(false);//屏蔽最大化
fra.setVisible(true);//显示Frame
//关闭windows窗口
fra.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
}
public void actionPerformed(ActionEvent e)
{
String str=e.getActionCommand();//获取用户点击按扭上的文字是什么。
String shuzi="0123456789";
String yunsuan="+-*/";
if(shuzi.indexOf(str)>=0)//判断返回的数值是否大于0
{
text.setText(text.getText()+str);
//ii=true;
}
else if(yunsuan.indexOf(str)>=0)
{
sum1=Float.parseFloat(text.getText());
text.setText("");
zifu=yunsuan.charAt(yunsuan.indexOf(str));
ii=true;
}
else if(str.equals(".") && ii==true)
{
text.setText(text.getText()+str);
ii=false;
}
else if(str.equals("="))
{
sum2=Float.parseFloat(text.getText());
switch(zifu)
{
case '+':
sum=sum1+sum2;
text.setText(""+sum);
break;
case '-':sum=sum1-sum2;
text.setText(""+sum);
break;
case '*':sum=sum1*sum2;
text.setText(""+sum);
break;
case '/':sum=sum1/sum2;
text.setText(""+sum);
break;
}
}
else if(str.equals("C"))
{
text.setText("");
sum=0;
sum1=0;
sum2=0;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
JiSuanQi ji = new JiSuanQi();
ji.m();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询