2024-3-18

This commit is contained in:
2024-03-18 20:18:12 +08:00
parent 8933bff3c7
commit 59e0405033
51 changed files with 5263 additions and 0 deletions

View File

@@ -0,0 +1,282 @@
#include<iostream> //cout,cin
using namespace std;
#include"SqStack.h"
char pause;
char Precede(char t1, char t2) //算符的优先级比较
{
char f;
switch (t2)
{
case '+':
case '-':if (t1 == '(' || t1 == '=')
f = '<';
else
f = '>';
break;
case '*':
case '/':if (t1 == '*' || t1 == '/' || t1 == ')')
f = '>';
else
f = '<';
break;
case '(':if (t1 == ')')
{
cout << "ERROR1" << endl;
exit(0);
}
else
f = '<';
break;
case ')':switch (t1)
{
case '(':f = '=';
break;
case '=':cout << "ERROR2" << endl;
exit(0);
default: f = '>';
}
break;
case '=':switch (t1)
{
case '=':f = '=';
break;
case '(':cout << "ERROR2" << endl;
exit(0);
default: f = '>';
}
}
return f;
}
bool In(char ch) // 判断 ch 是否为运算符
{
switch (ch)
{
case'+':
case'-':
case'*':
case'/':
case'(':
case')':
case'=':return true;
default:return false;
}
}
float Operate(float a, char theta, float b)
{ // 实施一次运算
float ch;
switch (theta)
{
case'+':ch = a + b;
break;
case'-':ch = a - b;
break;
case'*':ch = a * b;
break;
case'/':ch = a / b;
}
return ch;
}
//算法3.12
float Val_Exp(char* exp) // 中缀表达式求值。
{
SqStack<char> OP; // 运算符栈
SqStack<float> OD; // 运算数栈
InitStack(OP, 30);
InitStack(OD, 30);
char theta;
float a, b, result;
char ch, x; // 存放由键盘接收的字符
char z[6]; // 存放符点数字符串
int i;
Push(OP, '='); // # 是表达式结束标志
ch = *exp++;
GetTop(OP, x);
while (ch != '=' || x != '=')
{
if (ch >= '0' && ch <= '9' || ch == '.') // ch 是操作数
{
i = 0;
do
{
z[i] = ch;
i++;
ch = *exp++;
} while (ch >= '0' && ch <= '9' || ch == '.');
z[i] = '\0';
result = atof(z); // 将字符串数组转为符点型存于result
Push(OD, result);
}
else if (In(ch)) // 是7种运算符之一
switch (Precede(x, ch))
{
case'<':Push(OP, ch); // 栈顶元素优先权低
ch = *exp++;
break;
case'=':Pop(OP, x); // 脱括号并接收下一字符
ch = *exp++;
break;
case'>':Pop(OP, theta); // 退栈并将运算结果入栈
Pop(OD, b);
Pop(OD, a);
Push(OD, Operate(a, theta, b));
}
else // ch是非法字符
{
cout << "ERROR3" << endl;;
exit(0);
}
GetTop(OP, x);
}
GetTop(OD, result);
DestroyStack(OP);
DestroyStack(OD);
return result;
}
void CreatePostExp(char* exp, char*& postexp) // 由中缀式求后缀式
{
char ch, x;
int i = 0;
SqStack<char> OP;
InitStack(OP, 30);
Push(OP, '='); // # 是表达式结束标志
cout << "中缀表达式:" << exp << endl;
ch = *exp++;
while (ch)
{
if ((ch >= '0' && ch <= '9') || ch == '.')
{
postexp[i++] = ch;
ch = *exp++;
}
if (In(ch)) // 是7种运算符之一
{
postexp[i++] = ' ';
GetTop(OP, x);
switch (Precede(x, ch))
{
case'<':Push(OP, ch); // 栈顶元素优先权低
ch = *exp++;
break;
case'=':Pop(OP, x); // 脱括号并接收下一字符
ch = *exp++;
break;
case'>':
Pop(OP, postexp[i++]); // 运算符出栈输出
break;
}
}
postexp[i] = '\0';
}//while
cout << "\n后缀表达式为:" << postexp << endl;
DestroyStack(OP);
}
//算法3.13
float Val_PostExp(char* postexp) // 后缀表达式求值
{
int i;
char z[6];
float result = 0, d = 0, a, b;
char ch;
SqStack<float> OD;
InitStack(OD, 30);
ch = *postexp++;
while (ch != '\0')
{
if (ch >= '0' && ch <= '9' || ch == '.') // ch为操作数符号
{
i = 0;
do
{
z[i++] = ch;
ch = *postexp++; // 取下一个操作数符号
} while (ch >= '0' && ch <= '9' || ch == '.');
z[i] = '\0';
d = atof(z); // 将字符串数组转为符点型存于result
Push(OD, d); // 操作数进栈
}
if (In(ch)) // ch为运算符
{
Pop(OD, a);
Pop(OD, b);
Push(OD, Operate(b, ch, a));
ch = *postexp++;
}
ch = *postexp++;
}
Pop(OD, result);
DestroyStack(OD);
return result;
}
//主函数
void main()
{
//int i;
char exp[20] = "(2.2+5)+4*(5-3.1)=";
char* postexp;
postexp = new char[30];
*postexp = '\0';
float v;
system("cls"); // 清屏
cout << "\n缺省表达式:" << exp;
cout << endl;
int choice;
do
{ // 显示主菜单
cout << endl;
cout << "1-创建表达式\n";
cout << "2-表达式求值\n";
cout << "3-求后缀表达式\n";
cout << "4-后缀表达式求值\n";
cout << "5-显示表达式\n";
cout << "0-退出\n";
cout << "输入功能选项1~50 退出):\n";
cin >> choice;
switch (choice)
{
case 1: // 创建表达式
cout << "\n请输入表达式,以=结束" << endl;
cin >> exp;
break;
case 2: // 表达式求值
v = Val_Exp(exp);
cout << exp;
cout << v << endl;
break;
case 3: // 求后缀表达式
CreatePostExp(exp, postexp);
break;
case 4: // 后缀表达式求值
v = Val_PostExp(postexp);
cout << '\n' << postexp << "=" << v << endl;
break;
case 5: // 显示表达式
cout << endl;
cout << "\n已创建的表达式为:";
cout << exp << endl;
if (strlen(postexp))
{
cout << "\n后缀表达式为:";
cout << postexp << endl;
}
break;
case 0: // 退出
cout << "\n结束运行Bye-Bye!" << endl;
break;
default://
cout << "\n无效选择!\n";
break;
}
} while (choice != 0);
}//end main

