In: Computer Science
can you convert this cod from C++ to C language
#include<bits/stdc++.h>
using namespace std;
// Function for finding sum of two large numbers
string addThese(string a,string b){
// Before proceeding further, make sure length
// of a is larger.
if(a.size() < b.size())
swap(a,b);
int j = a.size() - 1;
// Traverse from end of both strings and add (b[i] - '0') to
a[j].
for(int i = b.size() - 1; i >= 0; i--, j--)
//(b[i] - '0') is nothing but numerical value of string b
char
a[j] += (b[i] - '0'); // if(b[i] ascii value is 49 then we subtract
ascii value of '0'== 48 from it.
// be store all the values to a string now check ascii value of
a[i]
for(int i = a.size()-1; i > 0; i--) {
if (a[i] > '9') { // if a[i] is greater than ascii value of '9'
means value must be greater than 9
int d = a[i] - '0'; // calculate value of perticular digit by
subtracting ascii value of a[i]-'9'
a[i-1] = ((a[i-1] - '0') + d/10) + '0'; // carry will be added to
previous char values
a[i] = (d%10) + '0'; //current a[i] chang to (d%10) + '0'. if d is
11 than a[i] is 1 and carray be 1 added tp previous char.
}
}
if(a[0] > '9') // if first char ascii value is grater than ascii
value of '9' then ther is a another char has to be added
{
string c; // carry of the first digits
c = a[0];// first store c as a[0]
a[0] = ((a[0]-'0')%10)+'0'; // now change a[0] as modulo 10 of the
value
c[0] = ((c[0]-'0')/10)+'0'; // and chnge c to carry of a[0]
a = c + a; // now concatinate string c with string a
}
return a;
}
int main(){
ifstream in ("input.in");
if (in.is_open())
{
string a,b;
int count=0;
while(in>>a>>b)
cout <<"sample "<<++count<<" output: "<<
addThese(a,b) <<"\n";
in.close();
}
else cout << "Unable to open file";
return 0;
}
Solution:
C code:
#include<stdio.h>
#include<string.h>
char* swap(char *a, char *b)
{
char temp[100];
strcpy(temp, a);
strcpy(a, b);
strcpy(b, temp);
}
// Function for finding sum of two large numbers
char* addThese( char a[], char b[]){
// Before proceeding further, make sure
length
// of a is larger.
if(strlen(a) < strlen(b))
swap(a,b);
int j = strlen(a) - 1;
// Traverse from end of both , char *s and add (b[i] -
'0') to a[j].
for(int i = strlen(b) - 1; i >= 0; i--, j--)
//(b[i] - '0') is nothing but
numerical value of , char * b char
a[j] += (b[i] - '0'); // if(b[i]
ascii value is 49 then we subtract ascii value of '0'== 48 from
it.
// be store all the values to a , char * now check
ascii value of a[i]
for(int i = strlen(a)-1; i > 0; i--) {
if (a[i] > '9') { // if a[i] is
greater than ascii value of '9' means value must be greater than
9
int d = a[i] -
'0'; // calculate value of perticular digit by subtracting ascii
value of a[i]-'9'
a[i-1] =
((a[i-1] - '0') + d/10) + '0'; // carry will be added to previous
char values
a[i] = (d%10) +
'0'; //current a[i] chang to (d%10) + '0'. if d is 11 than a[i] is
1 and carray be 1 added tp previous char.
}
}
if(a[0] > '9') // if first char ascii value is
grater than ascii value of '9' then ther is a another char has to
be added
{
char c[100]; // carry of the first
digits
strcpy(c, &a[0]);// first store
c as a[0]
a[0] = ((a[0]-'0')%10)+'0'; // now
change a[0] as modulo 10 of the value
c[0] = ((c[0]-'0')/10)+'0'; // and
chnge c to carry of a[0]
strcat(a, c); // now concatinate ,
char * c with string a
}
return a;
}
int main(){
FILE *fd = fopen("input.in", "rw");
if (fd != NULL)
{
char a[100],b[100];
int count=0;
//while(fd>>a>>b)
while(EOF)
printf("sample
%d, output: %s\n",++count, addThese(a,b));
fclose(fd);
}
else {
printf("Unable to open
file");
return 1;
}
return 0;
}
----------------------------------------------------------------------------------
Code screenshots:
-------------------------------------------------------------------------
Output:
------------------------------------------------------------------------