Quick question

正在查看此主题的用户

Bellum

Master Knight
Sorry if this seems blatantly obvious, but I'm mathematically retarded.

Is there a way to algorithmically determine if a number has a decimal, or whatever the technical term is?
 
I have no idea what you mean.

Besides, just, you know, looking at the number and see if it has anything to the right of the .
 
A computer can't do that without some wizardry well beyond my capabilities, unfortunately.

EDIT:

Ok, there is probably a better way to do this....

插入代码块:
    float OriginalNumber, NumberTest;
    int Manipulator;

    cin >> OriginalNumber;
    Manipulator = OriginalNumber;
    NumberTest = Manipulator;
    if ((OriginalNumber - NumberTest) > 0)
    {
        cout << "\nNumber has a decimal\n";
    }
    else
    {
        cout << "\nNumber doesn't have a decimal\n";
    }
    system("PAUSE");
    return 0;
 
You could also do this, trimming it down by a line, a variable, and an operation:

插入代码块:
    float OriginalNumber;
    int Manipulator;

    cin >> OriginalNumber;
    Manipulator = OriginalNumber;
    if (OriginalNumber != Manipulator)
    {
        cout << "\nNumber has a decimal\n";
    }
    else
    {
        cout << "\nNumber doesn't have a decimal\n";
    }
    system("PAUSE");
    return 0;
That would probably save a byte or two of size and memory usage :razz:
 
No problem. There's probably an even simpler way of doing it, but I cannot be bothered to try and find out how :razz:



Lord Sami: Learn! There's nothing to lose.
Well, except time.
And your sanity, when you get to the point of debugging.
 
插入代码块:
String input = System.in; // Something like that--I've never used System.in
if (input.contains('.'))
    System.out.println("Number has a decimal.");
else
    System.out.println("Number does NOT have a decimal.");

Java FTW!
 
Dunno anything about Java, but the number has to be manipulated mathematically, so you'd have to convert from string to float and back or whatever. The fact that it has a decimal point in it isn't so important as that there is actually a value to the right of it that isn't 0.
 
插入代码块:
double input;
cin >> input;

if (((int)(input * 100000000) % (int)input) != 0)
    cout << "Number is not integer.";
else
    cout << "Number is integer.";

Heh, not even sure if it would work, but it saves some lines, even if it is slower.

Ooh!  Or:
插入代码块:
double input;
cin >> input;

cout << "Number is ";

if (((int)(input * 100000000) % (int)input) != 0)
    cout << "not ";

cout << " integer.";

Saves on executable size, but is even slower.
 
Well, I've finally put it to good use, if anyone was wondering what I was trying to do...

插入代码块:
// File:            SquRed.cpp
// Author:          Aaron Paden
// Date:            08/23/08

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int SquaredNumb, Manipulator;
    double UsrInp, Quotient, Check;
    cout << "Input Square Root: ";
    cin >> UsrInp;
    Check = pow(UsrInp,.5);
    Manipulator = static_cast<int>(Check);
    if (Check != Manipulator)
    {
        for (int Start = static_cast<int>(Check); Start > 0; Start--)
        {
            SquaredNumb = Start * Start;
            Quotient = UsrInp / double(SquaredNumb);
            Manipulator = static_cast<int>(Quotient);
            if ((Quotient == Manipulator))
            {
                cout << "\nAnswer - " << pow(SquaredNumb,.5) << "|`" << Quotient;
                cout << "\nPress Return/Enter to exit...";
                cin.clear();
                cin.sync();
                cin.get();
                return 0;
            }
        }
    }
    else
    {
        cout << "\nAnswer - " << Check << "\n";
        cout << "Press Return/Enter to exit...";
        cin.clear();
        cin.sync();
        cin.get();
        return 0;
    }
}


Now if I could just factor worth a damn....
 
后退
顶部 底部