主题:一个简单的计算器!
不游泳的鱼
[专家分:620] 发布于 2004-05-15 17:08:00
#include<math.h>
#include<stdlib.h>
#include<iostream.h>
class Calculator
{
double a,b;
public:
Calculator(){a=0;b=0;}; //could be omitted
void newa()
{
double num;
cout<<"Input the number:";
cin>>num;
a=num;
}
void newab()
{
double num1,num2;
cout<<"Input the numbers.the first number:";
cin>>num1;
cout<<"Input the senond number:";
cin>>num2;
a=num1;
b=num2;
}
double Geta(){return a;}
double Getb(){return b;}
double Add(Calculator &A);
double Sub(Calculator *A);
double Mul(Calculator &A);
double Div(Calculator &A);
double Sin(Calculator &A);
double Cos(Calculator &A);
double Tan(Calculator &A);
double Exp(Calculator &A);
double Fabs(Calculator &A);
double LogE(Calculator &A);
double Log10(Calculator &A);
double Pow(double x,double y);
double Sqrt(double x);
};
double Calculator::Add(Calculator &A)
{
return A.a+A.b;
}
double Calculator::Sub(Calculator *A)
{
return A->a-A->b;
}
double Calculator::Mul(Calculator &A)
{
return A.a*A.b;
}
double Calculator::Div(Calculator &A)
{
if(A.b==0)
{cout<<"Error! The program will be terminated!"<<endl;exit(0);}
return A.a/A.b;
}
double Calculator::Sin(Calculator &A)
{
return sin(A.a);
}
double Calculator::Cos(Calculator &A)
{
return cos(A.a);
}
double Calculator::Tan(Calculator &A)
{
return tan(A.a);
}
double Calculator::Exp(Calculator &A)
{
return exp(A.a);
}
double Calculator::Fabs(Calculator &A)
{
return fabs(A.a);
}
double Calculator::LogE(Calculator &A)
{
return log(A.a);
}
double Calculator::Log10(Calculator &A)
{
return log10(A.a);
}
double Calculator::Pow(double x,double y)
{
return pow(x,y);
}
double Calculator::Sqrt(double x)
{
if(x<0)
{cout<<"Error! The program will be terminated!"<<endl;exit(0);}
return sqrt(x);
}
void main()
{
int sel;
Calculator cal;
cout<<"Welcome to use the calculator!please select."<<endl;
cout<<"1:\'+\' 2:\'-\' 3:\'*\' 4:\'/\' 5:\'sin\' 6:\'cos\' 7:\'tan\'"<<endl;
cout<<"8:\'exp\' 9:\'fabs\' 10:\'logE\' 11:\'log10\' 12:\'pow\' 13:\'sqrt\'"<<endl;
do
{
cout<<"Please select:(0--exit)";
cin>>sel;
switch (sel)
{
case 0: break;
case 1: cal.newab();
cout<<"The result is:"<<cal.Add(cal)<<endl;
break;
case 2: cal.newab();
cout<<"The result is:"<<cal.Sub(&cal)<<endl;
break;
case 3: cal.newab();
cout<<"The result is:"<<cal.Mul(cal)<<endl;
break;
case 4: cal.newab();
cout<<"The result is:"<<cal.Div(cal)<<endl;
break;
case 5: cal.newa();
cout<<"The result is:"<<cal.Sin(cal)<<endl;
break;
case 6: cal.newa();
cout<<"The result is:"<<cal.Cos(cal)<<endl;
break;
case 7: cal.newa();
cout<<"The result is:"<<cal.Tan(cal)<<endl;
break;
case 8: cal.newa();
cout<<"The result is:"<<cal.Exp(cal)<<endl;
break;
case 9: cal.newa();
cout<<"The result is:"<<cal.Fabs(cal)<<endl;
break;
case 10: cal.newa();
cout<<"The result is:"<<cal.LogE(cal)<<endl;
break;
case 11: cal.newa();
cout<<"The result is:"<<cal.Log10(cal)<<endl;
break;
case 12: cal.newab();
cout<<"The result is:"<<cal.Pow(cal.Geta(),cal.Getb())<<endl;
break;
case 13: cal.newa();
cout<<"The result is:"<<cal.Sqrt(cal.Geta())<<endl;
break;
default: cout<<"Your selection is ERROR! Select again!"<<endl;
}
}while(sel!=0);
}
本人水平有限!有错误请指正!谢谢
回复列表 (共17个回复)
沙发
xyz1 [专家分:0] 发布于 2004-05-15 11:56:00
我很想向你学习.请多指教!!
板凳
有事找我请烧钱 [专家分:500] 发布于 2004-05-15 12:49:00
这个程序是不是有点复杂化呢?
3 楼
jywzzz [专家分:1020] 发布于 2004-05-15 13:39:00
输入和输出有点复杂。不过毕竟是dos下的i/o啊,呵呵,我抄的一个html计数器。
大家可以看看,用的javascript。
<html>
<head><title>科学计数器</title>
<script language="javascript">
r=new Array(2);
function initialize(){
currOper="start";
r[0]="0";
r[1]="0";
operand="";
ix=0;
}
function addDigit(n){
if(currOper=="Int" || currOper=="Float")
r[ix]=appendDigit(r[ix],n)
else{
r[ix]=""+n;
currOper="Int";
}
showinForm(r[ix]);
}
function appendDigit(n1,n2){
if(n1=="0")
return ""+n2;
var s="";
s+=n1;
s+=n2;
return s;
}
function showinForm(s){
document.calculator.total.value=s;
}
function addDecimalPoint(){
if(currOper!="Float"){
decimal=true;
r[ix]+=".";
if(currOper=="Operator" || currOper=="getoperand")
r[ix]="0."
currOper="Float";
showinForm(r[ix]);
}
}
function clearDisplay(){
initialize();
showinForm(r[0]);
}
function changeSign(){
if(r[ix].charAt(0)=="-")
r[ix]=r[ix].substring(1,r[ix].length);
else if(parseFloat(r[ix])!=0)
r[ix]="-"+r[ix];
showinForm(r[ix]);
}
function setTo(n){
r[ix]=""+n;
currOper="Operator";
decimal=false;
showinForm(r[ix]);
}
function Count(){
if(currOper=="Int" || currOper=="Float" || currOper=="Operator"){
if(ix==1){
r[0]=doCounting(operand,r[0],r[1]);
ix=0;
}
}
else if(currOper=="getoperand2"){
r[0]=doCounting(operand,r[1],r[0]);
ix=0;
}
currOper="Operator";
decimal==false;
showinForm(r[ix]);
}
function doCounting(op,x,y){
var result="";
switch(op){
case "+":result=""+(parseFloat(x)+parseFloat(y));break;
case "-":result=""+(parseFloat(x)-parseFloat(y));break;
case "*":result=""+(parseFloat(x)*parseFloat(y));break;
case "/":if(parseFloat(y)==0){
alert("分子为0,错误!");
result=0;
}
else result=""+(parseFloat(x)/parseFloat(y));
break;
}
return result;
}
function performOp(op){
if(currOper=="start"){
++ix;
operand=op;
}
else if(currOper=="Int" || currOper=="Float" || currOper=="Operator"){
if(ix==0){
++ix;
operand=op;
}
else{
r[0]=doCounting(operand,r[0],r[1]);
showinForm(r[0]);
operand=op;
}
}
currOper="getoperand2";
decimal=false;
}
function doFunction(){
var selectionList=document.calculator.functions;
var selIX=selectionList.selectedIndex;
var sel=selectionList.options[selIX].value;
switch(sel){
case"abs":r[ix]=Math.abs(r[ix]);break;
case"acos":r[ix]=Math.acos(r[ix]);break;
case"asin":r[ix]=Math.asin(r[ix]);break;
case"atan":r[ix]=Math.atan(r[ix]);break;
case"ceil":r[ix]=Math.ceil(r[ix]);break;
case"cos":r[ix]=Math.cos(r[ix]);break;
case"exp":r[ix]=Math.exp(r[ix]);break;
case"floor":r[ix]=Math.floor(r[ix]);break;
case"log":r[ix]=Math.log(r[ix]);break;
case"sin":r[ix]=Math.sin(r[ix]);break;
case"sqrt":r[ix]=Math.sqrt(r[ix]);break;
case"tan":r[ix]=Math.tan(r[ix]);break;
}
decimal=false;
showinForm(r[ix]);
}
</script>
</head>
<body>
<script language="javascript">
initialize();
</script>
<h1><center>JYWZZZ.K做的计数器</h1>
<form name="calculator">
<table border="border" align="center">
<tr>
<td colspan="6">
<input type="text" name="total" value="0" size=44></td>
</tr>
<tr>
<td><input type="button" name="n1" value="1" onclick="addDigit(1)"></td>
<td><input type="button" name="n2" value="2" onclick="addDigit(2)"></td>
<td><input type="button" name="n3" value="3" onclick="addDigit(3)"></td>
<td><input type="button" name="plus" value="+" onclick="performOp('+')"></td>
<td colspan=1><input type="button" name="clearField" value="清除" onclick="clearDisplay()"></td>
<td colspan=1><input type="button" name="ln2" value="ln2" onclick="setTo(Math.LN2)"</td>
</tr>
<tr>
<td><input type="button" name="n4" value="4" onclick="addDigit(4)"></td>
<td><input type="button" name="n5" value="5" onclick="addDigit(5)"></td>
<td><input type="button" name="n6" value="6" onclick="addDigit(6)"></td>
<td><input type="button" name="minus" value="-" onclick="performOp('-')"></td>
<td colspan=1><input type="button" name="sqrt(2)" value="sqrt(2)" onclick="setTo(Math.SQRT2)"></td>
<td colspan=1><input type="button" name="ln10" value="ln10" onclick="setTo(Math.LN10)"</td>
</tr>
<tr>
<td><input type="button" name="n7" value="7" onclick="addDigit(7)"></td>
<td><input type="button" name="n8" value="8" onclick="addDigit(8)"></td>
<td><input type="button" name="n9" value="9" onclick="addDigit(9)"></td>
<td><input type="button" name="multiply" value="*" onclick="performOp('*')"></td>
<td colspan=1><input type="button" name="sqrt(1/2)" value="sqrt(1/2)" onclick="setTo(Math.SQRT1_2)"></td>
<td colspan=1><input type="button" name="log2e" value="log2(e)" onclick="setTo(Math.LOG2E)"</td>
</tr>
<tr>
<td><input type="button" name="n0" value="0" onclick="addDigit(0)"></td>
<td><input type="button" name="decimal" value="." onclick="addDecimalPoint()"></td>
<td><input type="button" name="sign" value="+/-" onclick="changeSign()"></td>
<td><input type="button" name="divide" value="/" onclick="performOp('/')"></td>
<td colspan=1><input type="button" name="random" value="random" onclick="setTo(Math.random())"></td>
<td colspan=1><input type="button" name="log10e" value="log10(e)" onclick="setTo(Math.LOG10E)"</td>
</tr>
<tr>
<td colspan=1 rowspan=1><input type="button" name="pi" value="pi" onclick="setTo(Math.PI)"</td>
<td colspan=1 rowspan=1><input type="button" name="equal" value="=" onclick="Count()"></td>
<td colspan=1 rowspan=1><input type="button" name="e" value="e" onclick="setTo(Math.E)"></td>
<td colspan=3 rowspan=1><b>函数:</b><select name="functions" size=1>
<option value="abs" selected>abs(x)</option>
<option value="acos">acos(x)</option>
<option value="asin">asin(x)</option>
<option value="atan">atan(x)</option>
<option value="ceil">ceil(x)</option>
<option value="cos">cos(x)</option>
<option value="exp">exp(x)</option>
<option value="floor">floor(x)</option>
<option value="log">log(x)</option>
<option value="sin">sin(x)</option>
<option value="sqrt">sqrt(x)</option>
<option value="tan">tan(x)</option>
</select>
<input type="button" name="apply" value="应用" onclick="doFunction()"></td>
</tr>
</table>
</form>
</body>
</html>
复制后存到一个*.htm文件,然后双击就可以打开了。
4 楼
zhangbaolong [专家分:0] 发布于 2004-05-16 04:48:00
[b]你好[/b][font=隶书][/font][size=5][/size][color=00FF00] [/color]实际上我并不会编程,但我很喜欢它。我爱好它。希望大家能帮帮我。
5 楼
jiali [专家分:0] 发布于 2004-05-16 10:32:00
那么复杂啊。。。我自己就是学计算机的,都不会啊。。怎么办?
6 楼
songxun [专家分:70] 发布于 2004-05-16 10:34:00
[em1]兄弟
你做的就是太好拉
7 楼
songxun [专家分:70] 发布于 2004-05-16 10:36:00
[em1]哥们
做的好啊
8 楼
小菜妹 [专家分:0] 发布于 2004-05-16 11:39:00
能不能给我一个通过编译恶毒复数四则运算的c程序??/不胜感激
9 楼
jakemanse [专家分:30] 发布于 2004-05-18 12:55:00
编的还不错,就是对于俺这个菜鸟来说真是一个难题啊,希望大哥们能多多指教啊!!俺的QQ是66740163.
10 楼
heason [专家分:0] 发布于 2004-05-22 20:23:00
总体的算法思想不错
但我想用运算符重载更好一点
我来回复