减去两个长正整数数组中的C ++ [英] Subtracting two long positive integers in an array c++
问题描述
我是比较新的编程:)。
假设我想创建一个程序,提示用户输入两个正数高达50位长,然后减去从第一号,第二号。
的例如:的
用户输入的第一个正数: 239834095803945862440385983452184985298358
第二个数字: 939542309853120721934217021372984729812
=============================================== ============================
程序输出的区别: 238894553494092741718901766430812000568564
或如果负: -29837430045
=============================================== ============================
的号中的每一个数字将被存储为在阵列的单个元素。这里是我如何目前正在在用户输入:
INT read_array(INT INT_ARRAY [],诠释MAX_SIZE){
字符数;
诠释计数= 0;
//设置所有的数组项为0。
的for(int i = 0; I< MAX_SIZE;我++){
INT_ARRAY [I] = 0;
} 做{//处理每个单独的字符在的IStream
cin.get(数);
//投入到阵列上的字符,直到它击中
//数端(行尾)
如果((数=的'\\ n')及!及(计数&下; MAX_SIZE)及及(ISDIGIT(数))){
INT_ARRAY [计数] = INT(数字) - INT('0');
}
算上++; //增量计数
}而(数量='\\ n'!); //测试,如果数目太大
INT digitcount =计数 - 1;
如果(digitcount> MAX_SIZE){
COUT<< ENDL<< 错误:数超过50个数字! << ENDL;
返回0;
}
问题:
如何做减法是躲避我。我一直在试图解决这个问题的两个星期,这是我已经错过了最有可能的一些小事。
我曾尝试:
- 转换元素的数组回成一个整体INT
- 写我自己的程序做对数长减法
等等......
但是,输出是唯一成功的,直到在一定的位数和/或如果它们是正/负号。我很为难,我不知道最好的办法是减去两个正数阵列来获得成功的输出如图所示的例子,可以为正数和负数适应什么。任何帮助非常AP preCIATED。)
编辑:我尝试:
的#includelong_sub.h
#包括LT&;&sstream GT;
#包括LT&;矢量>使用命名空间std;INT long_sub(int类型的[],INT B〔],const int的大小){
stringstream的SS;
INT常量大小= 50;
INT C = 0; //借用号
INT借= 1; //1,获取抬到借来的数
INT R [规格]; 的for(int i = 0; I<大小;我++){
ř[I] = 0;
}
//初始化数组答案为0。
的for(int i =大小 - 1; I> = 0;我 - ){
//处理零
如果(一个[1] - ; B [1] - 放大器;&放大器;一个由[i]){
//从下一页单位借,并追加到一个。
SS<<借<<一个由[i];
SS>> C;
ss.clear(); //清除了下一个潜在借stringstream的。 INT临时= C - B [I]
- [R [i] = ABS(TEMP);
}其他{
INT临时=一个[Ⅰ] - B [I];
- [R [i] = ABS(TEMP);
}
} 的for(int i = 0; I< =大小 - 1;我++){
COUT<< - [R [I]
}
COUT<< ENDL;
返回ř[规格];
}
所以,解决的办法是pretty大致相同,当你用手做的。
如果我们有:
4321
-1234
您会采取在两个号码的最后一个数字,减去上面的那个如下的 1 - 4
- 这当然意味着你必须从借钱下一个数字,所以我们remmeber了,然后拿出7.现在,采取下一个数字[和记借],并减去3从2-1 = 8。
同样的事情是你如何在电脑做减法大数 - 做一次一位的,如果你借用,那么你需要采取与你的下一个步骤。
I'm relatively new to programming :).
Suppose I wanted to create a program which prompts a user to enter two positive numbers up to 50 digits long, which then subtracts the second number from the first number.
For example:
The user enters the first positive number: 239834095803945862440385983452184985298358
second number: 939542309853120721934217021372984729812
===========================================================================
Program outputs the difference: 238894553494092741718901766430812000568564
OR, if negative: -29837430045
===========================================================================
Each digit of the numbers will be stored as individual elements in an array. Here is how I am currently taking in the user input:
int read_array(int int_array[], int MAX_SIZE) {
char number;
int count = 0;
//set all array entries to 0.
for (int i = 0; i < MAX_SIZE; i++){
int_array[i] = 0;
}
do { //processes each individual char in the istream
cin.get(number);
// puts char on to the array until it hits the
// end of the number (end of the line)
if(( number != '\n') && (count < MAX_SIZE) && (isdigit(number))) {
int_array[count] = int(number) - int('0');
}
count++; //increments count
} while (number != '\n');
//tests if number is too large
int digitcount = count - 1;
if (digitcount > MAX_SIZE) {
cout << endl << "ERROR: The number is above 50 digits!" << endl;
return 0;
}
THE PROBLEM:
HOW to do the subtraction is eluding me.I have been trying to solve this problem for two weeks and it's most likely something trivial I have missed.
I have tried:
- Converting array of elements back into one whole int
- Writing my own program to do long subtraction on the numbers
etc...
However, the output is ONLY successful up until a certain number of digits and/or if they're positive/negative numbers. I'm stumped and I'm not sure what the best way is to subtract two positive number arrays to get a successful output that can accommodate for positive and negative numbers as shown in the example. ANY HELP GREATLY APPRECIATED :).
EDIT: my attempts:
#include "long_sub.h"
#include <sstream>
#include <vector>
using namespace std;
int long_sub(int a[], int b[], const int size) {
stringstream ss;
int const sizes = 50;
int c = 0; //borrow number
int borrow = 1; // the '1' that gets carried to the borrowed number
int r[sizes];
for (int i = 0; i < size; i++) {
r[i] = 0;
}
//initialise answer array to 0.
for (int i = size - 1; i >= 0; i--) {
//handles zeros
if (a[i] < b[i] && a[i]) {
//takes the borrow from the next unit and appends to a.
ss << borrow << a[i];
ss >> c;
ss.clear(); // clears stringstream for next potential borrow.
int temp = c - b[i];
r[i] = abs(temp);
} else {
int temp = a[i] - b[i];
r[i] = abs(temp);
}
}
for (int i = 0; i <= size - 1; i++ ) {
cout << r[i];
}
cout << endl;
return r[sizes];
}
So the solution to this is pretty much the same as when you do it by hand.
If we have:
4321
-1234
You'd take the last digits in the two numbers, subtract the one below from the one above, 1 - 4
- which of course means you have to borrow from the next digit up, so we remmeber that, and then come up with 7. Now, take the next digit [and remember the "borrow"], and subtract 3 from 2-1 = 8.
The exact same thing is how you do subtraction on large numbers in computer - do one bit at a time, and if you "borrow", then you need to take that with you to the next step.
这篇关于减去两个长正整数数组中的C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!