Click here to Skip to main content
16,022,798 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
C++
#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Mint {
public:

	Mint();
	Mint(int);
	Mint (const char  *s);
	string afficher();
	Mint operator+=(const Mint &rhs); //returns mint + rhs
private:
	vector<char> num;
};
#include "Mint.h"
#include <string.h>

Mint::Mint()
{
	num.push_back(0);
}
Mint::Mint(int n)
{
	while(n!=0)
	{
		num.push_back(n%10);
		n = n/10;
	}
}
Mint::Mint(const char* s)
{
		int i = 0 ;
		for(i=strlen(s)-1;i>=0;i--)
		num.push_back(s[i] - '0');
}
string Mint::afficher(){
	string s="";
	int i;
	for(i=num.size()-1;i>=0;i--)
		s += char('0'+num[i]);
	return s;
}
Mint Mint::operator+=(const Mint &rhs){
	int len = num.size();
	int carry = 0;
	if(len < rhs.num.size())
	{
		len = rhs.num.size();
	}
	int i;
	for (i=0;i>=len-1;i++)
	{
		num.push_back((num[i]+rhs.num[i]+carry)%10);
		carry =(num[i]+rhs.num[i]+carry) / 10;
	}
	return *this;

}
#include "Mint.h"
#include <iostream>
#include <string>
using namespace std;



int main()
{
	Mint x,z;
	Mint a = "655478461469974272005572";
	Mint b = "8";
	a += b;
	cout << a.afficher()<<endl;

}


hii guys please check out the code and tell me what is wrong with the += operator because i get some strange results;
Posted
Comments
[no name] 26-Dec-15 19:21pm    
This is what a debugger is used for. Maybe try that first.
Member 12223678 26-Dec-15 19:26pm    
i did but it wasn't of any help
[no name] 27-Dec-15 2:57am    
That just confirms you don't know how to use it. Every one of the many bugs in your program can be found using this tool - this is the way all programmers work. You should make learning this a high priority because it will make you more professional and self reliant.

1 solution

There is more than one problem. The most obvious is that your loop will never get entered. You need to change your greater than to a less than).

The next problem is that you need to set the len variable to the smaller vector size (change the less than to a greater than). Otherwise you will get an exception when the loop goes out of bounds.

Third problem is that you are pushing the result of the calculation into the num vector - you need to be replacing the index value, rather than appending to what is already there.

The last, which will not become evident unless you start using bigger numbers on both side of the equation, is that you need to check the carry after the loop exists, and if it has a value, add it to the result.

C++
Mint Mint::operator+=(const Mint &rhs) {
	size_t len = num.size();
	char carry = 0;

	if (len > rhs.num.size())
		len = rhs.num.size();

	size_t i;
	for (i = 0; i <= len - 1; i++)
	{
		char result = num[i] + rhs.num[i] + carry;
		num[i] = result % 10;
		carry = result / 10;
	}

	while (carry)
	{
		if (i < num.size()) 
		{
			num[i] += carry;
			if (num[i] >= 10)
			{
				num[i] -= 10;
				i++;
			}
			else
				carry = 0;
		}
		else {
			num.push_back(carry);
			carry = 0;
		}
	}
	return *this;
}
 
Share this answer
 
Comments
Member 12223678 27-Dec-15 10:26am    
Midi_Mick
thank you for the answer but i don't think it will work if i have 2 numbers of different size;

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