
25-Apr-2007, 23:40
|
|
New Member
|
|
Join Date: Apr 2007
Posts: 1
|
|
|
Need help on C++ problem
need help on on this problem been trying for a week and i am completely lost
could you please point me in a direction that i should take
Here is the Problem:
Quote:
The Java SDK has classes that you can use to convert numbers, for instance the Double class allows you to convert a string into a double.
Double db("123.45");
double val = Double.doubleValue();
In C# all primitives are actually objects. It kind of makes things nice. C# also allows you to convert on the fly!
double d;
d = double.parse("123.45");
Wouldn't it be nice if we had the same kind of functionality for C++?
Well, now you get the chance to do just that. We are going to create somewhat of a hybrid, or a combination of the two.
Here is what is required:
Place your name in comments at the top of every page - 5pts.
Create a double class - 10 pts.
Constructor 1 - takes a string 5pts
Constructor 2 - takes a double 5pts.
equals method - takes a string, 5pts.
equals method - takes a double, 5pts.
toString method - Converts Number to a string, 5pts.
toDouble method - returns a double representation, 5pts.
isNAN function - returns true if number cannot be converted, 10pts.
overload the = operator to assign a string, 15pts.
overload the = operator to take a double, 15pts.
overload the += operator to take a double, 15pts.
Notes
You are going to have to use a couple of built in functions that we have not discussed yet. We will go over them before the exam. The first is
double atof(char *str)
Which stands for ascii to float. It will convert a string representation of a double value to its numeric value, here is an example:
char str[50];
strcpy(str,"123.456");
double d;
d = atof(str);
The second function is
sprintf(char *str, char *format, values);
This function allows you format a string with numeric values
for instance :
char str[50];
double d = 123.45;
sprintf(str,"%f",d)
will put the value of d (123.45 in this case) into the string called str. there are a few format specifiers that you might be interested in %d Formats and integer value
%f Formats a floating point value
%s Formats a string values
You can combine as many as you would like
char str[50];
double d = 123.45;
int x = 3;
char *s = "a string";
sprintf(str,"%f %s %d",d,s,x);
The arguments will placed in the string in the order in which they are listed. One last thing is that you can specify the number of places you want to the right of the decimal by doing this
char str[50];
double d = 123.45;
sprintf(str,"%.2f",d);
The above specifies that there should only be 2 places to the right of the decimal point
Constructor 1 and 2 represent overloaded constructors. One takes a string, the other takes a double
The equals methods should work like the overloaded equal operators.
Double d;
d.equals(123.45);
Double d1;
d1.equals("234.56");
The isNAN function should return a boolean false if the number cannot be converted. That is if someone puts in an invalid string.
double d;
d.equals("hello");
In the above case isNAN would return true. because the string "hello" is not an number
Bonus Question 5pts
Add a method called fraction that will return just the fractional portion of the double
double d = 7.234;
d.fraction();
The above would return
.234;
|
This is the code I have:
#include <iostream>
using namespace std;
class Double
{
private:
char str;
double num;
public:
Double(char *p);
Double(double *p);
void equals(char *p);
void equals(double *p);
void toString(char *p);
double toDouble(double *p);
void isNAN(*p);
Double & operator =(Double &f);
Double & operator =(Double &f);
Double operator +(Double &f);
};
int main()
{
double x;
cout<<"Enter a string of numbers"<<endl;
cin>>p;
}
Double::Double(char *p)
{
char str[20];
}
Double::Double(double *p)
{
double num[20];
}
equals(char *p)
{
Double d;
d.equals();
}
equals(double *p)
{
Double d;
d.equals();
}
void toString(char *p)
{
char str[20];
Double d =x;
sprintf(str,"%f",x)
}
double toDouble(double *p)
{
char str[20];
strcpy(str,x);
Double d;
d = atof(str);
}
Double Double::operator=(Double &f)
{
}
Double Double::operator +(Double &f)
{
}
Double Double::operator=(Double &f)
{
}
void isNAN(*p)
{
Double d;
d.equals();
char *p=str;
while (*p)
{
if (! isdigit(*p))
cout<<"not a digit"<<endl;
return false;
}
return true;
}
any help would be greatly appreciated
Last edited by admin : 26-Apr-2007 at 03:03.
Reason: Please insert your C code between [cpp] & [/cpp] tags
|
|