سؤال

أنا أعمل على برنامج لعامل أرقام كبيرة جدا (20 رقما أو أكثر) في C ++ و AM باستخدام GMP للتعامل مع مشكلات تجاوز الفائض.يعمل برنامجي بشكل جيد لأرقام حوالي 10 أرقام أو أقل، ولكن عندما أرمي رقم 15 رقما فيه، فإنه ينفجر.سأغلي برنامجي لأسفل ببساطة سطر واحد مثل هذا: giveacodicetagpre.

إذا قمت باستبدال هذا الخط مع giveacodicetagpre.

ثم كل شيء يعمل بشكل جيد فقط.

هنا هو الخطأ: giveacodicetagpre.

يعرف أي شخص كيفية إصلاح هذا حتى أتمكن من استخدام أرقام كبيرة؟اعتقدت أن GMP كان من المفترض أن يسمح بشيء مثل 500 رقما أو زائد أو ناقص.

شكرا!

هل كانت مفيدة؟

المحلول

The number you are attempting to assign to n is obviously too large to fit into any of the standard integer types, which explains the use of gmp, but this also means you (your program) will be unable to use the number as a integer in any capacity (including initialization/assignment functions). The easiest way to assign a large number to an mpz is by using a string literal representation of that number:

mpz_class n;
n = "48112959837082048697";

Note that combined initialization/assignment will not work, ie:

mpz_class n = "48112959837082048697";  // Error converting

Side note: You do not need to include stdio.h and gmp.h, as they are included from iostream and gmpxx.h respectively.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top