#include<stdio.h>
#include<math.h>
float newx()
{
float x;
printf("Input the value X:");
scanf("%f",&x);
return x;
}
float newy()
{
float y;
printf("Input the value 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(fabs(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 Operate1(float x,float (*fun)() ) /*µ¥²Ù×÷Êý*/
{
printf("%.1f\n", (*fun)(x) );
}
void Operate2(float x,float y,float (*fun)(float x,float y)) /*Ë«²Ù×÷Êý*/
{
printf("%.1f\n",(*fun)(x,y));
}
void main()
{
int sel;
printf("choose the mode of calculate:\n");
printf("1:\'+\' 2:\'-\' 3:\'*\' 4:\'/\' 5:\'pow\' 6:\'cos\' 7\'tan\'\n");
printf("8:\'exp\' 9:\'fabs\' 10:\'LogE\' 11:\'Log10\' 12:\'sin\' 13:\'Sqrt\'\n");
do
{
scanf("%d",&sel);
switch(sel)
{
case 0:break;
case 1: Operate2(newy(),newx(),add);break;
case 2: Operate2(newy(),newx(),sub);break;
case 3: Operate2(newy(),newx(),mul);break;
case 4: Operate2(newy(),newx(),div);break;
case 5: Operate2(newy(),newx(),Pow);break;
case 6: Operate1(newx(),Cos); break;
case 7: Operate1(newx(),Tan); break;
case 8: Operate1(newx(),Exp); break;
case 9: Operate1(newx(),Fabs); break;
case 10:Operate1(newx(),Log); break;
case 11:Operate1(newx(),Log10);break;
case 12:Operate1(newx(),Sin) ; break;
case 13:Operate1(newx(),Sqrt); break;
} /*end switch() */
}while(sel!=0);
}
这是本人对上面的程序一点改进
希望大家提提意见,继续努力!!
这个计算器 不能对表达式计算,不是很理想
希望继续改进
~~~~~~~~~~~~~~~好好学习~~~~~~~~~~~~~~~~~~~~~~~~~~~~~