CSE100 Principles of Programming with C++ Lab 10 :: 5 pts
1 Instructions
You may work in pairs (that is, as a group of two) with a partner on this lab project if you wish or you may work alone. If you work
with a partner, only submit one lab project with both of your names in the source code file to Blackboard for grading; you will each
earn the same number of points. What to hand in, and by when, is discussed in Section 5; read it.
2 Lab Objectives
After completing this assignment the student should be able to,
● Complete all of the objectives of the previous lab projects.
● Write a complete class declaration and class implementation from scratch.
● Understand very well how to write and use constructors, accessor, and mutator functions.
3 Prelab
● Download the Lab 10 Zip Archive from the course website to your Desktop or a temporary folder, e.g., C:\Temp. Extract the zip
archive to the desktop or the temporary folder. After extraction, you will find five source code files on your desktop or the
temporary folder: Lab10.cpp, Complex.hpp, Complex.cpp, ComplexTest.hpp, and ComplexTest.cpp. You will also find a file named
complex-in.txt that contains test cases to be used in testing the Complex class.
● Create a new Code::Blocks project named Lab10 and add these source code files to your project. Follow the instructions in Steps
28 through 32 of the Code::Blocks tutorial1 to add the source code files to your project.
● Move complex-in.txt to the appropriate folder based on where your IDE expects it to be. If you are using Code::Blocks, this will be
the folder where the .cpp and .hpp files are located.
4 Lab Exercise
A complex number is a mathematical number consisting of a real part and an imaginary part. Complex numbers are an extension of
one-dimensional real numbers into the two-dimensional complex plane. They are written using the syntax real + imag i where i is
√−1 . If c is a complex number, the real part can be referred to as R(c) and the imaginary part as I(c). For example, if c = −1.23 −
4.56i then,
R(c) = -1.23 and I(c) = -4.56
Basic arithmetic operations on complex numbers include addition, subtraction, multiplication, and division. Let c1 and c2 be two
complex numbers. The arithmetic operations are defined by the following rules.
Addition
The real part of the sum is the sum of the real parts. The imaginary part of the sum is the sum of the imaginary parts.
c1+c2 = ( R(c1) + R(c2)) + ( I (c1) + I ( c2)) i
Subtraction
The real part of the difference is the difference of the real parts. The imaginary part of the difference is the difference of the imagi –
nary parts.
c1−c2 = ( R(c1)− R(c2)) + ( I (c1)− I ( c2)) i
Note that c1 – c2 = c1 + -c2. Therefore, if we have the ability to form the negation of a complex number, then we can perform
subtraction by negating c2 and adding it to c1. We will use this technique in our subtraction function.
Multiplication
c1⋅c2 =( R(c1)⋅ R(c2)− I (c1)⋅ I (c2)) + ( I ( c1)⋅ R(c2) + R(c1)⋅ I (c2)) i
Division
c1
c2
=
R(c1)R( c2)+I (c1) I (c2)
R(c2)2+I ( c2)2 +
I (c1)R(c2)−R(c1) I (c2)
R(c2)2+ I (c2)2 i
Note that
c1
c2
=c1 × 1
c2
So if we have the ability to form the inverse of c2 then we can perform division by multiplying c1 by the inverse of c2. We will use this
technique in implementing the division function.
1 http://devlang.com/cse100_codeblocks
(c) Kevin R. Burger :: Computer Science & Engineering :: Arizona State University Page 1
CSE100 Principles of Programming with C++ Lab 10 :: 5 pts
Your lab project is to implement a class named Complex which represents complex numbers and provides functions for performing
addition, subtraction, multiplication, and division. You will also complete the code for another class named ComplexTest which tests
the Complex class. For testing, your program will open a text file named complex-in.txt for reading; this file will have this format,
operation R(c1) I(c1) R(c2) I(c2)
operation R(c1) I(c1) R(c2) I(c2)
operation R(c1) I(c1) R(c2) I(c2)
operation R(c1) I(c1) R(c2) I(c2)
operation will be a single character representing the arithmetic operation to be performed, i.e., operation will be one of ‘+’, ”,
‘*’, or “https://www.sweetstudy.com/”. Following operation, are the real and imaginary parts of two complex numbers a and b. For each pair, a and b, the
program shall perform the specified operation on a and b and output the result to the output file which is to be named complexout.
txt. There will be four test cases in the input file. A sample input file is,
sample complex-in.txt
+ 1 2 3 8
Calculates (1 + 2i) + (3 8i)
3.4
5.6 7.8 9.1
Calculates (3.4 + 5.6i) (
7.8 9.1i)
* 1.11 2.222
3.3333 4.44444
Calculates (1.11 2.22i)
* (3.3333 4.44444i)
/ 3.4567 0.0023
45.6789 4.37838
Calculates (3.4567 0.0023i)
/ (45.6789 4.37838i)
The output is to be sent to a text file named complex-out.txt. The output file for the above input file will be formatted as shown
below. Note that the real and imaginary parts are both printed in fixed notation with four digits after the decimal point―configure
the stringstream object in Complex::toString() with fixed and setprecision(4). Also, notice that if the imaginary part is negative, then a
‒ sign is printed between the real and imaginary parts rather than a + sign. This is handled with an if-else statement in Complex::
toString().
complex-out.txt for the complex-in.txt file above
(1.0000 + 2.0000i) + (3.0000 8.0000i)
= (4.0000 6.0000i)
(3.4000 + 5.6000i) (
7.8000 9.1000i)
= (4.4000
+ 14.7000i)
(1.1100 2.2220i)
* (3.3333 4.4444i)
= (6.1756
12.3399i)
(3.4567 0.0023i)
/ (45.6789 4.3784i)
= (0.0750 + 0.0071i)
Your project will consist of five source code files, named and described below in Sections 4.1−4.5.
4 .1 Complex.hpp
Write the class declaration for a class named Complex using the class diagram below as a guide.
+===============================================================+
| Complex |
++
| mReal
: double |
| mImag
: double |
++
| + Complex () : <<ctor>> |
| + Complex (pInitReal : double, pInitImag : double) : <<ctor>> |
| + add (Complex& pRHSOp) : Complex |
| + div (Complex& pRHSOp) : Complex |
| + getImag () : double |
| + getReal () : double |
| + invert () : Complex |
| + mult (Complex& pRHSOp) : Complex |
| + negate () : Complex |
| + setImag (pNewImag : double) : void |
| + setReal (pNewReal : double) : void |
| + sub (Complex& pRHSOp) : Complex |
| + toString () : string |
| init(
pInitReal : double, pInitImag : double) : void |
+===============================================================+
4.2 Complex.cpp
Contains the implementation (function definitions) for the Complex class. Here is a brief description of each function. See the
Complex.cpp source code file for the pseudocode descriptions of each function.
Complex::Complex ()
The default constructor. Initializes this Complex number to be 0 + 0 i by calling init(0, 0).
Complex::Complex (pInitReal : double, pInitImag : double)
A second constructor. Initializes this Complex number to be pInitReal + pInitImag i by calling init(pInitReal, pInitImag).
(c) Kevin R. Burger :: Computer Science & Engineering :: Arizona State University Page 2
CSE100 Principles of Programming with C++ Lab 10 :: 5 pts
Complex::add (Complex& pRHSOp) : Complex
Adds this Complex number and pRHSOp to form a new Complex number named sum and returns sum. Note: pRHSOp is passed byreference
because it is generally faster to pass objects by reference.
Complex::div (Complex& pRHSOp) : Complex
Divides this Complex number by pRHSOp to form and return a new Complex number which is the quotient. Note: to divide c1 by c2,
we can multiply c1 by 1/c2. Therefore, to divide this Complex number by pRHSOp, we call mult() passing the Complex number
returned by pRHSOp.invert() as the parameter.
Complex::getImag () : double
An accessor function for the mImag data member.
Complex::getReal () : double
An accessor function for the mReal data member.
Complex::init (pInitReal : double, pInitImag : double) : void
Initializes this Complex number so the mReal data member is initialized with pInitReal and the mImag data member with pInitImag.
Calls setReal() and setImag() mutator functions to initialize the data members.
Complex::invert () : Complex
Returns a new Complex number which is the inverse of this Complex number, i.e., 1 / this.
Complex::mult (Complex& pRHSOp) : Complex
Multiplies this Complex number by pRHSOp to form a new Complex number named product and returns product.
Complex::negate () : Complex
Returns a new Complex number which is the negation of this Complex number, i.e., -this.
Complex::setImag (pNewImag : double) : void
A mutator function for the mImag data member.
Complex::setReal (pNewReal : double) : void
A mutator function for the mReal data member.
Complex::sub (Complex& pRHSOp) : Complex
Subtracts pRHSOp from this Complex number to form and return a new Complex number which is the difference. Note: to subtract c2
from c1, we can add c1 and -c2. Therefore, to subtract pRHSOp from this Complex number, we call add() passing the Complex number
returned by pRHSOp.negate() as the parameter.
Complex::toString () : string
Returns a string representation of this Complex number. The format is “(mReal□±□mImagi)” where □ represents a space character
and ± means there will either be plus sign here or a minus sign. For example, if the mReal part of a number is 2.3123456 and the
mImag part is 4.543217, then the returned string will be “(2.3123□+□4.5432i)”. On the other hand, if the mImag part is negative,
e.g., -4.543217, then the return string will be “(2.3123□−□4.5432i)”. If the mImag part is 0 it will be omitted. For example, if the
mReal part of the number is 12.1234567 and the mImag part if 0, then the return string will be “(2.3123)”. Format the real and
imaginary parts in fixed notation with four digits after the decimal point.
4.3 ComplexTest.hpp
Here is the class declaration for a class named ComplexTest which will be used to test the Complex class. The code for this file is
already completed for you.
+=====================================================+
| ComplexTest |
++
++
| + ComplexTest () |
| + run () : void |
| runTest
(pFin: ifstream&, pFout : ofstream&) |
+=====================================================+
4.4 ComplexTest.cpp
Contains the implementation (function definitions) for the ComplexTest class.
ComplexTest::ComplexTest ()
Default constructor. Does nothing. It is written for you.
(c) Kevin R. Burger :: Computer Science & Engineering :: Arizona State University Page 3
CSE100 Principles of Programming with C++ Lab 10 :: 5 pts
ComplexTest::run ()
Tests the Complex class by opening the input file “complex-in.txt” for reading and calling runTest() four times to read and perform the
four test cases.
ComplexTest::runTest (ifstream& pFin, ofstream& pFout)
Reads the operation character and the four doubles from the input file. Creates two Complex objects a and b initialized with the
values read from the input file. Performs the required operation and writes the results of performing the test case to the output file.
Note: stream objects must always be passed by-reference because the << and >> operators change the values of some of the data
members of the stream object.
4.5 Lab10.cpp
Contains the function definition for main(). The code is completed for you.
4.6 Additional Programming Requirements
1. Modify the comment header block of each source code file with your name, email address, lab date/time, and your lab TA.
2. Carefully format your code and follow the indentation of the text as shown in the example programs of the textbook.
3. Testing: When you run your program, it will read and perform the test cases from complex-in.txt and it will write the results to
complex-out.txt. Update the header comment block at the top of ComplexTest.cpp with your testing results, i.e., the observed
output and document whether each test case PASSED or FAILED.
5 What to Submit for Grading and by When
When you are finished with the program, create a new empty folder named Lab10. Copy all five of the source code files to this
folder; there should only be five files in this folder: Lab10.cpp, Complex.hpp, Complex.cpp, ComplexTest.hpp, and ComplexTest.cpp.
Then compress this folder creating a zip archive named cse100-s15-lab10-auriteid.zip where asuriteid is your ASU user name that you
use to log in to Blackboard, e.g., mine is kburger2. If you worked with a partner then name the zip archive cse100-s15-lab10-
auriteid1-asurite2.zip where asurite1 and asurite2 are the user names of both partners. Then upload the .zip archive file to
Blackboard using the lab submission link by the deadline. If your program does not compile or run correctly, upload what you have
completed for grading anyway (you will generally receive some partial credit for effort). The deadline for the complete lab project is
4:00am Sat 11 Apr. Consult the online syllabus for the late and academic integrity policies.
6 Grading Rubric
1. Attendance (0 or 1 pt)
Assign 1 pt for attending the lab session.
2. Lab Exercise Program (0 to 4 pts)
Testing the Program: Compile and run the student’s program on this file, named complex-in.txt,
+ 123.456789 987.654321 1122.334455 55.443322
123.456789
987.654321 1122.334455 55.443322
* 123.456789 987.654321 1122.334455 55.443322
/ 123.456789 987.654321 1122.334455 55.443322
The program should create a new file named complex-out.txt which contains,
(123.4568 + 987.6543i) + (1122.3345 + 55.4433i) = (1245.7912 + 1043.0976i)
(123.4568 + 987.6543i) (
1122.3345 + 55.4433i) = (998.8777
+ 932.2110i)
(123.4568 + 987.6543i) * (1122.3345 + 55.4433i) = (83800.9715 + 1115323.3286i)
(123.4568 + 987.6543i) / (1122.3345 + 55.4433i) = (0.1531 + 0.8724i)
Assign pts per the following,
a. If the submitted program does, or does not, compile and the student completed less than 50% of the required code correctly, assign 1 pt.
b. If the submitted program does not compile and the student completed more than 50% of the required code correctly, assign 2 pts.
c. If the submitted program compiles and the student completed more than 50% of the required code correctly, assign 3 pts.
d. If the submitted program compiles and is implemented perfectly, or close to perfect with only one or two minor mistakes, assign 4 pts.
3. Deadline was 4:00am Sat 11 Apr.
1. Assign 20% bonus calculated on the earned pts for a submission prior to 4:00am Thu 9 Apr.
2. Assign 10% bonus calculated on the earned pts for a submission between 4:00am Thu 9 Apr and 4:00am Fri 10 Apr.
3. Deduct 0.5 pt for a submission between 4:00am Sat 11 Apr and 4:00am Sun 12 Apr.
4. Deduct 1 pt for a submission after 4:00am Sun 12 Apr.