联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp

您当前位置:首页 >> C/C++编程C/C++编程

日期:2021-10-22 10:17

Homework #4

In this assignment you will implement an algorithm that uses stacks to add numbers of any size.

The largest magnitude of integers is limited. We are not able to add

18,274,364,583,929,273,748,525 and 8,129,498,165,026,350,236 because integer variables cannot

hold such large values, let alone their sum.

The problem can be solved if we treat these numbers as strings of numerals, store the numbers

corresponding to these numerals on two stacks, and then perform addition by popping from the

stacks. The pseudocode for this algorithm is as follows:

addLargeNumbers(number1, number2)

read the numerals of the first number and store them on one stack

read the numerals of the second number and store them on another stack

var result := 0

while at least one stack is not empty

pop a numeral from each nonempty stack and add them to result

push the unit part of addition onto a new stack called the result stack

store the carry part of the addition in result

push result onto the result stack if it is not zero

pop numbers from the result stack and display them

The following diagram shows an example of adding numbers 592 and 3,784:

CSE-40049

2

a) (6 point) Implement the addLargeNumbers function with the following prototype:

void addLargeNumbers(const char *pNum1, const char *pNum2);

This function should output the result of adding the two numbers passed in as strings. Here

is an example call to this function with the expected output:

/* Sample call to addLargeNumbers */

addLargeNumbers("592", "3784");

/* Expected output */

4376

b) (3 points) Implement a test program that demonstrates adding at least three pairs of large

numbers (numbers larger than can be represented by a long).

c) (1 point) Make sure your source code is well-commented, consistently formatted, uses no

magic numbers/values, follows programming best-practices, and is ANSI-compliant.

Turn in all source code, program output, diagrams, and answers to questions in a single

Word or PDF document.


版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp