This commit is contained in:
2024-03-04 23:45:56 +08:00
commit 2e9a13a76c
33 changed files with 4563 additions and 0 deletions

702
.gitignore vendored Normal file
View File

@@ -0,0 +1,702 @@
### C++ template
# Prerequisites
*.d
# Compiled Object files
*.slo
*.lo
*.o
*.obj
# Precompiled Headers
*.gch
*.pch
# Compiled Dynamic libraries
*.so
*.dylib
*.dll
# Fortran module files
*.mod
*.smod
# Compiled Static libraries
*.lai
*.la
*.a
*.lib
# Executables
*.exe
*.out
*.app
### VisualStudioCode template
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/*.code-snippets
# Local History for Visual Studio Code
.history/
# Built Visual Studio Code Extensions
*.vsix
### MicrosoftOffice template
*.tmp
# Word temporary
~$*.doc*
# Word Auto Backup File
Backup of *.doc*
# Excel temporary
~$*.xls*
# Excel Backup File
*.xlk
# PowerPoint temporary
~$*.ppt*
# Visio autosave temporary files
*.~vsd*
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
# AWS User-specific
.idea/**/aws.xml
# Generated files
.idea/**/contentModel.xml
# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
# Gradle
.idea/**/gradle.xml
.idea/**/libraries
# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr
# CMake
cmake-build-*/
# Mongo Explorer plugin
.idea/**/mongoSettings.xml
# File-based project format
*.iws
# IntelliJ
out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Cursive Clojure plugin
.idea/replstate.xml
# SonarLint plugin
.idea/sonarlint/
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
# Editor-based Rest Client
.idea/httpRequests
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser
### Linux template
*~
# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*
# KDE directory preferences
.directory
# Linux trash folder which might appear on any partition or disk
.Trash-*
# .nfs files are created when an open file is removed but is still being accessed
.nfs*
### CMake template
CMakeLists.txt.user
CMakeCache.txt
CMakeFiles
CMakeScripts
Testing
Makefile
cmake_install.cmake
install_manifest.txt
compile_commands.json
CTestTestfile.cmake
_deps
### Emacs template
# -*- mode: gitignore; -*-
*~
\#*\#
/.emacs.desktop
/.emacs.desktop.lock
*.elc
auto-save-list
tramp
.\#*
# Org-mode
.org-id-locations
*_archive
# flymake-mode
*_flymake.*
# eshell files
/eshell/history
/eshell/lastdir
# elpa packages
/elpa/
# reftex files
*.rel
# AUCTeX auto folder
/auto/
# cask packages
.cask/
dist/
# Flycheck
flycheck_*.el
# server auth directory
/server/
# projectiles files
.projectile
# directory configuration
.dir-locals.el
# network security
/network-security.data
### VisualStudio template
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore
# User-specific files
*.rsuser
*.suo
*.user
*.userosscache
*.sln.docstates
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
# Mono auto generated files
mono_crash.*
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
[Ww][Ii][Nn]32/
[Aa][Rr][Mm]/
[Aa][Rr][Mm]64/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/
[Ll]ogs/
# Visual Studio 2015/2017 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/
# Visual Studio 2017 auto generated files
Generated\ Files/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
# NUnit
*.VisualState.xml
TestResult.xml
nunit-*.xml
# Build Results of an ATL Project
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c
# Benchmark Results
BenchmarkDotNet.Artifacts/
# .NET Core
project.lock.json
project.fragment.lock.json
artifacts/
# ASP.NET Scaffolding
ScaffoldingReadMe.txt
# StyleCop
StyleCopReport.xml
# Files built by Visual Studio
*_i.c
*_p.c
*_h.h
*.ilk
*.meta
*.obj
*.iobj
*.pch
*.pdb
*.ipdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*_wpftmp.csproj
*.log
*.tlog
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc
# Chutzpah Test files
_Chutzpah*
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opendb
*.opensdf
*.sdf
*.cachefile
*.VC.db
*.VC.VC.opendb
# Visual Studio profiler
*.psess
*.vsp
*.vspx
*.sap
# Visual Studio Trace Files
*.e2e
# TFS 2012 Local Workspace
$tf/
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user
# TeamCity is a build add-in
_TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover
# AxoCover is a Code Coverage Tool
.axoCover/*
!.axoCover/settings.json
# Coverlet is a free, cross platform Code Coverage Tool
coverage*.json
coverage*.xml
coverage*.info
# Visual Studio code coverage results
*.coverage
*.coveragexml
# NCrunch
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*
# MightyMoose
*.mm.*
AutoTest.Net/
# Web workbench (sass)
.sass-cache/
# Installshield output folder
[Ee]xpress/
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# Note: Comment the next line if you want to checkin your web deploy settings,
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj
# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
PublishScripts/
# NuGet Packages
*.nupkg
# NuGet Symbol Packages
*.snupkg
# The packages folder can be ignored because of Package Restore
**/[Pp]ackages/*
# except build/, which is used as an MSBuild target.
!**/[Pp]ackages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/[Pp]ackages/repositories.config
# NuGet v3's project.json files produces more ignorable files
*.nuget.props
*.nuget.targets
# Microsoft Azure Build Output
csx/
*.build.csdef
# Microsoft Azure Emulator
ecf/
rcf/
# Windows Store app package directories and files
AppPackages/
BundleArtifacts/
Package.StoreAssociation.xml
_pkginfo.txt
*.appx
*.appxbundle
*.appxupload
# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!?*.[Cc]ache/
# Others
ClientBin/
~$*
*~
*.dbmdl
*.dbproj.schemaview
*.jfm
*.pfx
*.publishsettings
orleans.codegen.cs
# Including strong name files can present a security risk
# (https://github.com/github/gitignore/pull/2483#issue-259490424)
#*.snk
# Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/
# RIA/Silverlight projects
Generated_Code/
# Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
ServiceFabricBackup/
*.rptproj.bak
# SQL Server files
*.mdf
*.ldf
*.ndf
# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings
*.rptproj.rsuser
*- [Bb]ackup.rdl
*- [Bb]ackup ([0-9]).rdl
*- [Bb]ackup ([0-9][0-9]).rdl
# Microsoft Fakes
FakesAssemblies/
# GhostDoc plugin setting file
*.GhostDoc.xml
# Node.js Tools for Visual Studio
.ntvs_analysis.dat
node_modules/
# Visual Studio 6 build log
*.plg
# Visual Studio 6 workspace options file
*.opt
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
*.vbw
# Visual Studio 6 auto-generated project file (contains which files were open etc.)
*.vbp
# Visual Studio 6 workspace and project file (working project files containing files to include in project)
*.dsw
*.dsp
# Visual Studio 6 technical files
*.ncb
*.aps
# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml
_Pvt_Extensions
# Paket dependency manager
.paket/paket.exe
paket-files/
# FAKE - F# Make
.fake/
# CodeRush personal settings
.cr/personal
# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
# Cake - Uncomment if you are using it
# tools/**
# !tools/packages.config
# Tabs Studio
*.tss
# Telerik's JustMock configuration file
*.jmconfig
# BizTalk build output
*.btp.cs
*.btm.cs
*.odx.cs
*.xsd.cs
# OpenCover UI analysis results
OpenCover/
# Azure Stream Analytics local run output
ASALocalRun/
# MSBuild Binary and Structured Log
*.binlog
# NVidia Nsight GPU debugger configuration file
*.nvuser
# MFractors (Xamarin productivity tool) working folder
.mfractor/
# Local History for Visual Studio
.localhistory/
# Visual Studio History (VSHistory) files
.vshistory/
# BeatPulse healthcheck temp database
healthchecksdb
# Backup folder for Package Reference Convert tool in Visual Studio 2017
MigrationBackup/
# Ionide (cross platform F# VS Code tools) working folder
.ionide/
# Fody - auto-generated XML schema
FodyWeavers.xsd
# VS Code files for those working on multiple tools
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
*.code-workspace
# Local History for Visual Studio Code
.history/
# Windows Installer files from build outputs
*.cab
*.msi
*.msix
*.msm
*.msp
# JetBrains Rider
*.sln.iml
### Windows template
# Windows thumbnail cache files
Thumbs.db
Thumbs.db:encryptable
ehthumbs.db
ehthumbs_vista.db
# Dump file
*.stackdump
# Folder config file
[Dd]esktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp
# Windows shortcuts
*.lnk
### Vim template
# Swap
[._]*.s[a-v][a-z]
!*.svg # comment out if you don't need vector files
[._]*.sw[a-p]
[._]s[a-rt-v][a-z]
[._]ss[a-gi-z]
[._]sw[a-p]
# Session
Session.vim
Sessionx.vim
# Temporary
.netrwhist
*~
# Auto-generated tag files
tags
# Persistent undo
[._]*.un~
### macOS template
# General
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

41
Chapter1/Chapter1.sln Normal file
View File

