文章目录:
帮我写一个网页计算器公式代码
写好了,代码如下,天气好冷啊!
!doctype html
html
head
meta charset="utf-8"
titletest/title
style
input{
width: 33px;
}
#compute{
width: 50px;
}
/style
/head
body
div id="content"
(input id="num1" type="text"*
input id="num2" type="text")/
input id="num3" type="text"+0.01=
input id="result" type="text"
input type="button" id="compute" value="计算"
/div
script
var n1 = document.getElementById('num1');
var n2 = document.getElementById('num2');
var n3 = document.getElementById('num3');
var rst = document.getElementById('result');
var com = document.getElementById("compute");
com.addEventListener("click",function(){
if(n1.value.length == 0 ||n2.value.length == 0 ||n3.value.length == 0){
alert("请填写完毕再进行计算!");
return false;
}
if(n3.value == 0){
alert("除数不能为 0!");
return false;
}
var result = (n1.value*n2.value)/n3.value+0.01;
rst.value = result;
})
/script
/body
/html
用C语言写的计算器源代码
#includestdio.h
#includeiostream.h
#includestdlib.h
#includestring.h
#includectype.h
typedef float DataType;
typedef struct
{
DataType *data;
int max;
int top;
}Stack;
void SetStack(Stack *S,int n)
{
S-data=(DataType*)malloc(n*sizeof(DataType));
if(S-data==NULL)
{
printf("overflow");
exit(1);
}
S-max=n;
S-top=-1;
}
void FreeStack(Stack *S)
{
free(S-data);
}
int StackEmpty(Stack *S)
{
if(S-top==-1)
return(1);
return(0);
}
DataType Peek(Stack *S)
{
if(S-top==S-max-1)
{
printf("Stack is empty!\n");
exit(1);
}
return(S-data[S-top]);
}
void Push(Stack *S,DataType item)
{
if(S-top==S-max-1)
{
printf("Stack is full!\n");
exit(1);
}
S-top++;
S-data[S-top]=item;
}
DataType Pop(Stack *S)
{
if(S-top==-1)
{
printf("Pop an empty stack!\n");
exit(1);
}
S-top--;
return(S-data[S-top+1]);
}
typedef struct
{
char op;
int inputprecedence;
int stackprecedence;
}DataType1;
typedef struct
{
DataType1 *data;
int max;
int top;
}Stack1;
void SetStack1(Stack1 *S,int n)
{
S-data=(DataType1*)malloc(n*sizeof(DataType1));
if(S-data==NULL)
{
printf("overflow");
exit(1);
}
S-max=n;
S-top=-1;
}
void FreeStack1(Stack1 *S)
{
free(S-data);
}
int StackEmpty1(Stack1 *S)
{
if(S-top==-1)
return(1);
return(0);
}
DataType1 Peek1(Stack1 *S)
{
if(S-top==S-max-1)
{
printf("Stack1 is empty!\n");
exit(1);
}
return(S-data[S-top]);
}
void Push1(Stack1 *S,DataType1 item)
{
if(S-top==S-max-1)
{
printf("Stack is full!\n");
exit(1);
}
S-top++;
S-data[S-top]=item;
}
DataType1 Pop1(Stack1 *S)
{
if(S-top==-1)
{
printf("Pop an empty stack!\n");
exit(1);
}
S-top--;
return(S-data[S-top+1]);
}
DataType1 MathOptr(char ch)
{
DataType1 optr;
optr.op=ch;
switch(optr.op)
{
case'+':
case'-':
optr.inputprecedence=1;
optr.stackprecedence=1;
break;
case'*':
case'/':
optr.inputprecedence=2;
optr.stackprecedence=2;
break;
case'(':
optr.inputprecedence=3;
optr.stackprecedence=-1;
break;
case')':
optr.inputprecedence=0;
optr.stackprecedence=0;
break;
}
return(optr);
}
void Evaluate(Stack *OpndStack,DataType1 optr)
{
DataType opnd1,opnd2;
opnd1=Pop(OpndStack);
opnd2=Pop(OpndStack);
switch(optr.op)
{
case'+':
Push(OpndStack,opnd2+opnd1);
break;
case'-':
Push(OpndStack,opnd2-opnd1);
break;
case'*':
Push(OpndStack,opnd2*opnd1);
break;
case'/':
Push(OpndStack,opnd2/opnd1);
break;
}
}
int isoptr(char ch)
{
if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='(')
return(1);
return(0);
}
void Infix(char *str)
{
int i,k,n=strlen(str);
char ch,numstr[10];
DataType opnd;
DataType1 optr;
Stack OpndStack;
Stack1 OptrStack;
SetStack(OpndStack,n);
SetStack1(OptrStack,n);
k=0;
ch=str[k];
while(ch!='=')
if(isdigit(ch)||ch=='.')
{
for(i=0;isdigit(ch)||ch=='.';i++)
{
numstr[i]=ch;
k++;
ch=str[k];
}
numstr[i]='\0';
opnd= atof(numstr);
Push(OpndStack,opnd);
}
else
if(isoptr(ch))
{
optr=MathOptr(ch);
while(Peek1(OptrStack).stackprecedence=optr.inputprecedence)
Evaluate(OpndStack,Pop1(OptrStack));
Push1(OptrStack,optr);
k++;
ch=str[k];
}
else if(ch==')')
{
optr=MathOptr(ch);
while(Peek1(OptrStack).stackprecedence=optr.inputprecedence)
Evaluate(OpndStack,Pop1(OptrStack));
Pop1(OptrStack);
k++;
ch=str[k];
}
while(!StackEmpty1(OptrStack))
Evaluate(OpndStack,Pop1(OptrStack));
opnd=Pop(OpndStack);
cout"你输入表达式的计算结果为"endl;
printf("%-6.2f\n",opnd);
FreeStack(OpndStack);
FreeStack1(OptrStack);
}
void main()
{
cout"请输入你要计算的表达式,并以“=”号结束。"endl;
char str[50];
gets(str);
Infix(str);
=================================================================
哈哈!给分吧!
怎样用c++类写一个可以运算公式的计算器源代码?
#includestdio.h #includectype.h #includestdlib.h #include conio.h char token[61]; int n=0; void error(void) { printf("ERROR!\n"); exit(1); } void match(char expected) { if(token[n]==expected) token[++n]=getchar(); else error(); } double term(void); double factor(void); double exp(void) { double temp=term(); while((token[n]=='+')||(token[n]=='-')) switch(token[n]) { case'+':match('+'); temp+=term(); break; case'-':match('-'); temp-=term(); break; } return temp; } double term(void) { double div; double temp=factor(); while((token[n]=='*')||(token[n]=='/')) switch(token[n]) { case'*':match('*'); temp*=factor(); break; case'/':match('/'); div=factor(); if(div==0) { printf("The divisor is zero!\n"); exit(1); } temp/=div; break; } return temp; } double factor(void) { double temp; char number[61]; int i=0; if(token[n]=='(') { match('('); temp=exp(); match(')'); } else if(isdigit(token[n])||token[n]=='.') { while(isdigit(token[n])||token[n]=='.') { number[i++]=token[n++]; token[n]=getchar(); } number='\0'; temp=atof(number); } else error(); return temp; } main() { double result; FILE *data=fopen("61590_4.dat","at"); if(data==NULL) data=fopen("61590_4.dat","wt"); if(data==NULL) return 0; token[n]=getchar(); result=exp(); if(token[n]=='\n') { token[n]='\0'; printf("%s=%g\n",token,result); fprintf(data,"%s=%g\n",token,result); } else error(); fclose(data); getch(); }
求基于Java开发的多功能计算器源码,要有注释的,谢谢!
import java.awt.*;
import java.awt.event.*;
public class Calculator extends WindowAdapter implements ActionListener
{
private double result=0,data1=0,radixPointDepth=1;
private boolean radixPointIndicate=false,resultIndicate=false;
private char prec='+';
private Frame f;
private TextField tf;
private Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17;
private Panel p;
static public void main(String args[])
{
Calculator de=new Calculator();
de.go();
}
public void go()
{
f=new Frame("计算器");
p=new Panel();
p.setLayout(new GridLayout(4,4));
tf=new TextField(30);
b1=new Button("7");
b2=new Button("8");
b3=new Button("9");
b4=new Button("+");
b5=new Button("4");
b6=new Button("5");
b7=new Button("6");
b8=new Button("-");
b9=new Button("1");
b10=new Button("2");
b11=new Button("3");
b12=new Button("*");
b13=new Button("0");
b14=new Button(".");
b15=new Button("=");
b16=new Button("/");
b17=new Button("清零");
f.add(tf,"North");
f.add(p,"Center");
f.add(b17,"South");
p.add(b1);
p.add(b2);
p.add(b3);
p.add(b4);
p.add(b5);
p.add(b6);
p.add(b7);
p.add(b8);
p.add(b9);
p.add(b10);
p.add(b11);
p.add(b12);
p.add(b13);
p.add(b14);
p.add(b15);
p.add(b16);
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);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b13.addActionListener(this);
b14.addActionListener(this);
b15.addActionListener(this);
b16.addActionListener(this);
b17.addActionListener(this);
f.addWindowListener(this);
f.setSize(250,190);
f.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String s;
s=e.getActionCommand();
switch(s.charAt(0))
{
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
if(resultIndicate)
{
result=0;
data1=0;
prec='+';
}
Integer Int1=new Integer(s);
if(radixPointIndicate)
{
radixPointDepth=radixPointDepth/10;
data1=data1+(Int1.intValue())*radixPointDepth;
}
else
{
data1=data1*10+(Int1.intValue());
}
Double displayNumber=new Double(data1);
tf.setText(displayNumber.toString());
resultIndicate=false;
break;
case '+': case '-':case '*':case '/':case '=':
if(s.charAt(0)!='='resultIndicate)
{
prec=s.charAt(0);
resultIndicate=false;
}
else
{
switch(prec)
{
case '+':
result=result+data1;
break;
case '-':
result=result-data1;
break;
case '*':
result=result*data1;
break;
case '/':
result=result/data1;
break;
}
}
radixPointIndicate=false;
radixPointDepth=1;
displayNumber=new Double(result);
tf.setText(displayNumber.toString());
if(s.charAt(0)!='=')
{
data1=0;
prec=s.charAt(0);
}
else
{
resultIndicate=true;
}
break;
case '.':
radixPointIndicate=true;
break;
}
if(s.equals("清零"))
{
result=0;
data1=0;
radixPointDepth=1;
tf.setText("");
}
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
*': case'/': optr.inputprecedence=2; optr.stackprecedence=2; break; case'(': optr.input
{ printf("overflow"); exit(1); } S-max=n; S-top=-1;}void FreeStack1(Stack1 *S){ free(S