Fraction Addition and Subtraction

来源:互联网 发布:淘宝申请售后服务培训 编辑:程序博客网 时间:2024/06/08 06:11

Given a string representing an expression of fraction addition and subtraction, you need to return the calculation result in string format. The final result should be irreducible fraction. If your final result is an integer, say 2, you need to change it to the format of fraction that has denominator 1. So in this case, 2 should be converted to 2/1.

Example 1:

Input:"-1/2+1/2"Output: "0/1"

Example 2:

Input:"-1/2+1/2+1/3"Output: "1/3"

Example 3:

Input:"1/3-1/2"Output: "-1/6"

Example 4:

Input:"5/3+1/3"Output: "2/1"

Note:

  1. The input string only contains '0' to '9''/''+' and '-'. So does the output.
  2. Each fraction (input and output) has format ±numerator/denominator. If the first input fraction or the output is positive, then '+' will be omitted.
  3. The input only contains valid irreducible fractions, where the numerator and denominator of each fraction will always be in the range [1,10]. If the denominator is 1, it means this fraction is actually an integer in a fraction format defined above.
  4. The number of given fractions will be in the range [1,10].
  5. The numerator and denominator of the final result are guaranteed to be valid and in the range of 32-bit int.

// 求两个数的最小公约数// a 和 b 都为正数int com_divisor(int a, int b) {    int common_divisor = 1;        int half;    if (a < b) {        half = b / 2;    } else if (a > b){        half = a / 2;    } else {        return a;    }        int i;    for (i = 2; i <= half; ++i) {        if (a % i == 0 && b % i == 0) {            common_divisor = i;        }    }    return common_divisor;}char* fractionAddition(char* expression) {    int len = strlen(expression);        int nums = 0;    // 分子    int numerator[100];    // 分母    int denumerator[100];    // 游标    int cursor = 0;    // 这个数字是正的    int is_positive = -1;        // 先判断第一个数字的符号    if (expression[cursor] == '-') {        is_positive = 0;        ++cursor;    } else {        is_positive = 1;        if (cursor == '+'){            ++cursor;        }    }        // 临时变量    int temp;    while(cursor < len) {        // 先求分子        temp = 0;        while(expression[cursor] != '/'){            temp = temp * 10 + expression[cursor] - 48;            ++cursor;        }        if (!is_positive) {            temp = 0 - temp;        }        numerator[nums] = temp;                // 跳过 '/'        ++cursor;                // 求分母        temp = 0;        while(cursor < len && expression[cursor] != '+' && expression[cursor] != '-'){            temp = temp * 10 + expression[cursor] - 48;            ++cursor;        }        denumerator[nums++] = temp;                // 判断下一次的分数的符号        if (cursor < len) {            if (expression[cursor] == '+') {                is_positive = 1;            } else {                is_positive = 0;            }            ++cursor;        }    }            // 计算分数的值    int i;    int fz = numerator[0];    int fm = denumerator[0];    int common_divisor;    for (i = 1; i < nums; ++i) {        fz = fz * denumerator[i] + numerator[i] * fm;        fm = fm * denumerator[i];        common_divisor = com_divisor(abs(fz), abs(fm));        fz /= common_divisor;        fm /= common_divisor;    }        // 输出 最后的结果    char * res = (char *) malloc(sizeof(char) * 100);        if (fz == 0){        res[0] = '0';        res[1] = '/';        res[2] = '1';        res[3] = '\0';        return res;    }        i = 0;    // fz < 0    if (fz < 0) {        res[i++] = '-';        fz = abs(fz);    }        char t_arr[100];        int j = 0;    t_arr[j++] = fz % 10 + 48;    fz /= 10;    while(fz != 0) {        t_arr[j++] = fz % 10 + 48;        fz /= 10;    }          while(j > 0) {        res[i++] = t_arr[--j];     }         res[i++] = '/';        t_arr[j++] = fm % 10 + 48;    fm /= 10;    while(fm != 0) {        t_arr[j++] = fm % 10 + 48;        fm /= 10;    }    while(j > 0) {        res[i++] = t_arr[--j];     }        res[i] = '\0';    return res;    }

阅读全文
0 0