📄 strtounionclass.cpp
字号:
#include "stdafx.h"
#include "StrToUnionClass.h"
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
//构造函数
StrToUnionClass::StrToUnionClass( char *Char )
{
strcpy(str,Char) ;
}
//析构函数
StrToUnionClass::~StrToUnionClass()
{
for(int i=0 ;i<100 ;i++)
str[i]='\0' ;
}
void StrToUnionClass::translate_char()
{
head = NULL ;
for(int i = 0 ; i < strlen(str) ;)
{
pnew=(struct strData *)malloc(sizeof(struct strData)) ;//开辟新结点
//pnew=new struct strData ;
///////////////////////////////结点初始化/////////////////////////////////
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化为一个字符 或 数字
for ( int 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 StrToUnionClass::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"} ;
for(int 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 前面几个是有顺序的
}
}
}
double StrToUnionClass::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 StrToUnionClass::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 StrToUnionClass::insert(strData *insert_befor, 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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -