Skip to content

Commit 3dda23f

Browse files
committed
Initial commit for question 67
1 parent 5d126d7 commit 3dda23f

File tree

4 files changed

+304
-0
lines changed

4 files changed

+304
-0
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|Win32">
5+
<Configuration>Debug</Configuration>
6+
<Platform>Win32</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|Win32">
9+
<Configuration>Release</Configuration>
10+
<Platform>Win32</Platform>
11+
</ProjectConfiguration>
12+
<ProjectConfiguration Include="Debug|x64">
13+
<Configuration>Debug</Configuration>
14+
<Platform>x64</Platform>
15+
</ProjectConfiguration>
16+
<ProjectConfiguration Include="Release|x64">
17+
<Configuration>Release</Configuration>
18+
<Platform>x64</Platform>
19+
</ProjectConfiguration>
20+
</ItemGroup>
21+
<PropertyGroup Label="Globals">
22+
<ProjectGuid>{23085B6E-4F6E-40EE-BA2E-547ADB5D91B9}</ProjectGuid>
23+
<Keyword>Win32Proj</Keyword>
24+
<RootNamespace>My67_StringToInt</RootNamespace>
25+
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
26+
</PropertyGroup>
27+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
28+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
29+
<ConfigurationType>Application</ConfigurationType>
30+
<UseDebugLibraries>true</UseDebugLibraries>
31+
<PlatformToolset>v140</PlatformToolset>
32+
<CharacterSet>Unicode</CharacterSet>
33+
</PropertyGroup>
34+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
35+
<ConfigurationType>Application</ConfigurationType>
36+
<UseDebugLibraries>false</UseDebugLibraries>
37+
<PlatformToolset>v140</PlatformToolset>
38+
<WholeProgramOptimization>true</WholeProgramOptimization>
39+
<CharacterSet>Unicode</CharacterSet>
40+
</PropertyGroup>
41+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
42+
<ConfigurationType>Application</ConfigurationType>
43+
<UseDebugLibraries>true</UseDebugLibraries>
44+
<PlatformToolset>v140</PlatformToolset>
45+
<CharacterSet>Unicode</CharacterSet>
46+
</PropertyGroup>
47+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
48+
<ConfigurationType>Application</ConfigurationType>
49+
<UseDebugLibraries>false</UseDebugLibraries>
50+
<PlatformToolset>v140</PlatformToolset>
51+
<WholeProgramOptimization>true</WholeProgramOptimization>
52+
<CharacterSet>Unicode</CharacterSet>
53+
</PropertyGroup>
54+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
55+
<ImportGroup Label="ExtensionSettings">
56+
</ImportGroup>
57+
<ImportGroup Label="Shared">
58+
</ImportGroup>
59+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
60+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
61+
</ImportGroup>
62+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
63+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
64+
</ImportGroup>
65+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
66+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
67+
</ImportGroup>
68+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
69+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
70+
</ImportGroup>
71+
<PropertyGroup Label="UserMacros" />
72+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
73+
<LinkIncremental>true</LinkIncremental>
74+
</PropertyGroup>
75+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
76+
<LinkIncremental>true</LinkIncremental>
77+
</PropertyGroup>
78+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
79+
<LinkIncremental>false</LinkIncremental>
80+
</PropertyGroup>
81+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
82+
<LinkIncremental>false</LinkIncremental>
83+
</PropertyGroup>
84+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
85+
<ClCompile>
86+
<PrecompiledHeader>
87+
</PrecompiledHeader>
88+
<WarningLevel>Level3</WarningLevel>
89+
<Optimization>Disabled</Optimization>
90+
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
91+
</ClCompile>
92+
<Link>
93+
<SubSystem>Console</SubSystem>
94+
<GenerateDebugInformation>true</GenerateDebugInformation>
95+
</Link>
96+
</ItemDefinitionGroup>
97+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
98+
<ClCompile>
99+
<PrecompiledHeader>
100+
</PrecompiledHeader>
101+
<WarningLevel>Level3</WarningLevel>
102+
<Optimization>Disabled</Optimization>
103+
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
104+
</ClCompile>
105+
<Link>
106+
<SubSystem>Console</SubSystem>
107+
<GenerateDebugInformation>true</GenerateDebugInformation>
108+
</Link>
109+
</ItemDefinitionGroup>
110+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
111+
<ClCompile>
112+
<WarningLevel>Level3</WarningLevel>
113+
<PrecompiledHeader>
114+
</PrecompiledHeader>
115+
<Optimization>MaxSpeed</Optimization>
116+
<FunctionLevelLinking>true</FunctionLevelLinking>
117+
<IntrinsicFunctions>true</IntrinsicFunctions>
118+
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
119+
</ClCompile>
120+
<Link>
121+
<SubSystem>Console</SubSystem>
122+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
123+
<OptimizeReferences>true</OptimizeReferences>
124+
<GenerateDebugInformation>true</GenerateDebugInformation>
125+
</Link>
126+
</ItemDefinitionGroup>
127+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
128+
<ClCompile>
129+
<WarningLevel>Level3</WarningLevel>
130+
<PrecompiledHeader>
131+
</PrecompiledHeader>
132+
<Optimization>MaxSpeed</Optimization>
133+
<FunctionLevelLinking>true</FunctionLevelLinking>
134+
<IntrinsicFunctions>true</IntrinsicFunctions>
135+
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
136+
</ClCompile>
137+
<Link>
138+
<SubSystem>Console</SubSystem>
139+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
140+
<OptimizeReferences>true</OptimizeReferences>
141+
<GenerateDebugInformation>true</GenerateDebugInformation>
142+
</Link>
143+
</ItemDefinitionGroup>
144+
<ItemGroup>
145+
<ClCompile Include="StringToInt.cpp" />
146+
</ItemGroup>
147+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
148+
<ImportGroup Label="ExtensionTargets">
149+
</ImportGroup>
150+
</Project>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<Filter Include="Source Files">
5+
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
6+
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
7+
</Filter>
8+
<Filter Include="Header Files">
9+
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
10+
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
11+
</Filter>
12+
<Filter Include="Resource Files">
13+
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
14+
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
15+
</Filter>
16+
</ItemGroup>
17+
<ItemGroup>
18+
<ClCompile Include="StringToInt.cpp">
19+
<Filter>Source Files</Filter>
20+
</ClCompile>
21+
</ItemGroup>
22+
</Project>

