Ograniczenie liczb do 30 cyfr

0

Poniżej podaję kod programu, który dodaje nieskończenie długie liczby.Jaki kod należy umieścić, aby ograniczyć wpisywane liczby do 30 cyfr?

#include <cstdio>
#include <cstdlib>
#include <vector>
#include <string>
#include <iostream>

#define MINUS '-'

using namespace std;

/*
 * Unlimited length integer.
 */
class VeryLongInt {
private:
	/*
	 * One digit long number's elements.
	 */
	vector<int> number;

	/*
	 * Sign indicator.
	 */
	bool negative;

	/*
	 * Gets number's element from given position. If there is no such position
	 * zero is returned (each number can have infinite number of leading zeros).
	 */
	int get(int position) const {
		if (position >= 0 && position < number.size()) {
			return number[position];
		}

		return 0;
	}

	/*
	 * Gets number of elements.
	 */
	int getSize() const {
		return number.size();
	}

	/*
	 * Appends value as last element.
	 */
	int append(int value) {
		number.push_back(value);
	}

	/*
	 * Sets elements of the numbers.
	 */
	void setNumbers(string number) {
		negative = false;

		for (int i = number.length() - 1; i >= 0; i--) {
			if(number[i] == MINUS) {
				negative = true;
			} else {
				this->number.push_back(number[i] - '0');
			}
		}
	}

	/*
	 * Clears all elements.
	 */
	void clear() {
		number.clear();
	}

	/*
	 * Gets greater value.
	 * x - first value.
	 * y - second value.
	 */
	int max(int x, int y) const {
		return x >= y ? x : y;
	}

	/*
	 * Gets absolute value.
	 * x - number to take absolute value from.
	 */
	int abs(int x) const {
		return (x >= 0 ? 1 : (-1)) * x;
	}

	/*
	 * Gets number with greater absolute value.
	 * v1 - first number.
	 * v2 - second number.
	 */
	VeryLongInt maxAbs(VeryLongInt v1, VeryLongInt v2) const {
		if(v1.getSize() > v2.getSize()) {
			return v1;
		}

		if (v2.getSize() > v1.getSize()) {
			return v2;
		}

		for(int i = v1.getSize() - 1; i >= 0 ; i--) {
			if(v1.get(i) > v2.get(i)) {
				return v1;
			} else if(v2.get(i) > v1.get(i)) {
				return v2;
			}
		}

		return v1;
	}

	/*
	 * Gets number with smaller absolute value.
	 * v1 - first number.
	 * v2 - second number.
	 */
	VeryLongInt minAbs(VeryLongInt v1, VeryLongInt v2) const {
			if(v1.getSize() < v2.getSize()) {
				return v1;
			}

			if (v2.getSize() < v1.getSize()) {
				return v2;
			}

			for(int i = v1.getSize() - 1; i >= 0 ; i--) {
				if(v1.get(i) < v2.get(i)) {
					return v1;
				} else if(v2.get(i) < v1.get(i)) {
					return v2;
				}
			}

			return v2;
		}


public:

	/*
	 * Creates new instance of VeryLongInt class.
	 */
	VeryLongInt() {
	}

	/*
	 * Creates new instance of VeryLongInt class.
	 * number - string representation of the number.
	 */
	VeryLongInt(string number) {
		setNumbers(number);
	}

	/*
	 * Destroys instance of VeryLongInt class.
	 */
	~VeryLongInt() {
		clear();
	}

	/*
	 * Overrides addition operator.
	 * veryLongInt - number to be added.
	 */
	VeryLongInt VeryLongInt::operator+(const VeryLongInt& veryLongInt) const {
		int maxSize = max(getSize(), veryLongInt.getSize());
		int carriage = 0;
		int value;

		VeryLongInt v1 = maxAbs(*this, veryLongInt);
		VeryLongInt v2 = minAbs(*this, veryLongInt);
		VeryLongInt outputValue;

		outputValue.negative = v1.negative;

		// if both v1 and v2 have same sign - add
		if (v1.negative == v2.negative) {
			for (int i = 0; i < maxSize; i++) {
				value = get(i) + veryLongInt.get(i) + carriage;

				// If value is longer than one digit - take care about carriage
				if (value >= 10) {
					carriage = 1;
					value -= 10;
				} else {
					carriage = 0;
				}

				outputValue.append(value);
			}

			if (carriage != 0) {
				outputValue.append(carriage);
			}
		} else { // otherwise subtract
			int* v1Copy = new int[maxSize];
			int* v2Copy = new int[maxSize];

			for(int i = 0; i < maxSize; i++) {
				v1Copy[i] = v1.get(i);
				v2Copy[i] = v2.get(i);
			}

			for (int i = 0; i < maxSize; i++) {
				// If it is not possible to subtract - borrow
				if(v1Copy[i] < v2Copy[i]) {
					for(int j = i + 1; j < maxSize; j++) {
						if(v1Copy[j] > 0) {
							v1Copy[j] -= 1;
							for(int k = j - 1; k > i; k--) {
								v1Copy[k] += 9;
							}
							v1Copy[i] += 10;
							break;
						}
					}
				}

				value = abs(v1Copy[i] - v2Copy[i]);

				outputValue.append(value);
			}
		}

		return outputValue;
	}

	/*
	 * Output (<<) operator.
	 * out - output stream.
	 * veryLongInt - number to be printed.
	 */
	friend ostream& operator<<(ostream& out, const VeryLongInt& veryLongInt) {
		int size = veryLongInt.getSize() - 1;

		// Omit leading zeros
		while(size >= 0 && veryLongInt.get(size) == 0) {
			--size;
		}

		// Show sign
		if(size >= 0 && veryLongInt.negative) {
			out << MINUS;
		}

		// Print number
		for(int i = size; i > 0; i--) {
			out << veryLongInt.get(i);
		}

		out << veryLongInt.get(0);

		return out;
	}

	/*
	 * Input (>>) operator.
	 * in - input stream.
	 * veryLongInt - number to be read.
	 */
	friend istream& operator>>(istream& in, VeryLongInt& veryLongInt) {
		string s;

		in >> s;

		veryLongInt.setNumbers(s);

		return in;
	}

	/*
	 * Assignment (=) operator
	 * veryLongint - number to assign to.
	 */
	VeryLongInt& VeryLongInt::operator=(const VeryLongInt& veryLongInt) {
		clear();

		for(int i = 0; i < veryLongInt.getSize(); i++) {
			append(veryLongInt.get(i));
		}

		// by convention, always return *this
		return *this;
	}
};

/*
 * Shows result of adding numbers v1 and v2.
 * v1 - first number to add.
 * v2 - second number to add.
 */
void showResult(VeryLongInt v1, VeryLongInt v2) {
	// Add and show result
	cout << v1 << " + " << v2 << " = " << (v1 + v2) << endl;
}

/*
 * Entry point
 */
int main() {
	string operation;
	VeryLongInt v1;
	VeryLongInt v2;

	// Read two numbers and addition sign between them
	cin >> v1 >> operation >> v2;

	// Show result
	showResult(v1, v2);

	// Exit
	return EXIT_SUCCESS;
}
0

W operatorze wejścia (>>) daj np.

if (s.size() > TWOJE_MAX) s.resize (TWOJE_MAX);

czy co tam chcesz zrobić jak będzie zbyt długie.

0

coś mi kod nie chodzi. Wstawiłem go, ale wyrzuca mi błąd. Możliwe że w złym miejscu go wbiłem. Nie wiem co zrobić

0
nathiasel napisał(a)

coś mi kod nie chodzi. Wstawiłem go, ale wyrzuca mi błąd. Możliwe że w złym miejscu go wbiłem. Nie wiem co zrobić

Jaki błąd i gdzie go wstawiłeś?

1 użytkowników online, w tym zalogowanych: 0, gości: 1