0 like 0 dislike
1.7k views

You are requested to help in writing a program to perform addition of two large number up to 50-digits. In C there is no builtin datatype for large number, but we can use characters and array to solve the problem.

Input: The input will contain two positive integers are separated by a blank, the two positive integers do not exceed 50-digits.

Output: Sum.

寫一個程式輸入兩個大數(最大50個位數),計算其總合。C語言不提供大數資料型態,但我們可以用字元和陣列來解決這個問題。

Example input:

999999999999999999999999999999 999999999999999999999999999999

Example output:

1999999999999999999999999999998

 

[Exam] asked in Final Exam
ID: 43460 - Available when: 2018-01-20 09:00 - Due to: Unlimited
| 1.7k views

4 Answers

0 like 0 dislike
Hidden content!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char input[500],s1[500],s2[500],temp;

int i,space,sum[500],carry,temp2,flag=0;

int main(){
* ** *** * ** *** *** * ** ** * %s",&s1,&s2);
* ** * **** ** * ** * * * * * ** **

///get space
***** ****** * ***** * *** * **** ********* *
* * * ** ** ** ** *** * ** * * * ** **** ** ** *** ** '){space=i;}
    }

///get s1
**** * * * * **** * ';i++){
* ****** * ** * ** ** * *** ** **** ****** *** **
    }

* * ** *** * * **** **** ** *
///reverse input
** * * *** ** * * * * *** * * * *
** *** ** * ** *** **** ** * **** * ** *** *
* **** *** * ***** * ** *** * * * **** ** ** * * ****** * * **
*** * * * ** * * *** * **** * * * * *** * * * *
    }

///get reversed s2
* * * **** **** * ** **** * ** ';i++){
* *** ** *** ***** *** **** * * * ** * * ** *
    }

///reverse s1

**** * *** ********** * *** * ** ***
* ** **** ** ** * ********* ** ** *** ** * **

** * * * ***** * ** **** *** * ** * **** * *
** *** ***** **** * *** ** ** *** * * ****** * * ******* ***
    }

///sum s1 and s2
** *** * * * * * *** *
* * ** ** **** * ****** ** * **
* ** * *** * * * * **** * ** ** **** * ***** * * * * * *
* * ** * * ** * ** * * ** *** * ************ * ***** **** * * ** *
** * *** * *** *** * *** ** ** *** * * * ** * ** * ** *
* * * * * * ** *** * * * * ** * ******** * * **
** ** ** **** *** * ** * * ** ** * ** *** * ***** * * *

    }

* * * * * ** * * * *** * **** *** rev:%s\n",input);
*** * * * *** * **** *** * *** rev:%s\n",s1);
* **** * * ** *** ** **** ** * * ** rev:%s\n",s2);
** * *** * *** * * * ** * ** * * ** ** * * * *** * ** *

///reverse output
*** * **** ****** ** * * ** ***
*** * ** **** * ** * ** * * * * * * *** ***

****** * * * * ** * **** * *** * ** * * ** *** * ******
** * *** *** * * **** *** *** * * ** **** ****** *
    }

///print
** * ***** ***** * * ** * ** * * ** * *
* *** * * *** ** *** ** *** * * * * ** * * * ******** * **
* ** * * ** * **** * *** *** * * ** ** ** ** * * ** ** *** * ****** * ***
*** * ** **** ** * * ** ** ** * * * ** ** ** ** * * * ** * **** * * ***** **** * ***** * ** ** **
* ********* *** *** * ** * *** ** * * *** * ** ***** * * ****** *** * * ** ** * *
** ** **** *** * ** * ** ** * ** *** ** ** *** * * ** ****

** *** * ** * * *** *** * ** * * * *

    }
* ** * ** * ** ** * *** * *
    return 0;
}
answered by (-286 points)
edited by
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
0 0
prog.c: In function 'main':
prog.c:80:12: warning: multi-character character constant [-Wmultichar]
     printf('10');
            ^~~~
prog.c:80:12: warning: passing argument 1 of 'printf' makes pointer from integer without a cast [-Wint-conversion]
In file included from prog.c:1:0:
/usr/include/stdio.h:364:12: note: expected 'const char * restrict' but argument is of type 'int'
 extern int printf (const char *__restrict __format, ...);
            ^~~~~~
prog.c:80:5: warning: format not a string literal and no format arguments [-Wformat-security]
     printf('10');
     ^~~~~~
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
0 0
Case 0: Wrong output
Case 1: Wrong output
Case 2: Wrong output
0 like 0 dislike
Hidden content!
#include<stdio.h>
#include<string.h>
int main()
{
** * *** **** * * * string1[50];
* * **** ** * ** * string2[50];

** * * * * *** * **** ***** * * *** string1);
** ** ** * *** * ** * ****** *** *** * ** * * * string2);
* ******* **** * * *** arr[51]={0},i;

*** * ** * ***** ** i<strlen(string1); i++)
* * ** **** *** **** ** * *
* * * * **** * ** ** * ** ** ** * * *** += string1[strlen(string1) - 1 - i] - 48;
    }

** ** * ** * **** ****** i<strlen(string2); i++)
* ***** * *
* ** * *** ***** ** * * * * * * * ** * * * += string2[strlen(string2) - 1 - i] - 48;
* ***** *** * ****

* *** * *** * ****** * i<50; i++)
* * * * * * * * ** *
* ***** * * * ** * * * *** ** ** * *** ** ** > 9)
*** * * * * * ** * ******** * ** *** *
* * * * *** ****** ** * * * * * **** * * ** * * * * * * ** * * -= 10;
** * ** ** * ** * **** * ** * * * ** * * * * * ** *** **** * ** * *
* * **** * ** **** **** * * ** * ** ** * **
** * ****** ** ** * **

** * ***** * * * i>=0; i--)
* **** * * ** * * *
* * ** * * **** * **** ** * *** *** **** ** * * ** ** != 0)
* ** * * ** * **** **** ** * * * *** * ***** ** * * ***** *** * *
* * * * ** * *

* * *** * * * * * * * i--)
* ***** ** ** * * **** ** ** * ** * *** ****** * ** * * * arr[i]);
}
answered by (-168 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
0 like 0 dislike
Hidden content!
#include <stdio.h>
int main (){
int s1,s2,l1,l2;
int num1[50],num2[50],sum[50];
printf("enter input 1: ");
* ** **** * ** ** * **
printf("enter input 2: ");
** * * ** * * *** *
for(l1=s1[l1]!='\0')
* * * ** * ** ** *
for(l2=s2[l2]!='\0')
* * * * ** * ** *** * **
int carry=0;
int k=0;
int i=l1-1;
int j=l2-1;
*** ** * * ** * ** * * *
{
**** * ** ** ***** * * * **
*** ** * * ** ** * *** *
**** ** * ** * ** *** ** ** ** *** * * ** *** *
****** * * * **** *** ** * *** ** ** * * *** *** *** * **** * * * ** * **** * *
** * * ** ** ** * ***** * *** *** * ** *** * ** ** * * *** **** *** *** ** *
*** * * ** * *** * if (j>=0)
** * * ***** ** ***** ** ** * * * ***
*** * ********** ** ** ** ** **** ** *** *** * *** * * *** ** * * * * * *
*** * * * ** * * * ** * * ****** * * ***** * ** ***** ** * ****** ******* * * * * **
* * * ** ** * *** **** ** **** *** ** if(carr>0)
* *** * * *** ** * * * *** * ****
* ***** * * ** ** ** * ** * * * ** * * ****** * ");
* * * ** * ** * ** * ** * *** * ** * ** *
}
for(k--;k>=0;k--)
* * ****** ** ** * **
* * * * ** * * *** ** *** * **
*** * * * * * ** *
  return 0;
}
answered by (16 points)
0 0
prog.c: In function 'main':
prog.c:9:10: error: subscripted value is neither array nor pointer nor vector
 for(l1=s1[l1]!='\0')
          ^
prog.c:9:20: error: expected ';' before ')' token
 for(l1=s1[l1]!='\0')
                    ^
prog.c:9:20: error: expected expression before ')' token
prog.c:10:10: error: subscripted value is neither array nor pointer nor vector
   num1=s1[l1]-'0';
          ^
prog.c:11:10: error: subscripted value is neither array nor pointer nor vector
 for(l2=s2[l2]!='\0')
          ^
prog.c:11:20: error: expected ';' before ')' token
 for(l2=s2[l2]!='\0')
                    ^
prog.c:11:20: error: expected expression before ')' token
prog.c:12:12: error: subscripted value is neither array nor pointer nor vector
     num2=s2[l2]-'0';
            ^
prog.c:17:15: error: expected ')' before ';' token
 for(;i>=0;j>=0;i--;j--;k++)
               ^
prog.c:23:13: error: expected ';' before 'carry'
             carry=(num1[i--]+num2[j]+carry)/10;
             ^~~~~
prog.c:27:13: error: expected ';' before 'carry'
             carry=(num1[j--]+num2[j]+carry)/10;
             ^~~~~
prog.c:28:18: error: 'carr' undeclared (first use in this function)
         }else if(carr>0)
                  ^~~~
prog.c:28:18: note: each undeclared identifier is reported only once for each function it appears in
0 like 0 dislike
Hidden content!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main()
{
** ** * *** * * * a[50],b[50];
**** * * ** ** * * ** * **** c[51]={0};
* ***** *** * * * ** *** i;
* * ** ** * **** * * * * ** x=0;

**** **** ** *** **** * %s",a,b);

** ***** ** **** ** * *** ***
    {
**** *** * * ****** * ** **** * ***** * *
    }

* * *** ** *** *** ** *** ** ***
* *** *** * *
* ****** * * *** *** *** * * ** ** * * * * * **
* **** * *** * *** ***


** * * * * **** * * *** * * * **
** ***** * * * ** **
* *** ***** * **** * ****** ***** **** * *
* * *** ** ** * ** ** ** * ** * ******
*** ** ** *** ****** * ** ** ** * ***** * *** * * ** * ******** *
** * *** * ** ** * *** ** **** ** * ** * ** * * * * * * *** * * * * **** **
** * ** * * *** * * ** * * ***** **
    }

* * ***** *** ** ** * * *** *** *** * * *
*** ***** ** * ***
*** * * ***** * *** *** * ** * ** * ***** ** * ** != 0)
* * * **** *** * * **** * * * ** * * * **
* * * * * * ** * * ** *** * * ** * ** ** ****** *** ** ****** * * * ** * ** *
* * *** * ** ** * * * *** * ** ** * *** ** **** * ** * ***
** ** * * * ***** * ** * * * ** * *
    }

** *** *** * * * * ** ** **** *
    {
***** * ***** * * **** ** * * * * ** ** * * ** * * ***** **
    }


* **** ** * * ** * ** **** * * 0;
}
answered by (-193 points)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:172.70.179.79
©2016-2025

Related questions

0 like 0 dislike
13 answers
[Exam] asked Jan 19, 2018 in Final Exam
ID: 43463 - Available when: 2018-01-20 09:00 - Due to: Unlimited
| 2.8k views
0 like 0 dislike
6 answers
[Exam] asked Jan 19, 2018 in Final Exam
ID: 43462 - Available when: 2018-01-20 09:00 - Due to: Unlimited
| 1.4k views
0 like 0 dislike
8 answers
[Exam] asked Jan 19, 2018 in Final Exam
ID: 43461 - Available when: 2018-01-20 09:00 - Due to: Unlimited
| 1.7k views
0 like 0 dislike
12 answers
[Exam] asked Jan 19, 2018 in Final Exam
ID: 43459 - Available when: 2018-01-20 09:00 - Due to: Unlimited
| 2.8k views
1 like 0 dislike
37 answers
[Exam] asked Jan 16, 2018 in Final Exam
ID: 42299 - Available when: 2018-01-17 14:00 - Due to: Unlimited
| 8.5k views
12,783 questions
183,442 answers
172,219 comments
4,824 users