導航:首頁 > 萬維百科 > 網頁設計中編寫一個計算器

網頁設計中編寫一個計算器

發布時間:2020-09-04 18:38:21

1、怎樣設計一個簡單的計算器(請寫明代碼)

什麼語言的?
下面是java的:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Counter2 extends WindowAdapter
{
static JFrame f=new JFrame("計算器");
static JTextField text1=new JTextField("0.");
static String source="";
static String cal="";
static String object="";
static boolean flag=false;
static boolean flag1=true;
static boolean flag2=false;
public void init()
{
try
{
Container c=f.getContentPane();
JPanel pan1=new JPanel();
JButton b1=new JButton("1");
JButton b2=new JButton("2");
JButton b3=new JButton("3");
JButton b4=new JButton("4");
JButton b5=new JButton("5");
JButton b6=new JButton("6");
JButton b7=new JButton("7");
JButton b8=new JButton("8");
JButton b9=new JButton("9");
JButton b0=new JButton("0");
JButton b11=new JButton("+");
JButton b12=new JButton("-");
JButton b13=new JButton("*");
JButton b14=new JButton("/");
JButton b15=new JButton(".");
JButton b16=new JButton("=");
JButton bclar=new JButton("清零");
text1.setHorizontalAlignment(JTextField.RIGHT);
c.add(text1,"North");
c.add(pan1);
A aa=new A();
Result re=new Result();
Opertion op=new Opertion();
Clar cl=new Clar();
b1.addActionListener(aa);
b2.addActionListener(aa);
b3.addActionListener(aa);
b4.addActionListener(aa);
b5.addActionListener(aa);
b6.addActionListener(aa);
b7.addActionListener(aa);
b8.addActionListener(aa);
b9.addActionListener(aa);
b0.addActionListener(aa);
b11.addActionListener(op);
b12.addActionListener(op);
b13.addActionListener(op);
b14.addActionListener(op);
b16.addActionListener(re);
b15.addActionListener(aa);
bclar.addActionListener(cl);
pan1.add(b1);
pan1.add(b2);
pan1.add(b3);
pan1.add(b11);
pan1.add(b4);
pan1.add(b5);
pan1.add(b6);
pan1.add(b12);
pan1.add(b7);
pan1.add(b8);
pan1.add(b9);
pan1.add(b13);
pan1.add(b0);
pan1.add(b15);
pan1.add(b16);
pan1.add(b14);
pan1.add(bclar);
f.setSize(200,220);
f.setVisible(true);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}

}
class A implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String a=text1.getText();
String s=e.getActionCommand();
if(a.equals("0.")||a.equals("+")||a.equals("-")||a.equals("*")||a.equals("/"))
text1.setText(s);
else {
if(flag2)
{
text1.setText(s);
flag2=false;
}
else
text1.setText(a+s);

}
}
}
class Opertion implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
cal=e.getActionCommand();
if(flag1==true)
source=text1.getText();
text1.setText(cal);
flag1=false;
flag=true;
}
}
class Result implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double num1;
num1=Double.parseDouble(source);
object=text1.getText();
double num2;
num2=Double.parseDouble(object);
double result=0;
if(cal.equals("+"))
result=num1+num2;
if(cal.equals("-"))
result=num1-num2;
if(cal.equals("*"))
result=num1*num2;
if(cal.equals("/"))
if(num2==0)
text1.setText("除數不能為0");
else
result=num1/num2;
String s1=Double.toString(result);
text1.setText(s1);
flag1=true;
flag2=true;
}
}
class Clar implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
text1.setText("0.");
}
}

public static void main(String[] args)
{
Counter2 count=new Counter2();
count.init();
}

public void windowClosing(WindowEvent e){
System.exit(1);
}
public void windowOpened(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
}

2、編寫設計一個簡單計算器

要什麼樣的?網頁版的可以嗎?

3、怎麼用dreamweaver製作一個簡單的類似計算器的東西?

1、點擊按鈕能夠計算,需要在按鈕源代碼上設置:onclick="vbscript:calc()"
calc()是測試和計運算元程序,就是說,在head部分,有這個:
<script language="vbscript">
sub calc()
end sub
</script>

