#include <iostream>
#include <cctype> // isdigit
using namespace std;
// Global buffer
const int LINE_LENGTH = 128;
char line[LINE_LENGTH];
int lineIndex;
void getLine () {
// Get a line of characters.
// Install a newline character as sentinel.
cin.getline (line, LINE_LENGTH);
line[cin.gcount () - 1] = '\n';
lineIndex = 0;
}
enum State {eI, eF, eM, eSTOP};
void parseNum (bool& v, int& n) {
int sign;
State state;
char nextChar;
v = true;
sta开发者_开发技巧te = eI;
do {
nextChar = line[lineIndex++];
switch (state) {
case eI:
if (nextChar == '+') {
sign = +1;
state = eF;
}
else if (nextChar == '-') {
sign = -1;
state = eF;
}
else if (isdigit (nextChar)) {
sign = +1;
n = nextChar - '0'; // line 41
state = eM;
}
else {
v = false;
}
break;
case eF:
if (isdigit (nextChar)) {
n = nextChar - '0';
state = eM;
}
else {
v = false;
}
break;
case eM:
if (isdigit (nextChar)) {
n = 10 * n + nextChar - '0';
}
else if (nextChar == '\n') {
n = sign * n;
state = eSTOP;
}
else {
v = false;
}
break;
}
}
while ((state != eSTOP) && v);
}
int main () {
bool valid;
int num;
cout << "Enter number: ";
getLine();
parseNum (valid, num);
if (valid) {
cout << "Number = " << num << endl;
}
else {
cout << "Invalid entry." << endl;
}
return 0;
}
What does the '0' in line 41 mean? Does this line assign n the next character minus the first character of nextChar?
nextChar - '0'
returns the integer value of a character. For example, if the character value is '1' (which is ASCII 0x31) then if you subtract '0' (which is 0x30) you get the value 1. Since the ASCII values '0' - '9' are continuous, this technique will work.
It converts the number (in ASCII) referenced in nextChar to its actual value. So, if the ASCII value is '5' subtracting '0' from '5' gives you the numeric value 5.
This conversion allows you to perform mathematical operations on the values you entered via the keyboard.
In the future, please strip your example down to the least amount of code to ask your question sufficiently.
'0' is different from 0 in that '0' is ASCII and 0 is, well, 0. Take a look at the ASCII standard. The character '0' is the value 48. The line
n = nextChar - '0';
could just as easily been written as
n = nextChar - 48
The line is doing some sort of converting a ASCII representation of a digit to an actual integer representation.
It converts from the ASCII code corresponding to the digit, to the number itself.
So it will take the character '1'
, and give you the number 1
. And so on for other digits.
'0' return the ascii code for the 0 character (namely 48). it allows an easy traslation between numeric value (for example 7) and it's ascii eqvivalent (which is 55, as you can see here): 7 + '0' = 7 + 48 = 55
Assuming c is a char
between '0' and '9'
By definition, chars are just small integers, so char variables and constants are identical to ints in arithmetic expressions. This is natural and convenient; for example c-'0' is an integer expression with a value between 0 and 9 corresponding to the character '0' to '9' stored in c. Kernighan and Ritchie
精彩评论