当前位置: 代码迷 >> C语言 >> 我的计算器
  详细解决方案

我的计算器

热度:154   发布时间:2006-08-05 17:34:04.0
我的计算器


#include<stdio.h>
#include<math.h>

float newx()
{
float x;
printf("请输入x的值:");
scanf("%f",&x);
return x;
}
float newy()
{
float y;
printf("请输入y的值:");
scanf("%f",&y);
return y;
}
float add(float m,float n)
{
return (m+n);
}
float sub(float m,float n)
{
return n-m;}
float mul(float m,float n)
{
return m*n;}
float div(float m,float n)
{
if(n==0)printf("ERROR!\n");
return n/m;}

float Sin(float n)
{return sin(n);}
float Cos(float n)
{return cos(n);}
float Tan(float n)
{
return tan(n);}
float Exp(float n)
{
return exp(n);}
float Fabs(float n)
{
return fabs(n);}
float Log( float n)
{
return log(n);}
float Log10(float n)
{
return log10(n);
}
float Pow(float m,float n)
{
return pow(m,n);
}
float Sqrt(float n)
{
if(n<0)printf("ERROR!\n");
return sqrt(n);
}
void main()
{int sel;

printf("请选择计算方式:\n");
printf("1:\'+\' 2:\'-\' 3:\'*\' 4:\'/\' 5:\'sin\' 6:\'cos\' 7\'tan\'\n");
printf("8:\'exp\' 9:\'fabs\' 10:\'LogE\' 11:\'Log10\ 12:\'pow\' 13:\'Sqrt\'\n");
scanf("%d",&sel);
do
{
switch(sel)
{ case 0:break;
case 1:
printf("%.1f\n",add(newy(),newx()));break;
case 2:
printf("%.1f\n",sub(newy(),newx()));break;
case 3:
printf("%.1f\n",mul(newy(),newx()));break;
case 4:
printf("%.1f\n",div(newy(),newx()));break;
case 5:
printf("%.1f\n",Sin(newx()));break;
case 6:
printf("%.1f\n",Cos(newx()));break;
case 7:
printf("%.1f\n",Tan(newx()));break;
case 8:
printf("%.1f\n",Exp(newx()));break;
case 9:
printf("%.1f\n",Fabs(newx()));break;
case 10:
printf("%.1f\n",Log(newx()));break;
case 11:
printf("%.1f\n",Log10(newx()));break;
case 12:
printf("%.1f\n",Pow(newx(),newy()));break;
case 13:
printf("%.1f\n",Sqrt(newx()));break;

}
scanf("%d",&sel);
}while(sel!=0);

}

搜索更多相关的解决方案: 计算器  

----------------解决方案--------------------------------------------------------

有看到有人求计算器的代码!这个是我写的!希望大家指点一下!


----------------解决方案--------------------------------------------------------
其中有处错误噢!仔细看下!
----------------解决方案--------------------------------------------------------

#include<stdio.h>
#include<math.h>

float newx()
{
float x;
printf("请输入x的值:");
scanf("%f",&x);
return x;
}
float newy()
{
float y;
printf("请输入y的值:");
scanf("%f",&y);
return y;
}
float add(float m,float n)
{
return (m+n);
}
float sub(float m,float n)
{
return n-m;}
float mul(float m,float n)
{
return m*n;}
float div(float m,float n)
{
if(n==0)printf("ERROR!\n");
return n/m;}

float Sin(float n)
{return sin(n);}
float Cos(float n)
{return cos(n);}
float Tan(float n)
{
return tan(n);}
float Exp(float n)
{
return exp(n);}
float Fabs(float n)
{
return fabs(n);}
float Log( float n)
{
return log(n);}
float Log10(float n)
{
return log10(n);
}
float Pow(float m,float n)
{
return pow(n,m);
}
float Sqrt(float n)
{
if(n<0)printf("ERROR!\n");
return sqrt(n);
}
void main()
{int sel;

printf("请选择计算方式:\n");
printf("1:\'+\' 2:\'-\' 3:\'*\' 4:\'/\' 5:\'sin\' 6:\'cos\' 7\'tan\'\n");
printf("8:\'exp\' 9:\'fabs\' 10:\'LogE\' 11:\'Log10\ 12:\'pow\' 13:\'Sqrt\'\n");
scanf("%d",&sel);
do
{
switch(sel)
{ case 0:break;
case 1:
printf("%.1f\n",add(newy(),newx()));break;
case 2:
printf("%.1f\n",sub(newy(),newx()));break;
case 3:
printf("%.1f\n",mul(newy(),newx()));break;
case 4:
printf("%.1f\n",div(newy(),newx()));break;
case 5:
printf("%.1f\n",Sin(newx()));break;
case 6:
printf("%.1f\n",Cos(newx()));break;
case 7:
printf("%.1f\n",Tan(newx()));break;
case 8:
printf("%.1f\n",Exp(newx()));break;
case 9:
printf("%.1f\n",Fabs(newx()));break;
case 10:
printf("%.1f\n",Log(newx()));break;
case 11:
printf("%.1f\n",Log10(newx()));break;
case 12:
printf("%.1f\n",Pow(newy(),newx()));break;
case 13:
printf("%.1f\n",Sqrt(newx()));break;

}
scanf("%d",&sel);
}while(sel!=0);

}


----------------解决方案--------------------------------------------------------

1. return log10(n); --〉 math.h中有这函数吗?我记得是log(n)吧;做对数运算时请判断一下真数是否大于0(即n>0),否则作出来的结果就是“复数”。

2. 我觉得main()函数中第一个scanf()可以放到do..while()中去,放在switch()语句上面,把第二个scanf()删掉。

3. 楼主应该不是女生吧!


----------------解决方案--------------------------------------------------------

谢谢啊!!


----------------解决方案--------------------------------------------------------

[QUOTE]math.h中有这函数吗?[/QUOTE]
double log10(double x) 计算x的常用对数

math.h 里的确有.不过他和log的具体区别.我也没太弄明白


----------------解决方案--------------------------------------------------------
double log10(double x)是求以10为底的对数,而double log(double x)是求以e为底的对数即ln(x).
----------------解决方案--------------------------------------------------------
恩,是这样的!
----------------解决方案--------------------------------------------------------
调用数学函数
----------------解决方案--------------------------------------------------------
  相关解决方案