forked from TheAlgorithms/C-Plus-Plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added fast_integer_input.cpp (TheAlgorithms#696)
* added fast_integer_input.cpp * fixed white spaces * fixed white spaces * fixed std:: * fixed std:: * \n Co-authored-by: Christian Clauss <cclauss@me.com>
- Loading branch information
Showing
1 changed file
with
36 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Read integers in the fastest way in c plus plus | ||
#include<iostream> | ||
void fastinput(int *number) { | ||
// variable to indicate sign of input integer | ||
bool negative = false; | ||
register int c; | ||
*number = 0; | ||
|
||
// extract current character from buffer | ||
c = std::getchar(); | ||
if (c == '-') { | ||
// number is negative | ||
negative = true; | ||
|
||
// extract the next character from the buffer | ||
c = std::getchar(); | ||
} | ||
|
||
// Keep on extracting characters if they are integers | ||
// i.e ASCII Value lies from '0'(48) to '9' (57) | ||
for (; (c > 47 && c < 58); c = std::getchar()) | ||
*number = *number *10 + c - 48; | ||
|
||
// if scanned input has a negative sign, negate the | ||
// value of the input number | ||
if (negative) | ||
*(number) *= -1; | ||
} | ||
|
||
// Function Call | ||
int main() { | ||
int number; | ||
fastinput(&number); | ||
std::cout << number << "\n"; | ||
return 0; | ||
} |