如何存储在C程序很长的整数值考试: - 98474737475747374739399 [英] How to store a very long integer value in a C program for an exam :- 98474737475747374739399
问题描述
我要存储整数类型的一个很长的价值不能被保存在的可变长
键入。我怎么能这样存储在C程序中一个长整型值。
如果可能的话,请说明它是通过一个例子/程序。
I have to store a very long value of type integer that can't be stored in a variable of long
type. How can I store such a long integer value in a C programme.
Please illustrate it through an example/ programme, if possible.
推荐答案
想想用结构像这样存储的数字为十进制数字序列:
Think about storing a numbers as sequences of decimal digits using a struct like this:
struct num {
int ndigits;
char d[MAXDIGITS];
};
例如,数123456可以初始化为
For example, the number 123456 could be initialized as
struct num n = { 6, { 6, 5, 4, 3, 2, 1 } };
该转位顺序原来是容易计算的重要。特别是,该位值 ND [I]
是 ND [I]
* 10 ^我。
现在,有几个问题:
- 您会如何添加一个到
NUM
? - 您会怎样添加任意一个数字到
NUM
? - 您会如何添加两个
NUM
取值在一起吗? - 您会如何乘以
NUM
由两个? - 您会如何乘以
NUM
由一个单一的数字? - 您会如何乘以
NUM
10? - 你将如何将两个
NUM
取值在一起吗?提示:做一些铅笔和纸张乘法,看看它们是如何工作的
- How would you add one to a
num
? - How would you add an arbitrary single digit to a
num
? - How would you add two
num
s together? - How would you multiply a
num
by two? - How would you multiply a
num
by a single digit? - How would you multiply a
num
by 10? - How would you multiply two
num
s together? HINT: Do some pencil and paper multiplications and see how they work.
如果您通过的问题,这一系列的工作,你应该能够编写一个函数每一步,并重新使用这些功能来回答以后的问题,并用一个非常简单的和未经优化的长结束了(哦,在这到 MAXDIGIT
位)整数包正数的加法和乘法。
If you work through this sequence of questions, you should be able to write a function for each step, and re-use those functions to answer the later questions, and end up with a very simple and unoptimized long (well, up to MAXDIGIT
digits) integer package for addition and multiplication of positive numbers.
其他问题:
- 你如何概括
NUM
重新present负数以及积极的? - 如何划分有一个
NUM
由另一个(忽略余数)?这比乘棘手,但同样,通过做一些铅笔和纸长师开始,仔细想想你做什么。
- How do you generalize
num
to represent negative numbers as well as positive? - How do you divide one
num
by another (ignoring remainders)? This is trickier than multiplication, but again, start by doing a few pencil and paper long divisions and think carefully about what you do.
这篇关于如何存储在C程序很长的整数值考试: - 98474737475747374739399的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!