-
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_10.c #10
base: main
Are you sure you want to change the base?
Create F2_10.c #10
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,101 @@ | ||
int add(int c1, int n1, int d1, int c2, int n2, int d2, char result[], int len) { | ||
int commonDenominator = d1 * d2; | ||
int numerator1 = c1 * commonDenominator + n1 * d2; | ||
int numerator2 = c2 * commonDenominator + n2 * d1; | ||
int numeratorSum = numerator1 + numerator2; | ||
|
||
int pos = 0; | ||
|
||
if (numeratorSum < 0) { | ||
result[pos++] = '-'; | ||
numeratorSum = -numeratorSum; | ||
} | ||
|
||
int characteristicSum = numeratorSum / commonDenominator; | ||
int temp = characteristicSum; | ||
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++] = '.'; | ||
|
||
numeratorSum %= commonDenominator; | ||
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; | ||
} | ||
|
||
int subtract(int c1, int n1, int d1, int c2, int n2, int d2, char result[], int len) { | ||
int commonDenominator = d1 * d2; | ||
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 validation to handle the cases where denominators d1 or d2 are 0? which will help to prevent division by zero error. |
||
int numerator1 = c1 * commonDenominator + n1 * d2; | ||
int numerator2 = c2 * commonDenominator + n2 * d1; | ||
int numeratorDiff = numerator1 - numerator2; | ||
|
||
int pos = 0; | ||
|
||
if (numeratorDiff < 0) { | ||
result[pos++] = '-'; | ||
numeratorDiff = -numeratorDiff; | ||
} | ||
|
||
int characteristicDiff = numeratorDiff / commonDenominator; | ||
int temp = characteristicDiff; | ||
if (temp == 0) { | ||
result[pos++] = '0'; | ||
} else { | ||
int divisor = 1; | ||
while (temp / divisor >= 10) { | ||
divisor *= 10; | ||
} | ||
while (divisor > 0) { | ||
Comment on lines
+69
to
+74
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 calculate the digits first and then convert? which will help to minimize the number of operations. |
||
result[pos++] = '0' + temp / divisor; | ||
temp %= divisor; | ||
divisor /= 10; | ||
} | ||
} | ||
|
||
result[pos++] = '.'; | ||
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 check the length of the result array, to avoid overflow issue? can we check this throughout the program? |
||
|
||
numeratorDiff %= commonDenominator; | ||
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; | ||
} |
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 ensure that the length of the result array is checked before appending characters to avoid buffer overflow?
if (numeratorSum < 0) {
if (pos < len - 1) {
result[pos++] = '-';
} else {
return 0;
}
numeratorSum = -numeratorSum;
}