C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 24th, 2006, 01:55 PM
InfinityZer0 InfinityZer0 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 1 InfinityZer0 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 m 58 sec
Reputation Power: 0
Need help on Calculator Problem, please!

Below is the exact problem required of me:

Design, implement, and test a program written in C++ that functions as a calculator for polynomials and rational functions.


And a more refined problem statement:

A polynomial in one variable is a linear combination of powers of that variable. A rational function is a fraction where the numerator and denominator are polynomials. At least three C++ classes need to be designed and implemneted for this project: (1) Complex, used to represent complex numbers, and to implement multiplication, division, addition, and subtraction of complex numbers, conversion of real numbers to complex numbers, and extraction of the real and imaginary parts of a complex number. (2) Polynomial, used to represent a finite linear combination of powers ofa number, where the number and the coefficients of the powers may be either real (type double) or complex (class Complex), and to implement. (3) RationalFraction, used to represent the ratio of two polynomials (represented by objects of class Polynomial) and to implement multiplication, division, addition, and subtraction of.RationalFunction objects and conversion of real or complex-valued numbers and polynomials to RationalFunction objects. Both the Polynomial and the RationalFunction classes must also implement an eval() member function, with evaluates the polynomial or rational function at a specified real or complex value.

The Polynomial and RationalFunction classes are to use class templates to allow their definition over either real (type double) or complex (class Complex) values. The Complex and RationalFunction classes are to use the C++ support for exceptions to signal errors such as division by zero. Neither class templates nor exception handling will be covered in the lectures, so you are required to extract the necessary material from the textbook on your own.

All classes are to implement methods to generate human-readable representations of their objects' values on C++ stream outputs, and to set and initialize their objects.

A program is to be designed that functions as a four-function calculator (addition, subtraction, multiplication, and division) and provides input, output, and registers (assignment of a value to a register and use of that value in an expression), where any value can be a real number (double), a complex number, a polynomial, or a rational function. The program is to allow evaluation of both polynomials and rational functions at a single value or for a range of values (similar to Matlab's for i=xstart:xincrement:xend construct, but you may design your own method of specifying this).



This program has been causing a lot of trouble for myself and my partner. We know that we need to make a class for each type of calculation. We have completed the complex class and the majority of the rational class but we are stumped on how to do the Polynomial class. Below is currently what our Complex class looks like, to just give you an idea.

Code:
#ifndef lint
static char *rcsid = "$Header$";
#endif

#include "Complex.h"

Complex::Complex(const Complex & c) : real(c.real), imag(c.imag) { }

Complex & Complex::operator = (const Complex & c) {
  if (this != &c) {
    real = c.real;
    imag = c.imag;
  }
  return *this;
}

Complex & Complex::operator ++ () {
  real ++;
  return *this;
}

Complex & Complex::operator ++ (int a) {
  Complex t(*this);
  real++;
  return t;
}
// End of functions always defined.

// Self-modifying binar arithemetic operators : only + is done

Complex & Complex::operator += (const Complex & c) {
  real += c.real;
  imag += c.imag;
  return *this;
}

Complex & Complex::operator -= (const Complex & c) {
  real -= c.real;
  imag -= c.imag;
  return *this;
}

Complex & Complex::operator *= (const Complex & c) {
  *this = *this * c;
  return *this;
}

Complex & Complex::operator /= (const Complex & c) {
  double denominator = (c * c.conjg()).Real();
  *this = *this * c.conjg();
  real /= denominator
  imag /= denominator
  return *this;
  }

// Friendly operators:

// Stream input and output operators -- define or delete region
// Include iostream.h if defined


ostream & operator << (ostream & os,const Complex & c) {
  os << '(' << c.real << ',' << c.imag << ')';
  return os;
}

istream & operator >> (istream & is,Complex & c) {
  ...
  return is;
}

//  Binary arithmetic operators : only + is done - use editor for others

Complex operator + (const Complex & c1,const Complex & c2) {
  Complex temp(c1);
  temp += c2;
  return temp;
}

Complex operator * (const Complex & c1,const Complex & c2) {
  Complex temp;
  temp.real = c1.real*c2.real - c1.imag*c2.imag;
  return temp;
}

//  Arithmetic comparisons, returning ints : only == is done

bool operator == (const Complex & c1,const Complex & c2) {
  bool result = false;
  ...code to set result to 1 of c1 == c2...
  return result;
}


And here is the header file to go along with it:
Code:
#ifndef _Complex_h
#define _Complex_h

// Uncomment the includes for the headers you need (or add more)
#include <iostream>
//#include <iomanip>
//#include <fstream>
//#include <cassert>
//#include <cctype>
//#include <cfloat>
//#include <climits>
#include <cmath>
//#include <cstdio>
//#include <cstdlib>
//#include <cstring>
//#include <ctime>
//#include <utility>
using namespace std;

class Complex {
protected:
  double real;
  double imag;
public:
  Complex() : real(0.0), imag(0.0) { }
  Complex(const Complex &); // the initialization constructor
  Complex(const double r, const double i = 0.0) : real(r), imag(i) { }

  ~Complex() { }

  Complex & operator = (const Complex &);
  Complex & operator ++ (); //prefix ++a is called as a a.operator++()
  Complex & operator -- (); //prefix --a is called as a a.operator--()
  Complex operator ++ (int); //postfix a++ called as a.operator++()
  Complex operator -- (int); //postfix a-- called as a.operator--()
  
// Accessor functions

  double getReal() const { return real; }
  double getImag() const { return imag; }
  double setReal(const double x) { real = x; return real; }
  double setImag(const double x) { imag = x; return imag; }
  Complex conjg() const { return Complex(real,-imag); }
  double angle() const { return atan2(real,imag); } //in radians!
  double magnitude() const { return sqrt(real*real+imag*imag); }
  Complex toRectangular (const double r, const double theta) {
    return Complex(r*cos(theta),r*sin(theta));
  }

//  Self-modifying binary arithmetic operators

  Complex & operator += (const Complex &);
  Complex & operator -= (const Complex &);
  Complex & operator *= (const Complex &);
  Complex & operator /= (const Complex &);

//  Friendly operators:

//  Stream input and output operators

  friend ostream & operator << (ostream &,const Complex &);
//  friend istream & operator >> (istream &, Complex &);

//  Binary arithmetic operators

  friend Complex operator + (const Complex &,const Complex &);
  friend Complex operator - (const Complex &,const Complex &);
  friend Complex operator * (const Complex &,const Complex &);
  friend Complex operator / (const Complex &,const Complex &);

//  Arithmetic comparisons, returning ints

  friend bool operator == (const Complex &,const Complex &); //.eq.
  friend bool operator != (const Complex &,const Complex &); //.ne.

};

#endif


Once again, any help on how to do the rational class would be very much appreciated. Thanks!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Need help on Calculator Problem, please!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT