Large number plus subtraction C language contains negative numbers

Source: Internet
Author: User

#define _crt_secure_no_warnings
#include <stdio.h>
#define MAXSIZE 10
void Add (char a[], Char b[], char c[]);
void Sub (char a[], Char b[], char c[]);
int Get_len (char a[]);
BOOL Max_a (char a[], char b[]);
int main (void)
{
Char A[maxsize];
Char B[maxsize];
Char Sum[maxsize];
bool symbol = TRUE;
bool Maxa = true;
scanf ("%s", a);
scanf ("%s", b);
if (*a! = '-' &&*b! = '-')
{
Symbol = TRUE;
Add (A, B, sum);
}
else if (*a = = '-' &&*b = = '-')
{
Symbol = FALSE;
ADD (a+1, b+1, sum);
}
Else
{
if (*a = = '-')
{
Maxa = Max_a (a+1, b);
if (Maxa)
{
Symbol = FALSE;
Sub (a+1, B, sum);
}
Else
{
Symbol = TRUE;
Sub (b, a+1, sum);
}
}
Else
{
Maxa = Max_a (A, b+1);
if (maxa==true)
{
Symbol = TRUE;
Sub (A, B + 1,sum);
}
Else
{
Symbol = FALSE;
Sub (b + 1, a, sum);
}
}
}
if (!symbol)
printf ("-");
int slen = Get_len (sum);
while (Slen >=0)
{
printf ("%c", sum[slen--]);
}
return 0;
}
void Add (char a[], Char b[], char c[])
{
int alen = Get_len (a);
int blen = Get_len (b);
int clen = 0;
for (int i = 0; i < MAXSIZE; i++)
{
C[i] = ' 0 ';
}
while (Alen >= 0 && blen >= 0)
{
C[clen] = a[alen--] + b[blen--] + C[clen]-48-48;
if (c[clen]>57)
{
C[clen] = C[clen]-10;
++C[CLEN+1];
}
++clen;
}
while (Alen >= 0)
{
C[clen] = a[alen--] + C[clen]-48;
if (c[clen]>57)
{
C[clen] = C[clen]-10;
++c[clen + 1];
}
++clen;
}
while (Blen >= 0)
{
C[clen] = b[blen--] + C[clen]-48;
if (c[clen]>57)
{
C[clen] = C[clen]-10;
++c[clen + 1];
}
++clen;
}
if (C[clen]! = 48)
++clen;
C[clen] = ' + ';
}
void Sub (char a[], Char b[], char c[])
{
int alen = Get_len (a);
int blen = Get_len (b);
int clen = 0;
int symble = 0;
for (int i = 0; i < MAXSIZE; i++)
C[i] = ' 0 ';
while (Blen >= 0)
{
C[clen] = a[alen--]-b[blen--]-symble + 48;
if (C[clen] < 48)
{
C[clen] + = 10;
symble = 1;
}
Else
symble = 0;
++clen;
}
while (Alen >= 0)
{
C[clen] = a[alen--] + C[clen]-symble-48;
if (C[clen] < 48)
{
C[clen] = C[clen] + 10;
symble = 1;
}
Else
symble = 0;
++clen;
}
while (c[clen-1] = = ' 0 ')
--clen;
C[clen] = ' + ';
}
int Get_len (char a[])
{
for (int i = 0; i < MAXSIZE; i++)
{
if (a[i] = = ' + ')
return i-1;
}
}
BOOL Max_a (char a[], char b[])
{
int alen = Get_len (a);
int blen = Get_len (b);
if (Alen>blen)
return true;
else if (Alen < Blen)
return false;
Else
{
for (int i = 0; I <=alen; i++)
{
if (A[i] > B[i])
return true;
if (A[i] < b[i])
return false;
}
}
}

Large number plus subtraction C language contains negative numbers

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.