📄 jisuanqinew.cpp
字号:
// jisuanqinew.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Number.h"
#include "iostream.h"
#include "stdlib.h"
#include <stdio.h> // In Out printf()..
#include <math.h> // pow(M,n) 开 M^n
#include <string.h> // strcpy...
#include <conio.h> //getch()...
#define Max 256 // 表达式长度定义,可以在这里调节
// ===============================================
// 函数声明
// 计算字符串(不带括号的),计算的核心部分
char *Calculate_f(char *chpString);
// 主操作过程,输入式子串,返回 double 型结果
double Operation(char *chpString);
// Source1、Source2 加起来到 Destination 中
char *AddStrings_f(char *chpDestination, char *chpSource1, char *chpSource2);
// 寻找 char_to_find 在 Source 中的位置,后移一位
int FindChar(char *chpSource, char chCharToFind);
// 获取字符串的长度
int Len_f(char *chpSource);
// 将 Source 左边 Length 个字符放在 Destination 中
char *Left_f(char *chpSource, char *chpDestination, int nLength);
// 将 Source 右边 Length 个字符放在 Destination 中
char *Right_f(char *chpSource, char *chpDestination, int nLength) ;
// 将 Source 中从 Start 开始 Length 长度的字符串截下来放在 Destination 中
char *Midstr_f(char *chpSource, char *chpDestination, int nStart, int nLength);
// 在字符串中删除一个字符 del '+'
void DelChar(char *chpString,int sPos);
// 在字符串中插入一个字符
int InsChar(char *chpString,int sPos,char sChar);
// 替换字符串中的某个字符
void StrReplace(char *chpString,char strOld ,char strNew);
// 将实数值变为字符串
char *Str_f(double nValue, char *chpDestination);
//计算字符串的值,返回实数值
double Val_f(char *chpSource) ;
//计算一个表达式的值
void Expression(void);
//创建有一个操作数的运算对象
Number Creat1();
//创建有二个操作数的运算对象
Number Creat2();
// ===============================================
//显示总菜单
void Menu();
// 表达式主菜单...
int Menu_Sel();
// 手工输入表达式求值
int Do_Press();
// 文件导入表达式求值
int Do_File();
// 文件检查 0 重新输入,1 继续
int FileChk(char *FN);
// 式子的合法性检查 0 含有非法字符,1 正常 2 关系运算
int StrChk(char *chpSource);
// 关系运算
int Nexus(char strIn[]);
// 显示关于
void Show_About();
// ===============================================
// 全局变量声明
char *TF_Info[3]={"FALSE","TURE","Error"}; // 关系运算信息
int Debug=0;
void main(void)
{
Number *ptr1,*ptr2; //用于指向操作数的两个指针
while(1)
{
Menu(); //调用总菜单
int i; //存储功能选项
cin>>i;
switch(i) //根据输入选择功能
{
case 1:
ptr1=&Creat1();
ptr2=&Creat2();
cout<<(*ptr1).a<<"+"<<(*ptr2).a<<"="<<(*ptr1).Add((*ptr2))<<endl;
break;
case 2:
ptr1=&Creat1();
ptr2=&Creat2();
cout<<(*ptr1).a<<"-"<<(*ptr2).a<<"="<<(*ptr1).Sub((*ptr2))<<endl;
break;
case 3:
ptr1=&Creat1();
ptr2=&Creat2();
cout<<(*ptr1).a<<"*"<<(*ptr2).a<<"="<<(*ptr1).Mul((*ptr2))<<endl;
break;
case 4:
ptr1=&Creat1();
ptr2=&Creat2();
cout<<(*ptr1).a<<"/"<<(*ptr2).a<<"="<<(*ptr1).Dev((*ptr2))<<endl;
break;
case 5:
ptr1=&Creat1();
cout<<(*ptr1).a<<"!"<<"="<<(*ptr1).Fac()<<endl;
break;
case 6:
ptr1=&Creat1();
cout<<"Sqrt"<<"("<<(*ptr1).a<<")"<<"="<<(*ptr1).Sqr()<<endl;
break;
case 7:
ptr1=&Creat1();
cout<<"sin("<<(*ptr1).a<<")"<<"="<<(*ptr1).Sin()<<endl;
break;
case 8:
ptr1=&Creat1();
cout<<"cos("<<(*ptr1).a<<")"<<"="<<(*ptr1).Cos()<<endl;
break;
case 9:
Expression(); //调用表达式计算函数
break;
case 10:
exit(0);
default:
cout<<"Input error !"<<endl;
exit(0);
}
}
}
void Menu() //主菜单
{
cout<<" *********************My Calculator*********************"<<endl;
cout<<" * Please select the options: *"<<endl;
cout<<" * 1.Add. *"<<endl;
cout<<" * 2.Subtruct. *"<<endl;
cout<<" * 3.Multiply. *"<<endl;
cout<<" * 4.Devide. *"<<endl;
cout<<" * 5.Factorial. *"<<endl;
cout<<" * 6.Square. *"<<endl;
cout<<" * 7.Sin. *"<<endl;
cout<<" * 8.Cos. *"<<endl;
cout<<" * 9.To calculate an expression. *"<<endl;
cout<<" * 10.Exit. *"<<endl;
cout<<"I select:";
}
Number Creat1() //构造第一个操作数
{
cout<<"Please input the operand:";
Number A;
return A;
}
Number Creat2() //构造第二个操作数
{
cout<<"Please input the second operand:";
Number A;
return A;
}
char *Left_f(char *chpSource, char *chpDestination, int nLength) //将 Source 左边 Length 个字符放在 Destination 中
{
*(chpDestination+ --nLength+1)=0; // 设置目标字符串最后一个为 NULL
while (nLength>=0) // 直到目标字符串的最后一个
{
*(chpDestination+nLength)=*(chpSource+nLength--);
}
return chpDestination;
}
char *Midstr_f(char *chpSource, char *chpDestination, int nStart, int nLength) // 将 Source 中从 Start 开始 Length 长度的字符串截下来放在 Destination 中
{
chpSource+=nStart-1; // 设置源起点
*(chpDestination+--nLength+1)=0; // 设置目标字符串最后一个为 NULL
while (nLength>=0) // 直到目标字符串的最后一个
{
*(chpDestination+nLength)=*(chpSource+nLength--);
}
return chpDestination;
}
char *Right_f(char *chpSource, char *chpDestination, int nLength) // 将 Source 右边 Length 个字符放在 Destination 中
{
while (*chpSource != 0)
{
chpSource++;
} // 将源指针移到最后
chpSource-=nLength; //将源指针跳到开始复制点
*(chpDestination+--nLength+1)=0; // 设置目标字符串最后一个为 NULL
while (nLength>=0) // 直到目标字符串的最后一个
{
*(chpDestination+nLength)=*(chpSource+nLength--);
}
return chpDestination;
}
void DelChar(char *chpString,int sPos) // 在字符串中删除一个字符 del '+'
{
char sBuf[Max];
int nCount;
strcpy(sBuf,chpString);
for(nCount=sPos;sBuf[nCount];nCount++)
{
sBuf[nCount]=sBuf[nCount+1];
}
strcpy(chpString,sBuf);
}
int InsChar(char *chpString,int sPos,char sChar) //在字符串中插入一个字符
{
char sBuf[Max];
int iLen;
int nCount;
strcpy(sBuf,chpString);
iLen=strlen(sBuf);
if(iLen<Max)
{
sBuf[iLen+1]='\0';
for(nCount=iLen;nCount>=sPos;nCount--)
{
sBuf[nCount+1]=sBuf[nCount];
}
sBuf[sPos]=sChar;
strcpy(chpString,sBuf);
}
else
return 0;
return 1;
}
void StrReplace(char *chpString,char strOld ,char strNew) //替换字符串中的某个字符 '#' to '-'
{
char sBuf[Max];
int nCount=0;
strcpy(sBuf,chpString);
while(sBuf[nCount])
{
if (sBuf[nCount]==strOld) sBuf[nCount]=strNew;
nCount++;
}
strcpy(chpString,sBuf);
}
int FindChar(char *chpSource, char chCharToFind) // 寻找 char_to_find 在 Source 中的位置,后移一位
{
int nPos=0;
while(*(chpSource+nPos)!=0) // 直到目标字符串的最后一个
{
if (chCharToFind == *(chpSource+nPos++)) // 比较..
{
return nPos; //返回第一个出现点,加一
}
}
return 0;
}
int Len_f(char *chpSource) //获取字符串的长度
{
int nRetval=0; //初始化长度
while (*(chpSource+nRetval++)!=0){} //移动指针到 Null
return --nRetval;
}
char *Str_f(double nValue, char *chpDestination) // 将实数值变为字符串
{
char strTmp[Max];
gcvt(nValue,sizeof(double)+1,strTmp); //实数值转字符串
if(strTmp[0]=='-') //将 '-' 负号 转译为 '#'
{
strTmp[0]='#';
}
strcpy(chpDestination,strTmp);
if(Debug) cout<<"...Conversion Double to String:"<<nValue<<chpDestination<<endl;
return chpDestination;
}
char *AddStrings_f(char *chpDestination, char *chpSource1, char *chpSource2) //Source1、Source2 加起来到 Destination 中
{
char *chpTempdest=chpDestination;
while(*chpSource1!=0) // 先把 chpSource1 放入 chpDestination
{
*(chpTempdest++)=*(chpSource1++);
}
while(*chpSource2!=0) // 在 chpDestination 后继续写入 chpSource2
{
*(chpTempdest++)=*(chpSource2++);
}
*chpTempdest=0; // 指针位置归零
return chpDestination;
}
double Val_f(char *chpSource) // 计算字符串的值,返回实数值
{
double nResult=0.;
char strTmp[Max];
strcpy(strTmp,chpSource);
if(strTmp[0]=='#') // 将 '#' 转译为 '-' 负号
{
strTmp[0]='-';
}
nResult=atof(strTmp); // 字符串转为实数
if(Debug) cout<<"...Conversion String to Double:"<<chpSource<<strTmp<<nResult<<endl;
return nResult;
}
char *Calculate_f(char *chpString) // 计算字符串(不带括号的),计算的核心部分
{
char szBuf1[Max], szBuf2[Max], szBuf3[Max], szBuf4[Max], szBuf5[Max]; // buffers for string handlers
char sOps[2][4]={"^*+","^/-"}; // 呵呵,符号优先级问题已经解决
double nLeftnr; // 操作符左边的结果
double nRightnr; // 操作符右边的结果
double nResult; // 表达式的结果
int nOppos; // 操作符的位置+1
int nOppos2;
int nOp=0; // 用哪一个同级的操作符
int nCount; // 长度计数,就是两个操作符间的内容的长度
int nPosInOpstr; //操作符索引
if(Debug) cout<<"\n...Starting Calculate, The Parameter is:"<< chpString<<endl ;
for (nPosInOpstr=0; nPosInOpstr<3; nPosInOpstr++) // 同级关系运算符问题,有待解决
{ // szOpstr 中操作符的顺序就是优先级顺序
while (FindChar(chpString,sOps[0][nPosInOpstr])!=0||FindChar(chpString,sOps[1][nPosInOpstr])!=0) /* 寻找五种符号,当找不到就退出*/
{ // 提取左边的操作数计算
nOppos=FindChar(chpString,sOps[0][nPosInOpstr]); // 行 0 找到操作符的位置,+1
nOppos2=FindChar(chpString,sOps[1][nPosInOpstr]); // 行 1 找到操作符的位置,+1
if(Debug) cout<<"...Operator Priority Level:nOppos="<<nOppos<<"nOppos2="<<nOppos2<<endl;
if(nOppos==0) // 取靠前的,0 是个问题,跳过...
{
nOp=1;
nOppos=nOppos2;
}
else if(nOppos2==0)
{
nOp=0;
}
else if(nOppos>nOppos2)
{
nOp=1;
nOppos=nOppos2;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -