⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dialog_slidercolor.c

📁 移植到嵌入式上的科学计算器
💻 C
📖 第 1 页 / 共 3 页
字号:
/*
*********************************************************************************************************
*                                                uC/GUI
*                        Universal graphic software for embedded applications
*
*                       (c) Copyright 2002, Micrium Inc., Weston, FL
*                       (c) Copyright 2002, SEGGER Microcontroller Systeme GmbH
*
*              礐/GUI is protected by international copyright laws. Knowledge of the
*              source code may not be used to write a similar product. This file may
*              only be used in accordance with a license and should not be redistributed
*              in any way. We appreciate your understanding and fairness.
*
----------------------------------------------------------------------
File        : Dialog_All.c
Purpose     : Example demonstrating DIALOG and widgets
----------------------------------------------------------------------
*/

#include <stddef.h>
#include "GUI.h"
#include "DIALOG.h"
//#include "DROPDOWN.h"

/*********************************************************************
*
*       static data
*
**********************************************************************
*/
#define idBtn 1000
static char strBtn[40][7]={ "tanh","cosh" ,"sin","cos" ,
							"["   ,"sinh" ,"tg" ,"ctg" ,
							"]"   ,"ln"   ,"^","lg"  ,
							"!"   ,"arcsin","e^","sqrt",
							"x"   ,"arcos","pi" ,"e"   ,

							"off" ,"("    ,")"  ,"back" ,
							"7"   ,"8"    ,"9"  ,"+"    ,
							"4"   ,"5"    ,"6"  ,"-"    ,
							"1"   ,"2"    ,"3"  ,"*"    ,
							"0"   ,"."    ,"="  ,"/"    
};

BUTTON_Handle ahButton[52];
BUTTON_Handle hBtn[40];
EDIT_Handle   hEdit;
//////////////////////////////////////////////////////////////////////
#include <MATH.H>
#include <STRING.H>
//#include <iostream.h>
#include <stdlib.h>
#define pi 3.1415926535897932384626433832795
#ifndef NULL
#define NULL 0
#endif

struct  strData
{  
	int             nCharDouble;
	char			cChar;
	double		    dDouble;		
	struct  strData *next;
	struct  strData *before;
} ;
struct strData *head, *temp, *pnew;//定义结构体的一个头结点,一个中间变动的结点,一个用以申请的临时结点	    	 
char str[100];//存要处理的字符串;
struct strData *phead, *p;//定义一个头指针和一个移动的指针

struct strData mydata[1024];

void Start_StrToUnionClass(char *Char);//构造函数 
void Kill_StrToUnionClass();//析构函数
void translate_char(); //把要处理的字符串转化为结构体
void insert(struct strData *insert_befor , struct strData *insert_new);//用于结构体的插入操作
void amend();        //修正得到的结构体,如4(3+1)-->4*(3+1) , sin7-->sin(7),提高程序的应用性
char check(char *funcName);//把常用的数学函数用一个大写字母表示  如sin 为 H ,sqrt 为 S;
double longNum(char *c,int tag,int bit);//把处理的字符串中的表示数字的字符化为double 型
//////////////////////
void doubletochar(char outchar[],double ind)
{
	char mychar[10]={'0','1','2','3','4','5','6','7','8','9'};
	int i=0;
	char tmp[255];
	int tempi=0;
	int ini;
	double xiao;
	if(ind<0)
	{
		outchar[0]='-';
		ind=-ind;
		i=1;
	}	
	ini=ind;
	xiao=ind-ini;
	while(ini)
	{
		int newi=(int)ini/10;
		tmp[tempi]=mychar[ini-newi*10];
		ini=newi;tempi++;
	}
	tmp[tempi]='0';tempi++;
	while(tempi)
	{
		outchar[i++]=tmp[--tempi];
	}
	outchar[i++]='.';
	outchar[i++]=0;
	//
	tempi=0;
	while(tempi<5 && xiao>0.0001)
	{
		int newi=xiao*10;
		tmp[tempi++]=mychar[newi];
		xiao=xiao*10-newi;
	}
	tmp[tempi++]=0;
	strcat(outchar,tmp);	
}
//构造函数
void Start_StrToUnionClass( char *Char ) 
{
  	 strcpy(str,Char) ;
}

//析构函数
void Kill_StrToUnionClass() 
{
	int i;
     for(i=0 ;i<100 ;i++)
		 str[i]='\0' ;
}
	

void translate_char()
{ 
	int i;
	head = NULL ; 

	for(i = 0 ; i < (int)strlen(str) ;)
	{
		//pnew=(struct  strData *)malloc(sizeof(struct strData)) ;//开辟新结点	 

		pnew=&mydata[i];
        ///////////////////////////////结点初始化/////////////////////////////////
		if(head  ==  NULL)  { temp = head = pnew ;   temp  ->  before = NULL ; }  //是头结点
		else  //连结链表
		{
			pnew -> before = temp  ;
		    temp=temp -> next = pnew ;
		}

		temp -> dDouble=0 ;//初值
		temp -> cChar=' ' ;
		temp -> next=NULL ;//它的下一个结点没东西

		switch( str[i] )//对具体的字符以具体的处理
		{//switch
			
		    case '+' : 				   
				        temp -> nCharDouble=1 ;//1表示是字符 0 是数字					
				        temp -> cChar='+' ;   //存相应的字符
						i++ ;
						break ;
					  
			case '-' :  temp -> nCharDouble=1 ;  temp -> cChar='-' ; i++ ;break ; 
			case '*' :  temp -> nCharDouble=1 ;  temp -> cChar='*' ; i++ ;break ; 
			case '/' :  temp -> nCharDouble=1 ;  temp -> cChar='/' ; i++ ;break ; 
			case '!' :  temp -> nCharDouble=1 ;  temp -> cChar='!' ; i++ ;break ; 
			case '(' :  temp -> nCharDouble=1 ;  temp -> cChar='(' ; i++ ;break ; 
			case ')' :  temp -> nCharDouble=1 ;  temp -> cChar=')' ; i++ ;break ; 
			case '^' :  temp -> nCharDouble=1 ;  temp -> cChar='^' ; i++ ;break ; 
			case '[' :  temp -> nCharDouble=1 ;  temp -> cChar='[' ; i++ ;break ;
			case ']' :  temp -> nCharDouble=1 ;  temp -> cChar=']' ; i++ ;break ;
			case '=' :  temp -> nCharDouble=1 ;  temp -> cChar='=' ; i++ ;break ;
			case 'e' :  temp -> nCharDouble=0 ;  temp -> dDouble =2.718281828 ;	 i++ ;break ;
			case 'p' :  temp -> nCharDouble=0 ;  temp -> dDouble =3.1415926535897932384626433832795 ;  i=i+2 ;break ;
			case 'P' :  temp -> nCharDouble=0 ;  temp -> dDouble =3.1415926535897932384626433832795 ;  i=i+2 ;break ;
           
			default :
				{
					int	j=0 ;
					int	tagdian=0;//标记小数点 1 有小数点,0 无小数点
						int	bit=0 ;//小数点后是几位
					char	tmpNum[20] ;//存要处理的临时字符串,如sin cos 2.334434,3343254化为一个字符 或 数字
					int n;
					for ( n=0 ; n < 20 ; tmpNum[n++] = '\0') ;//初始化,用处不大
					
					//////////////处理函数字符串//////////////// 
					
					if(  (str[i] >= 65 && str[i] <= 90) || (str[i] >= 97 && str[i] <= 122) )//要处理的是A- -> Z or a- -> z的字符 
					{   
						temp -> nCharDouble=1 ;					
						while( (str[i] >= 65  &&  str[i] <= 90) || (str[i] >= 97  &&  str[i] <= 122))//把要处理的字符用数组装起来
						{    
							tmpNum[j++]=str[i++] ;
							
						}
						if( strcmp( tmpNum , "log" ) == 0 && str[i++] == '1' && str[i++] == '0')temp -> cChar == 'Q' ;//log10
						else temp -> cChar=check(tmpNum) ;//调用函数把字符串用一个字母替换
						
					}
					
					if(j != 0)break ;//上面的操作用了j ,证明了输入的是字符,可以break了
					
					
					
					/////////////////////处理 数字 字符串//////////////////// 
					
					while( str[i] >= 48 && str[i] <= 57||str[i] == '.')//要处理的是数字 0- -> 9  加小数点.
					{  
						if(str[i] == '.') {tagdian=1 ;i++ ;continue  ;}//有小数 tagdian要标记
						if(tagdian == 1)  bit=bit+1 ;//有小数输入时 当前输入的是第几位小数
						temp -> nCharDouble=0 ;  //标记要存的是数字
						tmpNum[19]=str[i++] ;//用数组的一位存一位数字,用数组的最后一位是为了调用函数中的字符指针,用tmpNum+19也就刚好只一位了
						temp -> dDouble=longNum(tmpNum+19,tagdian,bit) ;//调用函数计算数字
					}        
					break ;
				}

			}//end switch
			
	}//end for

    amend();////修正得到的结构体,如4(3+1)-->4*(3+1) , sin7-->sin(7),提高程序的应用性
	

}



char check(char *funcName)
{
    char	compare[][7]={"sin","cos","tg","ctg","sinh","cosh","tgh","Arcsin","Arcos",
			                "lg","ln","sqrt",
							"tan" ,"tanh","asin","arcsin","acos","arcos","arcos","log"} ;
	int i;
	for(i=0 ;i<19 ;i++)
	  if(strcmp(compare[i],funcName) == 0)//比较得要处理的字符串和数组的第几个相同
	  {  
		 switch(i)
		 {
			 case 12: return 'J' ;
			 case 13: return 'N' ;
			 case 14: 
			 case 15: return 'O' ;
			 case 16: 
			 case 17: return 'P' ;
			 case 18: return 'R' ;
			 default :
			    return(72+i) ;// i >= 0,i <= 12 前面几个是有顺序的
		 }
	  } 
return 'R';
}

double longNum(char *c,int tag,int bit)
{
	
    if(tag == 0)     return temp -> dDouble*10+atoi(c) ;//输入的是整数部分

	else		     return temp -> dDouble+pow(10,-1*bit)*atoi(c) ;//输入的是小数部分


}



void amend()
{
	struct strData *pt, *ptnew;
    for(pt=head;pt->next !=NULL;pt=pt->next )
	{
	   if(pt->nCharDouble == 0/*是数字*/&&pt->next->cChar == '(')// 2(-->2*(
	   {  
           ptnew=(struct  strData *)malloc(sizeof(struct strData)) ;//开辟新结点
		   ptnew->cChar ='*';ptnew->dDouble =0;ptnew->nCharDouble =1;
		   insert(pt,ptnew);
		   
	   } 
       if(pt->cChar>=72&&pt->cChar<=83/*是函数*/&&pt->next->nCharDouble == 0)// sin8-->sin(8)  也是H8-->H(8)
	   {
            ptnew=(struct  strData *)malloc(sizeof(struct strData)) ;//开辟新结点
			ptnew->cChar ='(';ptnew->dDouble =0;ptnew->nCharDouble =1;
			insert(pt,ptnew);
			ptnew=(struct  strData *)malloc(sizeof(struct strData)) ;//开辟新结点
			ptnew->cChar =')';ptnew->dDouble =0;ptnew->nCharDouble =1;
			//cout<<pt->next->cChar << "         "<<pt->next->dDouble <<endl;
		
			insert(pt->next->next,ptnew);
	   }



	 }

}

void insert(struct strData *insert_befor , struct strData *insert_new)
{
	insert_new->before =insert_befor;
    insert_new->next =insert_befor->next ;
	insert_befor->next->before =insert_new;
	insert_befor->next=insert_new;

}
///////////////////
void Start_CountClass(struct strData *head);//构造函数
void OnSLOVE() ;//把存有数学表达式的结构体计算出来
void mistake(char cfun,double dkuo);//检查是否有错误,有就显示错误
void SiSlove(struct  strData *left);//计算最内层()[]中的表达式
double Fun_switch(double dkuo,char Funhuhao );//对一个函数字母转化为函数计算  如H(3)为sin(3), S(444.44)为sqrt(444.44)
void Start_CountClass(struct strData *tmphead)
{
	phead=p=tmphead ;
}
///////////////////////////////////////////////////////////////////
/////  5+(-5+(-6+55))+(454*34-5)
////   5+(-5+49)+(454*34-5)

void OnSLOVE() 
{   
    struct strData		*fabs_left,*left ;//记下左[和(
	int					nadd=0, naddFabs=0 ;//标记()[]中字符的长度
	double				 dTemp=0 ;//一个放数据的临时地方  

	for(  ;  p-> next !=NULL  ; (dTemp==0)?p=p-> next: p)
   {   //for 	    
	   dTemp = 0  ;
	   if( p-> cChar == '[')   { fabs_left=p ;  naddFabs=0 ; }  // 记住左绝对值的指针,naddFabs记下[]中的长度
	   if( p-> cChar == '(')   { left=p ;  nadd=0 ; }        // 记住左括号的指针,记下()中的长度
	   naddFabs++, nadd++ ;
	  
		/////////////////////////求一个最里层的括号或绝对值//////////////////////////
        if(p-> cChar == ')'||p-> cChar == ']')//nadd !=0标记了进入这个if
		{	//if1
			
		    dTemp=1  ;   //dTemp=0 或不用这条语句,答案就不对了 why???
		    if(p-> cChar == ']')   { nadd = naddFabs ; left = fabs_left ; } // left=fabs_left ;  //长度,指针 统一给nadd,
		     	 
		    if(nadd>3)  //sin(3)  刚好三位 即括号内还要计算
			{   			
				 SiSlove(left-> next) ;	//调用函数计算最内层()[]中的表达式			
			} 
	
            if(p-> cChar == ']') left -> next -> dDouble =fabs(left -> next -> dDouble ) ;    //计算绝对值
			  	///在这里为止,括号中只有一个数了
          
		    if( left -> before != NULL && left -> before -> cChar >='H' && left -> before -> cChar <= 'S')    //left 的前一个cChar是一个表示函数的符号
			  {//if2     求函数值 把结果替换掉函数名           
				  dTemp=Fun_switch(left -> next -> dDouble,left-> before -> cChar ) ;   //求函数值
				  left-> before-> nCharDouble =0 ;//是一个数
				  left-> before-> cChar =' ' ;
				  left-> before-> dDouble = dTemp ;//用这些取代函数名
             ///去掉(*)[] 中的东西, 以下再改变指针
				  
				  left-> before-> next =p-> next  ;

⌨️ 快捷键说明

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