mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-02-09 21:47:07 +08:00
document fast_integer
This commit is contained in:
@@ -1,7 +1,15 @@
|
||||
// Read integers in the fastest way in c plus plus
|
||||
#include<iostream>
|
||||
/**
|
||||
* @file
|
||||
* @brief Read integers from stdin continuously as they are entered without
|
||||
* waiting for the `\n` character
|
||||
*/
|
||||
#include <iostream>
|
||||
|
||||
/** Function to read the number from stdin. The function reads input until a non
|
||||
* numeric character is entered.
|
||||
*/
|
||||
void fastinput(int *number) {
|
||||
// variable to indicate sign of input integer
|
||||
// variable to indicate sign of input integer
|
||||
bool negative = false;
|
||||
register int c;
|
||||
*number = 0;
|
||||
@@ -19,18 +27,17 @@ void fastinput(int *number) {
|
||||
// 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;
|
||||
*number = *number * 10 + c - 48;
|
||||
|
||||
// if scanned input has a negative sign, negate the
|
||||
// value of the input number
|
||||
if (negative)
|
||||
*(number) *= -1;
|
||||
if (negative) *(number) *= -1;
|
||||
}
|
||||
|
||||
// Function Call
|
||||
/** Main function */
|
||||
int main() {
|
||||
int number;
|
||||
fastinput(&number);
|
||||
std::cout << number << "\n";
|
||||
std::cout << number << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user