-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStringToInt.h
More file actions
130 lines (106 loc) · 4 KB
/
StringToInt.h
File metadata and controls
130 lines (106 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Alfred Ledgin
// 10/31/2015
// CS 303
// Project 2
#pragma once
#include <iostream>
#include <string>
using namespace std;
class StringToInt
// The purpose of this class is to convert a string of characters representing
// a positive integer to the integer value represented. This will only
// convert strings consisting of the characters '0' through '9'.
{
public:
StringToInt()
{
numString = "";
numInt = 0;
}
// Preconditions: A converter needs to be declared.
// Postconditions: This is the default constructor. It allows
// declaration of an empty converter, with an empty string,
// and the integer value set to 0 by default.
StringToInt(const string& input)
{
numString = input;
try
{
numInt = convert();
}
catch(exception)
{
throw std::exception("String must only contain digits.");
}
}
// Preconditions: A converter needs to be created with a given string.
// Postconditions: This constructor creates a converter with the given
// string and calls convert() to convert it to the integer value.
void input(const string& input)
{
numString = input;
try
{
numInt = convert();
}
catch (exception)
{
throw std::exception("String must only contain digits.");
}
}
// Preconditions: A converter's string needs to be changed.
// Postconditions: This changes a converter's string to the given
// string and calls convert() to convert it to the integer value.
const int obtainInt() const {return numInt;}
// Preconditions: A converter's integer value is needed.
// Postconditions: This function returns the integer value, which has
// already been converted from the input string, or was set to 0
// if no input string was given.
const int obtainInt(const string& input)
{
numString = input;
try
{
numInt = convert();
}
catch (exception)
{
throw std::exception("String must only contain digits.");
}
return numInt;
}
// Preconditions: Single-statement conversion from a string to an
// integer is needed.
// Postconditions: This function takes an input string, converts it,
// and returns the resulting integer.
private:
const int convert() const
{
int result = 0;
for (int counter = numString.length() - 1; counter >= 0; counter--)
// Iterate from the last char through the first.
{
if (isdigit(numString[counter]))
result += ((numString[counter] - 48)
// First convert ASCII char to corresponding int value.
* pow(10, (numString.length() - 1) - counter));
// Then make that value the appropriate digit by
// multiplying it by the appropriate power of 10.
else
throw std::exception("String must only contain digits.");
}
return result;
}
// Preconditions: The converted result of a converter's string is
// needed.
// Postconditions: This function converts the stored string to the
// integer value represented by the string.
// Reference (Inspiration for Algorithm):
// Doutt, Gerald. "Lectures on Subroutines,
// ASCII-Binary Conversion."
// University of Missouri-Kansas City.
// Royall Hall, Kansas City, MO.
// Spring 2015. CS 282 Course Lectures.
string numString;
int numInt;
};