fun_file_format.cpp

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

CPP
259
字号
#include "head.h"

//expression expre;
extern FILE *fp;
extern Bolan bolan[MAX]; 

void GenerateFileFormat() 
{
	char in_exp[MAX] ;
	char string[MAX]={'\0'} ;
	int i=0 , j= 0 ;
	expression  sigle_expression ;
	listExpression  *head ,*node ,*temp , *bolan_node , *p , *new_bolanNode ;
	node = MakeList(FileName);                    //把FileName文件夹内容输入到链表node 中
	p =(listExpression *)malloc(sizeof(listExpression));  // 为结构体分配内存
	p->next = NULL ;
	bolan_node = p ;
	new_bolanNode = bolan_node ;
	head = node ;
	while(head != NULL) //while(head->next !=NULL)      
	{
		while(*head->p_Expression != '\0')
		{
			in_exp[i] = *head->p_Expression ;
			i++;
			head->p_Expression++;
		}
		in_exp[i] = '\0' ;	
		TranslationRPorland(in_exp) ;
		for(i=0;bolan[i].oper!='#';i++)
		{
			if(bolan[i].flag==0)
			{
				string[j] = bolan[i].value ;
				j++ ;
				continue ;
			}
			if(bolan[i].flag==1)
			{
				string[j] = bolan[i].oper ;
				j++ ;
				continue ;
			}
			if(bolan[i].flag==2)
			{
				switch(bolan[i].func)
				{
					case 1:    //反余弦函数acos(x) 
						string[j++] = 'a' ;
						string[j++] = 'c' ;
						string[j++] = 'o' ;
						string[j++] = 's' ;
						break ;
					case 2:   //反正弦函数asin(x)
						string[j++] = 'a' ;
						string[j++] = 's' ;
						string[j++] = 'i' ;
						string[j++] = 'n' ;
						break ;
					case 3:   //反正切函数atan(x)
						string[j++] = 'a' ;
						string[j++] = 't' ;
						string[j++] = 'a' ;
						string[j++] = 'n' ;
						break;
					case 4:   //反正切函数atan2(y,x)
						string[j++] = 'a' ;
						string[j++] = 't' ;
						string[j++] = 'a' ;
						string[j++] = 'n' ;
						string[j++] = '2' ;
					    break;
					case 5:   //余弦函数cos(x)
						string[j++] = 'c' ;
						string[j++] = 'o' ;
						string[j++] = 's' ;
						break;
					case 6:    //正弦函数sin(x)
						string[j++] = 's' ;
						string[j++] = 'i' ;
						string[j++] = 'n' ;
						break;
					case 7:    //正切函数tan(x)
						string[j++] = 't' ;
						string[j++] = 'a' ;
						string[j++] = 'n' ;
						break;
					case 8:    //双曲余弦cosh(x)
						string[j++] = 'c' ;
						string[j++] = 'o' ;
						string[j++] = 's' ;
						string[j++] = 'h' ;
						break;
					case 9:    //双曲正弦sinh(x)
						string[j++] = 's' ;
						string[j++] = 'i' ;
						string[j++] = 'n' ;
						string[j++] = 'h' ;
						break;
					case 10:    //双曲正切函数tanh(x)
						string[j++] = 't' ;
						string[j++] = 'a' ;
						string[j++] = 'n' ;
						string[j++] = 'h' ;
						break;
					case 11:    //e的x次方函数exp(x)
						string[j++] = 'e' ;
						string[j++] = 'x' ;
						string[j++] = 'p' ;
						break;
					case 12:    //把x分成尾数和指数 frexp(x,eptr)		
						string[j++] = 'f' ;
						string[j++] = 'r' ;
						string[j++] = 'e' ;
						string[j++] = 'x' ;
						string[j++] = 'p' ;
						break ;
					case 13 : //返回x*2的n幂  ldexp(x,n)
						string[j++] = 'l' ;
						string[j++] = 'd' ;
						string[j++] = 'e' ;
						string[j++] = 'x' ;
						string[j++] = 'p' ;
						break ;
					case 14:    //取对数函数log(x)
						string[j++] = 'l' ;
						string[j++] = 'o' ;
						string[j++] = 'g' ;
						break ;
					case 15 :   //取10的对数     log10(x)            
						string[j++] = 'l' ;
						string[j++] = 'o' ;
						string[j++] = 'g' ;
						string[j++] = '1' ;
						string[j++] = '0' ;
						break ;
					case 16:   //取模x%y函数modf(x,y)
						string[j++] = 'm' ;
						string[j++] = 'o' ;
						string[j++] = 'd' ;
						string[j++] = 'f' ;
						break ;
					case 17:    //x的y次方函数pow(x,y)
						string[j++] = 'p' ;
						string[j++] = 'o' ;
						string[j++] = 'w' ;
						break;
					case 18:   //开方函数sqrt(x)
						string[j++] = 's' ;
						string[j++] = 'q' ;
						string[j++] = 'r' ;
						string[j++] = 't' ;
						break ;
					case 19:  //不小于x的最大整数 ceil(x)
						string[j++] = 'c' ;
						string[j++] = 'e' ;
						string[j++] = 'i' ;
						string[j++] = 'l' ;
						break ;
					case 20:    //求绝对值函数fabs(x)
						string[j++] = 'f' ;
						string[j++] = 'a' ;
						string[j++] = 'b' ;
						string[j++] = 's' ;
						break;
					case 21:   //求不大于x的最大整数 floor(x)
						string[j++] = 'f' ;
						string[j++] = 'l' ;
						string[j++] = 'o' ;
						string[j++] = 'o' ;
						string[j++] = 'r' ;
						break;
					case 22:   //取模x%y函数fmod(x,y)
						string[j++] = 'f' ;
						string[j++] = 'm' ;
						string[j++] = 'o' ;
						string[j++] = 'd' ;
						break ;
			    }//switch(bolan[i].func)
				continue;
			}//if(bolan[i].flag==2)
		}
		string[j] = '\0' ;
		j = 0 ;
//		p =(listExpression *)malloc(sizeof(listExpression));  // 为结构体分配内存
//		bolan_node->next = p ;
		bolan_node->p_Expression = (char*)malloc(50);

	//	bolan_node->p_Expression = string;

		while(string[j] != '\0')								//把波兰表达式放入结构体的变量成员中
		{
			*bolan_node->p_Expression = string[j] ;
			j++ ;
			bolan_node->p_Expression++ ;
		}
		*bolan_node->p_Expression = '\0' ;
		bolan_node->p_Expression = bolan_node->p_Expression - j ;


		bolan_node->g_iNumber = head->g_iNumber ;
		bolan_node->next = p->next ;
		p =(listExpression *)malloc(sizeof(listExpression));  // 为结构体分配内存
	
		p->next=NULL;
		bolan_node->next = p ;
		bolan_node = bolan_node->next ;


		head = head->next ;
		for(i=0;bolan[i].oper!='#';i++)   //对存放波兰表达式的结构体初始化,为下次存储作准备
		{
			bolan[i].digit = 0.0 ;
			bolan[i].flag = 0 ;
			bolan[i].func = 0 ;
			bolan[i].oper = '\0' ;
			bolan[i].value = '\0' ;
		}
		bolan[i].oper = '\0' ;
		j = 0 ;
		while(string[j] !='\0')     //对字符数组string[MAX]初始化,为下次存储作准备
		{
			string[j] = '\0' ;
			j++ ; 
		}
		j = 0 ;
		while(in_exp[i] != '\0')     //对字符数组in_exp[MAX]初始化,为下次存储作准备
		{
			in_exp[i] = '\0' ;
			i++ ;
		}
		i = 0 ;
	}
	fp=fopen("FormatExpressions.txt" ,"w");

	do
	{                                        //把链表node 中的内容写入到文件FormatExression.txt 中
		sigle_expression.g_iNumber = new_bolanNode->g_iNumber ;
		sigle_expression.p_Expression = new_bolanNode->p_Expression ;
	//	fp=fopen("FormatExpressions.txt" ,"w");
		WriteExpression(fp,&sigle_expression);
		temp  =  new_bolanNode->next;
		new_bolanNode = temp ;							
	}while(new_bolanNode->next !=NULL);
	fclose(fp);                              // 关闭文件
	printf("The file has formated.\n");
}


		

		
		



	

⌨️ 快捷键说明

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