What I have to do is to cin in a variable, and tell what it is: positive or negative, and integer or floating point using binary operators only.
I did one with string, however, I have no idea how to do it with binary operators.
In a 32bit system, a number should be stored as 1 sign bit + 7 opponent bits + 24 mantissa. So I thought I could just shift the variable to the left by 31 times, I should get a 0 or 1, which tells me the sign of the number. But I get some weird numbers.
And i think it got something to do with the opponent bits to determine if it's integer or not. But the professor said it's got nothing to do with it. So I just need some hint to start with.
Here is a piece of code to show that I have done something, not just asking for the answer.
#include %26lt;cstdlib%26gt;
#include %26lt;iostream%26gt;
#include %26lt;string%26gt;
using namespace std;
void welcome();
void getLine(string %26amp;line);
void process(string %26amp;line);
void processLine(string %26amp;line);
bool again();
int main(int argc, char *argv[])
{
string line;
welcome();
do
{
getLine(line);
processLine(line);
}while(again());
system("PAUSE");
return EXIT_SUCCESS;
}
void welcome()
{
cout%26lt;%26lt;"This program identify the input as an integer or a double then negate the input"%26lt;%26lt;endl%26lt;%26lt;endl;
}
void getLine(string %26amp;line)
{
int redo=false;
do
{
int pos=0, dash=0, dot=0;
line.clear();
cout%26lt;%26lt;"Please enter a positive or a negative number without space: ";
getline(cin, line);
while((pos = line.find_first_not_of("-.0123456789")) != string::npos)
{
line.erase(pos,1);
}
for(int i=0; i%26lt;line.length(); i++)
{
if(line[i] == '-')
{
dash++;
}
if(line[i] == '.')
{
dot++;
}
}
if(dot%26gt;1 ||dash%26gt;1)
{
redo=true;
cout%26lt;%26lt;"You have entered more than 1 dot or 1 negative sign. "%26lt;%26lt;endl;
}
if(dash==1 %26amp;%26amp; line[0] != '-')
{
redo=true;
cout%26lt;%26lt;"The negative sign you entered is not at the front. "%26lt;%26lt;endl;
}
}while(redo==true);
}
void processLine(string %26amp;line)
{
int dash=0, dot=0;
for(int i=0; i%26lt;line.length(); i++)
{
if(line[i] == '-')
{
dash++;
}
if(line[i] == '.')
{
dot++;
}
}
cout%26lt;%26lt;"The number you entered is a ";
if(dash==0)
{
cout%26lt;%26lt;"positive ";
line.insert(0, "-");
}
else
{
cout%26lt;%26lt;"negative ";
line.erase(0, 1);
}
if(dot==0)
{
cout%26lt;%26lt;"integer. ";
}
else
{
cout%26lt;%26lt;"double. ";
}
cout%26lt;%26lt;endl%26lt;%26lt;"The negated number is : ";
for(int i=0; i%26lt;line.length(); i++)
{
cout%26lt;%26lt;line[i];
}
cout%26lt;%26lt;endl;
}
bool again()
{
char ans;
cout%26lt;%26lt;"Do you want to do this again? (Y)es/(N)o) ";
cin%26gt;%26gt;ans;
fflush(stdin);
cout%26lt;%26lt;endl;
return (toupper(ans) == 'Y');
}|||'%26lt;' and '%26gt;' are binary operators, I assume you're not allowed to say:
x %26gt; 0;
though, because that's the obvious solution.
A bad practice that might come in handy here would be to use the C function printf that's in stdio.h and use the %x or %p to print the actual hex representations of a few number to see what you're really working with.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment