Hirdetés

Új hozzászólás Aktív témák

  • blaces

    tag

    Hello!

    Ismét egy feladathoz kéne segítség.
    A feladat két hosszú egész szám összeszorzása (olyannyira hosszú, hogy a long double-kbe se ment bele, ezért stringeztem) 3 napig tartott az összehegesztése...

    Feladat: Írj programot, amely két hosszú egész számot szoroz össze! Csak pozitív számok fordulnak elő a bemeneten, viszont ezek tetszőlegesen sok 0-val kezdődhetnek. Egy szám legfeljebb 100 számjegyből áll. A két szám két egymást követő sorban helyezkedik el. A program kimenetén nem lehetnek vezető nullák!
    Bemenet: Kimenet:
    2. eset:
    0000789675 19375465800
    24536
    3. eset:
    0000000000000000000000000000000000000000000000000000 0.
    35363567

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    #define MAX_NUMBER 200

    int toInt(char ch) {
    return (ch - '0');
    }

    char toChar(int num) {
    return (num + '0');
    }

    void eraseNewLine(char* str) {
    size_t len;

    len = strlen(str);
    if (len > 0 && str[len-1] == '\n') {
    str[len-1] = '\0';
    }
    }

    void eraseNulls(char* str) {
    unsigned int count = 0;
    size_t i = 0;
    size_t len;
    char c;

    len = strlen(str);
    c = str[0];

    while (c != '\0') {
    c = str[i];
    i++;
    if (c == '0') {
    count++;
    } else {
    break;
    }
    }

    if (count == len) {
    str[0] = '0';
    str[1] = '\0';
    } else if (count > 0) {
    memcpy(str, str+count, (len-count+1));
    }
    }

    void addNulls(int num, char* solution) {
    size_t len;
    unsigned int i;

    len = strlen(solution);
    if (num > 0) {
    for (i = 0; i < num; i++) {
    solution[len+i] = '0';
    }
    solution[i+len] = '\0';
    }
    }

    void changeStr(char* s1, char* s2) {
    char temp[2*MAX_NUMBER];
    size_t l1;
    size_t l2;

    l1 = strlen(s1);
    l2 = strlen(s2);

    memcpy(temp, s1, l1+1);
    memcpy(s1, s2, l2+1);
    memcpy(s2, temp, l1+1);
    }

    void multiplyWithOneChar(char x, char* y, char* solution) {
    unsigned int xv = toInt(x);
    unsigned int yi;
    unsigned int tempi;
    unsigned int carry = 0;
    char temp[2*MAX_NUMBER];
    unsigned int i;
    size_t len;

    len = strlen(y);
    for (i = 0; i < len; i++) {
    yi = toInt(y[len-i-1]);
    tempi = yi * xv + carry;
    if (tempi > 9) {
    carry = tempi / 10;
    yi = tempi % 10;
    } else {
    carry = 0;
    yi = tempi;
    }
    temp[i] = toChar(yi);
    }
    if (carry > 0) {
    temp[i] = toChar(carry);
    temp[i+1] = '\0';
    } else {
    temp[i] = '\0';
    }

    len = strlen(temp);
    for (i = 0; i < len; i++) {
    solution[i] = temp[len-i-1];
    }
    solution[i] = '\0';
    }

    void add(char* y, char* solution) { // solution = solution + y;
    unsigned int yi;
    unsigned int si;
    unsigned int tempi;
    unsigned int carry = 0;
    char temp[2*MAX_NUMBER];
    unsigned int i;
    size_t len;
    size_t lens;

    len = strlen(y);
    lens = strlen(solution);
    if (lens > len) {
    changeStr(y, solution);
    len = strlen(y);
    lens = strlen(solution);
    }
    for (i = 0; i < len; i++) {
    yi = toInt(y[len-i-1]);
    if (i < lens) {
    si = toInt(solution[lens-i-1]);
    } else {
    si = 0;
    }
    tempi = yi + si + carry;
    if (tempi > 9) {
    carry = tempi / 10;
    yi = tempi % 10;
    } else {
    carry = 0;
    yi = tempi;
    }
    temp[i] = toChar(yi);
    }
    if (carry > 0) {
    temp[i] = toChar(carry);
    temp[i+1] = '\0';
    } else {
    temp[i] = '\0';
    }

    len = strlen(temp);
    for (i = 0; i < len; i++) {
    solution[i] = temp[len-i-1];
    }
    solution[i] = '\0';
    }

    void multiply(char* x, char* y, char* solution) {
    size_t len;
    unsigned int i;
    char temp[2*MAX_NUMBER];

    eraseNewLine(x);
    eraseNewLine(y);
    eraseNulls(x);
    eraseNulls(y);
    solution[0] = '0';
    solution[1] = '\0';

    len = strlen(y);
    for (i = 0; i < len; i++) {
    multiplyWithOneChar(y[len-i-1], x, temp);
    addNulls(i, temp);
    add(temp, solution);
    }
    }

    int main() {
    char first[MAX_NUMBER];
    char second[MAX_NUMBER];
    char solution[2*MAX_NUMBER];

    fgets(first, MAX_NUMBER, stdin);
    fgets(second, MAX_NUMBER, stdin);
    multiply(first, second, solution);
    printf("%s\n", solution);

    return 0;
    }

    és a második esett az ahol, a második számjegyig végig adja ki a nullákat, az elsőre(ezt nem írtam fentebb) és másodikra jó!

Új hozzászólás Aktív témák