67_StringToInt/StringToInt.cpp

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
//==================================================================
2+
// 《剑指Offer——名企面试官精讲典型编程题》代码
3+
// 作者:何海涛
4+
//==================================================================
5+
6+
// 面试题67:把字符串转换成整数
7+
// 题目:请你写一个函数StrToInt,实现把字符串转换成整数这个功能。当然,不
8+
// 能使用atoi或者其他类似的库函数。
9+
10+
#include <cstdio>
11+
12+
long long StrToIntCore(const char* str, bool minus);
13+
14+
enum Status {kValid = 0, kInvalid};
15+
int g_nStatus = kValid;
16+
17+
int StrToInt(const char* str)
18+
{
19+
g_nStatus = kInvalid;
20+
long long num = 0;
21+
22+
if(str != nullptr && *str != '\0')
23+
{
24+
bool minus = false;
25+
if(*str == '+')
26+
str ++;
27+
else if(*str == '-')
28+
{
29+
str ++;
30+
minus = true;
31+
}
32+
33+
if(*str != '\0')
34+
num = StrToIntCore(str, minus);
35+
}
36+
37+
return (int)num;
38+
}
39+
40+
long long StrToIntCore(const char* digit, bool minus)
41+
{
42+
long long num = 0;
43+
44+
while(*digit != '\0')
45+
{
46+
if(*digit >= '0' && *digit <= '9')
47+
{
48+
int flag = minus ? -1 : 1;
49+
num = num * 10 + flag * (*digit - '0');
50+
51+
if((!minus && num > 0x7FFFFFFF)
52+
|| (minus && num < (signed int)0x80000000))
53+
{
54+
num = 0;
55+
break;
56+
}
57+
58+
digit++;
59+
}
60+
else
61+
{
62+
num = 0;
63+
break;
64+
}
65+
}
66+
67+
if(*digit == '\0')
68+
g_nStatus = kValid;
69+
70+
return num;
71+
}
72+
73+
// ====================测试代码====================
74+
void Test(const char* string)
75+
{
76+
int result = StrToInt(string);
77+
if(result == 0 && g_nStatus == kInvalid)
78+
printf("the input %s is invalid.\n", string);
79+
else
80+
printf("number for %s is: %d.\n", string, result);
81+
}
82+
83+
int main(int argc, char* argv[])
84+
{
85+
Test(nullptr);
86+
87+
Test("");
88+
89+
Test("123");
90+
91+
Test("+123");
92+
93+
Test("-123");
94+
95+
Test("1a33");
96+
97+
Test("+0");
98+
99+
Test("-0");
100+
101+
//有效的最大正整数, 0x7FFFFFFF
102+
Test("+2147483647");
103+
104+
Test("-2147483647");
105+
106+
Test("+2147483648");
107+
108+
//有效的最小负整数, 0x80000000
109+
Test("-2147483648");
110+
111+
Test("+2147483649");
112+
113+
Test("-2147483649");
114+
115+
Test("+");
116+
117+
Test("-");
118+
119+
return 0;
120+
}

