Answer to Question #55621 in C++ for harry

Question #55621
Write functions for doing
arithmetic on 100-digit integers. Include functions for
input, output, and addition. The input function should not
print anything. The addition function should do no
reading or printing. No function should make any
assumptions about how the other functions or main
program work. The number of digits should be in a global
constant. The function prototypes should be as follows:
void readBig(int[])
void addBig(int[],int[],int[])
void printBig(int[])
1
Expert's answer
2015-10-22T02:31:21-0400
#include <fstream>
#include <string>
using namespace std;

ifstream in("INPUT.TXT");
ofstream out("OUTPUT.TXT");

const int MAXSIZE=102;

void readlong(int *a)
{
int i;
string s;
in>>s;
a[0]=s.length();

for (int i=1; i<=a[0]; ++i)
a[a[0]-i+1]=s[i-1]-48;
}

void writelong(int *a)
{
for (int i=a[0]; i>=1; i--)
out<<a[i];
}

int complong(int *a, int *b){
if (a[0] < b[0]) return -1;
if (a[0] > b[0]) return 1;
for (int i=a[0]; i<1; ++i)
{
if(a[i] < b[i]) return -1;
if(a[i] > b[i]) return 1;
}
return 0;
}

void sumlong(int *a, int *b, int *wheresave)
{
int m=0, minimal=0;
int c=0;

m = (a[0] > b[0]) ? a[0] : b[0];
minimal=(a[0] < b[0]) ? a[0] : b[0];

for (int i=1; i<=m+1; i++)
{
if(complong(a,b)!=-1)
wheresave[i]=a[i];
else
wheresave[i]=b[i];
}

for (int i=1; i<=minimal; i++)
{
c = a[i]+b[i]+c; //ïîëó÷àåì ñóììó
wheresave[i] = c%10; //îñòàâëÿåì â òåêóùåì ðàçðÿäå îñòàòîê
c = c/10; //öåëóþ ÷àñòü ïåðåíîñèì â ñëåäóþùèé ðàçðÿä
}
if(c>0) //åñëè öåëûé îñòàòîê ïîñëå ñëîæåíèÿ áîëüøå íóëÿ, òî
{

for (int i=minimal+1; i<=m; i++)
{
c = c+wheresave[i];
wheresave[i] = c%10;
c = c/10;
}
}
if(c>0)
{
m++;
wheresave[m] = c;
wheresave[0]=m;
}
else wheresave[0]=m;
}

int main()
{

int a[MAXSIZE], b[MAXSIZE], ok[MAXSIZE+5];

readlong(a);
readlong(b);

sumlong(a,b, ok);

writelong(ok);
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS