COMP10002 Foundations of Algorithms-大数加减乘乘方运算

COMP10002 Foundations of Algorithms
Semester 1, 2018
Assignment 1
1 Learning Outcomes
In this assignment, you will demonstrate your understanding of arrays, pointers, input processing, and
functions. You will also extend your skills in terms of code reading, program design, testing, and debugging.
2 The Story…
On 2 December 2014, a post appeared on YouTube’s Google+ account1: \‘Gangnam Style’ has been viewed
so many times we have to upgrade!” It turned out that the Korean rapper PSY’s viral music video had been
viewed for over 2,147,483,647 (231 − 1) times which surpassed the limit of a signed 32-bit integer counter.
YouTube had since upgraded to a 64-bit integer counter. This upgraded counter would not be broken again
in a foreseeable future. However, in many other scenarios such as popular cryptography algorithms (e.g.,
RSA), even larger integers are still needed.
In C, an int variable can only store values up to 231 − 1. A long variable can handle up to 263 − 1 =
9; 223; 372; 036; 854; 775; 807 (machine dependant). For even larger numbers, you will have to use double,
which is inaccurate. In this assignment, you will enhance the C language by supporting \huge” integers.
3 Your Task
Your task is to develop a huge integer calculator that works with integers of up to 100 digits. The
calculator has a simple user interface, and 10 \variables” (n0, n1, …, n9) into which integers can be
stored. For example, a session with your calculator might look like:
mac: ./assmt1

n0=2147483648
n0+3
n0?
2147483651
n1=1000000000000000000
n1+n0
n1?
1000000002147483651
n0?
2147483651
exit
mac:
Note: \mac: ” is the terminal prompt and > ” is the prompt of the calculator.
The calculator to be implemented has a very limited syntax: constants or other variables can be assigned to
variables using an ‘=’ operator; variable values can be altered using a single operator and another variable
or constant; variable values can be printed using ‘?’. Each line of input command always starts with a variable followed by a single operator, which is then followed by (optional depending on the operator) another
variable or a constant. The only exception is the \exit” command, which has no additional parameters.
To allow storage of huge integers, your calculator needs to use arrays of integers, with one digit stored per
element in the array. The number of digits in a huge integer needs to be stored as well. We assume a
maximum of 100 digits in a huge integer, and use a \reverse order” representation. For example, 123 will
be represented by:
define INT_SIZE 100
typedef int huge_t[INT_SIZE];
huge_t var_n0 = {3, 2, 1};
int var_len0 = 3;
Here, array var_n0 stores the digits of 123 in a reversed way. The reverse order representation will make
calculations such as addition and multiplication easier. However, if you wish, you are free to change how
the digits are stored in the array. By using struct we can further simplify the representation, but you are
not required to do so as it has not been covered yet.
The expected input format for all stages is identical - a sequence of simple commands as shown above. You
may assume that the input is always valid, that is, you do not need to consider input errors.
All input integers are unsigned. You do not need to consider negative numbers or subtraction. There
will not be \n0=+0”, \n0=+123”, \n0=-123”, \n0-123”, or \n0++123” in the input. It will just be in the
forms of, e.g., \n0=0”, \n0=123”, \n0+123”, or \n0*123”. There will not be leading 0’s in the input
numbers, e.g., 001.
You will be given a skeleton code file named \assmt1.c” for this assignment in LMS. The skeleton code file
contains a main function where a loop is used to continuously read in user commands, and calls relevant
functions to process the commands. The exit function has been implemented already, which can handle
the exit command if you compile and run the skeleton code. The echo function for the ‘?’ operator to
print out a huge integer has been given to you for reference as well, but you need to implement the init
function to initialise the variables first before echo can work properly. All the other function bodies are
empty. Your task is to add code into them to process the other calculator commands. Note that you
should not change the main function, but you are free to modify any other parts of the skeleton
code (including adding more functions). You can change echo as well if you wish to change how a
huge integer is stored in an array.
3.1 Stage 1 - Getting Started (Up to 3 Marks)
Your first task is to understand the skeleton code. Note the use of the type huge_t in the skeleton code.
Each of the huge_t variables in the array vars stores a huge integer, with 10 variables available in total,
named \n0”, \n1”, …, \n9”, respectively. In this stage, you will start with implementing the init function
to initialise the variable values to be 0. A sample execution session of this stage is shown below.
n0?
0
n8?
0
3.2 Stage 2 - Reading in a Huge Integer (Up to 8 Marks)
Next, you will implement the assign function to enable assigning a constant value to a variable, or the
value of a variable to another variable (both could be the same variable, e.g., \n0=n0”). A sample execution
session of this stage is shown below.
n0=2147483648
n0?
2147483648
2> n1=n0
n1?
2147483648
You should plan carefully, rather than just leaping in and starting to edit the skeleton code. Then, before
moving through the rest of the stages, you should test your program thoroughly to make sure its correctness.
3.3 Stage 3 - Adding up Two Huge Integers (Up to 13 Marks)
Now add code to the add function to enable adding up two huge integers. Note that the ‘+’ operator always
starts with a variable, e.g., \n0”, followed by ‘+’ itself. After that there are two cases: an integer constant
or another variable. Your code should be able to handle both cases: adding up a variable with a constant
value, and adding up two variables (both could be the same variable). The sum should always be stored in
the starting variable of the command. A sample execution session of this stage is shown below.
n0=2147483648
n0+1
n0?
2147483649
n0+2
n0?
2147483651
n1=10000000000000000000000000000000000000000
n1+n0
n1?
10000000000000000000000000000002147483651
n0?
2147483651
Note that we assume integers with up to 100 digits. They can still overflow if we are adding up even larger
numbers. In this case, we will simply ignore the overflowed digits and keep the remaining part in the sum.
You should also remove any leading 0’s in the sum, i.e., your program should not produce
numbers like 0001 (which should be 1).
3.4 Stage 4 - Multiplying Two Huge Integers (Up to 15 Marks)
The next operator to add is ‘*’. You will need to modify the multiply function for this stage. When this
stage is done, your program should be able to process the following sample input.
n0=123
n1=4567
n0*n1
n0?
561741
n1?
4567
n2=100000000000000000000000000000000000000000000000000
n2*100000000000000000000000000000000000000000000000000
n2?
0
When there is an overflow, we follow the same procedure as in addition. That is, to keep the non-overflowed
part only. You should also remove any leading 0’s in the product.
You need to think carefully about the algorithm to use in this task. You may implement the process to do
long multiplication that you learned at school. Some supporting functions may be needed. For example,
a function multiply_digit that takes a huge_t variable and multiplies it by a single-digit integer may be
useful; as might a function multiply_base that multiplies a huge_t variable by 10.
There are also other (more efficient) ways to do multiplication, and you can be creative if you wish. Just
do not try and do it by repeated addition (penalties apply).
33.5 Stage 5 - Supporting the \Power of” Operator (Up to 15 Marks)
This stage is for a challenge. Please do not start on this stage until your program through to
Stage 4 is (in your opinion) perfect, and is submitted, and is verified, and is backed up.
For a challenge, implement the power function for the \power of” operator ‘^’ (no need to consider 00):
n0=2
n0^4
n0?
16
n1=2
n1^n0
n1?
65536
Hint: You may use repeated multiplications for this stage.
If you are successful in this stage, you will be able to earn back 1 mark you lost in the earlier stages (assuming
that you lost some). Your total mark will not exceed 15.

需要源码,请联系QQ:1097228821,备注:CSDN

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: COBOL中,COMP和COMP-3都是数据类型,但它们有一些区别。 COMP是一种二进制数据类型,它占用一个字(4个字节)的存储空间。它可以存储整数、小数和浮点数等数据类型。但是,它不能存储带有小数点的数值。 COMP-3是一种压缩的二进制数据类型,它占用不定长的存储空间。它可以存储整数、小数和浮点数等数据类型,并且可以存储带有小数点的数值。但是,它的存储方式比较特殊,需要进行压缩和解压缩操作,所以在处理时需要特别注意。 因此,如果需要存储带有小数点的数值,建议使用COMP-3类型。如果只需要存储整数、小数和浮点数等数据类型,可以使用COMP类型。 ### 回答2: COBOL(通用商务语言,英文全称:Common Business-Oriented Language)是一种广泛用于企业应用的编程语言。在COBOL中,主要有两种数据类型:二进制和十进制。 在二进制数据类型中,COBOL中有两个最主要的数据类型── COMP和COMP-3。这两个数据类型的区别可以从编程语言的使用和存储方面来阐述。 首先,COMP(有时称为COMP-1)是一种二进制数据类型,它存储的是32位或64位的二进制数。它最初是用来存储小数的数值,但它还可以用于存储整数。在运算时,二进制数被转换为十进制数,在计算结束时再转换回二进制数。这种数据类型通常用于存储非常大的数字,因为二进制位数越多,数值就越大。如果您的应用程序需要处理非常大的数字,那么COMP类型是非常有用的。 其次,COMP-3(也称为Packed-Decimal)是一种可压缩的二进制数据类型,它可以存储数字和数值。它被广泛用于金融应用程序中,因为它可以存储小数点位置并且可以通过使用压缩算法来减少存储空间。该算法通过将数字加上13(十进制),然后将结果除以10,并将余数加入表示数字长度的字段中,来将数字压缩成不同的字节数。例如,一个俩位的数字在COMP-3中只需要1个字节,而一个四位数字需要2个字节,并且这个数字只能使用10进制表示。 总之,COBOL中的COMP和COMP-3数据类型的区别在于它们存储数据的方式,以及数据类型的存储大小。COMP主要用于存储非常大的数字,而COMP-3则用于存储金融应用程序中的数字和数值,通过压缩算法来节约存储空间。需要根据具体的应用场景来选择使用哪种数据类型。 ### 回答3: COBOL是一种面向商业应用的高级编程语言。在使用COBOL时,我们常会听到两个概念:comp和comp-3。这两者有什么区别?下面就来一一讲解: 1. comp(二进制) comp是COBOL中的一种数据类型,它表示二进制数。comp所占据的存储空间是由所存储数值的位数决定的,例如:pic S9(9) comp 表示一个9位带符号的二进制数。 使用comp类型变量的好处是它占用的存储空间少,节省了系统资源。它也更适合于整数计算,被广泛用于所有的计算机中(从小到大,64位计算机除外)。 2. comp-3(压缩型二进制) COBOL语言中,comp-3也是一种数据类型,它表示一种压缩型二进制数。它将数字压缩成BCD码表现,占据的存储空间比comp要少得多。 同时,它也更适用于金额计算或者其他金融计算。尤其是在大型银行和金融机构中,comp-3被广泛使用。 总结: - comp是一个二进制数据类型,它占用比较小的存储空间,适用于整数计算。 - comp-3是一种压缩型二进制数据类型,它将数字压缩成BCD码表现,占据的存储空间比comp要少得多,适用于金额等金融计算。 根据实际需求来选择不同的存储方式是非常重要的,能够提高应用程序的执行效率和节省系统资源。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值