Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
hello guys
i need some help with the following class
here is the hpp file :
C++
#include "iostream"
using namespace std;
#include "string"
#include "vector"


class Mint{
public:
	Mint();
	Mint(int);
	Mint(const char*);
	Mint operator+=(const Mint &rhs);
	bool operator<(const Mint&);
	bool operator>(const Mint&);
	void display();
private:
	unsigned char* num;
	int size;
};


and here is the cpp file
C++
#include "Mint.h"




Mint::Mint()
{
	num = new unsigned char[size= 1];
	num[0] = 0;
}
Mint::Mint(const char* s)
{
		num = new unsigned char[size= strlen(s)/2 + strlen(s)%2];
		if(strlen(s)%2 == 1)
		num[0] = s[0]-'0';
			unsigned int i;
			int j=strlen(s)%2;
			for(i=strlen(s)%2;i<strlen(s);i+=2)
			{
							int left = s[i] - '0';
							int right = s[i+1] - '0';
							num[j] = left << 4 ;
							num[j] |= right;
							j++;
			}

}
Mint Mint::operator+=(const Mint  &rhs){
		int i,carry=0,sum;
		for(i=this->size-1;i>=0;i--)
		{
			sum = ((num[i]  ^ rhs.num[i])  ^ carry);
			carry = ((num[i] & rhs.num[i]) | (num[i] & carry)) | (rhs.num[i] & carry);
			num[i] = sum;
		}
		return *this;
}
bool Mint::operator<(const Mint& rhs)
{
	unsigned int i;
	if (size  < rhs.size)
			return true;
	if (size == rhs.size)
	for(i=0;i<size;i++)
	{
		if(num[i] < rhs.num[i])
			return true;
	}
	return false;
}
bool Mint::operator>(const Mint& rhs)
{
	if (*this < rhs)
	 return false;
	return true;
}
void Mint::display()
{
	 int i;
	for (i=0;i<size ;i++)
		{
		int first_digit = (num[i] & '\xF0')>>4;
		int second_digit = (num[i] & '\x0F');
		if (i || first_digit)
			cout << first_digit;
		if (i || second_digit)
			cout << second_digit;
		}
}


the idea is to work with big numbers that an int can't handle but as you can see i took each couple of digits and store them in one byte each taking 4 bits
now i need help with + - and * operators
i gave it a try but it didn't work so please check out the code and give me your suggestions
Posted
Comments
Jochen Arndt 21-Jan-16 10:02am    
You are storing the highest digits at the lowest position. This makes operations more complicated. I suggest to redesign your class to store the lowest digits in the lowest byte.

Then the add operation just needs to add the nibbles (4 bit values) and set a carry. Your actual code did not check for different number of digits. You should create a temporary value that can hold the result (max. number of digits plus one for carry), add the nibbles with carry while smaller than the size of both numbers, and finally add with zero when reaching the size of the smaller number.
Member 12223678 21-Jan-16 10:12am    
would you please elaborate on that
i have a presentation tomorrow and i'm in kind of a hurry so if it's not a problem can you give an example in code
thank you
Jochen Arndt 21-Jan-16 10:24am    
I can't give you example code for your current implementation (storage method), because I would not do it that way. That's why I wrote my comment.

Providing sample code for the other storage method requires implementation of that method first. I'm sorry, but that has to be done by you. Even then you should at least try to implement the methods and then ask here if you got stuck because I (and probably most others here) have not the time to write code for you.

Maybe you should search the web for long number classes. There are many out there.
jeron1 21-Jan-16 10:02am    
What exactly didn't work?
Member 12223678 21-Jan-16 10:12am    
the =+ operator didn't work
i didn't know how to implement it

1 solution

Member 12223678 wrote:
i didn't implement + either if i did i would have used it in +=
that is my problem i don't know how to implement + nor +=
please if you know how to implement it
help me because i'm stuck
Perhaps you have to start with
operator overloading — cppreference.com[^],
Operator Overloading in C++ - Cprogramming.com[^].

In last of the articles referenced above, the case closest to yours is the code sample of class "Complex"; implementation of + operator is shown.

—SA
 
Share this answer
 
Comments
CPallini 21-Jan-16 16:17pm    
5.
Sergey Alexandrovich Kryukov 21-Jan-16 16:26pm    
Thank you, Carlo.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900