⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 number test.cpp

📁 这个源程序是很有用地
💻 CPP
字号:
//**************************************
//INCLUDE files for :Number Test
//**************************************
#include <iostream.h>
#include <string.h>
#include <ctype.h>
//**************************************
// Name: Number Test
// Description:This program executes a very precise test to determining if the user input is a number.
// This test valid for any real numbers.Example: +56, -896.223, 123,.2335, 3.141592653 are all accept as numbers.
// But 142-555, 23+56, (55*898) etc are not accept as numbers by this program.
// By: Gonzales Cenelia
//
//
// Inputs: a real number
//
// Returns:The program will print a message on the screen to tell you if your input is a number or if it is not.
//
// Assumes:None
//
//Side Effects:no side effects
//This code is copyrighted and has limited warranties.
//Please see http://www.Planet-Source-Code.com/xq/ASP/txtCodeId.3803/lngWId.3/qx/vb/scripts/ShowCode.htm
//for details.
//**************************************

void main()
{
	char number[20];
    bool isnumber = true;
    cout<<"Enter a number: ";
    cin>>number;
    int len = strlen( number );
    for( int i = 0; i < len; i++ ) 
	{
        if( isdigit( number[i] ) == 0 )
		{
            if( number[i] == '.' )
			{
                if( i + 1 > len - 1 ) 
				{
                    isnumber = false;
                    break;
				}
			}
            if( number[i] == '+' || number[i] == '-' ) 
			{
				if( i + 1 > len - 1 || i - 1 >= 0 )
				{
					isnumber = false;
                    break;
				}
			}
            if( number[i] != '+' && number[i] != '-' && number[i] != '.' ) 
			{
				isnumber = false;
                break;
			}
		}
	}
    if( isnumber == false ) cout<<"This is not a number"<<endl;
    else cout<<"Thanks for entering a number!"<<endl;
}
		

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -