主题:求如何使一个计算器连续计算?菜鸟求教高手斧正!
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
#define i 1e-6
#define e 3.1415926
void xiaoquan(float data1,float data2,char op);
void weijia(double d,char t[]);
main() /*这是一个用于简单计算的计算器*/
{
char judge1,judge2;
float data1,data2;
char op;
char t[3];
double d,a,b,c;
do
{
do
{
printf("该运算涉及三角函数吗(y/n)?:\n"); /*判断是否包含三角函数y/n*/
scanf(" %c",&judge1);
if(judge1=='n'||judge1=='N')
{
printf("请输入计算表达式:\n");
scanf("%f %c %f",&data1,&op,&data2);
printf("输入数是%f,%f\n输入符是%c\n",data1,data2,op);
/*确定操作数与操作符*/
xiaoquan(data1,data2,op); /*调用樊炽权的部分*/
}
else if(judge1=='y'||judge1=='Y') /*包含三角函数的部分*/
{
printf("请输入三角函数(后加空格或回车)和角度:");
scanf("%s%lf",t,&d);
printf("输入的三角函数是:%s\n",t); /*确定操作数与操作符*/
printf("角度是:%.2f\n",d);
weijia(d,t); /*调用刘伟佳的部分*/
}
else /*当错误时做出的提醒*/
{
printf("输入错误,请再次输入\n");
}
}while(judge1!='y'&&judge1!='Y'&&judge1!='n'&&judge1!='N');
/*判断是否输入错误*/
printf("continue(y/n)?\t"); /*判断用户是否想再次运算程序*/
scanf(" %c",&judge2);
printf("\n");
printf("\n");
}while (judge2=='Y'||judge2=='y');
system("pause");
}
void xiaoquan(float data1,float data2,char op)
{
int rem;
switch(op)
{
case'+':
printf("%f+%f=%f\n",data1,data2,data1+data2);
break;
case'-':
printf("%f-%f=%f\n",data1,data2,data1-data2);
break;
case'*':
printf("%f*%f=%f\n",data1,data2,data1*data2);
break;
case'/':
if(fabs(data2)<=i)
printf("Division by zero!\n");
else
printf("%f/%f=%f\n",data1,data2,data1/data2);
break;
case'%':
rem=(int)data1%(int)data2;
printf("%d\n",rem);
break;
default:
printf("Unknow operator!\n");
}
}
/*函数功能:对数据进行普通运算
函数入口参数:浮点型变量data1,data2
字符型变量op
函数返回值:无*/
void weijia(double d,char t[])
{
double w,a,b,c;
w=(d*e)/180;
a=sin(w);
printf("sin%.2f=%.2f\n",d,a);
b=cos(w);
printf("cos%.2f=%.2f\n",d,b);
c=a/b;
printf("tan%.2f=%.2f\n",d,c);
}
/*函数功能:计算三角函数的复杂运算
函数参数:双精度变量d,存储角度
字符串数组t
函数返回值:无
#include <stdlib.h>
#include<math.h>
#define i 1e-6
#define e 3.1415926
void xiaoquan(float data1,float data2,char op);
void weijia(double d,char t[]);
main() /*这是一个用于简单计算的计算器*/
{
char judge1,judge2;
float data1,data2;
char op;
char t[3];
double d,a,b,c;
do
{
do
{
printf("该运算涉及三角函数吗(y/n)?:\n"); /*判断是否包含三角函数y/n*/
scanf(" %c",&judge1);
if(judge1=='n'||judge1=='N')
{
printf("请输入计算表达式:\n");
scanf("%f %c %f",&data1,&op,&data2);
printf("输入数是%f,%f\n输入符是%c\n",data1,data2,op);
/*确定操作数与操作符*/
xiaoquan(data1,data2,op); /*调用樊炽权的部分*/
}
else if(judge1=='y'||judge1=='Y') /*包含三角函数的部分*/
{
printf("请输入三角函数(后加空格或回车)和角度:");
scanf("%s%lf",t,&d);
printf("输入的三角函数是:%s\n",t); /*确定操作数与操作符*/
printf("角度是:%.2f\n",d);
weijia(d,t); /*调用刘伟佳的部分*/
}
else /*当错误时做出的提醒*/
{
printf("输入错误,请再次输入\n");
}
}while(judge1!='y'&&judge1!='Y'&&judge1!='n'&&judge1!='N');
/*判断是否输入错误*/
printf("continue(y/n)?\t"); /*判断用户是否想再次运算程序*/
scanf(" %c",&judge2);
printf("\n");
printf("\n");
}while (judge2=='Y'||judge2=='y');
system("pause");
}
void xiaoquan(float data1,float data2,char op)
{
int rem;
switch(op)
{
case'+':
printf("%f+%f=%f\n",data1,data2,data1+data2);
break;
case'-':
printf("%f-%f=%f\n",data1,data2,data1-data2);
break;
case'*':
printf("%f*%f=%f\n",data1,data2,data1*data2);
break;
case'/':
if(fabs(data2)<=i)
printf("Division by zero!\n");
else
printf("%f/%f=%f\n",data1,data2,data1/data2);
break;
case'%':
rem=(int)data1%(int)data2;
printf("%d\n",rem);
break;
default:
printf("Unknow operator!\n");
}
}
/*函数功能:对数据进行普通运算
函数入口参数:浮点型变量data1,data2
字符型变量op
函数返回值:无*/
void weijia(double d,char t[])
{
double w,a,b,c;
w=(d*e)/180;
a=sin(w);
printf("sin%.2f=%.2f\n",d,a);
b=cos(w);
printf("cos%.2f=%.2f\n",d,b);
c=a/b;
printf("tan%.2f=%.2f\n",d,c);
}
/*函数功能:计算三角函数的复杂运算
函数参数:双精度变量d,存储角度
字符串数组t
函数返回值:无