?? integer test.cpp
字號:
/*
program name: Integer Test
Sometimes we need a way to test the user input to know if it is an integer, i have
seen many algorythm for doing this,but a lot of them are incomplete since they only check
to see if the input contain the numeric values from 0 to 9 if so it is said that the input
is a number.Here you will fine a more precise way to test the user input for knowing if it
is an integer.
If you like this code,please vote for it !
written by Gonzales Cenelia.
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
// This is the function that makes the integer test
bool IsInteger( char *number )
{
int len = strlen( number );
bool isnumber = true;
int i = 0;
if( len == 0 || len > 9 ) // if the user did not enter an input
{ // or if the input is too big to be
return false; // a "long" value.
}
while( i < len && isnumber ) // scanning the the input
{
if( isdigit( number[i] ) == 0 ) // check if there is a none digit character in the input
{
if( number[i] == '+' || number[i] == '-' ) // if so we verify if it is "+" or "-"
{
if( i + 1 > len - 1 || i - 1 >= 0 ) // check the position of "+" or "-"
{ // this signs could only be infront of
isnumber = false; // the number,no where else.
}
}
if( number[i] != '+' && number[i] != '-' ) // if it's not "+" or "-" than
{ // the expression is not a number
isnumber = false;
}
}
i++;
}
return isnumber;
}
void main()
{
char number[20]; // buffer for storing number
printf("Enter a number: "); // geting a number from the user
gets(number);
if( IsInteger( number ) == false ) // testing the input number
{
printf("This is not an integer.\nOr maybe this expression is too big "
"to be tested.\n");
}
else
{
printf("Thanks for entering an integer !\n");
}
// Since the number is stored as a string,if you need to convert
// it to numeric value, you can use something like
// "long numvalue = atol(number)"
// you might start testing the program with the following numbers:
// 14563,35.1223,-6853,+655
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -