-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create F2_1.c #1
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
int add(int c1, int n1, int d1, int c2, int n2, int d2, char result[], int len) { | ||
int commonDenominator = d1 * d2; | ||
int numeratorSum = n1 * d2 + n2 * d1; | ||
int characteristicSum = c1 + c2 + numeratorSum / commonDenominator; | ||
numeratorSum %= commonDenominator; | ||
|
||
int pos = 0; | ||
int temp = characteristicSum; | ||
|
||
if (temp < 0) { | ||
result[pos++] = '-'; | ||
temp = -temp; | ||
} | ||
|
||
if (temp == 0) { | ||
result[pos++] = '0'; | ||
} else { | ||
int divisor = 1; | ||
while (temp / divisor >= 10) { | ||
divisor *= 10; | ||
} | ||
while (divisor > 0) { | ||
result[pos++] = '0' + temp / divisor; | ||
temp %= divisor; | ||
divisor /= 10; | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add comments for better readability and understanding of the logical implementation? |
||
result[pos++] = '.'; | ||
if (numeratorSum == 0) { | ||
result[pos++] = '0'; | ||
} else { | ||
int divisor = commonDenominator / 10; | ||
while (divisor > 0 && numeratorSum / divisor == 0) { | ||
result[pos++] = '0'; | ||
divisor /= 10; | ||
} | ||
while (divisor > 0) { | ||
result[pos++] = '0' + numeratorSum / divisor; | ||
numeratorSum %= divisor; | ||
divisor /= 10; | ||
} | ||
} | ||
|
||
result[pos] = '\0'; | ||
return 1; // return true | ||
} | ||
|
||
int subtract(int c1, int n1, int d1, int c2, int n2, int d2, char result[], int len) { | ||
int commonDenominator = d1 * d2; | ||
int numeratorDiff = n1 * d2 - n2 * d1; | ||
int characteristicDiff = c1 - c2 + numeratorDiff / commonDenominator; | ||
numeratorDiff %= commonDenominator; | ||
|
||
int pos = 0; | ||
int temp = characteristicDiff; | ||
|
||
if (temp < 0) { | ||
result[pos++] = '-'; | ||
temp = -temp; | ||
} | ||
|
||
if (temp == 0) { | ||
result[pos++] = '0'; | ||
} else { | ||
int divisor = 1; | ||
while (temp / divisor >= 10) { | ||
divisor *= 10; | ||
} | ||
while (divisor > 0) { | ||
result[pos++] = '0' + temp / divisor; | ||
temp %= divisor; | ||
divisor /= 10; | ||
} | ||
} | ||
|
||
result[pos++] = '.'; | ||
if (numeratorDiff == 0) { | ||
result[pos++] = '0'; | ||
} else { | ||
int divisor = commonDenominator / 10; | ||
while (divisor > 0 && numeratorDiff / divisor == 0) { | ||
result[pos++] = '0'; | ||
divisor /= 10; | ||
} | ||
while (divisor > 0) { | ||
result[pos++] = '0' + numeratorDiff / divisor; | ||
numeratorDiff %= divisor; | ||
divisor /= 10; | ||
} | ||
} | ||
|
||
result[pos] = '\0'; | ||
return 1; // return true | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In both add and substract functions, we have a repeated code for the storing the answer in the result array. Can we move this repeated code into a separate function to avoid the redundancy? Both functions return 1 accordingly, but can we use bool data type which would add more semantic meaning to the code? In the given code, there is no way where the both functions could return false according to the implementation. Can we return false while validating the inputs? The length of result array is fixed to 100. But if the integer is very large? Can we avoid these buffer overflows by allocating the memory to the result array dynamically? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if the any of values(d1, d2) is 0? If it is so, then there would be division by zero runtime error. Can we validate the inputs before proceeding with them?