-
Notifications
You must be signed in to change notification settings - Fork 1
Create F2_16.c #16
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
base: main
Are you sure you want to change the base?
Create F2_16.c #16
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,80 @@ | ||
| void intToString(int num, char result[], int* pos) { | ||
| if (num == 0) { | ||
| result[(*pos)++] = '0'; | ||
| return; | ||
| } | ||
|
|
||
| int digits[10]; | ||
| int count = 0; | ||
| if (num < 0) { | ||
| result[(*pos)++] = '-'; | ||
| num = -num; | ||
| } | ||
| while (num > 0) { | ||
| digits[count++] = num % 10; | ||
| num /= 10; | ||
| } | ||
| for (int i = count - 1; i >= 0; i--) { | ||
| result[(*pos)++] = '0' + digits[i]; | ||
| } | ||
| } | ||
|
|
||
| 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; | ||
|
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 a check to make sure that the commonDenominator not zero by using the if-else conditional statement. |
||
| numeratorSum %= commonDenominator; | ||
|
|
||
| int pos = 0; | ||
| intToString(characteristicSum, result, &pos); | ||
|
|
||
| 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; | ||
|
Comment on lines
+28
to
+49
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 move this piece of code to the new function to avoid duplication. |
||
| } | ||
|
|
||
| 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; | ||
|
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 a check to make sure that the commonDenominator not zero by using the if-else conditional statement. |
||
| numeratorDiff %= commonDenominator; | ||
|
|
||
| int pos = 0; | ||
| intToString(characteristicDiff, result, &pos); | ||
|
|
||
| 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; | ||
|
Comment on lines
+58
to
+79
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 replace this piece of code with the newly created function ( which we created for removing the duplicate code in the add function) |
||
| } | ||
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.
Can we add the comments for the code for better readability and the maintainability.