commit 2e9a13a76cb250588162b1637d223f0bae714f13 Author: Launchcore Date: Mon Mar 4 23:45:56 2024 +0800 2024-3-4 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8ada255 --- /dev/null +++ b/.gitignore @@ -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 + diff --git a/Chapter1/Chapter1.sln b/Chapter1/Chapter1.sln new file mode 100644 index 0000000..6d89f48 --- /dev/null +++ b/Chapter1/Chapter1.sln @@ -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 diff --git a/Chapter1/app1-4/app1-4.cpp b/Chapter1/app1-4/app1-4.cpp new file mode 100644 index 0000000..244bb33 --- /dev/null +++ b/Chapter1/app1-4/app1-4.cpp @@ -0,0 +1,21 @@ +// USE B.CPP INSTEAD + +#include +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< + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + true + + + + 17.0 + Win32Proj + {7a2bd8ec-2595-4070-be3a-2bf2140848b2} + app14 + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + \ No newline at end of file diff --git a/Chapter1/app1-4/app1-4.vcxproj.filters b/Chapter1/app1-4/app1-4.vcxproj.filters new file mode 100644 index 0000000..b5de964 --- /dev/null +++ b/Chapter1/app1-4/app1-4.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 源文件 + + + \ No newline at end of file diff --git a/Chapter1/b/b.cpp b/Chapter1/b/b.cpp new file mode 100644 index 0000000..3268c74 --- /dev/null +++ b/Chapter1/b/b.cpp @@ -0,0 +1,20 @@ +#include +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; +} \ No newline at end of file diff --git a/Chapter1/b/b.vcxproj b/Chapter1/b/b.vcxproj new file mode 100644 index 0000000..ae621c9 --- /dev/null +++ b/Chapter1/b/b.vcxproj @@ -0,0 +1,135 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + + + 17.0 + Win32Proj + {51ce097c-c445-4a3e-a678-539c70e334df} + b + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + \ No newline at end of file diff --git a/Chapter1/b/b.vcxproj.filters b/Chapter1/b/b.vcxproj.filters new file mode 100644 index 0000000..a43892b --- /dev/null +++ b/Chapter1/b/b.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 源文件 + + + \ No newline at end of file diff --git a/Chapter2/2-1-SqList/2-1-SqList.vcxproj b/Chapter2/2-1-SqList/2-1-SqList.vcxproj new file mode 100644 index 0000000..1d76ea3 --- /dev/null +++ b/Chapter2/2-1-SqList/2-1-SqList.vcxproj @@ -0,0 +1,138 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + + + + + + 17.0 + Win32Proj + {15e9e8ad-d18e-45b2-861a-699717de99bb} + My21SqList + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + \ No newline at end of file diff --git a/Chapter2/2-1-SqList/2-1-SqList.vcxproj.filters b/Chapter2/2-1-SqList/2-1-SqList.vcxproj.filters new file mode 100644 index 0000000..8c7484a --- /dev/null +++ b/Chapter2/2-1-SqList/2-1-SqList.vcxproj.filters @@ -0,0 +1,27 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 源文件 + + + + + 头文件 + + + \ No newline at end of file diff --git a/Chapter2/2-1-SqList/SqList.cpp b/Chapter2/2-1-SqList/SqList.cpp new file mode 100644 index 0000000..65f8a5f --- /dev/null +++ b/Chapter2/2-1-SqList/SqList.cpp @@ -0,0 +1,156 @@ +// 2-1-SqList-˳ + +#include //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 L; //Ϊ20ԪΪ͵Ŀ˳ + system("cls"); //ִϵͳcls + + int choice; + do { + dispmenu(); //ʾ˵ + cout << "Enter choice(1~120 ˳):"; + cin >> choice; + switch (choice) { + case 1: //ʼԱ + cout << "Ҫ˳ijȣ"; + 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 << "޸ijɹ" << 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 + diff --git a/Chapter2/2-1-SqList/SqList.h b/Chapter2/2-1-SqList/SqList.h new file mode 100644 index 0000000..8f055b0 --- /dev/null +++ b/Chapter2/2-1-SqList/SqList.h @@ -0,0 +1,175 @@ +template +struct SqList // ˳ +{ + DT* elem; // ַ + int length; // + int size; // +}; + +//㷨2.1 +template +bool PriorElem_e(SqList
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 +bool InitList(SqList
& 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 +bool CreateList(SqList
& 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 +void DestroyList(SqList
& L) +{ + delete[] L.elem; // 1.ͷűռ + L.length = 0; // 2.Ըֵ + L.size = 0; +} + +//㷨2.5 ȡiԪֵ +template +bool GetElem_i(SqList
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 +int LocateElem_e(SqList
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 +bool InsertElem_i(SqList
& 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 +bool DeleElem_i(SqList
& 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 +bool PutElem(SqList
& 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.޸ijɹtrue +} + +// ˳ +template +void ClearList(SqList
& L) +{ + L.length = 0; // ձΪ0 +} + + +// +template +int ListLength(SqList
L) +{ + return L.length; +} + + +template +bool ListEmpty(SqList
L) // +{ + if (L.length == 0) // ձtrue + return true; + else + return false; // ǿձfalse +} + +template +bool ListFull(SqList
L) +{ + if (L.length == L.size) // true + return true; + else + return false; // false +} + +//㷨2.10 +template +void DispList(SqList
L) +{ + int i; + for (i = 0; i < L.length; i++) // λԪֵ + { + cout << L.elem[i] << "\t"; + + } + cout << endl; +} + + diff --git a/Chapter2/2-2-LinkList/2-2-LinkList.vcxproj b/Chapter2/2-2-LinkList/2-2-LinkList.vcxproj new file mode 100644 index 0000000..4f5cda0 --- /dev/null +++ b/Chapter2/2-2-LinkList/2-2-LinkList.vcxproj @@ -0,0 +1,138 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + + + + + + 17.0 + Win32Proj + {631ed17d-100b-4089-8ae8-768fb5d19442} + My22LinkList + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + false + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + \ No newline at end of file diff --git a/Chapter2/2-2-LinkList/2-2-LinkList.vcxproj.filters b/Chapter2/2-2-LinkList/2-2-LinkList.vcxproj.filters new file mode 100644 index 0000000..fd4eab8 --- /dev/null +++ b/Chapter2/2-2-LinkList/2-2-LinkList.vcxproj.filters @@ -0,0 +1,27 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 源文件 + + + + + 头文件 + + + \ No newline at end of file diff --git a/Chapter2/2-2-LinkList/LinkList.cpp b/Chapter2/2-2-LinkList/LinkList.cpp new file mode 100644 index 0000000..4ed2aec --- /dev/null +++ b/Chapter2/2-2-LinkList/LinkList.cpp @@ -0,0 +1,188 @@ +// 2-2-LinkList- +// WARNING: /sdl is disabled to pass the compilation process. + +#include //cout,cin +using namespace std; +#include "LinkList.h" + +//㷨2.25 +void ReverseLinkList(LNode*& L) +{ + LNode* 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* 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 << "޸ijɹ" << 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 \ No newline at end of file diff --git a/Chapter2/2-2-LinkList/LinkList.h b/Chapter2/2-2-LinkList/LinkList.h new file mode 100644 index 0000000..e41b0e3 --- /dev/null +++ b/Chapter2/2-2-LinkList/LinkList.h @@ -0,0 +1,253 @@ + +template +struct LNode // +{ + DT data; //򣬴洢Ԫֵ + LNode* next; //ָָһ +}; + +//㷨2.1 +template +bool PriorElem_e(LNode
* L, DT e, DT& pre_e) // ֵΪeԪǰ +{ + int k; + k = LocateElem_e(L, e); // 1.ȡeλk + if (k > 1) // 2.λk1 + { + GetElem_i(L, k - 1, pre_e); // k-1ԪΪeǰ + return true; + } + else // 3.Ԫeǰ + return false; // false +} + +//㷨2.14 յ +template +bool InitList(LNode
*& L) +{ + L = new LNode
; // 1.ͷ + if (!L) exit(1); // 2.ʧܣ˳ + L->next = NULL; // 3.ɹ + return true; // true +} + +//㷨2.15 β巨nԪ +template +bool CreateList_1(LNode
*& L, int n) +{ + int i; + LNode
* p, * s; + p = L; //1.ָʼָβ + cout << "" << n << "Ԫأ" << endl; + for (i = 1; i <= n; i++) // 2.Ԫλ򴴽 + { + s = new LNode
; // 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 +bool CreateList_2(LNode
* (&L), int n) +{ + int i; + LNode
* s; + cout << "" << n << "Ԫأ" << endl; + for (i = 1; i <= n; i++) // 1.Ԫλ򴴽 + { + s = new LNode
; // 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 +void DestroyList(LNode
* (&L)) // ͷռռ +{ + LNode
* p; + while (L) // 1. ǿ,ͷ㿪ʼͷŽ + { + p = L; // 1.1 ͷ + L = L->next; // 1.2 ͷָ + delete p; // 1.3 ͷűͷռڴ + } + L = NULL; // 2.ͷָָ +} + +//㷨2.18 ȡiԪ +template +bool GetElem_i(LNode
* L, int i, DT& e) +{ + LNode
* 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 +int LocateElem_e(LNode
* L, DT e) +{ + + LNode
* 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 +bool InsertElem_i(LNode
*& L, int i, DT e) +{ + + int j = 0; + LNode
* 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
* s; + s = new LNode
; // 3.2.1½ + s->data = e; // 3.2.2½㸳ֵ + s->next = p->next; // 3.2.3Sӵp֮ + p->next = s; + return true; // 3.2.4 ɹtrue + } + +} + +//㷨2.21 ɾiԪ +template +bool DeleElem_i(LNode
* (&L), int i) +{ + + LNode
* 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 +bool PutElem_i(LNode
* (&L), int i, DT e) +{ + LNode
* 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 +void ClearList(LNode
* (&L)) +{ + + LNode
* p; + while (L->next) // Ԫ㿪ʼͷŽ + { + p = L->next; + L->next = p->next; + delete p; + } + cout << endl << "!" << endl; +} + + +//㷨2.23 +template +int ListLength(LNode
* L) +{ // 1.ʼ + int len = 0; // 1.1 ֵ0 + LNode
* p; // 1.2ùָ + p = L; // ָͷ + while (p->next) // 2.к̽㣬 + { + len++; p = p->next; // 1ָ + } + return len; // 3.ر +} + +// +template +bool ListEmpty(LNode
* L) // +{ + if (L->next == NULL) + return true; //ձ1 + else + return false; //գ0 +} + + +//㷨2.24 +template +void DispList(LNode
* L) // ʾ +{ + LNode
* p; // 1. ùָ + p = L; // Ԫ㿪ʼ + while (p->next) // 2.ֵ + { + p = p->next; cout << p->data << "\t"; + + + } + cout << endl; +} diff --git a/Chapter2/2-3-SqListApp/2-3-SqListApp.vcxproj b/Chapter2/2-3-SqListApp/2-3-SqListApp.vcxproj new file mode 100644 index 0000000..3812e46 --- /dev/null +++ b/Chapter2/2-3-SqListApp/2-3-SqListApp.vcxproj @@ -0,0 +1,138 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + + + + + + 17.0 + Win32Proj + {6816c6c8-1b6d-4773-812a-e7a50324cb12} + My23SqListApp + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + \ No newline at end of file diff --git a/Chapter2/2-3-SqListApp/2-3-SqListApp.vcxproj.filters b/Chapter2/2-3-SqListApp/2-3-SqListApp.vcxproj.filters new file mode 100644 index 0000000..2fa3354 --- /dev/null +++ b/Chapter2/2-3-SqListApp/2-3-SqListApp.vcxproj.filters @@ -0,0 +1,27 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 源文件 + + + + + 头文件 + + + \ No newline at end of file diff --git a/Chapter2/2-3-SqListApp/SqList.h b/Chapter2/2-3-SqListApp/SqList.h new file mode 100644 index 0000000..5a18d59 --- /dev/null +++ b/Chapter2/2-3-SqListApp/SqList.h @@ -0,0 +1,160 @@ +template +struct SqList //˳ +{ + DT* elem; //ַ + int length; // + int size;// +}; + +//㷨2.2 +template +bool InitList(SqList
& 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 +bool CreateList(SqList
& 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 +void DestroyList(SqList
& L) //˳ͷűռ +{ + delete[] L.elem; + L.length = 0; + L.size = 0; +} + +//㷨2.5 +template +bool GetElem_i(SqList
L, int i, DT& e)// ȡiԪֵ +{ + if (i<0 || i>L.length) + return false; + e = L.elem[i - 1]; + return true; +} + +//㷨2.6 +template +int LocateElem_e(SqList
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 +bool InsertElem_i(SqList
& 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 +bool DeleElem_i(SqList
& 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 +bool PutElem(SqList
& 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.޸ijɹtrue +} + +//˳ +template +void ClearList(SqList
& L) +{ + L.length = 0; +} + + +// +template +int ListLength(SqList
L) +{ + return L.length; +} + +// +template +bool ListEmpty(SqList
L) +{ + if (L.length == 0) + return true; + else + return false; +} + +// +template +bool ListFull(SqList
L) +{ + if (L.length == L.size) + return true; + else + return false; +} + +//㷨2.10 +template +void DispList(SqList
L) // +{ + int i; + for (i = 0; i < L.length; i++) + { + cout << L.elem[i] << "\t"; + + } + cout << endl; +} + diff --git a/Chapter2/2-3-SqListApp/SqListApp.cpp b/Chapter2/2-3-SqListApp/SqListApp.cpp new file mode 100644 index 0000000..febfb3f --- /dev/null +++ b/Chapter2/2-3-SqListApp/SqListApp.cpp @@ -0,0 +1,188 @@ +// 2-3-SqListApp-˳Ӧ + +#include//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 +void Union(SqList
& La, SqList
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 +void ReverseSqList(SqList
& 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 la, SqList lb, SqList& 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 La, Lb; // AB + SqList Lc; // ˳ + SqList fa, fb, fc; // ʽABC + + system("cls"); // + + int choice; + do + { + dispmenu(); // ʾ˵ + cout << "Enter choice(1~4,0˳):"; + cin >> choice; + switch (choice) + { + case 1: // 󼯺ϲ + cout << "AB\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 diff --git a/Chapter2/2-4-PolyAdd/2-4-PolyAdd.vcxproj b/Chapter2/2-4-PolyAdd/2-4-PolyAdd.vcxproj new file mode 100644 index 0000000..3e5f471 --- /dev/null +++ b/Chapter2/2-4-PolyAdd/2-4-PolyAdd.vcxproj @@ -0,0 +1,135 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + + + + 17.0 + Win32Proj + {3bbbc9d3-2aea-48cc-abf7-908da7982a0b} + My24PolyAdd + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + false + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + \ No newline at end of file diff --git a/Chapter2/2-4-PolyAdd/2-4-PolyAdd.vcxproj.filters b/Chapter2/2-4-PolyAdd/2-4-PolyAdd.vcxproj.filters new file mode 100644 index 0000000..f1ce64b --- /dev/null +++ b/Chapter2/2-4-PolyAdd/2-4-PolyAdd.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 源文件 + + + \ No newline at end of file diff --git a/Chapter2/2-4-PolyAdd/PolyAdd.cpp b/Chapter2/2-4-PolyAdd/PolyAdd.cpp new file mode 100644 index 0000000..de41251 --- /dev/null +++ b/Chapter2/2-4-PolyAdd/PolyAdd.cpp @@ -0,0 +1,241 @@ +// 2-4-PolyAdd-ϡʽ +// WARNING: /sdl is disabled to pass the compilation process. + +#include//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С + { // paqa + 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 LALBͬ + { + sum = qa->coef + qb->coef; // ϵ + if (sum != 0) // 2.3.1 ϵͲΪ0 + { + qa->coef = sum; // 2.3.1.1 qa->coefsum + 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Ϊpạ + 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. LALBδ + pa->next = qb; // 3.1 qbqa֮ + 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; // p1p2ǰ + 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; // p1q + 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 diff --git a/Chapter2/Chapter2.sln b/Chapter2/Chapter2.sln new file mode 100644 index 0000000..2c77d0e --- /dev/null +++ b/Chapter2/Chapter2.sln @@ -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 diff --git a/OrigFiles/1-绪/app1-4.cpp b/OrigFiles/1-绪/app1-4.cpp new file mode 100644 index 0000000..e3fd65c --- /dev/null +++ b/OrigFiles/1-绪/app1-4.cpp @@ -0,0 +1,19 @@ +#include +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< +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<//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 L; //Ϊ20ԪΪ͵Ŀ˳ + system("cls"); //ִϵͳcls + + int choice; + do { + dispmenu(); //ʾ˵ + cout << "Enter choice(1~120 ˳):"; + cin >> choice; + switch (choice) { + case 1: //ʼԱ + cout << "Ҫ˳ijȣ"; + 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 << "޸ijɹ" << 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 + diff --git a/OrigFiles/2-线性表/2-1-SqList-顺序表/SqList.h b/OrigFiles/2-线性表/2-1-SqList-顺序表/SqList.h new file mode 100644 index 0000000..774f16e --- /dev/null +++ b/OrigFiles/2-线性表/2-1-SqList-顺序表/SqList.h @@ -0,0 +1,175 @@ +template +struct SqList // ˳ +{ + DT *elem; // ַ + int length; // + int size; // +}; + +//㷨2.1 +template +bool PriorElem_e(SqList
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 +bool InitList(SqList
&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 +bool CreateList(SqList
&L,int n) +{ + int i; + if(n>L.size) // 1.Ԫظڱܴ + { + cout<<"Ԫظڱܴ"<>L.elem[i-1]; + L.length=n; // 3.ΪԪظ + return true; +} + +//㷨2.4 ˳ +template +void DestroyList(SqList
&L) +{ + delete [] L.elem; // 1.ͷűռ + L.length=0; // 2.Ըֵ + L.size=0; +} + +//㷨2.5 ȡiԪֵ +template +bool GetElem_i(SqList
L,int i, DT &e) +{ + if(i<1 || i>L.length) // 1.λ򲻺false + { + cout<<"Ԫزڣ"< +int LocateElem_e(SqList
L, DT e) +{ + for(int i=0; i +bool InsertElem_i(SqList
&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 +bool DeleElem_i(SqList
&L,int i) +{ + if(L.length==0) // 1.գɾ + return false; + if(i<1 || i>L.length) // 2.ɾλòܲ + return false; + for (int j=i; j +bool PutElem(SqList
&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.޸ijɹtrue +} + + // ˳ +template +void ClearList(SqList
&L) +{ + L.length=0; // ձΪ0 +} + + + // +template +int ListLength(SqList
L) +{ + return L.length; +} + + +template +bool ListEmpty(SqList
L) // +{ + if(L.length==0) // ձtrue + return true; + else + return false; // ǿձfalse +} + +template +bool ListFull(SqList
L) +{ + if(L.length==L.size) // true + return true; + else + return false; // false +} + +//㷨2.10 +template +void DispList(SqList
L) +{ + int i; + for(i=0;i //cout,cin +using namespace std; +#include "LinkList.h" + +//㷨2.25 +void ReverseLinkList(LNode *&L) +{ + LNode *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< * L; + system("cls"); // + + int choice; + do + { + dispmenu(); // ʾ˵ + cout<<"Enter choice(1~120 ˳):"; + cin>>choice; + switch(choice) + { + case 1: // ʼ + InitList(L); + cout<>n; + cout<>n; + cout<>i; + cout<>e; + i=LocateElem_e(L, e); + if(i) + cout<>i; + cout<<"Ԫֵ"<>e; + if(InsertElem_i(L,i,e)) + { + cout<>i; + if(DeleElem_i(L,i)) + { + cout<>i; + cout<<"Ԫֵ"<>e; + if(PutElem_i(L,i,e)) + { + cout<>e; + if(PriorElem_e(L,e,pre_e)) + cout< +struct LNode // +{ + DT data; //򣬴洢Ԫֵ + LNode *next; //ָָһ +}; + +//㷨2.1 +template +bool PriorElem_e(LNode
*L, DT e, DT &pre_e) // ֵΪeԪǰ +{ + int k; + k=LocateElem_e(L,e); // 1.ȡeλk + if(k>1) // 2.λk1 + { + GetElem_i(L,k-1,pre_e); // k-1ԪΪeǰ + return true; + } + else // 3.Ԫeǰ + return false; // false +} + +//㷨2.14 յ +template +bool InitList(LNode
*&L) +{ + L=new LNode
; // 1.ͷ + if(!L) exit(1); // 2.ʧܣ˳ + L->next=NULL; // 3.ɹ + return true; // true +} + +//㷨2.15 β巨nԪ +template +bool CreateList_1(LNode
*&L,int n) +{ + int i; + LNode
*p,*s; + p=L; //1.ָʼָβ + cout<<""<; // 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 +bool CreateList_2(LNode
*(&L),int n) +{ + int i; + LNode
*s; + cout<<""<; // 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 +void DestroyList(LNode
*(&L)) // ͷռռ +{ + LNode
*p; + while(L) // 1. ǿ,ͷ㿪ʼͷŽ + { + p=L; // 1.1 ͷ + L=L->next; // 1.2 ͷָ + delete p; // 1.3 ͷűͷռڴ + } + L=NULL; // 2.ͷָָ +} + +//㷨2.18 ȡiԪ +template +bool GetElem_i(LNode
*L,int i, DT &e) +{ + LNode
*p; // 1.ʼ + p=L->next; // 1.1 ùָ룬׽㿪ʼ + int j=1; // 1.2 ʼ + while(p&&jnext;j++; + } + if(!p ||j>i ) // 3 δҵfalse + return false; + else // 4. ҵ + { + e=p->data; // ȡiԪֵ + return true; // true + } +} + +//㷨2.19 ֵΪeԪλ +template +int LocateElem_e(LNode
*L, DT e) +{ + + LNode
*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 +bool InsertElem_i(LNode
*&L,int i, DT e) +{ + + int j=0; + LNode
*p; // 1.ʼ + p=L; // ָʼ + while(p && jnext; + j++; + } + if(!p||j>i-1) // 3.ж϶λǷɹ + return false; // 3.1 λʧܣܲ + else // 3.2 λɹ + { + LNode
*s; + s=new LNode
; // 3.2.1½ + s->data=e; // 3.2.2½㸳ֵ + s->next=p->next; // 3.2.3Sӵp֮ + p->next=s; + return true; // 3.2.4 ɹtrue + } + +} + +//㷨2.21 ɾiԪ +template +bool DeleElem_i(LNode
*(&L),int i) +{ + + LNode
*p,*q; //1.ʼùָ + p=L; //Ҵͷ㿪ʼ + int j=0; //ʼ + while(p->next && jnext; + 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 +bool PutElem_i(LNode
*(&L),int i, DT e) +{ + LNode
*p; // 1.ʼ:ùָ + p=L->next; // ׽㿪ʼ + int j=1; // ʼ + while(p&&jnext;j++; + } + if(!p||j>i) // 3.Ԫز,false + return false; + else // 4.λɹ + { + p->data=e; // ޸Ԫֵ + return true; // true + } +} + + // ͷռռ +template +void ClearList(LNode
*(&L)) +{ + + LNode
*p; + while(L->next) // Ԫ㿪ʼͷŽ + { + p=L->next; + L->next=p->next; + delete p; + } + cout< +int ListLength(LNode
*L) +{ // 1.ʼ + int len=0; // 1.1 ֵ0 + LNode
*p; // 1.2ùָ + p=L; // ָͷ + while(p->next) // 2.к̽㣬 + { + len++;p=p->next; // 1ָ + } + return len; // 3.ر +} + +// +template +bool ListEmpty(LNode
*L) // +{ + if(L->next==NULL) + return true; //ձ1 + else + return false; //գ0 +} + + +//㷨2.24 +template +void DispList(LNode
*L) // ʾ +{ + LNode
*p; // 1. ùָ + p=L; // Ԫ㿪ʼ + while(p->next) // 2.ֵ + { + p=p->next;cout<data<<"\t"; + + + } + cout< +struct SqList //˳ +{ + DT *elem; //ַ + int length; // + int size;// +}; + +//㷨2.2 +template +bool InitList(SqList
&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 +bool CreateList(SqList
&L,int n) //Ϊn˳ +{ + int i; + if(n>L.size) + { + cout<<"Ԫظڱܴ"<>L.elem[i-1]; + L.length=n; + return true; +} + +//㷨2.4 +template +void DestroyList(SqList
&L) //˳ͷűռ +{ + delete [] L.elem; + L.length=0; + L.size=0; +} + +//㷨2.5 +template +bool GetElem_i(SqList
L,int i, DT &e)// ȡiԪֵ +{ + if(i<0 || i>L.length) + return false; + e=L.elem[i-1]; + return true; +} + +//㷨2.6 +template +int LocateElem_e(SqList
L, DT e) // ֵ +{ + int i; + for(i=0; i +bool InsertElem_i(SqList
&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 +bool DeleElem_i(SqList
&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 +bool PutElem(SqList
&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.޸ijɹtrue +} + +//˳ +template +void ClearList(SqList
&L) +{ + L.length=0; +} + + +// +template +int ListLength(SqList
L) +{ + return L.length; +} + +// +template +bool ListEmpty(SqList
L) +{ + if(L.length==0) + return true; + else + return false; +} + +// +template +bool ListFull(SqList
L) +{ + if(L.length==L.size) + return true; + else + return false; +} + +//㷨2.10 +template +void DispList(SqList
L) // +{ + int i; + for(i=0;i//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 +void Union(SqList
&La, SqList
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 +void ReverseSqList(SqList
&L) // ˳Ԫ +{ + DT t; + int i; + for(i=0; iL.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 la, SqList lb, SqList &lc) +{ + int i=0; // 1.ʼôʼλ + while(ilb.length) // 3.laδ꣬lbѴ + { + while(i La,Lb; // AB + SqList Lc; // ˳ + SqList fa,fb,fc; // ʽABC + + system("cls"); // + + int choice; + do + { + dispmenu(); // ʾ˵ + cout<<"Enter choice(1~4,0˳):"; + cin>>choice; + switch(choice) + { + case 1: // 󼯺ϲ + cout<<"AB\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Ϊ"<>nc; + InitList(Lc,nc); + cout<>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 Ϊ "<=nb)?na:nb; + InitList(fc,nc); // ʽC + PolyAdd(fa,fb,fc); // ʽ C=A+B + cout<<"\nʽA + ʽB = "<//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<<""<>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<coef<<"x^"<exp<<" + "; + p=p->next; + } + cout<coef<<"x^"<exp; + cout<next; + pb=LB; + qb=pb->next; + while (qa!=NULL && qb!=NULL ) // 2. + { + if (qa->exp < qb->exp ) // 2.1 LAС + { // paqa + 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 LALBͬ + { + sum=qa->coef+qb->coef; // ϵ + if (sum!=0 ) // 2.3.1 ϵͲΪ0 + { + qa->coef=sum; // 2.3.1.1 qa->coefsum + 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Ϊpạ + 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. LALBδ + pa->next=qb; // 3.1 qbqa֮ + 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; // p1p2ǰ + if(p2==NULL || p2->next==NULL) // ձֻ1ĶʽҪ + { + cout<<"Ҫ!"<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; // p1q + p1->next=q; + } + q=r->next; // һ账 + + } + return; +} + + + +void dispmenu() +{//ʾ˵ + cout<>choice; + switch(choice) + { + case 1: // ʽA + InitPoly(LA); + cout<<"ʽ A "; + cin>>m; + CreatePoly(LA, m ); + cout<<"Ķʽ A Ϊ"<>n; + CreatePoly(LB, n ); + cout<<"ĶʽBΪ"<>c; + if(c=='A'||c=='a') + DispPoly(LA); + else if(c=='B'||c=='b') + DispPoly(LB); + else + cout<<"ѡ"<