Due Date: See Blackboard

Similar documents
Due Date: See Blackboard

Due Date: See Blackboard

Due Date: See Blackboard

CS 1337 Computer Science II Page 1

Due Date: See Blackboard. {a n+1 b 2n n 0}

CS 4301 Algorithmic Languages and Compilers Page 1

Due Date: See Blackboard

Due Date: See Blackboard

Due Date: See Blackboard

Due Date: See Blackboard

Due Date: See Blackboard

Due Date: See Blackboard

Due Date: See Blackboard

Lab 6 Due Date: Wednesday, April 5, /usr/local/3302/include/direct linking loader.h Driver File:

Lab 2: ADT Design & Implementation

Circle all of the following which would make sense as the function prototype.

Overloading Operators in C++

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

CS 247: Software Engineering Principles. ADT Design

Set Implementation Version 1

Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that

Object oriented programming

Lecture 7. Log into Linux New documents posted to course webpage

CS 117 Programming II, Spring 2018 Dr. Ghriga. Midterm Exam Estimated Time: 2 hours. March 21, DUE DATE: March 28, 2018 at 12:00 PM

Programming Assignment 3

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Review: C++ Basic Concepts. Dr. Yingwu Zhu

More Advanced Class Concepts

Ch 6. Functions. Example: function calls function

CSI33 Data Structures

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

G52CPP C++ Programming Lecture 17

The American University in Cairo Computer Science & Engineering Department CSCE Dr. KHALIL Exam II Spring 2010

CSCE 110 PROGRAMMING FUNDAMENTALS

EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science

Introduction to C++ (Extensions to C)

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Operator Overloading

Chapter 18 - C++ Operator Overloading

Note 11/13/2014. They are like those i s, j s, and temp s that appear and disappear when the function starts and finishes...

III. Classes (Chap. 3)

CSS 342 Data Structures, Algorithms, and Discrete Mathematics I. Lecture 2. Yusuf Pisan

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Linked List using a Sentinel

Use the dot operator to access a member of a specific object.

CS3157: Advanced Programming. Outline

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Dr. Khalil Exam II Fall 2011

for (int i = 1; i <= 3; i++) { do { cout << "Enter a positive integer: "; cin >> n;

CMSC 202 Midterm Exam 1 Fall 2015

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington

Object oriented programming

#include <iostream> #include <algorithm> #include <cmath> using namespace std; int f1(int x, int y) { return (double)(x/y); }

struct Buffer { Buffer(int s) { buf = new char[s]; } ~Buffer() { delete [] buf; } char *buf; };

A <Basic> C++ Course

A <Basic> C++ Course

Midterm Practice Exam

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

Chapter 1 INTRODUCTION

Ch 2 ADTs and C++ Classes

Makefiles Makefiles should begin with a comment section of the following form and with the following information filled in:

Strings and Stream I/O

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Instructor: Final Exam Fall Section No.

Homework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Vectors of Pointers to Objects. Vectors of Objects. Vectors of unique ptrs C++11. Arrays of Objects

Short Notes of CS201

Unit Testing. Contents. Steven Zeil. July 22, Types of Testing 2. 2 Unit Testing Scaffolding Drivers Stubs...

CS201 - Introduction to Programming Glossary By

System Design and Programming II

EECE.3220: Data Structures Spring 2017

Unit Testing. Steven Zeil. July 22, Types of Testing 2. 2 Unit Testing Scaffolding Drivers Stubs...

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

Searching Algorithms. Chapter 11

CSCE 110 PROGRAMMING FUNDAMENTALS

UEE1303(1070) S12: Object-Oriented Programming Constant Pointer and Class

Object-Oriented Programming in C++

CSE 333 Midterm Exam July 24, Name UW ID#

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

CS2141 Software Development using C/C++ C++ Basics

Object oriented programming

Variables. Data Types.

The American University in Cairo Computer Science & Engineering Department CSCE 106 Fundamentals of Computer Science. Instructor: Final Exam Fall 2011

Review. What is const member data? By what mechanism is const enforced? How do we initialize it? How do we initialize it?

Appendix M: Introduction to Microsoft Visual C Express Edition

CSC 306 LEXICAL ANALYSIS PROJECT SPRING 2017

Study Guide for Test 2

Module 9. Templates & STL

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

CS 247: Software Engineering Principles. Modules

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

Chapter 7: Stacks. Exercises 7.2

UEE1303(1070) S 12 Object-Oriented Programming in C++

Introduction to C++ Systems Programming

IV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard

Module Operator Overloading and Type Conversion. Table of Contents

CISC 2200 Data Structure Fall, C++ Review:3/3. 1 From last lecture:

Introduction to Programming

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

PIC 10A. Lecture 17: Classes III, overloading

Operator Overloading in C++ Systems Programming

Transcription:

Source File: ~/2315/45/lab45.(C CPP cpp c++ cc cxx cp) Input: under control of main function Output: under control of main function Value: 4 Integer data is usually represented in a single word on a computer. The number of bits in a word determines the range of integers representable. For example, a 16-bit word allows integers in the range from 32768 through 32767. Doubling the word size to 32 bits increases the range from 2147483648 through 2147483647. In this latter case, results that are representable within ten digits are possible. It is desirable to be able to represent integral results that are larger (in absolute value) than this. In this assignment you are to develop a class for representing large integers. The large integer will be implemented as a list of chars. Each node in the list will contain a digit of the large integer. The large integer is to be stored such that the least significant digit is contained in the first node, the next-to-least significant digit in the second node, and so on. No leading zeroes should be stored with the large integer. The number zero (0) should be stored as a single digit of zero (0). For this assignment, you are to create several constructors and overloaded operators that can be used for comparing large integers. A header file is shown in Figure 1, a sample main function for testing your implementation is shown in Figure 2, and a sample execution sequence is shown in Figure 3. To use the Makefile as distributed in class, add a target of lab45 to targets2srcfiles. For the constructor that takes a string parameter, if the fsaforint function returns false, the number should be stored as zero. 1 #ifndef LAB45_H 2 #define LAB45_H 3 4 #include <iostream> 5 #include <string> 6 #include <list> 7 8 using namespace std; 9 10 typedef enum {NEGATIVE, ZERO, POSITIVE} Sign; 11 12 int fsaforint(string s); 13 14 class BigInt 15 { 16 friend ostream& operator<<( ostream& output, const BigInt& ); 17 friend istream& operator>>( istream& input, BigInt& ); 18 public: 19 BigInt(); // constructor; digits = 0 20 BigInt( int num ); // constructor; digits = num 21 BigInt( const string str ); // constructor; digits = str 22 BigInt( const BigInt& other ); // copy constructor 23 24 bool operator==( const BigInt& rhs ) const; // Equality 25 bool operator< ( const BigInt& rhs ) const; // Less Than 26 Figure 1. /usr/local/2315/include/lab45.h (Part 1 of 2) CS 2315 Data Structures and Algorithms Page 1

27 private: 28 Sign sign; // Sign of # 29 list<char> digits; // List of digits of # 30 }; 31 32 #endif Figure 1. /usr/local/2315/include/lab45.h (Part 2 of 2) 1 #include <lab45.h> 2 #include <cstdlib> 3 #include <iomanip> 4 5 using namespace std; 6 7 void comparezeroes(const BigInt& a, const BigInt& b, const BigInt& c, 8 const BigInt& d, const BigInt& e, const BigInt& f, 9 string v1, string v2, string v3, 10 string v4, string v5, string v6); 11 12 int main() 13 { 14 BigInt a, b(-507), c("abc"), d("275"), 15 e(" -1111111111222222222233333333334444 "); 16 BigInt f(e), g(" 0 "), h(" -0 "); 17 BigInt i(" +0 "), j(+0), k(-0); 18 19 cout << boolalpha; 20 cout << "a = " << a << endl; 21 cout << "g = " << g << endl; 22 cout << "h = " << h << endl; 23 cout << "i = " << i << endl; 24 cout << "j = " << j << endl; 25 cout << "k = " << k << endl; 26 27 comparezeroes(a, g, h, i, j, k, "a", "g", "h", "i", "j", "k"); 28 comparezeroes(g, h, i, j, k, a, "g", "h", "i", "j", "k", "a"); 29 comparezeroes(h, i, j, k, a, g, "h", "i", "j", "k", "a", "g"); 30 comparezeroes(i, j, k, a, g, h, "i", "j", "k", "a", "g", "h"); 31 comparezeroes(j, k, a, g, h, i, "j", "k", "a", "g", "h", "i"); 32 comparezeroes(k, a, g, h, i, j, "k", "a", "g", "h", "i", "j"); 33 Figure 2. /usr/local/2315/src/lab45main.c (Part 1 of 3) Page 2 CS 2315 Data Structures and Algorithms

34 cout << "a = " << a << endl << "b = " << b << endl; 35 cout << "c = " << c << endl << "d = " << d << endl; 36 cout << "e = " << e << endl << "f = " << f << endl; 37 c = a = b; 38 cout << "a = " << a << endl << "b = " << b << endl; 39 cout << "c = " << c << endl; 40 cout << "a == b = " << (a == b) << endl; 41 cout << "c == b = " << (c == b) << endl; 42 43 while (cin >> a >> b) 44 { 45 cout << "a = " << a << " b = " << b << endl; 46 cout << a << " == " << b << " = " << (a == b) << endl; 47 cout << a << " < " << b << " = " << (a < b) << endl; 48 } 49 50 return EXIT_SUCCESS; 51 } 52 53 void comparezeroes(const BigInt& a, const BigInt& b, const BigInt& c, 54 const BigInt& d, const BigInt& e, const BigInt& f, 55 string v1, string v2, string v3, 56 string v4, string v5, string v6) 57 { 58 cout << v1 << " == " << v1 << " = " << (a == a) << endl; 59 cout << v1 << " == " << v2 << " = " << (a == b) << endl; 60 cout << v1 << " == " << v3 << " = " << (a == c) << endl; 61 cout << v1 << " == " << v4 << " = " << (a == d) << endl; 62 cout << v1 << " == " << v5 << " = " << (a == e) << endl; 63 cout << v1 << " == " << v6 << " = " << (a == f) << endl; 64 } 65 66 int fsaforint(string s) 67 { 68 int state = 0; 69 string::iterator itr; 70 71 for (itr = s.begin(); itr < s.end(); ++itr) 72 switch (state) 73 { 74 case 0: if (isspace(*itr)) 75 state = 0; 76 else if (*itr == + *itr == - ) 77 state = 1; 78 else if (isdigit(*itr)) 79 state = 2; 80 else 81 state = 4; 82 break; Figure 2. /usr/local/2315/src/lab45main.c (Part 2 of 3) CS 2315 Data Structures and Algorithms Page 3

83 case 1: if (isdigit(*itr)) 84 state = 2; 85 else 86 state = 4; 87 break; 88 case 2: if (isdigit(*itr)) 89 state = 2; 90 else if (isspace(*itr)) 91 state = 3; 92 else 93 state = 4; 94 break; 95 case 3: if (isspace(*itr)) 96 state = 3; 97 else 98 state = 4; 99 break; 100 case 4: ; 101 } 102 103 return state == 2 state == 3; 104 } 105 106 istream& operator>>(istream& input, BigInt& num) 107 { 108 string s; 109 110 input >> s; 111 num = BigInt(s); 112 113 return input; 114 } 115 116 ostream& operator<<(ostream& output, const BigInt& num) 117 { 118 list<char>::const_reverse_iterator itr; 119 120 if (num.sign == NEGATIVE) 121 output << - ; 122 123 for (itr = num.digits.crbegin(); itr!= num.digits.crend(); ++itr) 124 output << *itr; 125 126 return output; 127 } Figure 2. /usr/local/2315/src/lab45main.c (Part 3 of 3) Page 4 CS 2315 Data Structures and Algorithms

1 newuser@csunix ~> cd 2315 2 newuser@csunix ~/2315>./getlab.ksh 45 3 * Checking to see if a folder exists for Lab 45...No 4 * Creating a folder for Lab 45 5 * Checking to see if Lab 45 has sample input and output files...yes 6 * Copying input and output files for Lab 45 7 from folder /usr/local/2315/data/45 to folder./45 8 * Checking to see if /usr/local/2315/src/lab45main.c exists...yes 9 * Copying file /usr/local/2315/src/lab45main.c to folder./45 10 * Checking to see if /usr/local/2315/include/lab45.h exists...yes 11 * Copying file /usr/local/2315/include/lab45.h to folder./45 12 * Copying file /usr/local/2315/src/makefile to folder./45 13 * Adding a target of lab45 to targets2srcfiles 14 * Touching file./45/lab45.cpp 15 * Edit file./45/lab45.cpp in Notepad++ 16 newuser@csunix ~/2315> cd 45 17 newuser@csunix ~/2315/45> ls 18 01.dat 01.out Makefile lab45.cpp lab45.h lab45main.c 19 newuser@csunix ~/2315/45> make lab45 20 g++ -g -Wall -std=c++11 -c lab45main.c -I/usr/local/2315/include -I. 21 g++ -g -Wall -std=c++11 -c lab45.cpp -I/usr/local/2315/include -I. 22 g++ -o lab45 lab45main.o lab45.o -L/usr/local/2315/lib -lm -lbits 23 newuser@csunix ~/2315/45> cat 01.dat 24-12345678901234567890 0 25-1 1 26 +0-12345678901234567890 27-0 12345678901234567890 28 12345678901234567890-12345678901234567890 29 12345678901234567890 0 30 0 0 31-98765432109876543210 -12345678901234567890 32-99999999999999999999 -99999999999999999998 33-99999999999999999998 -99999999999999999999 34-1234567890123456789 -1234 35-1234 -1234567890123456789 36 12345678901234567890 12345678901234567890 37 99999999999999999999 99999999999999999998 38 99999999999999999998 99999999999999999999 39 12345678901234567890 98765432109876543210 40 98765432109876543210 12345678901234567890 41 1234567890123456789 1234 42 1234 1234567890123456789 43-0 -0 44 0-0 45 +0-0 46-0 0 47 0 0 48 +0 0 49-0 +0 50 0 +0 51 +0 +0 52 newuser@csunix ~/2315/45> cat 01.dat./lab45 53 a = 0 54 g = 0 55 h = 0 56 i = 0 57 j = 0 58 k = 0 59 a == a = true 60 a == g = true 61 a == h = true 62 a == i = true 63 a == j = true 64 a == k = true 65 g == g = true 66 g == h = true 67 g == i = true 68 g == j = true 69 g == k = true 70 g == a = true 71 h == h = true 72 h == i = true 73 h == j = true 74 h == k = true 75 h == a = true 76 h == g = true 77 i == i = true Figure 3. Commands to Compile, Link, & Run Lab 45 (Part 1 of 3) CS 2315 Data Structures and Algorithms Page 5

Page 6 CS 2315 Data Structures and Algorithms 78 i == j = true 79 i == k = true 80 i == a = true 81 i == g = true 82 i == h = true 83 j == j = true 84 j == k = true 85 j == a = true 86 j == g = true 87 j == h = true 88 j == i = true 89 k == k = true 90 k == a = true 91 k == g = true 92 k == h = true 93 k == i = true 94 k == j = true 95 a = 0 96 b = -507 97 c = 0 98 d = 275 99 e = -1111111111222222222233333333334444 100 f = -1111111111222222222233333333334444 101 a = -507 102 b = -507 103 c = -507 104 a == b = true 105 c == b = true 106 a = -12345678901234567890 b = 0 107-12345678901234567890 == 0 = false 108-12345678901234567890 < 0 = true 109 a = -1 b = 1 110-1 == 1 = false 111-1 < 1 = true 112 a = 0 b = -12345678901234567890 113 0 == -12345678901234567890 = false 114 0 < -12345678901234567890 = false 115 a = 0 b = 12345678901234567890 116 0 == 12345678901234567890 = false 117 0 < 12345678901234567890 = true 118 a = 12345678901234567890 b = -12345678901234567890 119 12345678901234567890 == -12345678901234567890 = false 120 12345678901234567890 < -12345678901234567890 = false 121 a = 12345678901234567890 b = 0 122 12345678901234567890 == 0 = false 123 12345678901234567890 < 0 = false 124 a = 0 b = 0 125 0 == 0 = true 126 0 < 0 = false 127 a = -98765432109876543210 b = -12345678901234567890 128-98765432109876543210 == -12345678901234567890 = false 129-98765432109876543210 < -12345678901234567890 = true 130 a = -99999999999999999999 b = -99999999999999999998 131-99999999999999999999 == -99999999999999999998 = false 132-99999999999999999999 < -99999999999999999998 = true 133 a = -99999999999999999998 b = -99999999999999999999 134-99999999999999999998 == -99999999999999999999 = false 135-99999999999999999998 < -99999999999999999999 = false 136 a = -1234567890123456789 b = -1234 137-1234567890123456789 == -1234 = false 138-1234567890123456789 < -1234 = true 139 a = -1234 b = -1234567890123456789 140-1234 == -1234567890123456789 = false 141-1234 < -1234567890123456789 = false 142 a = 12345678901234567890 b = 12345678901234567890 143 12345678901234567890 == 12345678901234567890 = true 144 12345678901234567890 < 12345678901234567890 = false 145 a = 99999999999999999999 b = 99999999999999999998 146 99999999999999999999 == 99999999999999999998 = false 147 99999999999999999999 < 99999999999999999998 = false 148 a = 99999999999999999998 b = 99999999999999999999 149 99999999999999999998 == 99999999999999999999 = false 150 99999999999999999998 < 99999999999999999999 = true 151 a = 12345678901234567890 b = 98765432109876543210 152 12345678901234567890 == 98765432109876543210 = false 153 12345678901234567890 < 98765432109876543210 = true 154 a = 98765432109876543210 b = 12345678901234567890 155 98765432109876543210 == 12345678901234567890 = false 156 98765432109876543210 < 12345678901234567890 = false 157 a = 1234567890123456789 b = 1234 Figure 3. Commands to Compile, Link, & Run Lab 45 (Part 2 of 3) Lab 45

158 1234567890123456789 == 1234 = false 159 1234567890123456789 < 1234 = false 160 a = 1234 b = 1234567890123456789 161 1234 == 1234567890123456789 = false 162 1234 < 1234567890123456789 = true 163 a = 0 b = 0 164 0 == 0 = true 165 0 < 0 = false 166 a = 0 b = 0 167 0 == 0 = true 168 0 < 0 = false 169 a = 0 b = 0 170 0 == 0 = true 171 0 < 0 = false 172 a = 0 b = 0 173 0 == 0 = true 174 0 < 0 = false 175 a = 0 b = 0 176 0 == 0 = true 177 0 < 0 = false 178 a = 0 b = 0 179 0 == 0 = true 180 0 < 0 = false 181 a = 0 b = 0 182 0 == 0 = true 183 0 < 0 = false 184 a = 0 b = 0 185 0 == 0 = true 186 0 < 0 = false 187 a = 0 b = 0 188 0 == 0 = true 189 0 < 0 = false 190 newuser@csunix ~/2315/45> cat 01.dat./lab45 > my.out 191 newuser@csunix ~/2315/45> diff 01.out my.out 192 newuser@csunix ~/2315/45> Figure 3. Commands to Compile, Link, & Run Lab 45 (Part 3 of 3) CS 2315 Data Structures and Algorithms Page 7