當前位置:首頁 » 編程軟體 » 用腳本語言打造求解器

用腳本語言打造求解器

發布時間: 2022-07-18 10:19:37

㈠ 求高手幫忙,用javascript腳本語言編寫個最簡單的計算器,如圖所示

Calculator.java:
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.applet.*;
public class Calculator extends Applet implements ActionListener{
Panel buttonPanel;
TextField tf; //用於顯示輸入的數字的文本框
Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,bDot,bPlus,bSub,bDiv,bMulti,bEqual; //按鍵
String onDisplay=""; //顯示在文本框中的字元串
boolean isDotPressed=false; //按鍵'.'是否被按下
float operand; //通過按鍵輸入的數
float operand1,operand2;//operand1和operand2為計算用的操作數
char operator; //計錄所用的操作符
float result; //計算結果
int times=1; //小數點後有幾位的10次方冪
public void init(){
setLayout(new BorderLayout(5,5));
tf=new TextField(30);
add(tf,BorderLayout.NORTH);
buttonPanel=new Panel();
buttonPanel.setLayout(new GridLayout(4,4,5,5));
add(buttonPanel,BorderLayout.CENTER);
//下面依次把按鈕添加上去

b1=new Button("1");
buttonPanel.add(b1);
b1.addActionListener(this);
b1.setActionCommand("1");

b2=new Button("2");
buttonPanel.add(b2);
b2.addActionListener(this);
b2.setActionCommand("2");

b3=new Button("3");
buttonPanel.add(b3);
b3.addActionListener(this);
b3.setActionCommand("3");

bPlus=new Button("+");
buttonPanel.add(bPlus);
bPlus.addActionListener(this);
bPlus.setActionCommand("+");

b4=new Button("4");
buttonPanel.add(b4);
b4.addActionListener(this);
b4.setActionCommand("4");

b5=new Button("5");
buttonPanel.add(b5);
b5.addActionListener(this);
b5.setActionCommand("5");
b6=new Button("6");
buttonPanel.add(b6);
b6.addActionListener(this);
b6.setActionCommand("6");

bSub=new Button("-");
buttonPanel.add(bSub);
bSub.addActionListener(this);
bSub.setActionCommand("-");

b7=new Button("7");
buttonPanel.add(b7);
b7.addActionListener(this);
b7.setActionCommand("7");

b8=new Button("8");
buttonPanel.add(b8);
b8.addActionListener(this);
b8.setActionCommand("8");

b9=new Button("9");
buttonPanel.add(b9);
b9.addActionListener(this);
b9.setActionCommand("9");

bMulti=new Button("*");
buttonPanel.add(bMulti);
bMulti.addActionListener(this);
bMulti.setActionCommand("*");

b0=new Button("0");
buttonPanel.add(b0);
b0.addActionListener(this);
b0.setActionCommand("0");

bDot=new Button(".");
buttonPanel.add(bDot);
bDot.addActionListener(this);
bDot.setActionCommand(".");

bEqual=new Button("=");
buttonPanel.add(bEqual);
bEqual.addActionListener(this);
bEqual.setActionCommand("=");

bDiv=new Button("/");
buttonPanel.add(bDiv);
bDiv.addActionListener(this);
bDiv.setActionCommand("/");

}

public void actionPerformed(ActionEvent e){
String str=e.getActionCommand();
char b=str.charAt(0);
switch(b){
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': onDisplay+=b;
operand=operand*10+Integer.parseInt(str);
if(isDotPressed) times*=10;
tf.setText(onDisplay);
break;
case '.': onDisplay+=b;
isDotPressed=true;
tf.setText(onDisplay);
break;
case '+':
case '-':
case '*':
case '/': operator=b;
operand1=operand/times;
System.out.println(operand1);
onDisplay="";
times=1;
isDotPressed=false;
operand=0;
break;
case '=': operand2=operand/times;
System.out.println(operand2);
switch(operator){
case '+': result=operand1+operand2;break;
case '-': result=operand1-operand2;break;
case '*': result=operand1*operand2;break;
case '/': result=operand1/operand2;break;
}
tf.setText(float.toString(result));
onDisplay="";
times=1;
isDotPressed=false;
operand=0;
break;
}
}
}

Calculator.html:
<html>
<applet code="Calculator.class" width=250 height=250>
</applet>
</html>

㈡ 利用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>

㈢ 用javascript腳本語言編寫一個 計算器計算加減乘除 謝謝,很急!大家幫幫忙呀!!!

<html>

<head>

<title>runcode</title>

<scripttype="text/javascript">

functionrun(t){

varsa=parseInt(document.getElementById('sa').value);

varsb=parseInt(document.getElementById('sb').value);

varsc=document.getElementById('sc');

if(isNaN(sa)||isNaN(sb)){

alert("輸入錯誤,請重新輸入!");

return;

}

switch(t){

case1:

sc.value=sa+sb;

break;

case2:

sc.value=sa-sb;

break;

case3:

sc.value=sa*sb;

break;

case4:

if(sb==0){

alert("被除數不能為0!");

return;

}

sc.value=sa/sb;

break;

}

}

</script>

</head>

<body>

<tablewidth="255"height="136"border="1"style="background:#66CCFF;font-size:12px;">

<tr>

<tdcolspan="3">計算器</td>

</tr>

<tr>

<tdwidth="62"height="34">第一個數</td>

<tdwidth="102"><inputname="text"type="text"id="sa"width="100px"onclick="this.value=''"value=""/></td>

<tdwidth="29"rowspan="3"><inputname="button"type="button"onclick="run(1)"value="+"/>

<inputname="button2"type="button"onclick="run(2)"value="-"/>

<inputname="button3"type="button"onclick="run(3)"value="*"/>

<inputname="button4"type="button"onclick="run(4)"value="/"/></td>

</tr>

<tr>

<tdheight="32">第二個數</td>

<td><inputname="text2"type="text"id="sb"width="100px"onclick="this.value=''"value=""/></td>

</tr>

<tr>

<td>計算結果</td>

<td><inputname="text3"type="text"id="sc"width="100px"value=""/></td>

</tr>

</table>

<p>&nbsp;</p>

</body>

</html>

<html>

<head>

<title>runcode</title>

<scripttype="text/javascript">

functionrun(t){

varsa=parseInt(document.getElementById('sa').value);

varsb=parseInt(document.getElementById('sb').value);

varsc=document.getElementById('sc');

if(isNaN(sa)||isNaN(sb)){

alert("輸入錯誤,請重新輸入!");

return;

}

switch(t){

case1:

sc.value=sa+sb;

break;

case2:

sc.value=sa-sb;

break;

case3:

sc.value=sa*sb;

break;

case4:

if(sb==0){

alert("被除數不能為0!");

return;

}

sc.value=sa/sb;

break;

}

}

</script>

</head>

<body>

<tablewidth="255"height="136"border="1"style="background:#66CCFF;font-size:12px;">

<tr>

<tdcolspan="3">計算器</td>

</tr>

<tr>

<tdwidth="62"height="34">第一個數</td>

<tdwidth="102"><inputname="text"type="text"id="sa"width="100px"onclick="this.value=''"value=""/></td>

<tdwidth="29"rowspan="3"><inputname="button"type="button"onclick="run(1)"value="+"/>

<inputname="button2"type="button"onclick="run(2)"value="-"/>

<inputname="button3"type="button"onclick="run(3)"value="*"/>

<inputname="button4"type="button"onclick="run(4)"value="/"/></td>

</tr>

<tr>

<tdheight="32">第二個數</td>

<td><inputname="text2"type="text"id="sb"width="100px"onclick="this.value=''"value=""/></td>

</tr>

<tr>

<td>計算結果</td>

<td><inputname="text3"type="text"id="sc"width="100px"value=""/></td>

</tr>

</table>

<p>&nbsp;</p>

</body>

</html>

c語言寫腳本解釋器

記錄大括弧!!!
遇到if則判斷語句,接下來,然後假設判斷式成立,執行if緊跟著的語句,把大括弧括起來的當成一個語句,執行下去,如果沒有else的話就繼續,有else的話跳過else所包含的范圍.

㈤ 我想自己用C/C++做一個腳本語言解釋器,但是不知道需要什麼知識

對於腳本解釋的,只是調用了系統的api吧,應該是這樣子,你自己用程序寫,然後調用系統api,然後獲取返回內容就行了
比如普通的命令,,復制,你可以定義成其他名字,調用的api或者是直接用cmd下可以用的命令直接用
至於讀取你的程序執行內容,那根讀取文本一樣
舉個例子
包含必要頭文件
if讀取內容正確,先讀命令,讀到空格,一直往後讀到參數

執行相關代碼,比如特定的api函數,或者是直接用cmd下的命令,system("calc");這個是打開計算器,包含頭文件windows.h
繼續循環到開頭重新讀

㈥ 如何用 ANTLR 4 實現自己的腳本語言

在 ANTLR 里,我們寫的規則,會生成解析器的代碼,這個解析器,會把目標腳本,解析成一個抽象語法樹。這顆抽象語法樹上,越是靠近葉子節點的地方,結合優先順序越高,越是靠近根的地方,結合優先順序越低,根據這個特點,我們就可以讓 ANTLR 幫我們完成以上的規則:
addExpression
: mulExpression (('+' | '-') mulExpression)*
;
mulExpression
: primaryExpression (('*' | '/') primaryExpression)*
;
primaryExpression
: Decimal
| '(' addExpression ')'
;

上面展示的 ANTLR 規則,在 primaryExpression
中,包括兩個可選項,要麼是數字,要麼是括弧表達式,是最高優先順序,然後是 mulExpression,優先順序最低的是 addExpression
。括弧表達式內,是一個 addExpression ,所以,這是一個循環結構,可以處理無限長的四則運算式,比如
1+2*3-(4+5)/6+7+8,會被解析為如下的語法樹:
addExpression : 1 + child1_1 - child1_2 + 7 + 8
child1_1 mulExpression : 2 * 3
child1_2 mulExpression : child1_2_1 / 6
child1_2_1 addExpression : 4 + 5

熱點內容
ceph緩存變慢 發布:2025-02-07 11:46:52 瀏覽:923
python做什麼用的 發布:2025-02-07 11:46:46 瀏覽:563
o2o與資料庫設計 發布:2025-02-07 11:35:27 瀏覽:928
ftp伺服器推薦 發布:2025-02-07 11:35:16 瀏覽:700
吉利星瑞豪華加6000是有哪些配置 發布:2025-02-07 11:25:18 瀏覽:971
李字加工編程 發布:2025-02-07 11:23:50 瀏覽:881
linux安全運維 發布:2025-02-07 11:14:19 瀏覽:737
阿里雲集群伺服器 發布:2025-02-07 11:12:38 瀏覽:453
如何選擇家庭最佳配置 發布:2025-02-07 11:06:50 瀏覽:89
javatomcat伺服器搭建伺服器 發布:2025-02-07 10:55:22 瀏覽:624