View File

@@ -0,0 +1,89 @@
template <class DT>
struct SqStack // 顺序栈
{
DT* base; // 栈首址
int top; // 栈顶指针
int stacksize; // 栈容量
};
//基本操作的实现
//【算法3.1】 // 初始化栈
template <class DT>
void InitStack(SqStack<DT>& S, int m)
{
S.base = new DT[m]; // 申请栈空间
if (S.base == NULL) // 申请失败,退出
{
cout << "未创建成功!";
exit(1);
}
S.top = -1; // 设置空栈属性
S.stacksize = m;
}
//算法3.2】 // 销毁栈
template <class DT>
void DestroyStack(SqStack<DT>& S)
{
delete[] S.base; // 释放栈空间
S.top = -1;
S.stacksize = 0; // 设置栈属性
}
//【算法3.3】 // 入栈
template<class DT>
bool Push(SqStack<DT>& S, DT e)
{
if (S.top == S.stacksize - 1) // 栈满,不能入栈
return false; // 返回false
S.top++;
S.base[S.top] = e;
return true; // 入栈成功返回true
}
//【算法3.4】 // 出栈
template<class DT>
bool Pop(SqStack<DT>& S, DT& e)
{
if (S.top == -1) // 栈空
return false; // 返回false
e = S.base[S.top]; // 取栈顶元素
S.top--; // 栈顶指针下移
return true; // 出栈成功返回true
}
//【算法3.5】 // 获取栈顶元素
template<class DT>
bool GetTop(SqStack<DT> S, DT& e)
{
if (S.top == -1) // 栈空
return false; // 返回false
e = S.base[S.top]; // 栈非空,取栈顶元素
return true; // 返回true
}
// 测栈空
template<class DT>
bool StackEmpty(SqStack<DT> S)
{
if (S.top == -1) // 空栈返回true
return true;
else // 空栈返回false
return false;
}
//显示栈内容
template<class DT>
void DispStack(SqStack<DT> S)
{
int i = S.top;
while (i >= 0)
{
cout << S.base[i--] << "\t";
}
cout << endl;
}

View File

@@ -0,0 +1,138 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{85ff5098-d9c7-4d2e-b248-4c3e402ad373}</ProjectGuid>
<RootNamespace>ValExpression</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="SqStack.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="Expresion.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="源文件">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="头文件">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="资源文件">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="SqStack.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Expresion.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
</Project>