import_main.cpp
来自「本程序是完成一个函数计算器的功能,通过输入表达式,然输入表达的未知数,则可以计算」· C++ 代码 · 共 260 行
CPP
260 行
/*//////////////////////////////////////////////////////////////////////
//
// 函数计算器
//
// 广控信息技术(上海)有限公司
// 朱定芬
// zhu-dfhiro-con.com.cn
// 2007.9.30
//
//////////////////////////////////////////////////////////////////////*/
/*//////////////////////////////////////////////////////////////////////
//
// 功能:
// 求输入的函数表达式值,
//
// 特点:
// 1. 公式输入的限制,如"a+sin(b*log(c))/exp(d)"
// 2. 关于文件登录的限制
// 3. 登录函数一览表
// 4. 登录规定值
//////////////////////////////////////////////////////////////////////*/
/*//////////////////////////////////////////////////////////////////////
//
// 使用方法:
// 1. 主菜单
// 2. 一览表显示
// 3. 公式输入
// 4. 公式删除
// 5. 运算
// 6. 文件格式
//
/////////////////////////////////////////////////////////////////////*
/*/////////////////////////////////////////////////////////////////////
//
// 支持的函数的编号及功能
//
// 编号 函数 功能 调用的math.h库函数 备注
//
// 1 acos(x) 反余弦函数 acos(x) -1<=x<=1
// 2 asin(x) 反正弦函数 asin(x) -1<=x<=1
// 3 atan(x) 反正切函数 atan(x)
// 4 atan2(y,x) 反正切函数 atan(y,x)
// 5 cos(x) 余弦函数 cos(x)
// 6 sin(x) 正弦函数 sin(x)
// 7 tan(x) 正切函数 tan(x)
// 8 cosh(x) 双曲余弦函数 cosh(x)
// 9 sinh(x) 双曲正弦函数 sinh(x)
// 10 tanh(x) 双曲正切函数 tanh(x)
// 11 exp(x) e的x次方 exp(x)
// 12 frexp(x,eptr) 把x分成尾数和指数 frexp(x,eptr)
// 13 ldexp(x,n) 返回x*2的n幂 ldexp(x,n)
// 14 log(x) 取自然对数 log10(x) x>0
// 15 log10(x) 取10的对数 log10(x) x>0
// 16 modf(x,y) x%y mod(x,y) y!=0
// 17 pow(x,y) x的y次方 pow(x,y)
// 18 sqrt(x) 对x开方 sqrt(x) x>=0
// 19 ceil(x) 不小于x的最大整数 ceil(x)
// 20 fabs(x) 取绝对值|x| fabs(x)
// 21 floor(x) ⊥x⊥ floor(x) 求不大于x的最大整数
// 22 fmodf(x,y) x%y fmod(x,y) y!=0
//
/////////////////////////////////////////////////////////////////////*/
#include "head.h"
expression expre; //表达式结构体变量
FILE *fp ; //表达式链表结构体变量
Bolan bolan[MAX];
double result ; //算术表达式的值
void main(void)
{
int i_mainLabel=0 , i_subLabel=0 ,l_number = 0 ,errorFlag,type;
char c ;
listExpression *buffer, *head ; //缓存用结构体链表表示
buffer = MakeList(FileName ) ; //构造链表buffer ,把文件FileName 的内容写入到链表中,即是缓存中
head = buffer ;
expre.p_Expression =(char*)malloc(50); // 为结构体分配内存
while(1)
{
//clrscr(); //清屏操作
MainMenu() ;
scanf("%d" , &i_mainLabel);
switch(i_mainLabel)
{
case 1: InputExpression() ; // 输入公式
if(CheckExpression(expre.p_Expression) ==0) //判断公式是否输入正确
{
printf("you input expression error\n");
break ;
}
printf("\nWhether save the expression.(Y/N)\n>"); // 是否把公式保存在文件中
getchar();
c=getchar();
if(c =='Y'||c =='y')
{
SaveExpression(); // 保存公式到文件中
}
if(CheckLoginFileOrExpression()==1) // 是否从公式登入还是文件登入
{
printf("Please input expression number that you want to login(1-50)\n");
printf(">");
fp=fopen(FileName ,"r");
while(1)
{
scanf("%d" , &l_number); //从文件登入,指定公式序号
if(SerchExpression(l_number , fp)==1) //若指定号存在,退出
{
break ;
}
else //若不存在,继续输入公式序号
{
printf("Please input again.\n");
printf(">");
continue ;
}
}
}
else
{
printf("Directly read expression from memory to login\n"); //直接从公式登入
}
printf("Whether Operate.(Y/N)\n>"); //是否进行运算
getchar();
c=getchar();
if(c =='Y'||c =='y') //若是,则继续进行运算
{
goto OPERATE;
}
else
{
break ;
}
case 2: ListExprssion(FileName); //显示公式列表
break ;
case 3: RemoveExpression(); //删除公式列表
break ;
case 4: printf("Read expression from 2 types( 1:file 2:buffur 3 Directly read )\n"); //进行演算三种类型:1 从文件登入 , 2 从缓存登入 3 直接登入
printf("Please input you want to select type:");
scanf("%d" , &type);
switch(type)
{
case 1: printf("Whether display all expressions ?(Y/N)\n"); // 是否显示文件中所有公式
printf(">");
getchar();
c = getchar() ;
if(c == 'Y' || c == 'y')
{
ViewExpression3(25) ;
}
printf("Please input expression number that you want to login(1-50)\n");
printf(">");
fp=fopen(FileName ,"r");
while(1)
{
scanf("%d" , &l_number); //从文件登入,指定公式序号
if(SerchExpression(l_number , fp)==1) //若指定号存在,进行运算
{
goto OPERATE ;
}
else //若不存在,继续输入公式序号
{
printf("Please input again.\n");
printf(">");
continue ;
}
}
case 2 :while(1) //从缓存中读取公式:输入公式序号 ,若不存在,继续输入,若存在,进行演算
{
printf("Please input expression number that you want to operate(1-50)\n");
printf(">");
scanf("%d" , &l_number);
do
{
if(head->g_iNumber == l_number) //判断缓存中是否有以输入的公式序号相等,有就进行运算,否则继续扫描缓存
{
expre.p_Expression = head->p_Expression ;
goto OPERATE ;
}
else
{
head = head->next ;
}
}while(head != NULL) ; //while(head != NULL)
printf("The number is%d expression is not exist buffer. \n" , l_number); // 输入公式序号不存在,继续输入
printf("Please input again.\n>");
}
case 3 : InputExpression() ; //直接从 输入公式登入
if(CheckExpression(expre.p_Expression) ==0) //判断公式是否输入正确,若正能确,进行运算,否则退出
{
printf("you input expression error\n");
break ;
}
else
{
break ;
}
}
OPERATE: TranslationRPorland(expre.p_Expression) ; //转化为逆波兰表达式
errorFlag = CalcMain() ; //对逆波兰表达式进行解析并计算其结果
switch(errorFlag)
{
case 0 : printf("result=%lf", result); //计算正确,返回0
break ;
case 1 : printf("acos(x) of x is not satisfied conditions\n"); //反余弦函数acos(x)中的x不满足条件,返回1
break ;
case 2 : printf("asin(x) of x is not satisfied conditions\n"); // 反正弦函数asin(x)中的x不满足条件,返回2
break ;
case 3 : printf("modf(x,y) of y is not satisfied conditions\n");//取模x%y函数modf(x,y)中y为0,返回3
break ;
case 4 : printf("log(x) of x is not satisfied conditions\n");//自然对数函数log(x),如果x<=0,则返回4
break ;
case 5 : printf("log10(x) of x is not satisfied conditions\n");//取10的对数函数log10(x),如果x<=0,则返回5
break ;
case 6 : printf("sqrt(x) of x is not satisfied conditions\n");//开方函数sqrt(x),如果x<0,则返回6
break ;
case 7 : printf(" Divisor mustn't be zero\n"); //除数不能为0 如果y=0 , 则返回7
break ;
default : printf("other error");// 计算中其它错误
break ;
}
printf("\nWhether Continue to Operate.(Y/N)\n>"); // 是否继续进行运算
getchar();
c=getchar();
if(c =='Y'||c =='y')
{
goto OPERATE;
}
else
{
break ;
}
case 5: printf("Whether Format the %s file.(Y/N)\n>" , FileName); //是否对文件FileName进行格式化,/*注没有完成此功能*/
getchar();
c=getchar();
if(c =='Y'||c =='y')
{
GenerateFileFormat(); //对FileName文件进行格式化
}
else
{
break ;
}
break ;
case 6: printf("Whether exit ?(Y/N)\n"); // 是否退出程序
printf(">");
getchar();
c = getchar() ;
if(c == 'Y' || c == 'y')
{
return ;
}
else
{
break ;
}
default: printf("I'm sorry ,you input label that is not exist . Please input again!!!\n "); //输入的菜单号不存在,提示重新输入
break ;
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?