@@ -0,0 +1,41 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34322.80
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "app1-4", "app1-4\app1-4.vcxproj", "{7A2BD8EC-2595-4070-BE3A-2BF2140848B2}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "b", "b\b.vcxproj", "{51CE097C-C445-4A3E-A678-539C70E334DF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{7A2BD8EC-2595-4070-BE3A-2BF2140848B2}.Debug|x64.ActiveCfg = Debug|x64
{7A2BD8EC-2595-4070-BE3A-2BF2140848B2}.Debug|x64.Build.0 = Debug|x64
{7A2BD8EC-2595-4070-BE3A-2BF2140848B2}.Debug|x86.ActiveCfg = Debug|Win32
{7A2BD8EC-2595-4070-BE3A-2BF2140848B2}.Debug|x86.Build.0 = Debug|Win32
{7A2BD8EC-2595-4070-BE3A-2BF2140848B2}.Release|x64.ActiveCfg = Release|x64
{7A2BD8EC-2595-4070-BE3A-2BF2140848B2}.Release|x64.Build.0 = Release|x64
{7A2BD8EC-2595-4070-BE3A-2BF2140848B2}.Release|x86.ActiveCfg = Release|Win32
{7A2BD8EC-2595-4070-BE3A-2BF2140848B2}.Release|x86.Build.0 = Release|Win32
{51CE097C-C445-4A3E-A678-539C70E334DF}.Debug|x64.ActiveCfg = Debug|x64
{51CE097C-C445-4A3E-A678-539C70E334DF}.Debug|x64.Build.0 = Debug|x64
{51CE097C-C445-4A3E-A678-539C70E334DF}.Debug|x86.ActiveCfg = Debug|Win32
{51CE097C-C445-4A3E-A678-539C70E334DF}.Debug|x86.Build.0 = Debug|Win32
{51CE097C-C445-4A3E-A678-539C70E334DF}.Release|x64.ActiveCfg = Release|x64
{51CE097C-C445-4A3E-A678-539C70E334DF}.Release|x64.Build.0 = Release|x64
{51CE097C-C445-4A3E-A678-539C70E334DF}.Release|x86.ActiveCfg = Release|Win32
{51CE097C-C445-4A3E-A678-539C70E334DF}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2E7BE105-4A42-43C5-A5F2-51518A78298B}
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,21 @@
// USE B.CPP INSTEAD
#include<iostream>
using namespace std;
void main()
{
int i=0,j=0,count=0;
for (i=2;i<=1000;i++)
{
for (j=2;j<=i-1;j++)
if(i%j==0)
break;
if(j>i-1)
{
cout<<j<<'\t';
count++:
if(cout%5==0) cout<<endl;
}
return ;
}

View File

@@ -0,0 +1,137 @@
<?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>
<ItemGroup>
<ClCompile Include="app1-4.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
</ClCompile>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{7a2bd8ec-2595-4070-be3a-2bf2140848b2}</ProjectGuid>
<RootNamespace>app14</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>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@@ -0,0 +1,22 @@
<?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>
<ClCompile Include="app1-4.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
</Project>

20
Chapter1/b/b.cpp Normal file
View File

@@ -0,0 +1,20 @@
#include<iostream>
using namespace std;
void main()
{
int i = 0, j = 0, count = 0;
for (i = 2; i <= 1000; i++)
{
for (j = 2; j <= i - 1; j++)
if (i % j == 0)
break;
if (j > i - 1)
{
cout << j << '\t';
count++;
if (count % 5 == 0) cout << endl;
}
}
return;
}

135
Chapter1/b/b.vcxproj Normal file
View File

@@ -0,0 +1,135 @@
<?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>
<ItemGroup>
<ClCompile Include="b.cpp" />
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{51ce097c-c445-4a3e-a678-539c70e334df}</ProjectGuid>
<RootNamespace>b</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>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@@ -0,0 +1,22 @@
<?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>
<ClCompile Include="b.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
</Project>

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>
<ItemGroup>
<ClCompile Include="SqList.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="SqList.h" />
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{15e9e8ad-d18e-45b2-861a-699717de99bb}</ProjectGuid>
<RootNamespace>My21SqList</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>
<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>
<ClCompile Include="SqList.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="SqList.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,156 @@
// 2-1-SqList-顺序表
#include <iostream>//cout,cin
using namespace std;
#include "SqList.h"
char pause;
void dispmenu() {
//显示主菜单
cout << endl;
cout << "*** 顺 序 表 ***\n";
cout << " 1-初始化顺序表\n";
cout << " 2-创建顺序表\n";
cout << " 3-获取第i个元素\n";
cout << " 4-按值查找元素位序\n";
cout << " 5-插入第i个元素\n";
cout << " 6-删除第i个元素\n";
cout << " 7-修改第i个元素\n";
cout << " 8-清空表\n";
cout << " 9-测表长\n";
cout << " 10-测表空\n";
cout << " 11-测表满\n";
cout << " 12-显示表\n";
cout << " 13-按值查找前驱\n";
cout << " 0-退出\n";
}
//主函数
void main() {
int i;
int e, pre_e;
SqList<int> L; //建立容量为20、元素类型为整型的空顺序表
system("cls"); //执行系统命令cls清屏
int choice;
do {
dispmenu(); //显示主菜单
cout << "Enter choice(1~120 退出):";
cin >> choice;
switch (choice) {
case 1: //初始化线性表
cout << "请输入要创建的顺序表的长度:";
cin >> i;
cout << endl;
InitList(L, i);
cout << endl << "创建成功!" << endl;
break;
case 2: //创建线性表
cout << "请输入要创建的元素个数:";
cin >> i;
cout << endl;
CreateList(L, i);
cout << "创建的顺序表元素为:\n"; //显示表元素
DispList(L);
cout << endl;
break;
case 3: //获取第i个元素
cout << "请输入元素序号:";
cin >> i;
cout << endl;
if (GetElem_i(L, i, e))
cout << endl << "" << i << "个元素为:" << e << endl;
else
cout << endl << "元素不存在!" << endl;
break;
case 4: //按值查找
cout << "请输入要查询的元素值:";
cin >> e;
i = LocateElem_e(L, e);
if (i)
cout << endl << e << "是第" << i << "个数据元素" << endl;
else
cout << endl << "不存在此元素!" << endl;
break;
case 5: // 在第i个位置插入元素
cout << "输入插入位置:" << endl;
cin >> i;
cout << "输入插入元素值:" << endl;
cin >> e;
if (InsertElem_i(L, i, e)) {
cout << endl << "插入成功!" << endl;
DispList(L);
}
else
cout << endl << "插入不成功!" << endl;;
break;
case 6: //删除第i个元素
cout << "输入删除元素位置:" << endl;
cin >> i;
if (DeleElem_i(L, i)) {
cout << endl << "删除成功!" << endl;
DispList(L);
}
else
cout << endl << "删除失败!" << endl;
break;
case 7: //修改第i个元素的值
cout << "输入修改元素位置:" << endl;
cin >> i;
cout << "输入新元素值:" << endl;
cin >> e;
if (PutElem(L, i, e)) {
cout << endl << "修改成功!" << endl;
DispList(L);
}
else
cout << endl << "修改失败!" << endl;
break;
case 8: // 清空表
ClearList(L);
break;
case 9: // 测表长
cout << "表长为:" << ListLength(L) << endl;
break;
case 10: //测表空
if (ListEmpty(L))
cout << endl << "空表!" << endl;
else
cout << endl << "不是空表!" << endl;
break;
case 11: // 未表满
if (ListFull(L))
cout << endl << "表满!" << endl;
else
cout << endl << "表不满!" << endl;
break;
case 12: //遍历显示表
DispList(L);
cout << endl;
cin.get(pause);
system("pause");
break;
case 13:
cout << "测试顺序表为\n";
DispList(L);
cout << "输入查找前驱的元素值:\n";
cin >> e;
if (PriorElem_e(L, e, pre_e))
cout << e << "的前驱元素为:" << pre_e << endl;
else
cout << e << "无前驱元素!" << endl;
break;
case 0: //退出
cout << "结束运行bye-bye!" << endl;
break;
default: //无效选择
cout << "无效选择!\n";
break;
}
} while (choice != 0);
}//end of main function

View File

@@ -0,0 +1,175 @@
template <class DT>
struct SqList // 顺序表
{
DT* elem; // 表首址
int length; // 表长
int size; // 表容量
};
//算法2.1
template <class DT>
bool PriorElem_e(SqList<DT> L, DT e, DT& pre_e) // 求值为e的元素前驱
{
int k;
k = LocateElem_e(L, e); //
if (k > 1)
{
GetElem_i(L, k - 1, pre_e);
return false;
}
else
return true;
}
//【算法2.2】 初始化
template <class DT>
bool InitList(SqList<DT>& L, int m)
{
L.elem = new DT[m]; // 申请表空间
if (L.elem == NULL)
{
cout << "未创建成功!"; // 申请不成功,退出
exit(1);
}
L.length = 0; // 申请成功属性赋值。空表表长为0
L.size = m; // 表容量为m
return true;
}
//【算法2.3】 创建表元素
template <class DT>
bool CreateList(SqList<DT>& L, int n)
{
int i;
if (n > L.size) // 1.元素个数大于表容量,不能创建。
{
cout << "元素个数大于表长,不能创建!" << endl;
return false;
}
cout << "请依次输入" << n << "个元素值:" << endl; // 2.依位序输入各元素值
for (i = 1; i <= n; i++)
cin >> L.elem[i - 1];
L.length = n; // 3.表长为创建的元素个数
return true;
}
//【算法2.4】 销毁顺序表
template <class DT>
void DestroyList(SqList<DT>& L)
{
delete[] L.elem; // 1.释放表空间
L.length = 0; // 2.属性赋值
L.size = 0;
}
//【算法2.5】 获取第i个元素值
template<class DT>
bool GetElem_i(SqList<DT> L, int i, DT& e)
{
if (i<1 || i>L.length) // 1.位序不合理返回false
{
cout << "该元素不存在!" << endl;
return false;
}
e = L.elem[i - 1]; // 2. 否则获取第i个元素值
return true; // 返回true
}
//【算法2.6】 按值查找
template<class DT>
int LocateElem_e(SqList<DT> L, DT e)
{
for (int i = 0; i < L.length; i++) // 顺序查找
if (L.elem[i] == e) // 1.找到
return i + 1; // 返回元素位序
return 0; // 2.未找到返回0
}
//【算法2.7】
template<class DT>
bool InsertElem_i(SqList<DT>& L, int i, DT e)
{
if (L.length >= L.size) // 1.表满,不能插入
return false;
if (i<1 || i>L.length + 1) // 2.插入位置不合理,不能插入
return false;
for (int j = L.length; j >= i; j--) // 3. an~ai依次后移
L.elem[j] = L.elem[j - 1];
L.elem[i - 1] = e;
L.length++;
return true; // 插入成功返回true
}
//【算法2.8】 删除第i个元素
template<class DT>
bool DeleElem_i(SqList<DT>& L, int i)
{
if (L.length == 0) // 1.表空,不能删除
return false;
if (i<1 || i>L.length) // 2.删除位置不合理,不能插入
return false;
for (int j = i; j < L.length; j++) // 3. ai+1~an依次前移
L.elem[j - 1] = L.elem[j];
L.length--;
return true; // 删除成功返回true
}
//【算法2.9】
template<class DT>
bool PutElem(SqList<DT>& L, int i, DT e) // 修改第i个元素的值
{
if (i<1 || i>L.length) // 1.位序不合理,不能修改,
return false; // 返回false
L.elem[i - 1] = e; // 2.重置第i个元素值
return true; // 3.修改成功返回true
}
// 清空顺序表
template<class DT>
void ClearList(SqList<DT>& L)
{
L.length = 0; // 空表表长为0
}
// 测表长
template<class DT>
int ListLength(SqList<DT> L)
{
return L.length;
}
template<class DT>
bool ListEmpty(SqList<DT> L) // 测表空
{
if (L.length == 0) // 空表返回true
return true;
else
return false; // 非空表返回false
}
template<class DT>
bool ListFull(SqList<DT> L)
{
if (L.length == L.size) // 表满返回true
return true;
else
return false; // 表不满返回false
}
//【算法2.10】 遍历输出
template <class DT>
void DispList(SqList<DT> L)
{
int i;
for (i = 0; i < L.length; i++) // 依位序输出元素值
{
cout << L.elem[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>
<ItemGroup>
<ClCompile Include="LinkList.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="LinkList.h" />
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{631ed17d-100b-4089-8ae8-768fb5d19442}</ProjectGuid>
<RootNamespace>My22LinkList</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>false</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>
<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>
<ClCompile Include="LinkList.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="LinkList.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,188 @@
// 2-2-LinkList-单链表
// WARNING: /sdl is disabled to pass the compilation process.
#include<iostream> //cout,cin
using namespace std;
#include "LinkList.h"
//算法2.25 单链表逆置
void ReverseLinkList(LNode<int>*& L)
{
LNode<int>* p, * q; // 1.设置工作指针
p = L->next; // 原链表头结点,作为逆置后表的头结点
L->next = NULL;
while (p) // 2. 依次摘除原链表结点,以头插法插入到逆置链表中
{
q = p; // 2.1 q取当前结点位置
p = p->next; // 2.2 p指向下一个待处理结点
q->next = L->next; // 2.3 将q 插入到头结点之后
L->next = q;
}
}
void dispmenu()
{ // 显示主菜单
cout << endl;
cout << " *** 单 链 表 ***\n";
cout << " 1-初始化单链表\n";
cout << " 2-尾插法顺序表\n";
cout << " 3-头插法建表\n";
cout << " 4-获取第i个元素\n";
cout << " 5-按值查找\n";
cout << " 6-插入第i个元素\n";
cout << " 7-删除第i个元素\n";
cout << " 8-修改第i个元素\n";
cout << " 9-清空表\n";
cout << "10-测表长\n";
cout << "11-测表空\n";
cout << "12-遍历输出\n";
cout << "13-按值查找前驱\n";
cout << "14-单链表逆置\n";
cout << "0-退出\n";
}
char pause;
// 主函数
int main()
{
int i, n;
int e, pre_e;
LNode<int>* L;
system("cls"); // 清屏
int choice;
do
{
dispmenu(); // 显示主菜单
cout << "Enter choice(1~120 退出):";
cin >> choice;
switch (choice)
{
case 1: // 初始化单链表
InitList(L);
cout << endl << "创建成功!" << endl;
break;
case 2: // 尾插法建单链表
cout << "尾插法创建单链表" << endl;
cout << "输入要创建的顺序表中元素个数:";
cin >> n;
cout << endl;
CreateList_1(L, n);
cout << "创建的单链表为:";
DispList(L);
cout << endl;
break;
case 3: // 头插法创建单链表
cout << "头插法创建单链表" << endl;
cout << "输入要创建的顺序表中元素个数:";
cin >> n;
cout << endl;
CreateList_2(L, n);
cout << "创建的单链表为:";
DispList(L);
cout << endl;
break;
case 4: // 获取第i个元素
cout << "请输入元素序号:";
cin >> i;
cout << endl;
if (GetElem_i(L, i, e))
cout << endl << "" << i << "个元素为:" << e << endl;
else
cout << endl << "元素不存在!" << endl;
break;
case 5: // 查询元素位序
cout << "请输入要查询的元素值:";
cin >> e;
i = LocateElem_e(L, e);
if (i)
cout << endl << e << "是第" << i << "个数据元素" << endl;
else
cout << endl << "不存在此元素!" << endl;
break;
case 6: // 插入第i个元素
cout << "输入插入位置:" << endl;
cin >> i;
cout << "输入插入元素值:" << endl;
cin >> e;
if (InsertElem_i(L, i, e))
{
cout << endl << "插入成功!" << endl;
cout << "插入元素后的单链表为:" << endl;
DispList(L);
}
else
cout << endl << "插入不成功!" << endl;
break;
case 7: // 删除第i个元素
cout << "输入删除元素位置:" << endl;
cin >> i;
if (DeleElem_i(L, i))
{
cout << endl << "删除成功!" << endl;
cout << "删除元素后的单链表为:" << endl;
DispList(L);
}
else
cout << endl << "删除失败!" << endl;
break;
case 8: // 修改第i个元素
cout << "输入修改元素位置:" << endl;
cin >> i;
cout << "输入新元素值:" << endl;
cin >> e;
if (PutElem_i(L, i, e))
{
cout << endl << "修改成功!" << endl;
cout << "修改后的单链表为:" << endl;
DispList(L);
}
else
cout << endl << "修改失败!" << endl;
break;
case 9: // 清空表
ClearList(L);
break;
case 10: // 测表长
cout << "表长为:" << ListLength(L) << endl;
break;
case 11: // 测表空
if (ListEmpty(L))
cout << endl << "空表!" << endl;
else
cout << endl << "不是空表!" << endl;
break;
case 12: //遍历显示表
DispList(L);
cout << endl;
break;
case 13:
cout << "测试链表为\n";
DispList(L);
cout << "输入查找前驱的元素值:\n";
cin >> e;
if (PriorElem_e(L, e, pre_e))
cout << e << "的前驱元素为:" << pre_e << endl;
else
cout << e << "无前驱元素!" << endl;
break;
case 14: // 逆置单链表
cout << "逆置前的单链表为:";
DispList(L);
ReverseLinkList(L);
cout << "逆置后的单链表为:";
DispList(L);
cout << endl;
break;
case 0: //退出,销毁链表
DestroyList(L);
cout << "结束运行bye-bye!" << endl;
break;
default: //无效选择
cout << "无效选择!\n";
break;
}
} while (choice != 0);
return 0;
}//end of main function

View File

@@ -0,0 +1,253 @@
template <class DT>
struct LNode //链表结点
{
DT data; //数据域,存储数据元素值
LNode* next; //指针域,指向下一个结点
};
//算法2.1
template <class DT>
bool PriorElem_e(LNode<DT>* L, DT e, DT& pre_e) // 求值为e的元素前驱
{
int k;
k = LocateElem_e(L, e); // 1.获取e的位序k
if (k > 1) // 2.位序k大于1
{
GetElem_i(L, k - 1, pre_e); // 第k-1个元素为e的前驱
return true;
}
else // 3.元素e无前驱
return false; // 返回false
}
//【算法2.14】 创建空单链表
template <class DT>
bool InitList(LNode<DT>*& L)
{
L = new LNode<DT>; // 1.创建头结点
if (!L) exit(1); // 2.创建失败,退出
L->next = NULL; // 3.创建成功
return true; // 返回true
}
//【算法2.15】 尾插法创建n的元素
template <class DT>
bool CreateList_1(LNode<DT>*& L, int n)
{
int i;
LNode<DT>* p, * s;
p = L; //1.工作指针初始化,指向尾结点
cout << "依次输入" << n << "个数据元素:" << endl;
for (i = 1; i <= n; i++) // 2.按元素位序正序创建各结点
{
s = new LNode<DT>; // 2.1 新建一个结点s
if (!s) // 2.2 创建失败返回false
return false;
cin >> s->data; // 2.3 输入结点值
s->next = p->next; // 2.4 s 链在表尾
p->next = s;
p = s; // 2.5 工作指针指向 s
}
return true; // 3.创建成功返回true
}
//【算法2.16】 头插法创建n个元素
template <class DT>
bool CreateList_2(LNode<DT>* (&L), int n)
{
int i;
LNode<DT>* s;
cout << "逆序输入" << n << "个数据元素:" << endl;
for (i = 1; i <= n; i++) // 1.按元素位序逆序创建各结点
{
s = new LNode<DT>; // 1.1 新建一个结点 s
if (!s) // 1.2 创建失败返回false
return false;
cin >> s->data; // 1.3 输入结点值
s->next = L->next; // 1.4 s 在头结点后
L->next = s;
}
return true; // 1.创建成功返回true
}
//【算法2.17】
template <class DT>
void DestroyList(LNode<DT>* (&L)) // 释放链表所占空间
{
LNode<DT>* p;
while (L) // 1. 表非空,从头结点开始,依次释放结点
{
p = L; // 1.1 处理表头结点
L = L->next; // 1.2 头指针后移
delete p; // 1.3 释放表头结点所占内存
}
L = NULL; // 2.头指针指向空
}
//【算法2.18】 获取第i个元素
template<class DT>
bool GetElem_i(LNode<DT>* L, int i, DT& e)
{
LNode<DT>* p; // 1.初始化
p = L->next; // 1.1 设置工作指针,从首结点开始数结点
int j = 1; // 1.2 计数器初始化
while (p && j < i) // 2.定位到第i个元素结点
{
p = p->next; j++;
}
if (!p || j > i) // 3 未找到返回false
return false;
else // 4. 找到
{
e = p->data; // 获取第i个元素值
return true; // 返回true
}
}
//【算法2.19】 查找值为e的元素位序
template<class DT>
int LocateElem_e(LNode<DT>* L, DT e)
{
LNode<DT>* p; // 1.初始化从首元开始查找
p = L->next; // 1.1从首元开始查找
int j = 1; // 1.2 计数器初值
while (p && p->data != e) // 2.顺序查找
{
p = p->next; // 2.1未找到指针后移
j++; // 2.2 计数器增1
}
if (p == NULL) // 3. 判断是否找到
return 0; // 3.1末找到返回0
else
return j; // 3.2 找到,返回位序
}
//【算法2.20】 插入第i个元素
template<class DT>
bool InsertElem_i(LNode<DT>*& L, int i, DT e)
{
int j = 0;
LNode<DT>* p; // 1.初始化
p = L; // 工作指针初始化
while (p && j < i - 1) // 2. 定位到插入点前驱
{
p = p->next;
j++;
}
if (!p || j > i - 1) // 3.判断定位是否成功:
return false; // 3.1 定位失败,不能插入
else // 3.2 定位成功
{
LNode<DT>* s;
s = new LNode<DT>; // 3.2.1建立新结点
s->data = e; // 3.2.2新结点赋值
s->next = p->next; // 3.2.3结点S链接到p结点之后
p->next = s;
return true; // 3.2.4 插入成功返回true
}
}
//【算法2.21】 删除第i个元素
template<class DT>
bool DeleElem_i(LNode<DT>* (&L), int i)
{
LNode<DT>* p, * q; //1.初始化:设置工作指针
p = L; //查找从头结点开始
int j = 0; //计数器初始化
while (p->next && j < i - 1) //2.p定位到删除点的前驱
{
p = p->next;
j++;
}
if (!p->next || j > i - 1) //3.删除位置不合理,不能删除
return false; //返回false
else //4.删除操作
{
q = p->next; //4.1暂存删除结点位置
p->next = q->next; //4.2从链表中摘除删除结点
delete q;
return true; //4.3删除成功返回true
}
}
//【算法2.22】 修改第i个元素值
template<class DT>
bool PutElem_i(LNode<DT>* (&L), int i, DT e)
{
LNode<DT>* p; // 1.初始化:设置工作指针
p = L->next; // 从首结点开始,数结点
int j = 1; // 计数器初始化
while (p && j < i) // 2.查找第i个元素结点
{
p = p->next; j++;
}
if (!p || j > i) // 3.元素不存在,返回false
return false;
else // 4.定位成功
{
p->data = e; // 修改元素值
return true; // 返回true
}
}
// 释放链表所占空间
template<class DT>
void ClearList(LNode<DT>* (&L))
{
LNode<DT>* p;
while (L->next) // 从首元结点开始,依次释放结点
{
p = L->next;
L->next = p->next;
delete p;
}
cout << endl << "表已清空!" << endl;
}
//【算法2.23】 测表长
template<class DT>
int ListLength(LNode<DT>* L)
{ // 1.初始化
int len = 0; // 1.1 结点计数器赋初值0
LNode<DT>* p; // 1.2设置工作指针
p = L; // 指向头结点
while (p->next) // 2.数结点个数。有后继结点,
{
len++; p = p->next; // 结点数增1指针后移
}
return len; // 3.返回表长
}
//
template<class DT>
bool ListEmpty(LNode<DT>* L) //测表空
{
if (L->next == NULL)
return true; //空表返回1
else
return false; //不空返回0
}
//【算法2.24】 遍历表
template <class DT>
void DispList(LNode<DT>* L) // 显示表内容
{
LNode<DT>* p; // 1. 设置工作指针
p = L; // 从首元结点开始遍历
while (p->next) // 2.依次输出各结点值
{
p = p->next; cout << p->data << "\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>
<ItemGroup>
<ClCompile Include="SqListApp.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="SqList.h" />
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{6816c6c8-1b6d-4773-812a-e7a50324cb12}</ProjectGuid>
<RootNamespace>My23SqListApp</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>
<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>
<ClCompile Include="SqListApp.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="SqList.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,160 @@
template <class DT>
struct SqList //顺序表类
{
DT* elem; //表首址
int length; //表长
int size;//表容量
};
//【算法2.2】
template <class DT>
bool InitList(SqList<DT>& L, int m)
{//构建函数创建一表容量为m的空表
L.elem = new DT[m];// 申请表空间
if (L.elem == NULL)
{
cout << "未创建成功!";
exit(1);
}
L.length = 0; // 空表表长为0
L.size = m; //表容量为m
return true;
}
//【算法2.3】
template <class DT>
bool CreateList(SqList<DT>& L, int n) //创建表长度为n的顺序表
{
int i;
if (n > L.size)
{
cout << "元素个数大于表长,不能创建!" << endl;
return false;
}
cout << "请依次输入" << n << "个元素值:" << endl;
for (i = 1; i <= n; i++)
cin >> L.elem[i - 1];
L.length = n;
return true;
}
//【算法2.4】
template <class DT>
void DestroyList(SqList<DT>& L) //销毁顺序表,释放表空间
{
delete[] L.elem;
L.length = 0;
L.size = 0;
}
//【算法2.5】
template<class DT>
bool GetElem_i(SqList<DT> L, int i, DT& e)// 获取第i个元素值
{
if (i<0 || i>L.length)
return false;
e = L.elem[i - 1];
return true;
}
//【算法2.6】
template<class DT>
int LocateElem_e(SqList<DT> L, DT e) // 按值查找
{
int i;
for (i = 0; i < L.length; i++) // 顺序查找
if (L.elem[i] == e) // 找到
return i + 1;
return 0; // 未找到
}
//【算法2.7】
template<class DT>
bool InsertElem_i(SqList<DT>& L, int i, DT e) // 在第i个位置插入新元素
{
int j;
if (L.length >= L.size) //1.表满,不能插入
return false;
if (i<1 || i>L.length + 1) //2.插入位置不合理,不能插入
return false;
for (j = L.length; j >= i; j--) //3. an~ai依次后移
L.elem[j] = L.elem[j - 1];
L.elem[i - 1] = e;
L.length++;
return true; // 插入成功返回true
}
//【算法2.8】
template<class DT>
bool DeleElem_i(SqList<DT>& L, int i) // 删除第i个元素
{
int j;
if (L.length == 0) //1.表空,不能删除
return false;
if (i<1 || i>L.length) //2.删除位置不合理,不能插入
return false;
for (j = i; j < L.length; j++) //3. ai~an依次前移
L.elem[j - 1] = L.elem[j];
L.length--;
return true; // 删除成功返回true
}
//【算法2.9】
template<class DT>
bool PutElem(SqList<DT>& L, int i, DT e) // 修改第i个元素的值
{
if (i<1 || i>L.length) //1.位置不合理,不能修改
return false;
L.elem[i - 1] = e; //2.重置第i个元素值
return true; //3.修改成功返回true
}
//清空顺序表
template<class DT>
void ClearList(SqList<DT>& L)
{
L.length = 0;
}
//测表长
template<class DT>
int ListLength(SqList<DT> L)
{
return L.length;
}
//测表空
template<class DT>
bool ListEmpty(SqList<DT> L)
{
if (L.length == 0)
return true;
else
return false;
}
//测表满
template<class DT>
bool ListFull(SqList<DT> L)
{
if (L.length == L.size)
return true;
else
return false;
}
//【算法2.10】
template <class DT>
void DispList(SqList<DT> L) //遍历输出
{
int i;
for (i = 0; i < L.length; i++)
{
cout << L.elem[i] << "\t";
}
cout << endl;
}

View File

@@ -0,0 +1,188 @@
// 2-3-SqListApp-顺序表应用
#include<iostream>//cout,cin
using namespace std;
#include "SqList.h"
char pause;
void dispmenu()
{ // 显示主菜单
cout << "\n*** 顺序表的应用 ***\n";
cout << " 1-集合并A=AB\n";
cout << " 2-顺序表逆置\n";
cout << " 3-多项式求和\n";
cout << " 0-退出\n";
}
//算法2.11
template <class DT>
void Union(SqList<DT>& La, SqList<DT> Lb) // 求La=LaLa
{
DT e;
int k, i;
for (i = 1; i <= Lb.length; i++) // 扫描Lb
{
GetElem_i(Lb, i, e); // 1. 获取Lb的第i个元素
if (!LocateElem_e(La, e)) // 2.如果La中无此元素
{
k = La.length + 1; // 添加在La的表尾
InsertElem_i(La, k, e);
}
}
}
//算法2.12
template <class DT>
void ReverseSqList(SqList<DT>& L) // 顺序表元素逆置
{
DT t;
int i;
for (i = 0; i < L.length / 2; i++) // 2.L.elem[i]<-->L.elem[L.length-i-1]
{
t = L.elem[i];
L.elem[i] = L.elem[L.length - i - 1];
L.elem[L.length - i - 1] = t;
}
return;
}
//算法2.13 多项式求和 lc=la+lb
void PolyAdd(SqList<float> la, SqList<float> lb, SqList<float>& lc)
{
int i = 0; // 1.初始化,设置处理起始位置
while (i < la.length && i < lb.length) // 2.两个多项式均未处理完
{
lc.elem[i] = la.elem[i] + lb.elem[i]; // 相同位序上的系数相加
i++;
}
if (la.length > lb.length) // 3.la未处理完lb已处理完
{
while (i < la.length) // lc取la中剩余项
{
lc.elem[i] = la.elem[i];
i++;
}
}
else // 3.lb未处理完la已处理完
{
while (i < lb.length) // lc取lb中剩余项
{
lc.elem[i] = lb.elem[i];
i++;
}
}
}
//显示多顶式
void DispPoly(float A[], int n)
{
int i;
cout << A[0] << "+";
for (i = 1; i < n - 1; i++)
cout << A[i] << "x^" << i << " + ";
cout << A[i] << "x^" << i;
cout << endl;
}
//主函数
int main()
{
int e;
int na, nb, nc;
SqList<int> La, Lb; // 集合A、B
SqList<int> Lc; // 顺序表
SqList<float> fa, fb, fc; // 多项式A、B、C
system("cls"); // 清屏
int choice;
do
{
dispmenu(); // 显示主菜单
cout << "Enter choice(1~4,0退出):";
cin >> choice;
switch (choice)
{
case 1: // 求集合并
cout << "创建集合A、B\n";
cout << "输入集合A元素个数";
cin >> na;
cout << "输入集合B元素个数";
cin >> nb;
InitList(La, na + nb); // 创建集合A
cout << "创建集合A的元素\n";
CreateList(La, na);
InitList(Lb, nb); // 创建集合B
cout << "创建集合B的元素\n";
CreateList(Lb, nb);
cout << "集合A为" << endl; // 显示集合A
DispList(La);
cout << "集合B为" << endl; // 显示集合B
DispList(Lb);
Union(La, Lb); // 求集合并
cout << "AB为:" << endl; // 显示结果
DispList(La);
cout << endl;
DestroyList(La);
DestroyList(Lb);
break;
case 2: // 顺序表逆置
cout << "请输入要创建的顺序表中元素个数:";
cin >> nc;
InitList(Lc, nc);
cout << endl;
CreateList(Lc, nc);
cout << "创建的顺序表为:" << endl; // 显示集合A
DispList(Lc);
ReverseSqList(Lc);
cout << "逆置后的顺序表为:" << endl; // 显示集合A
DispList(Lc);
cout << endl;
DestroyList(Lc);
break;
case 3: //多项式求和
cout << "\n创建多项式A\n"; // 创建多项式A
cout << "输入多项式A的项数";
cin >> na;
InitList(fa, na);
cout << "按幂升序输入多项式A各项系数\n";
CreateList(fa, na);
cout << "\n创建多项式B\n"; // 创建多项式B
cout << "输入多项式B的项数";
cin >> nb;
InitList(fb, nb);
cout << "按幂升序输入多项式B各项系数\n";
CreateList(fb, nb);
cout << "\n多项式 A 为 " << endl; // 显示多项式A
DispPoly(fa.elem, na);
cout << "\n多项式 B 为 " << endl; // 显示多项式B
DispPoly(fb.elem, nb);
nc = (na >= nb) ? na : nb;
InitList(fc, nc); // 创建多项式C
PolyAdd(fa, fb, fc); // 求多项式 C=A+B
cout << "\n多项式A + 多项式B = " << endl; // 显示结果
DispPoly(fc.elem, nc);
cout << endl;
DestroyList(fa);
DestroyList(fb);
DestroyList(fc);
break;
case 0: // 退出
cout << " 结束运行bye-bye!" << endl;
break;
default: // 无效选择
cout << "无效选择!\n";
break;
}
} while (choice != 0);
return 0;
}//end of main function

View File

@@ -0,0 +1,135 @@
<?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>
<ItemGroup>
<ClCompile Include="PolyAdd.cpp" />
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{3bbbc9d3-2aea-48cc-abf7-908da7982a0b}</ProjectGuid>
<RootNamespace>My24PolyAdd</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>false</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>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@@ -0,0 +1,22 @@
<?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>
<ClCompile Include="PolyAdd.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,241 @@
// 2-4-PolyAdd-稀疏多项式求和
// WARNING: /sdl is disabled to pass the compilation process.
#include<iostream>//cout,cin
using namespace std;
struct PolyNode // 多项式结点
{
float coef; // 系数
int exp; // 指数
PolyNode* next; // 指向下一项结点
};
void InitPoly(PolyNode*& L)
{ //创建一空多项式
L = new PolyNode;
L->next = NULL;
}
bool CreatePoly(PolyNode*& L, int n) // 尾插法创建n项多项式
{
int i;
PolyNode* p, * s;
p = L;
for (i = 1; i <= n; i++) // 按幂升序依次输入多项式各项系数与幂指数
{
s = new PolyNode;
if (!s)
return false;
cout << "输入第" << i << "项系数和幂指数:";
cin >> s->coef >> s->exp;
s->next = p->next;
p->next = s;
p = s;
}
return true;
}
//显示多顶式
void DispPoly(PolyNode* L) // 通过遍历结点,输出多项式
{
PolyNode* p;
if (!L) // 空表,返回
{
cout << "空表!";
return;
}
p = L->next;
while (p && p->next)
{
cout << p->coef << "x^" << p->exp << " + ";
p = p->next;
}
cout << p->coef << "x^" << p->exp;
cout << endl;
}
//【算法2.26】 求多项式LA=LA+LB
void PolyAdd(PolyNode*& LA, PolyNode*& LB)
{
float sum;
PolyNode* pa, * pb, * qa, * qb; // 1.工作指针初始化
pa = LA;
qa = pa->next;
pb = LB;
qb = pb->next;
while (qa != NULL && qb != NULL) // 2. 两表均不空
{
if (qa->exp < qb->exp) // 2.1 LA的幂小
{ // pa、qa后移
pa = qa; qa = qa->next;
}
else if (qa->exp > qb->exp) //2.2 LA 幂大
{
pb->next = qb->next; // qb链接到pa之后
qb->next = qa;
pa->next = qb;
pa = qb;
qb = pb->next;
}
else // 2.3 LA与LB幂相同
{
sum = qa->coef + qb->coef; // 计算系数和
if (sum != 0) // 2.3.1 系数和不为0
{
qa->coef = sum; // 2.3.1.1 qa->coef←sum
pa = qa; qa = qa->next; // 2.3.1.2 paqa后移
pb->next = qb->next;
delete qb; // 2.3.1.3删除qb
qb = pb->next;
}
else // 2.3.2 系数和为0
{
pa->next = qa->next;
delete qa; // 2.3.2.1 删除qa
qa = pa->next; // 2.3.2.2 qa为pa后继
pb->next = qb->next;
delete qb; // 2.3.2.3 删除qb
qb = pb->next; // 2.3.2.4 qb为pb的后继
}
}
}//while
if (qb != NULL) // 3. LA处理结束LB未结束
pa->next = qb; // 3.1 qb链到qa之后
delete pb; // 3.2 删除lb头结点
LB = NULL;
}//Add
void DestroyPoly(PolyNode*& L) // 释放链表所占空间
{
PolyNode* p;
while (L) // 从头结点开始,依次释放结点
{
p = L;
L = L->next;
delete p;
}
L = NULL; // 头结点指向空
}
void SortPoly(PolyNode*& L) // 将多项式按幂升序排序
{
PolyNode* p1, * p2, * q, * r; // 采用插入排序算法
p1 = L; p2 = p1->next; // p1是p2的前驱
if (p2 == NULL || p2->next == NULL) // 空表或只有1项的多项式不需要排序
{
cout << "不需要排序!" << endl;
return;
}
r = L->next; // 有序表表尾
q = r->next; // q为当前处理项有序表的后一项
while (q) // 未处理完
{ // 从首元结点开始查找插入点
p1 = L; p2 = p1->next;
while (q->exp > p2->exp && p2 != q) // 当前结点幂大,插入点后移
{
p1 = p2; p2 = p2->next;
}
if (p2 == q) // 当前项无需移动
{
r = q; // 有序表表尾顺移
}
else // q插入到p2前面
{
r->next = q->next; // 摘除q结点
q->next = p1->next; // 在p1后插入结点q
p1->next = q;
}
q = r->next; // 下一个需处理的项
}
return;
}
void dispmenu()
{//显示主菜单
cout << endl;
cout << "*** 稀疏多项式求和 ***\n";
cout << "1-创建多项式A\n";
cout << "2-创建多项式B\n";
cout << "3-多项式求和A=A+B\n";
cout << "4-显示多项式\n";
cout << "0-退出\n";
}
//主函数
int main()
{
int m, n;
char c;
PolyNode* LA, * LB;
system("cls"); // 执行系统命令cls清屏
int choice;
do
{
dispmenu(); // 显示主菜单
cout << "Enter choice(1~4,0 退出):";
cin >> choice;
switch (choice)
{
case 1: // 创建多项式A
InitPoly(LA);
cout << "请输入多项式 A 的项数: ";
cin >> m;
CreatePoly(LA, m);
cout << "创建的多项式 A 为:" << endl;
DispPoly(LA);
SortPoly(LA);
cout << "排序后多项式 A 为:" << endl;
DispPoly(LA);
break;
case 2: // 创建多项式B
InitPoly(LB);
cout << "请输入多项式B的项数 ";
cin >> n;
CreatePoly(LB, n);
cout << "创建的多项式B为" << endl;
DispPoly(LB);
SortPoly(LB);
cout << "排序后多项式 B 为:" << endl;
DispPoly(LB);
break;
case 3: //多项式求和
cout << "A = ";
DispPoly(LA);
cout << "B = ";
DispPoly(LB);
PolyAdd(LA, LB); // 求多项式 LA=LA+LB
cout << "A + B = "; // 显示结果
DispPoly(LA);
cout << endl;
break;
case 4: // 显示多项式
cout << "选择要显示的多项式 A 或 B" << endl;
cin >> c;
if (c == 'A' || c == 'a')
DispPoly(LA);
else if (c == 'B' || c == 'b')
DispPoly(LB);
else
cout << "选择错误!" << endl;
break;
case 5: //退出
DestroyPoly(LA);
DestroyPoly(LB);
cout << "结束运行bye-bye!" << endl;
break;
default: //非法选择
cout << "非法选择!\n";
break;
}
} while (choice != 0);
return 0;
}//end of main function

61
Chapter2/Chapter2.sln Normal file
View File

@@ -0,0 +1,61 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.8.34322.80
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "2-1-SqList", "2-1-SqList\2-1-SqList.vcxproj", "{15E9E8AD-D18E-45B2-861A-699717DE99BB}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "2-2-LinkList", "2-2-LinkList\2-2-LinkList.vcxproj", "{631ED17D-100B-4089-8AE8-768FB5D19442}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "2-3-SqListApp", "2-3-SqListApp\2-3-SqListApp.vcxproj", "{6816C6C8-1B6D-4773-812A-E7A50324CB12}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "2-4-PolyAdd", "2-4-PolyAdd\2-4-PolyAdd.vcxproj", "{3BBBC9D3-2AEA-48CC-ABF7-908DA7982A0B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{15E9E8AD-D18E-45B2-861A-699717DE99BB}.Debug|x64.ActiveCfg = Debug|x64
{15E9E8AD-D18E-45B2-861A-699717DE99BB}.Debug|x64.Build.0 = Debug|x64
{15E9E8AD-D18E-45B2-861A-699717DE99BB}.Debug|x86.ActiveCfg = Debug|Win32
{15E9E8AD-D18E-45B2-861A-699717DE99BB}.Debug|x86.Build.0 = Debug|Win32
{15E9E8AD-D18E-45B2-861A-699717DE99BB}.Release|x64.ActiveCfg = Release|x64
{15E9E8AD-D18E-45B2-861A-699717DE99BB}.Release|x64.Build.0 = Release|x64
{15E9E8AD-D18E-45B2-861A-699717DE99BB}.Release|x86.ActiveCfg = Release|Win32
{15E9E8AD-D18E-45B2-861A-699717DE99BB}.Release|x86.Build.0 = Release|Win32
{631ED17D-100B-4089-8AE8-768FB5D19442}.Debug|x64.ActiveCfg = Debug|x64
{631ED17D-100B-4089-8AE8-768FB5D19442}.Debug|x64.Build.0 = Debug|x64
{631ED17D-100B-4089-8AE8-768FB5D19442}.Debug|x86.ActiveCfg = Debug|Win32
{631ED17D-100B-4089-8AE8-768FB5D19442}.Debug|x86.Build.0 = Debug|Win32
{631ED17D-100B-4089-8AE8-768FB5D19442}.Release|x64.ActiveCfg = Release|x64
{631ED17D-100B-4089-8AE8-768FB5D19442}.Release|x64.Build.0 = Release|x64
{631ED17D-100B-4089-8AE8-768FB5D19442}.Release|x86.ActiveCfg = Release|Win32
{631ED17D-100B-4089-8AE8-768FB5D19442}.Release|x86.Build.0 = Release|Win32
{6816C6C8-1B6D-4773-812A-E7A50324CB12}.Debug|x64.ActiveCfg = Debug|x64
{6816C6C8-1B6D-4773-812A-E7A50324CB12}.Debug|x64.Build.0 = Debug|x64
{6816C6C8-1B6D-4773-812A-E7A50324CB12}.Debug|x86.ActiveCfg = Debug|Win32
{6816C6C8-1B6D-4773-812A-E7A50324CB12}.Debug|x86.Build.0 = Debug|Win32
{6816C6C8-1B6D-4773-812A-E7A50324CB12}.Release|x64.ActiveCfg = Release|x64
{6816C6C8-1B6D-4773-812A-E7A50324CB12}.Release|x64.Build.0 = Release|x64
{6816C6C8-1B6D-4773-812A-E7A50324CB12}.Release|x86.ActiveCfg = Release|Win32
{6816C6C8-1B6D-4773-812A-E7A50324CB12}.Release|x86.Build.0 = Release|Win32
{3BBBC9D3-2AEA-48CC-ABF7-908DA7982A0B}.Debug|x64.ActiveCfg = Debug|x64
{3BBBC9D3-2AEA-48CC-ABF7-908DA7982A0B}.Debug|x64.Build.0 = Debug|x64
{3BBBC9D3-2AEA-48CC-ABF7-908DA7982A0B}.Debug|x86.ActiveCfg = Debug|Win32
{3BBBC9D3-2AEA-48CC-ABF7-908DA7982A0B}.Debug|x86.Build.0 = Debug|Win32
{3BBBC9D3-2AEA-48CC-ABF7-908DA7982A0B}.Release|x64.ActiveCfg = Release|x64
{3BBBC9D3-2AEA-48CC-ABF7-908DA7982A0B}.Release|x64.Build.0 = Release|x64
{3BBBC9D3-2AEA-48CC-ABF7-908DA7982A0B}.Release|x86.ActiveCfg = Release|Win32
{3BBBC9D3-2AEA-48CC-ABF7-908DA7982A0B}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {DE77319E-CE24-481A-B0F4-8B3DEFA26F5A}
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,19 @@
#include<iostream>
using namespace std;
void main()
{
int i=0,j=0,count=0;
for (i=2;i<=1000;i++)
{
for (j=2;j<=i-1;j++)
if(i%j==0)
break;
if(j>i-1)
{
cout<<j<<'\t';
count++:
if(cout%5==0) cout<<endl;
}
return ;
}

20
OrigFiles/1-绪/b.cpp Normal file
View File

@@ -0,0 +1,20 @@
#include<iostream>
using namespace std;
void main()
{
int i=0,j=0,count=0;
for (i=2;i<=1000;i++)
{
for (j=2;j<=i-1;j++)
if(i%j==0)
break;
if(j>i-1)
{
cout<<j<<'\t';
count++;
if(count%5==0) cout<<endl;
}
}
return ;
}

View File

@@ -0,0 +1,152 @@
#include <iostream>//cout,cin
using namespace std;
#include "SqList.h"
char pause;
void dispmenu() {
//显示主菜单
cout << endl;
cout << "*** 顺 序 表 ***\n";
cout << " 1-初始化顺序表\n";
cout << " 2-创建顺序表\n";
cout << " 3-获取第i个元素\n";
cout << " 4-按值查找元素位序\n";
cout << " 5-插入第i个元素\n";
cout << " 6-删除第i个元素\n";
cout << " 7-修改第i个元素\n";
cout << " 8-清空表\n";
cout << " 9-测表长\n";
cout << " 10-测表空\n";
cout << " 11-测表满\n";
cout << " 12-显示表\n";
cout << " 13-按值查找前驱\n";
cout << " 0-退出\n";
}
//主函数
void main() {
int i;
int e, pre_e;
SqList<int> L; //建立容量为20、元素类型为整型的空顺序表
system("cls"); //执行系统命令cls清屏
int choice;
do {
dispmenu(); //显示主菜单
cout << "Enter choice(1~120 退出):";
cin >> choice;
switch (choice) {
case 1: //初始化线性表
cout << "请输入要创建的顺序表的长度:";
cin >> i;
cout << endl;
InitList(L, i);
cout << endl << "创建成功!" << endl;
break;
case 2: //创建线性表
cout << "请输入要创建的元素个数:";
cin >> i;
cout << endl;
CreateList(L, i);
cout << "创建的顺序表元素为:\n"; //显示表元素
DispList(L);
cout << endl;
break;
case 3: //获取第i个元素
cout << "请输入元素序号:";
cin >> i;
cout << endl;
if (GetElem_i(L, i, e))
cout << endl << "" << i << "个元素为:" << e << endl;
else
cout << endl << "元素不存在!" << endl;
break;
case 4: //按值查找
cout << "请输入要查询的元素值:";
cin >> e;
i = LocateElem_e(L, e);
if (i)
cout << endl << e << "是第" << i << "个数据元素" << endl;
else
cout << endl << "不存在此元素!" << endl;
break;
case 5: // 在第i个位置插入元素
cout << "输入插入位置:" << endl;
cin >> i;
cout << "输入插入元素值:" << endl;
cin >> e;
if (InsertElem_i(L, i, e)) {
cout << endl << "插入成功!" << endl;
DispList(L);
} else
cout << endl << "插入不成功!" << endl;;
break;
case 6: //删除第i个元素
cout << "输入删除元素位置:" << endl;
cin >> i;
if (DeleElem_i(L, i)) {
cout << endl << "删除成功!" << endl;
DispList(L);
} else
cout << endl << "删除失败!" << endl;
break;
case 7: //修改第i个元素的值
cout << "输入修改元素位置:" << endl;
cin >> i;
cout << "输入新元素值:" << endl;
cin >> e;
if (PutElem(L, i, e)) {
cout << endl << "修改成功!" << endl;
DispList(L);
} else
cout << endl << "修改失败!" << endl;
break;
case 8: // 清空表
ClearList(L);
break;
case 9: // 测表长
cout << "表长为:" << ListLength(L) << endl;
break;
case 10: //测表空
if (ListEmpty(L))
cout << endl << "空表!" << endl;
else
cout << endl << "不是空表!" << endl;
break;
case 11: // 未表满
if (ListFull(L))
cout << endl << "表满!" << endl;
else
cout << endl << "表不满!" << endl;
break;
case 12: //遍历显示表
DispList(L);
cout << endl;
cin.get(pause);
system("pause");
break;
case 13:
cout << "测试顺序表为\n";
DispList(L);
cout << "输入查找前驱的元素值:\n";
cin >> e;
if (PriorElem_e(L, e, pre_e))
cout << e << "的前驱元素为:" << pre_e << endl;
else
cout << e << "无前驱元素!" << endl;
break;
case 0: //退出
cout << "结束运行bye-bye!" << endl;
break;
default: //无效选择
cout << "无效选择!\n";
break;
}
} while (choice != 0);
}//end of main function

View File

@@ -0,0 +1,175 @@
template <class DT>
struct SqList // 顺序表
{
DT *elem; // 表首址
int length; // 表长
int size; // 表容量
};
//算法2.1
template <class DT>
bool PriorElem_e(SqList<DT> L, DT e, DT &pre_e) // 求值为e的元素前驱
{
int k;
k=LocateElem_e(L,e); //
if(k>1)
{
GetElem_i(L,k-1,pre_e);
return false;
}
else
return true;
}
//【算法2.2】 初始化
template <class DT>
bool InitList(SqList<DT> &L, int m)
{
L.elem=new DT[m]; // 申请表空间
if(L.elem==NULL)
{
cout<<"未创建成功!"; // 申请不成功,退出
exit (1);
}
L.length=0; // 申请成功属性赋值。空表表长为0
L.size=m; // 表容量为m
return true;
}
//【算法2.3】 创建表元素
template <class DT>
bool CreateList(SqList<DT> &L,int n)
{
int i;
if(n>L.size) // 1.元素个数大于表容量,不能创建。
{
cout<<"元素个数大于表长,不能创建!"<<endl;
return false;
}
cout<<"请依次输入"<<n<<"个元素值:"<<endl; // 2.依位序输入各元素值
for(i=1;i<=n;i++)
cin>>L.elem[i-1];
L.length=n; // 3.表长为创建的元素个数
return true;
}
//【算法2.4】 销毁顺序表
template <class DT>
void DestroyList(SqList<DT> &L)
{
delete [] L.elem; // 1.释放表空间
L.length=0; // 2.属性赋值
L.size=0;
}
//【算法2.5】 获取第i个元素值
template<class DT>
bool GetElem_i(SqList<DT> L,int i, DT &e)
{
if(i<1 || i>L.length) // 1.位序不合理返回false
{
cout<<"该元素不存在!"<<endl;
return false;
}
e=L.elem[i-1]; // 2. 否则获取第i个元素值
return true; // 返回true
}
//【算法2.6】 按值查找
template<class DT>
int LocateElem_e(SqList<DT> L, DT e)
{
for(int i=0; i<L.length; i++) // 顺序查找
if(L.elem[i]==e) // 1.找到
return i+1; // 返回元素位序
return 0; // 2.未找到返回0
}
//【算法2.7】
template<class DT>
bool InsertElem_i(SqList<DT> &L,int i, DT e)
{
if(L.length>=L.size) // 1.表满,不能插入
return false;
if(i<1 || i>L.length+1) // 2.插入位置不合理,不能插入
return false;
for (int j=L.length; j>=i; j--) // 3. an~ai依次后移
L.elem[j]=L.elem[j-1];
L.elem[i-1]=e;
L.length++;
return true; // 插入成功返回true
}
//【算法2.8】 删除第i个元素
template<class DT>
bool DeleElem_i(SqList<DT> &L,int i)
{
if(L.length==0) // 1.表空,不能删除
return false;
if(i<1 || i>L.length) // 2.删除位置不合理,不能插入
return false;
for (int j=i; j<L.length; j++) // 3. ai+1~an依次前移
L.elem[j-1]=L.elem[j];
L.length--;
return true; // 删除成功返回true
}
//【算法2.9】
template<class DT>
bool PutElem(SqList<DT> &L,int i, DT e) // 修改第i个元素的值
{
if(i<1 || i>L.length) // 1.位序不合理,不能修改,
return false; // 返回false
L.elem[i-1]=e; // 2.重置第i个元素值
return true; // 3.修改成功返回true
}
// 清空顺序表
template<class DT>
void ClearList(SqList<DT> &L)
{
L.length=0; // 空表表长为0
}
// 测表长
template<class DT>
int ListLength(SqList<DT> L)
{
return L.length;
}
template<class DT>
bool ListEmpty(SqList<DT> L) // 测表空
{
if(L.length==0) // 空表返回true
return true;
else
return false; // 非空表返回false
}
template<class DT>
bool ListFull(SqList<DT> L)
{
if(L.length==L.size) // 表满返回true
return true;
else
return false; // 表不满返回false
}
//【算法2.10】 遍历输出
template <class DT>
void DispList(SqList<DT> L)
{
int i;
for(i=0;i<L.length;i++) // 依位序输出元素值
{
cout<<L.elem[i]<<"\t";
}
cout<<endl;
}

View File

@@ -0,0 +1,185 @@
#include<iostream> //cout,cin
using namespace std;
#include "LinkList.h"
//算法2.25 单链表逆置
void ReverseLinkList(LNode<int> *&L)
{
LNode<int> *p,*q; // 1.设置工作指针
p=L->next; // 原链表头结点,作为逆置后表的头结点
L->next=NULL;
while(p) // 2. 依次摘除原链表结点,以头插法插入到逆置链表中
{
q=p; // 2.1 q取当前结点位置
p=p->next; // 2.2 p指向下一个待处理结点
q->next=L->next; // 2.3 将q 插入到头结点之后
L->next=q;
}
}
void dispmenu()
{ // 显示主菜单
cout<<endl;
cout<<" *** 单 链 表 ***\n";
cout<<" 1-初始化单链表\n";
cout<<" 2-尾插法顺序表\n";
cout<<" 3-头插法建表\n";
cout<<" 4-获取第i个元素\n";
cout<<" 5-按值查找\n";
cout<<" 6-插入第i个元素\n";
cout<<" 7-删除第i个元素\n";
cout<<" 8-修改第i个元素\n";
cout<<" 9-清空表\n";
cout<<"10-测表长\n";
cout<<"11-测表空\n";
cout<<"12-遍历输出\n";
cout<<"13-按值查找前驱\n";
cout<<"14-单链表逆置\n";
cout<<"0-退出\n";
}
char pause;
// 主函数
int main()
{
int i,n;
int e,pre_e;
LNode<int> * L;
system("cls"); // 清屏
int choice;
do
{
dispmenu(); // 显示主菜单
cout<<"Enter choice(1~120 退出):";
cin>>choice;
switch(choice)
{
case 1: // 初始化单链表
InitList(L);
cout<<endl<<"创建成功!"<<endl;
break;
case 2: // 尾插法建单链表
cout<<"尾插法创建单链表"<<endl;
cout<<"输入要创建的顺序表中元素个数:";
cin>>n;
cout<<endl;
CreateList_1(L,n);
cout<<"创建的单链表为:";
DispList(L);
cout<<endl;
break;
case 3: // 头插法创建单链表
cout<<"头插法创建单链表"<<endl;
cout<<"输入要创建的顺序表中元素个数:";
cin>>n;
cout<<endl;
CreateList_2(L,n);
cout<<"创建的单链表为:";
DispList(L);
cout<<endl;
break;
case 4: // 获取第i个元素
cout<<"请输入元素序号:";
cin>>i;
cout<<endl;
if (GetElem_i(L,i, e))
cout<<endl<<""<<i<<"个元素为:"<<e<<endl;
else
cout<<endl<<"元素不存在!"<<endl;
break;
case 5: // 查询元素位序
cout<<"请输入要查询的元素值:";
cin>>e;
i=LocateElem_e(L, e);
if(i)
cout<<endl<<e<<"是第"<<i<<"个数据元素"<<endl;
else
cout<<endl<<"不存在此元素!"<<endl;
break;
case 6: // 插入第i个元素
cout<<"输入插入位置:"<<endl;
cin>>i;
cout<<"输入插入元素值:"<<endl;
cin>>e;
if(InsertElem_i(L,i,e))
{
cout<<endl<<"插入成功!"<<endl;
cout<<"插入元素后的单链表为:"<<endl;
DispList(L);
}
else
cout<<endl<<"插入不成功!"<<endl;
break;
case 7: // 删除第i个元素
cout<<"输入删除元素位置:"<<endl;
cin>>i;
if(DeleElem_i(L,i))
{
cout<<endl<<"删除成功!"<<endl;
cout<<"删除元素后的单链表为:"<<endl;
DispList(L);
}
else
cout<<endl<<"删除失败!"<<endl;
break;
case 8: // 修改第i个元素
cout<<"输入修改元素位置:"<<endl;
cin>>i;
cout<<"输入新元素值:"<<endl;
cin>>e;
if(PutElem_i(L,i,e))
{
cout<<endl<<"修改成功!"<<endl;
cout<<"修改后的单链表为:"<<endl;
DispList(L);
}
else
cout<<endl<<"修改失败!"<<endl;
break;
case 9: // 清空表
ClearList(L);
break;
case 10: // 测表长
cout<<"表长为:"<<ListLength(L)<<endl;
break;
case 11: // 测表空
if(ListEmpty(L))
cout<<endl<<"空表!"<<endl;
else
cout<<endl<<"不是空表!"<<endl;
break;
case 12: //遍历显示表
DispList(L);
cout<<endl;
break;
case 13:
cout<<"测试链表为\n";
DispList(L);
cout<<"输入查找前驱的元素值:\n";
cin>>e;
if(PriorElem_e(L,e,pre_e))
cout<<e<<"的前驱元素为:"<<pre_e<<endl;
else
cout<<e<<"无前驱元素!"<<endl;
break;
case 14: // 逆置单链表
cout<<"逆置前的单链表为:";
DispList(L);
ReverseLinkList(L);
cout<<"逆置后的单链表为:";
DispList(L);
cout<<endl;
break;
case 0: //退出,销毁链表
DestroyList(L);
cout<<"结束运行bye-bye!"<<endl;
break;
default: //无效选择
cout<<"无效选择!\n";
break;
}
}while(choice!=0);
return 0;
}//end of main function

View File

@@ -0,0 +1,253 @@
template <class DT>
struct LNode //链表结点
{
DT data; //数据域,存储数据元素值
LNode *next; //指针域,指向下一个结点
};
//算法2.1
template <class DT>
bool PriorElem_e(LNode<DT> *L, DT e, DT &pre_e) // 求值为e的元素前驱
{
int k;
k=LocateElem_e(L,e); // 1.获取e的位序k
if(k>1) // 2.位序k大于1
{
GetElem_i(L,k-1,pre_e); // 第k-1个元素为e的前驱
return true;
}
else // 3.元素e无前驱
return false; // 返回false
}
//【算法2.14】 创建空单链表
template <class DT>
bool InitList(LNode<DT> *&L)
{
L=new LNode<DT>; // 1.创建头结点
if(!L) exit(1); // 2.创建失败,退出
L->next=NULL; // 3.创建成功
return true; // 返回true
}
//【算法2.15】 尾插法创建n的元素
template <class DT>
bool CreateList_1(LNode<DT> *&L,int n)
{
int i;
LNode<DT> *p,*s;
p=L; //1.工作指针初始化,指向尾结点
cout<<"依次输入"<<n<<"个数据元素:"<<endl;
for(i=1; i<=n;i++) // 2.按元素位序正序创建各结点
{
s=new LNode<DT>; // 2.1 新建一个结点s
if (!s) // 2.2 创建失败返回false
return false;
cin>>s->data; // 2.3 输入结点值
s->next=p->next; // 2.4 s 链在表尾
p->next=s;
p=s; // 2.5 工作指针指向 s
}
return true; // 3.创建成功返回true
}
//【算法2.16】 头插法创建n个元素
template <class DT>
bool CreateList_2(LNode<DT> *(&L),int n)
{
int i;
LNode<DT> *s;
cout<<"逆序输入"<<n<<"个数据元素:"<<endl;
for(i=1; i<=n;i++) // 1.按元素位序逆序创建各结点
{
s=new LNode<DT>; // 1.1 新建一个结点 s
if (!s) // 1.2 创建失败返回false
return false;
cin>>s->data; // 1.3 输入结点值
s->next=L->next; // 1.4 s 在头结点后
L->next=s;
}
return true; // 1.创建成功返回true
}
//【算法2.17】
template <class DT>
void DestroyList(LNode<DT> *(&L)) // 释放链表所占空间
{
LNode<DT> *p;
while(L) // 1. 表非空,从头结点开始,依次释放结点
{
p=L; // 1.1 处理表头结点
L=L->next; // 1.2 头指针后移
delete p; // 1.3 释放表头结点所占内存
}
L=NULL; // 2.头指针指向空
}
//【算法2.18】 获取第i个元素
template<class DT>
bool GetElem_i(LNode<DT> *L,int i, DT &e)
{
LNode<DT> *p; // 1.初始化
p=L->next; // 1.1 设置工作指针,从首结点开始数结点
int j=1; // 1.2 计数器初始化
while(p&&j<i) // 2.定位到第i个元素结点
{
p=p->next;j++;
}
if(!p ||j>i ) // 3 未找到返回false
return false;
else // 4. 找到
{
e=p->data; // 获取第i个元素值
return true; // 返回true
}
}
//【算法2.19】 查找值为e的元素位序
template<class DT>
int LocateElem_e(LNode<DT> *L, DT e)
{
LNode<DT> *p; // 1.初始化从首元开始查找
p=L->next; // 1.1从首元开始查找
int j=1; // 1.2 计数器初值
while(p && p->data!=e) // 2.顺序查找
{
p=p->next; // 2.1未找到指针后移
j++; // 2.2 计数器增1
}
if(p==NULL) // 3. 判断是否找到
return 0; // 3.1末找到返回0
else
return j; // 3.2 找到,返回位序
}
//【算法2.20】 插入第i个元素
template<class DT>
bool InsertElem_i(LNode<DT> *&L,int i, DT e)
{
int j=0;
LNode<DT> *p; // 1.初始化
p=L; // 工作指针初始化
while(p && j<i-1) // 2. 定位到插入点前驱
{
p=p->next;
j++;
}
if(!p||j>i-1) // 3.判断定位是否成功:
return false; // 3.1 定位失败,不能插入
else // 3.2 定位成功
{
LNode<DT> *s;
s=new LNode<DT>; // 3.2.1建立新结点
s->data=e; // 3.2.2新结点赋值
s->next=p->next; // 3.2.3结点S链接到p结点之后
p->next=s;
return true; // 3.2.4 插入成功返回true
}
}
//【算法2.21】 删除第i个元素
template<class DT>
bool DeleElem_i(LNode<DT> *(&L),int i)
{
LNode<DT> *p,*q; //1.初始化:设置工作指针
p=L; //查找从头结点开始
int j=0; //计数器初始化
while(p->next && j<i-1) //2.p定位到删除点的前驱
{
p=p->next;
j++;
}
if(!p->next||j>i-1) //3.删除位置不合理,不能删除
return false; //返回false
else //4.删除操作
{
q=p->next; //4.1暂存删除结点位置
p->next=q->next; //4.2从链表中摘除删除结点
delete q;
return true; //4.3删除成功返回true
}
}
//【算法2.22】 修改第i个元素值
template<class DT>
bool PutElem_i(LNode<DT> *(&L),int i, DT e)
{
LNode<DT> *p; // 1.初始化:设置工作指针
p=L->next; // 从首结点开始,数结点
int j=1; // 计数器初始化
while(p&&j<i) // 2.查找第i个元素结点
{
p=p->next;j++;
}
if(!p||j>i) // 3.元素不存在,返回false
return false;
else // 4.定位成功
{
p->data=e; // 修改元素值
return true; // 返回true
}
}
// 释放链表所占空间
template<class DT>
void ClearList(LNode<DT> *(&L))
{
LNode<DT> *p;
while(L->next) // 从首元结点开始,依次释放结点
{
p=L->next;
L->next=p->next;
delete p;
}
cout<<endl<<"表已清空!"<<endl;
}
//【算法2.23】 测表长
template<class DT>
int ListLength(LNode<DT> *L)
{ // 1.初始化
int len=0; // 1.1 结点计数器赋初值0
LNode<DT> *p; // 1.2设置工作指针
p=L; // 指向头结点
while(p->next) // 2.数结点个数。有后继结点,
{
len++;p=p->next; // 结点数增1指针后移
}
return len; // 3.返回表长
}
//
template<class DT>
bool ListEmpty(LNode<DT> *L) //测表空
{
if(L->next==NULL)
return true; //空表返回1
else
return false; //不空返回0
}
//【算法2.24】 遍历表
template <class DT>
void DispList(LNode<DT> *L) // 显示表内容
{
LNode<DT> *p; // 1. 设置工作指针
p=L; // 从首元结点开始遍历
while(p->next) // 2.依次输出各结点值
{
p=p->next;cout<<p->data<<"\t";
}
cout<<endl;
}

View File

@@ -0,0 +1,160 @@
template <class DT>
struct SqList //顺序表类
{
DT *elem; //表首址
int length; //表长
int size;//表容量
};
//【算法2.2】
template <class DT>
bool InitList(SqList<DT> &L, int m)
{//构建函数创建一表容量为m的空表
L.elem=new DT[m];// 申请表空间
if(L.elem==NULL)
{
cout<<"未创建成功!";
exit (1);
}
L.length=0; // 空表表长为0
L.size=m; //表容量为m
return true;
}
//【算法2.3】
template <class DT>
bool CreateList(SqList<DT> &L,int n) //创建表长度为n的顺序表
{
int i;
if(n>L.size)
{
cout<<"元素个数大于表长,不能创建!"<<endl;
return false;
}
cout<<"请依次输入"<<n<<"个元素值:"<<endl;
for(i=1;i<=n;i++)
cin>>L.elem[i-1];
L.length=n;
return true;
}
//【算法2.4】
template <class DT>
void DestroyList(SqList<DT> &L) //销毁顺序表,释放表空间
{
delete [] L.elem;
L.length=0;
L.size=0;
}
//【算法2.5】
template<class DT>
bool GetElem_i(SqList<DT> L,int i, DT &e)// 获取第i个元素值
{
if(i<0 || i>L.length)
return false;
e=L.elem[i-1];
return true;
}
//【算法2.6】
template<class DT>
int LocateElem_e(SqList<DT> L, DT e) // 按值查找
{
int i;
for(i=0; i<L.length; i++) // 顺序查找
if(L.elem[i]==e) // 找到
return i+1;
return 0; // 未找到
}
//【算法2.7】
template<class DT>
bool InsertElem_i(SqList<DT> &L,int i, DT e) // 在第i个位置插入新元素
{
int j;
if(L.length>=L.size) //1.表满,不能插入
return false;
if(i<1 || i>L.length+1) //2.插入位置不合理,不能插入
return false;
for (j=L.length; j>=i; j--) //3. an~ai依次后移
L.elem[j]=L.elem[j-1];
L.elem[i-1]=e;
L.length++;
return true; // 插入成功返回true
}
//【算法2.8】
template<class DT>
bool DeleElem_i(SqList<DT> &L,int i) // 删除第i个元素
{
int j;
if(L.length==0) //1.表空,不能删除
return false;
if(i<1 || i>L.length) //2.删除位置不合理,不能插入
return false;
for (j=i; j<L.length; j++) //3. ai~an依次前移
L.elem[j-1]=L.elem[j];
L.length--;
return true; // 删除成功返回true
}
//【算法2.9】
template<class DT>
bool PutElem(SqList<DT> &L,int i, DT e) // 修改第i个元素的值
{
if(i<1 || i>L.length) //1.位置不合理,不能修改
return false;
L.elem[i-1]=e; //2.重置第i个元素值
return true; //3.修改成功返回true
}
//清空顺序表
template<class DT>
void ClearList(SqList<DT> &L)
{
L.length=0;
}
//测表长
template<class DT>
int ListLength(SqList<DT> L)
{
return L.length;
}
//测表空
template<class DT>
bool ListEmpty(SqList<DT> L)
{
if(L.length==0)
return true;
else
return false;
}
//测表满
template<class DT>
bool ListFull(SqList<DT> L)
{
if(L.length==L.size)
return true;
else
return false;
}
//【算法2.10】
template <class DT>
void DispList(SqList<DT> L) //遍历输出
{
int i;
for(i=0;i<L.length;i++)
{
cout<<L.elem[i]<<"\t";
}
cout<<endl;
}

View File

@@ -0,0 +1,187 @@
#include<iostream>//cout,cin
using namespace std;
#include "SqList.h"
char pause;
void dispmenu()
{ // 显示主菜单
cout<<"\n*** 顺序表的应用 ***\n";
cout<<" 1-集合并A=AB\n";
cout<<" 2-顺序表逆置\n";
cout<<" 3-多项式求和\n";
cout<<" 0-退出\n";
}
//算法2.11
template <class DT>
void Union(SqList<DT> &La, SqList<DT> Lb) // 求La=LaLa
{
DT e;
int k,i;
for(i=1;i<=Lb.length;i++) // 扫描Lb
{
GetElem_i(Lb,i,e); // 1. 获取Lb的第i个元素
if(!LocateElem_e(La,e)) // 2.如果La中无此元素
{
k=La.length+1; // 添加在La的表尾
InsertElem_i(La,k,e);
}
}
}
//算法2.12
template <class DT>
void ReverseSqList(SqList<DT> &L) // 顺序表元素逆置
{
DT t;
int i;
for(i=0; i<L.length/2; i++) // 2.L.elem[i]<-->L.elem[L.length-i-1]
{
t=L.elem[i];
L.elem[i]=L.elem[L.length-i-1];
L.elem[L.length-i-1]=t;
}
return;
}
//算法2.13 多项式求和 lc=la+lb
void PolyAdd(SqList<float> la, SqList<float> lb, SqList<float> &lc)
{
int i=0; // 1.初始化,设置处理起始位置
while(i<la.length && i<lb.length) // 2.两个多项式均未处理完
{
lc.elem[i]=la.elem[i]+lb.elem[i]; // 相同位序上的系数相加
i++;
}
if(la.length>lb.length) // 3.la未处理完lb已处理完
{
while(i<la.length) // lc取la中剩余项
{
lc.elem[i]=la.elem[i];
i++;
}
}
else // 3.lb未处理完la已处理完
{
while(i<lb.length) // lc取lb中剩余项
{
lc.elem[i]=lb.elem[i];
i++;
}
}
}
//显示多顶式
void DispPoly(float A[],int n)
{
int i;
cout<<A[0]<<"+";
for (i=1; i<n-1;i++)
cout<<A[i]<<"x^"<<i<<" + ";
cout<<A[i]<<"x^"<<i;
cout<<endl;
}
//主函数
int main()
{
int e;
int na,nb,nc;
SqList<int> La,Lb; // 集合A、B
SqList<int> Lc; // 顺序表
SqList<float> fa,fb,fc; // 多项式A、B、C
system("cls"); // 清屏
int choice;
do
{
dispmenu(); // 显示主菜单
cout<<"Enter choice(1~4,0退出):";
cin>>choice;
switch(choice)
{
case 1: // 求集合并
cout<<"创建集合A、B\n";
cout<<"输入集合A元素个数";
cin>>na;
cout<<"输入集合B元素个数";
cin>>nb;
InitList(La,na+nb); // 创建集合A
cout<<"创建集合A的元素\n";
CreateList(La,na);
InitList(Lb,nb); // 创建集合B
cout<<"创建集合B的元素\n";
CreateList(Lb,nb);
cout<<"集合A为"<<endl; // 显示集合A
DispList(La);
cout<<"集合B为"<<endl; // 显示集合B
DispList(Lb);
Union(La, Lb); // 求集合并
cout<<"AB为:"<<endl; // 显示结果
DispList(La);
cout<<endl;
DestroyList(La);
DestroyList(Lb);
break;
case 2: // 顺序表逆置
cout<<"请输入要创建的顺序表中元素个数:";
cin>>nc;
InitList(Lc,nc);
cout<<endl;
CreateList(Lc,nc);
cout<<"创建的顺序表为:"<<endl; // 显示集合A
DispList(Lc);
ReverseSqList(Lc);
cout<<"逆置后的顺序表为:"<<endl; // 显示集合A
DispList(Lc);
cout<<endl;
DestroyList(Lc);
break;
case 3: //多项式求和
cout<<"\n创建多项式A\n"; // 创建多项式A
cout<<"输入多项式A的项数";
cin>>na;
InitList(fa,na);
cout<<"按幂升序输入多项式A各项系数\n";
CreateList(fa,na);
cout<<"\n创建多项式B\n"; // 创建多项式B
cout<<"输入多项式B的项数";
cin>>nb;
InitList(fb,nb);
cout<<"按幂升序输入多项式B各项系数\n";
CreateList(fb,nb);
cout<<"\n多项式 A 为 "<<endl; // 显示多项式A
DispPoly(fa.elem,na);
cout<<"\n多项式 B 为 "<<endl; // 显示多项式B
DispPoly(fb.elem,nb);
nc=(na>=nb)?na:nb;
InitList(fc,nc); // 创建多项式C
PolyAdd(fa,fb,fc); // 求多项式 C=A+B
cout<<"\n多项式A + 多项式B = "<<endl; // 显示结果
DispPoly(fc.elem,nc);
cout<<endl;
DestroyList(fa);
DestroyList(fb);
DestroyList(fc);
break;
case 0: // 退出
cout<<" 结束运行bye-bye!"<<endl;
break;
default: // 无效选择
cout<<"无效选择!\n";
break;
}
}while(choice!=0);
return 0;
}//end of main function

View File

@@ -0,0 +1,238 @@
#include<iostream>//cout,cin
using namespace std;
struct PolyNode // 多项式结点
{
float coef; // 系数
int exp; // 指数
PolyNode *next; // 指向下一项结点
};
void InitPoly(PolyNode *&L)
{ //创建一空多项式
L=new PolyNode;
L->next=NULL;
}
bool CreatePoly(PolyNode *&L,int n) // 尾插法创建n项多项式
{
int i;
PolyNode *p,*s;
p=L;
for(i=1; i<=n; i++) // 按幂升序依次输入多项式各项系数与幂指数
{
s=new PolyNode;
if (!s)
return false;
cout<<"输入第"<<i<<"项系数和幂指数:";
cin>>s->coef>>s->exp;
s->next=p->next;
p->next=s;
p=s;
}
return true;
}
//显示多顶式
void DispPoly(PolyNode *L) // 通过遍历结点,输出多项式
{
PolyNode *p;
if(!L) // 空表,返回
{
cout<<"空表!";
return;
}
p=L->next;
while(p && p->next)
{
cout<<p->coef<<"x^"<<p->exp<<" + ";
p=p->next;
}
cout<<p->coef<<"x^"<<p->exp;
cout<<endl;
}
//【算法2.26】 求多项式LA=LA+LB
void PolyAdd(PolyNode *&LA,PolyNode *&LB)
{
float sum;
PolyNode *pa,*pb,*qa,*qb; // 1.工作指针初始化
pa=LA;
qa=pa->next;
pb=LB;
qb=pb->next;
while (qa!=NULL && qb!=NULL ) // 2. 两表均不空
{
if (qa->exp < qb->exp ) // 2.1 LA的幂小
{ // pa、qa后移
pa=qa;qa=qa->next; }
else if ( qa->exp > qb->exp ) //2.2 LA 幂大
{
pb->next =qb->next; // qb链接到pa之后
qb->next=qa;
pa->next=qb;
pa=qb;
qb=pb->next;
}
else // 2.3 LA与LB幂相同
{
sum=qa->coef+qb->coef; // 计算系数和
if (sum!=0 ) // 2.3.1 系数和不为0
{
qa->coef=sum; // 2.3.1.1 qa->coef←sum
pa=qa; qa=qa->next; // 2.3.1.2 paqa后移
pb->next=qb->next;
delete qb; // 2.3.1.3删除qb
qb=pb->next;
}
else // 2.3.2 系数和为0
{
pa->next=qa->next;
delete qa; // 2.3.2.1 删除qa
qa=pa->next; // 2.3.2.2 qa为pa后继
pb->next=qb->next;
delete qb; // 2.3.2.3 删除qb
qb=pb->next; // 2.3.2.4 qb为pb的后继
}
}
}//while
if(qb!=NULL) // 3. LA处理结束LB未结束
pa->next=qb; // 3.1 qb链到qa之后
delete pb; // 3.2 删除lb头结点
LB=NULL;
}//Add
void DestroyPoly(PolyNode *&L) // 释放链表所占空间
{
PolyNode *p;
while(L) // 从头结点开始,依次释放结点
{
p=L;
L=L->next;
delete p;
}
L=NULL; // 头结点指向空
}
void SortPoly(PolyNode *&L) // 将多项式按幂升序排序
{
PolyNode *p1,*p2,*q,*r; // 采用插入排序算法
p1=L;p2=p1->next; // p1是p2的前驱
if(p2==NULL || p2->next==NULL) // 空表或只有1项的多项式不需要排序
{
cout<<"不需要排序!"<<endl;
return;
}
r=L->next; // 有序表表尾
q=r->next; // q为当前处理项有序表的后一项
while(q) // 未处理完
{ // 从首元结点开始查找插入点
p1=L;p2=p1->next;
while(q->exp>p2->exp && p2!=q) // 当前结点幂大,插入点后移
{
p1=p2;p2=p2->next;
}
if(p2==q) // 当前项无需移动
{
r=q; // 有序表表尾顺移
}
else // q插入到p2前面
{
r->next=q->next; // 摘除q结点
q->next=p1->next; // 在p1后插入结点q
p1->next=q;
}
q=r->next; // 下一个需处理的项
}
return;
}
void dispmenu()
{//显示主菜单
cout<<endl;
cout<<"*** 稀疏多项式求和 ***\n";
cout<<"1-创建多项式A\n";
cout<<"2-创建多项式B\n";
cout<<"3-多项式求和A=A+B\n";
cout<<"4-显示多项式\n";
cout<<"0-退出\n";
}
//主函数
int main()
{
int m,n;
char c;
PolyNode *LA, *LB;
system("cls"); // 执行系统命令cls清屏
int choice;
do
{
dispmenu(); // 显示主菜单
cout<<"Enter choice(1~4,0 退出):";
cin>>choice;
switch(choice)
{
case 1: // 创建多项式A
InitPoly(LA);
cout<<"请输入多项式 A 的项数: ";
cin>>m;
CreatePoly(LA, m );
cout<<"创建的多项式 A 为:"<<endl;
DispPoly(LA);
SortPoly(LA);
cout<<"排序后多项式 A 为:"<<endl;
DispPoly(LA);
break;
case 2: // 创建多项式B
InitPoly(LB);
cout<<"请输入多项式B的项数 ";
cin>>n;
CreatePoly(LB, n );
cout<<"创建的多项式B为"<<endl;
DispPoly(LB);
SortPoly(LB);
cout<<"排序后多项式 B 为:"<<endl;
DispPoly(LB);
break;
case 3: //多项式求和
cout<<"A = ";
DispPoly(LA);
cout<<"B = ";
DispPoly(LB);
PolyAdd(LA,LB); // 求多项式 LA=LA+LB
cout<<"A + B = "; // 显示结果
DispPoly(LA);
cout<<endl;
break;
case 4: // 显示多项式
cout<<"选择要显示的多项式 A 或 B"<<endl;
cin>>c;
if(c=='A'||c=='a')
DispPoly(LA);
else if(c=='B'||c=='b')
DispPoly(LB);
else
cout<<"选择错误!"<<endl;
break;
case 5: //退出
DestroyPoly(LA);
DestroyPoly(LB);
cout<<"结束运行bye-bye!"<<endl;
break;
default: //非法选择
cout<<"非法选择!\n";
break;
}
}while(choice!=0);
return 0;
}//end of main function