fun_displayexpression.cpp

来自「本程序是完成一个函数计算器的功能,通过输入表达式,然输入表达的未知数,则可以计算」· C++ 代码 · 共 166 行

CPP
166
字号
#include "head.h"

extern FILE *fp;
extern expression expre;
extern double result ;

//显示文件夹中前10个公式
void ViewExpression()
{
	expression  s;
	int l_number = 1 ;
	if((fp=fopen(FileName ,"r"))==NULL) //FileName 文件不存在,则退出
	{
		printf("Can't open file %s.\n",FileName );
		return ;
	}
	s.p_Expression = (char*)(char*)malloc(50);
	while(ReadExpression(fp,&s)!=0)       //读取文件中的内容,为文件未尾结束
	{
		if(l_number <= 10)                //若文件的内容个数大于10时,则显示前10个,若<10,则全显示出来
		{
			DisplayExpression(&s);          /* 显示一个公式 */
			printf("\n");
		}
		else 
		{
			return ;
		}
		l_number++ ;		
	}
	fclose(fp);
	return;
}
//显示文件夹中次10个公式
void ViewExpression2()
{
	expression  s;
	int l_number = 1 ;
	if((fp=fopen(FileName ,"r"))==NULL)      //FileName 文件不存在,则退出
	{
		printf("Can't open file %s.\n",FileName );
		return ;
	}
	s.p_Expression = (char*)(char*)malloc(50);
	while(ReadExpression(fp,&s)!=0)           //读取文件中的内容,为文件未尾结束
	{
		if(l_number <= 10)                     //若文件的内容个数大于10时,则不显示前10个
		{
			l_number++ ; 
			continue ;
		}
		else if(l_number <= 20)              //若文件的内容个数大于10时,则次10个,若<20,则全显示出来第11到未尾的公式
			{
				DisplayExpression(&s);         //* 显示一个公式 *//
				printf("\n");
			}
			else
			{
				return ;
			}
		l_number++ ;		
	}
	fclose(fp);
	return;
}
//显示文件夹中全部公式
void ViewExpression3(int page_no)
{
	expression  s;
	int l_number = 0  ;
	if((fp=fopen(FileName ,"r"))==NULL)          //FileName 文件不存在,则退出
	{
		printf("Can't open file %s.\n",FileName );
		return ;
	}
	s.p_Expression = (char*)(char*)malloc(50);
	while(ReadExpression(fp,&s)!=0)              //显示文件中所有内容
	{
		DisplayExpression(&s);                   ///* 显示一个公式 */
		printf("\n");
		l_number++ ;
		if(l_number % page_no == 0)            //并且每次显示25个公式,按任意键显示下面未显示公式
		{
		//	clrscr();                          //清屏操作
			getchar();
		}
	}
	fclose(fp);
	return;
}
void ListExprssion(char *fname)
{
	int i_subLabel = 0 ,errorFlag ,l_number;
	char c ;
	while(1)
	{
		SubMenu();
		scanf("%d" , &i_subLabel); // 输入菜单号
		switch(i_subLabel)
		{
			case 1: ViewExpression(); //显示文件夹中前10个公式
				    break ;
			case 2: ViewExpression2() ;//显示文件夹中次10个公式
				    break ;
			case 3: ViewExpression3(25) ; //显示文件夹中全部公式程序,因屏幕范围有限,所以每次显示25个,按任意键显示下页
					break ;
OPERATE:	case 4: printf("you only read the expression which you want to Operate from the file .\n");
					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 ;
						}
					}
					TranslationRPorland(expre.p_Expression) ;//转化为逆波兰表达式
					 errorFlag = CalcMain() ;                //对逆波兰表达式进行解析并计算其结果
					 switch(errorFlag)
					 {
						 case 0 : printf("%lf", result);  //计算正确,返回0
								  break ;
						case 1 : printf("acos(x) of x is not satisfied conditions\n"); //反余弦函数acos(x)中的x不满足条件,返回2'
							      break ;
						 case 2 : printf("asin(x) of x is not satisfied conditions\n"); // 反正弦函数asin(x)中的x不满足条件,返回3
							      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: RemoveExpression(); //删除公式
					break ;
			case 6: return ;        //返回主菜单
			default : printf("I'm sorry ,you input label that is not exist . Please input again!!!\n ");
				     break  ;      //输入菜单号不存,重新输入
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?