I am writing a class that can multiply and add very large values with hundreds and thousands of digits in C++. Cannot add any more #includes
Please implement with array of size 1000
#include <iostream>
#include <cassert>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
class LargeNumber {
private:
int
digits[1000];
// digits are stored in for operational convenience
public:
LargeNumber() {
for (int i = 0; i < 1000;
i++)
//default constructor initializes all digits to 0
digits[i] =
0;
}
LargeNumber(int n) {
if (n < 0)
for (int i = 0;
i < 1000; i++)
digits[i] = 0;
else
{
int i = 0;
while
(n)
// adds digits of n in reverse order to the biginning of
array
{
digits[i++] = n % 10;
n /= 10;
}
for (; i <
1000;
i++)
// assigns the rest of the array as 0
digits[i] = 0;
}
}
LargeNumber(const string& str) {
bool isValid = true;
for (int i = 0; i <
str.length(); i++) //
checks if the string is a number
{
if (str[i] <
'0' || str[i] > '9')
{
isValid = false;
break;
}
}
if (isValid)
{
int i = 0;
for (int j =
str.length() - 1; j >= 0; j--) // store
digits in reverse order
digits[i++] = str[j] -
48;
// 48 is subtracted to convert '0' to number 0 (ASCII value of '0'
is 48)
for (; i <
1000; i++) //initialize rest to
0
digits[i] = 0;
}
}
LargeNumber(const LargeNumber& other) {
string str =
other.to_string();
//since array of other is private we have to get the string
form
int i = 0;
for (int j = str.length() - 1; j
>= 0; j--) //remaining procedure is
similar to string parameter constructor
digits[i++] =
str[j] - 48;
for (; i < 1000; i++)
digits[i] =
0;
}
int num_digits() const {
int i = 999;
while (i >= 0 &&
digits[i] == 0) // starts to check from 1000 for non
zero nos., the first non-zero index is no. of digits
i--;
return i + 1;
}
bool is_zero() const {
return num_digits() ==
0; //checks if no. of digits is zero which
implies that number is zero
}
LargeNumber operator+(const LargeNumber& a)
const {
int carry =
0;
// number to store the carry over value
int l1 = num_digits();
int l2 = a.num_digits();
string str =
a.to_string();
//get second parameter as string and reverse it
for (int i = 0; i < str.length()
/ 2; i++)
{
char t =
str[str.length() - i - 1];
str[str.length()
- i - 1] = str[i];
str[i] =
t;
}
string ans;
int i = 0;
for (; i < l2; i++)
{
int sum =
digits[i] + (str[i] - 48) +
carry;
//sum is digit of one num + second num + carry over
carry = sum /
10;
// rest is carried over
ans += (sum %
10) +
48;
//one digit is stored
}
for (; i < l1; i++)
{
int sum =
digits[i] + carry;
carry = sum /
10;
ans += (sum %
10) + 48;
}
if (carry != 0)
ans += carry +
48;
for (int i = 0; i < ans.length()
/ 2;
i++)
//reverse the answer string for constructor use
{
char t =
ans[ans.length() - i - 1];
ans[ans.length()
- i - 1] = ans[i];
ans[i] =
t;
}
LargeNumber
S(ans);
//make a LargeNumber using ans
return
S;
//return it
}
LargeNumber operator*(const LargeNumber& a)
const {
int l1 = num_digits();
int l2 = a.num_digits();
string str =
a.to_string();
// get string from a
for (int i = 0; i < str.length()
/ 2; i++)
//reverse it
{
char t =
str[str.length() - i - 1];
str[str.length()
- i - 1] = str[i];
str[i] =
t;
}
string *addends = new
string[l1]; //an array of
string to store product of each digit of 1st no. with whole of 2nd
no.
for (int i = 0; i < l1;
i++)
{
int carry =
0;
//calculate product for each digit of 1st no.
for (int j = 0;
j < i;
j++)
//add the ending zeros
{
addends[i] += '0';
}
for (int j = 0;
j < l2;
j++)
//find the product
{
int sum = carry + (str[j] - 48) *
digits[i];
addends[i] += sum % 10 + 48;
carry = sum / 10;
}
while
(carry)
//append the carry
{
addends[i] += carry % 10 + 48;
carry /= 10;
}
}
LargeNumber
ans; // to
store final answer
for (int i = 0; i < l1;
i++)
{
for (int j = 0;
j < addends[i].length() / 2;
j++)
//reverse the string to pass to constructor
{
char t = addends[i][addends[i].length() - j -
1];
addends[i][addends[i].length() - j - 1] =
addends[i][j];
addends[i][j] = t;
}
ans = ans +
LargeNumber(addends[i]);
//add each part to ans
}
return
ans; //return
ans
}
string to_string() const {
string str;
for (int i = num_digits() - 1; i
>= 0;
i--)
//since digits are stored in reverse order append nos. from the
back
{
str += digits[i]
+ 48;
}
return str; //return
answer
}
};
I am writing a class that can multiply and add very large values with hundreds and...
help implementing canvas.cpp but with only #include "canvas.h", class header file doesnt have height ----------------------------------------------------------------------------------------------------------------------- canvas.h #ifndef CANVAS_H #define CANVAS_H #include <string> using namespace std; // In this homework, you'll manipulate ASCII art images // consisting of a rectangular grid of chararacter pixels. class Canvas { public: // Allocates a canvas of the given width and height 5 that // consists entirely of ' ' (space) chars. Canvas(int width); // Allocates a canvas with width 5 and height 5 that...
I need help filling in the the code in the methods add, multiply, and evaluate package poly; import java.io.IOException; import java.util.Scanner; /** * This class implements evaluate, add and multiply for polynomials. * * * */ public class Polynomial { /** * Reads a polynomial from an input stream (file or keyboard). The storage format * of the polynomial is: * <pre> * <coeff> <degree> * <coeff> <degree> * ... *...
I am having trouble understanding how this code is able to use the contents of the header file. Can someone please provide comments in the main code to describe what is happening? (especially on the bool isNumber) THE MAIN CODE: #include<bits/stdc++.h> #include "MyCartesianPoint.h" #include <math.h> #include <iostream> using namespace std; bool isNumber(string s) { if(!isdigit (s[0])) { if(s[0] != '-') return false; else if(s.length() == 1) return false;...
I am having trouble understanding how this code is able to use the contents of the header file. Can someone please provide brief comments in the top code to show what is happening? THE CODE: #include<bits/stdc++.h> #include "MyCartesianPoint.h" #include <math.h> #include <iostream> using namespace std; bool isNumber(string s) { if(!isdigit (s[0])) { if(s[0] != '-') return false; else if(s.length() == 1) return false; } for...
My C++ program is not compiling. Please explain how you fixed with detailed inline comments. I am using Visual Studio 2017. It's a character count program that keeps and displays a count of all the upper, lower case and digits in a .txt file. The requirement is to use classes to implement it. -----------------------------------------------------------------HEADER FILE - Text.h--------------------------------------------------------------------------------------------- /* Header file contains only the class declarations and method prototypes. Comple class definitions will be in the class file.*/ #ifndef TEXT_H #define...
class AVLTree The following functions are the minimum requirements for the AVL class. You can add any function from Assignment 2 to this class. You should modify the BSTree insert function so that the tree remains balanced after each insertion. Required Public Member Functions void insert(const string &): Insert an item to the binary search tree and perform rotation if necessary. int balanceFactor(Node*): Return the balance factor of a given node. void printBalanceFactors(): Traverse and print the tree in inorder...
Stack help. I need help with my lab assignment. Complete a method for a class named Palindrome that evaluates a string phrase to determine if the phrase is a palindrome or not. A palindrome is a sequence of characters that reads the same both forward and backward. When comparing the phrase to the same phrase with the characters in reverse order, an uppercase character is considered equivalent to the same character in lowercase, and spaces and punctuation are ignored. The...
SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! mystring.h: //File: mystring1.h // ================ // Interface file for user-defined String class. #ifndef _MYSTRING_H #define _MYSTRING_H #include<iostream> #include <cstring> // for strlen(), etc. using namespace std; #define MAX_STR_LENGTH 200 class String { public: String(); String(const char s[]); // a conversion constructor void append(const String &str); // Relational operators bool operator ==(const String &str) const; bool operator !=(const String &str) const; bool operator >(const...
C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...
C++ Lab 9A Inheritance Employee Class Create a project C2010Lab9a; add a source file Lab9a.cpp to the project. Copy and paste the code is listed below: Design a class named Employee. The class should keep the following information in member variables: Employee name Employee number Hire date // Specification file for the Employee class #ifndef EMPLOYEE_H #define EMPLOYEE_H #include <string> using namespace std; class Employee { private: // Declare the Employee name string variable here. // Declare the Employee...