GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 25-Apr-2007, 23:40
b5100 b5100 is offline
New Member
 
Join Date: Apr 2007
Posts: 1
b5100 is on a distinguished road

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:

CPP / C++ / C Code:
#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
  #2  
Old 26-Apr-2007, 20:02
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about

Re: Need help on C++ problem


Quote:
Originally Posted by b5100
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:


[snipped] ... [/snipped]

any help would be greatly appreciated
I am not going to read through all that. If you can ask a more specific question on what is giving you trouble that would be great. If you just pick one thing at a time that would works best.
 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Graphic problem in Unreal Tournament 2004 zerox Computer Software Forum - Games 10 09-Oct-2005 12:31
Runtime Problem involving "printf" in C Program supamakia C Programming Language 2 09-Oct-2005 10:09
a significant problem after installing Xp mohammad Computer Software Forum - Windows 10 09-Aug-2005 07:03

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:37.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.