This project implements a custom MyInt class for handling large integers, allowing arithmetic operations such as addition and subtraction. The class uses arrays to represent digits and supports basic operations like addition (+) and subtraction (-) on large integer values. The MyInt class provides functionality to perform operations on large numbers that cannot fit in standard data types like int or long.
- Addition (
+): Adds two large integers, handling carries appropriately. - Subtraction (
-): Subtracts two large integers, with handling for negative results (returns nothing if the result is negative). - Dynamic Memory Management: Utilizes dynamically allocated arrays to store and manipulate digits of large numbers.
- Flexible Size Handling: Can handle numbers of arbitrary size, represented digit-by-digit in arrays.
To build and run this project, you need the following:
- A C++ compiler (e.g., g++, clang++)
- Basic understanding of C++ and object-oriented programming (OOP)
git clone https://github.com/your-username/MyInt-Project.git
cd MyInt-Project
2. Compile the Code
Use a C++ compiler to compile the source files. For example, if you're using g++, run the following command:
g++ -o myint main.cpp MyInt.cpp
3. Run the Program
Execute the program:
./myint
4. Example Usage
Once the program runs, it will perform operations on large integers. The code includes example operations that demonstrate adding and subtracting large integers.
Code Overview
MyInt Class
The core class for representing large integers. It uses a dynamically allocated array (arrInt) to store the digits of the number. The size attribute keeps track of the number of digits.
Arithmetic Operations
Addition (operator+): Adds two MyInt objects by aligning their digits and performing digit-by-digit addition from least significant to most significant, managing carries.
Subtraction (operator-): Subtracts two MyInt objects, handling borrowing and ensuring the result is non-negative.
Dynamic Memory Management
Memory for the arrInt array is allocated dynamically to ensure that large integers with varying digit lengths are handled correctly. Temporary arrays are used to align the digits of the operands for arithmetic operations.
Code Example
Here's a simplified example of how the MyInt class is used:
MyInt x, y;
x.setValue("12345");
y.setValue("6789");
MyInt sum = x + y; // Adds the two MyInt objects
MyInt diff = x - y; // Subtracts y from x
Functions in MyInt:
setValue(string value): Sets the value of the MyInt object from a string.
operator+: Performs addition of two MyInt objects.
operator-: Performs subtraction of two MyInt objects.
Limitations
Subtraction with Negative Results: The current implementation doesn't handle negative results directly; instead, it returns an empty MyInt object.
Memory Constraints: The size of the integers that can be handled is constrained by the available system memory. Very large integers may lead to memory allocation issues if system memory is exhausted.
Multiplication and Division: Extend the class to support multiplication and division of large integers.
Efficient Memory Management: Improve the memory management for handling very large numbers more efficiently.
License
This project is licensed under the MIT License - see the LICENSE file for details.