2、要檢測輸入的數值,比如檢測a的數值:
if len(window.text1.value)=0 then '沒有輸入a
msgbox "請輸入a的值"
window.text1.focus() '將焦點設置到text1,並退出計運算元程序
exit sub
elseif not isnumeric(window.text1.value) then ''text1不是數字
msgbox "請輸入一個數字"
window.text1.focus() '將焦點設置到text1,並退出計運算元程序
exit sub
else
a=csng(window.text1.value)
if a<=0 then
'''''''''在這裡面處理a的數值非法的情況,如果你需要
exit sub
end if
end if

3、依次檢測b、c、d的數值,其中的d我不太明白,你說允許位0,那麼其對數值無異議。

4、程序的最後,是計算
e=3.78*log(a) + 11.2*Log(b)+ 9.57*Log(c) + 6.43*Log(d)
window.text5.value=cstr(e)

5、不考慮錯誤檢測的代碼
<input type='button' id='btnCalc' onclick="vbscript:calc()">
<script language="vbscript">
sub calc()
dim a,b,c,d,e
a=csng(window.text1.value)
b=csng(window.text2.value)
c=csng(window.text3.value)
d=csng(window.text4.value)
e=3.78*log(a) + 11.2*Log(b)+ 9.57*Log(c) + 6.43*Log(d)
window.text5.value=cstr(e)
end sub
</script>

4、html網頁計算器代碼怎麼寫?

html網頁計算器代碼編寫過程如下:

5、編寫程序實現一個簡單的計算器。(用菜單設計)

界面設計的代碼我就不寫了,我在這里描述一下:
放置兩個TextBox,分別命名為txtP1,txtP2
放置一個TextBox,命名為txtResult
放置一個ComboBox,命名為drpOperation,在Items屬性裡面添加(Collection),分別為+,-,*,/
放置一個Button,命名為btnCalculate
雙擊btnCalculate
代碼如下:
private void btnCalculate_Click(object sender, EventArgs e)
{
double p1=Convert.ToDouble(txtP1.Text);//從txtP1中獲取參數p1
double p2=Convert.ToDouble(txtP2.Text);//從txtP1中獲取參數p1
double result;//定義運算結果
string Operation=drpOperation.SelectedItem.Text;//獲取運算符
switch(Operation)
{
case "+":result=p1+p2;break;
case "-":result=p1-p2;break;
case "*":result=p1*p2;break;
case "/":result=p1/p2;break;
}//這里判斷從ComboBox獲取的符號,以求進行相應的運算
txtResult.Text=result.ToString();//顯示運算結果
}

6、編程設計一個簡單的計算器程序

這種運算比較麻煩,不過4種運算符號優先順序相同應該簡單寫,我這里有個演算法,能進行簡單的四則運算,delphi的,供參考 

Function Math_Evaluate(S0:string):Extended; 

Function Evaluate(S0:String):Extended;Forward; 

Procere CleanUp(var s0:string); 

Var 

I:integer; 

Begin 

S0:=LowerCase(s0); 

I:=Pos(' ',s0); 

While I>0 Do 

Begin 

Delete(S0,I,1); 

I:=Pos(' ',S0); 

End; 

End; 

Function GetFirstOpp(Tot:Integer;S0:String):Integer; 

Const 

Sopps:String=('+-*/^'); 

Var 

I:Integer; 

Begin 

If Tot=0 Then Tot:=Length(S0); 

For I:=1 To 5 Do 

Begin 

Result:=Pos(Sopps[i],S0); 

If ((I<3) And (Result>0)) Then 

If ((Result=1) Or (Pos(S0[Result-1],Sopps)>0)) Then 

Result:=0; 

If Result>0 Then 

If Result<Tot Then 

Exit; 

End; 

If Result>Tot Then 

Result:=0; 

End; 

Function SpecialF(P1:Integer;S0:String):Extended; 

Var 

Operstr:String; 

Arg:Extended; 

Begin 

Result:=0; 

Operstr:=Copy(S0,1,P1-1); 

If S0[Length(S0)]<>')' Then 

Exit; 

Operstr:=LowerCase(Operstr); 

Arg:=Evaluate(Copy(S0,P1+1,Length(S0)-P1-1)); 

if Operstr ='sin' Then 

Result:=Sin(Arg) 

Else if Operstr ='cos' Then 

Result:=Cos(Arg) 

Else if Operstr ='tan' Then 

Result:=Sin(Arg)/Cos(Arg) 

Else if Operstr ='arctan' Then 

Result:=Arctan(Arg) 

Else if Operstr ='log' Then 

Result:=Ln(Arg)/Ln(10) 

Else if Operstr ='ln' Then 

Result:=Ln(Arg) 

Else if Operstr ='exp' Then 

Result:=Exp(Arg) 

Else if Operstr ='sqrt' Then 

Result:=Sqrt(Arg) 

{enter additional functions here} 

Else Exit; 

End; 

Function GetValue(S0:String):Extended; 

Begin 

Result:=0; 

If Length(S0)<1 Then Exit; 

If Length(S0)=1 Then 

Result:=StrToFloat(S0) 

Else 

Case s0[1] Of 

'x':Result:=1; 

'y':Result:=1; 

'z':Result:=1; 

Else Result:=StrToFloat(S0); 

End; 

End; 

Procere MatchBracket(Var I:Integer;S0:String); 

Var 

J,Len:Integer; 

Begin 

J:=1; 

Len:=Length(S0); 

Repeat Inc(I); 

If I>Len Then Exit; 

If S0[I]='(' Then Inc(J); 

If S0[I]=')' Then Dec(J); 

If J<0 Then Exit; 

Until J=0; 

End; 

Function Calculate(P1:Integer;S0:String):Extended; 

Var 

V1,V2:Extended; 

Begin 

Result:=0; 

V1:=Evaluate(Copy(S0,1,P1-1)); 

V2:=Evaluate(Copy(S0,P1+1,Length(s0)-P1)); 

Case S0[P1] Of 

'+': Result:=V1+V2; 

'-': Result:=V1-V2; 

'/': Result:=V1/V2; 

'*': Result:=V1*V2; 

'^': Result:=Exp(V2*Ln(V1)); 

Else Exit; 

End; 

End; 

Function Evaluate(S0:string):Extended; 

Var 

P1,P2,Q1:Integer; 

Begin 

P1:=Pos('(',S0); 

P2:=P1; 

If P2>0 Then 

MatchBracket(P2,S0); 

If P1=1 Then 

Begin 

If P2=Length(S0) Then 

Begin 

Delete(S0,P2,1); 

Delete(S0,1,1); 

Result:=Evaluate(S0); 

End Else 

Result:=Calculate(P2+1,S0); 

Exit; 

End; 

Q1:=GetFirstOpp(P1,S0); 

If (P1+Q1=0) Then 

Begin 

Result:=GetValue(S0); 

Exit; 

End; 

If Q1<>0 Then 

Result:=Calculate(Q1,S0) 

Else If Length(S0)>P2 Then 

Result:=Calculate(P2+1,S0) 

Else 

Result:=SpecialF(P1,S0); 

End; 

Begin 

Try 

CleanUp(S0); 

Result:=Evaluate(S0); 

Except 

Result:=0; 

End; 

End;

7、網頁設計與製作設計一個頁面,實現一個四則計算器

這個代碼其實網上有很多現成的,而且做有CSS布局各種形式的都有,自己多花點時間搜索下就可以找到 。

8、用記事本編寫一個可以在網頁上進行簡單計算的計算器,要能看到過程的

我有javascript版本的

9、利用JavaScript腳本語言編寫一個簡單的「網頁計算器」

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>無標題文檔</title>
<script language="javascript">
function jsq(fh)
{
var num1,num2;
num1=parseFloat(document.form1.text1.value);
num2=parseFloat(document.form1.text2.value);
if(fh=="+")
document.form1.text3.value=num1+num2;
if(fh=="-")
document.form1.text3.value=num1-num2;
if(fh=="*")
document.form1.text3.value=num1*num2;
if(fh=="/")
if(num2!=0)
{
document.form1.text3.value=num1/num2;
}
else
{
alert ("除數不能為零!")
}
}
</script>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
<label>
<input name="text1" type="text" id="text1" />
</label>
<p>
<label>
<input name="text2" type="text" id="text2" />
</label>
</p>
<p>
<label>
<input name="Button1" type="Button" id="Button1" value="+" onClick="jsq('+')">
<input name="Button2" type="Button" id="Button2" value="-" onClick="jsq('-')"/>
<input name="Button3" type="Button" id="Button3" value="*" onClick="jsq('*')"/>
<input name="Button4" type="Button" id="Button4" value="/" onClick="jsq('/')"/>
</label>
</p>
<p>
<label>
<input name="text3" type="text" id="text3" />
</label>
</p>
</form>
</body>
</html>

10、怎麼在網頁上製作一個簡易計算器

(1)先畫個頁面,把每個數字 運算符號放進去;
(2)然後每個數字 符號綁定一個事件,這個事件獲取數字或者符號的值,放進一個Input框;
(3)點擊計算,把Input框的字元串表達式計算,eval("1*2");
(4)還有計算前要對表達式進行校驗,不規則的表達式不能計算。

與網頁設計中編寫一個計算器相關的知識