CodingInterviewChinese2.sln

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "65_AddTwoNumbers", "65_AddT
164164
EndProject
165165
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "66_ConstuctArray", "66_ConstuctArray\66_ConstuctArray.vcxproj", "{56BEBB41-747D-4556-83FB-D609C28E1DE5}"
166166
EndProject
167+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "67_StringToInt", "67_StringToInt\67_StringToInt.vcxproj", "{23085B6E-4F6E-40EE-BA2E-547ADB5D91B9}"
168+
EndProject
167169
Global
168170
GlobalSection(SolutionConfigurationPlatforms) = preSolution
169171
Debug|Any CPU = Debug|Any CPU
@@ -966,6 +968,16 @@ Global
966968
{56BEBB41-747D-4556-83FB-D609C28E1DE5}.Release|x64.Build.0 = Release|x64
967969
{56BEBB41-747D-4556-83FB-D609C28E1DE5}.Release|x86.ActiveCfg = Release|Win32
968970
{56BEBB41-747D-4556-83FB-D609C28E1DE5}.Release|x86.Build.0 = Release|Win32
971+
{23085B6E-4F6E-40EE-BA2E-547ADB5D91B9}.Debug|Any CPU.ActiveCfg = Debug|Win32
972+
{23085B6E-4F6E-40EE-BA2E-547ADB5D91B9}.Debug|x64.ActiveCfg = Debug|x64
973+
{23085B6E-4F6E-40EE-BA2E-547ADB5D91B9}.Debug|x64.Build.0 = Debug|x64
974+
{23085B6E-4F6E-40EE-BA2E-547ADB5D91B9}.Debug|x86.ActiveCfg = Debug|Win32
975+
{23085B6E-4F6E-40EE-BA2E-547ADB5D91B9}.Debug|x86.Build.0 = Debug|Win32
976+
{23085B6E-4F6E-40EE-BA2E-547ADB5D91B9}.Release|Any CPU.ActiveCfg = Release|Win32
977+
{23085B6E-4F6E-40EE-BA2E-547ADB5D91B9}.Release|x64.ActiveCfg = Release|x64
978+
{23085B6E-4F6E-40EE-BA2E-547ADB5D91B9}.Release|x64.Build.0 = Release|x64
979+
{23085B6E-4F6E-40EE-BA2E-547ADB5D91B9}.Release|x86.ActiveCfg = Release|Win32
980+
{23085B6E-4F6E-40EE-BA2E-547ADB5D91B9}.Release|x86.Build.0 = Release|Win32
969981
EndGlobalSection
970982
GlobalSection(SolutionProperties) = preSolution
971983
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)