• A+B Problem Plus and A-B Problem Plus and A*B Problem Plus


     

     

    //we have defined the necessary header files here for this problem.
    //If additional header files are needed in your program, please import here.
    
    #include <stdio.h>
    #include <string.h>
    
    #define LENGHT 1000
    
    void sumPlus(char *a, char *b);
    
    void reverse(char *a);
    
    int main() {
        int times;
        scanf("%d", &times);
        char operator[2][LENGHT] = {{'0'},
                                    {'0'}};
        for (int i = 0; i < times; ++i) {
            scanf("%s %s", operator[0], operator[1]);
            printf("Case %d:
    ", i + 1);
            printf("%s + %s = ", operator[0], operator[1]);
            sumPlus(operator[0], operator[1]);
            if (i != times - 1) {
                printf("
    ");
                printf("
    ");
            }
        }
        return 0;
    }
    
    void sumPlus(char *a, char *b) {
        int res[LENGHT + 1] = {0};
        reverse(a);
        reverse(b);
        int flag_a = 0, flag_b = 0, carry = 0;  //进位
        for (int i = 0; i < LENGHT; ++i) {
            int x = 0, y = 0;
            if (a[i] == '')
                flag_a = 1;
            if (b[i] == '')
                flag_b = 1;
            if (!flag_a) {
                x = a[i] - '0';
            }
            if (!flag_b) {
                y = b[i] - '0';
            }
            int temp = x + y + carry; //字符类型转int类型
            res[i] = temp % 10;
            carry = temp / 10;
        }
        int i = LENGHT;
        while (res[i] == 0) {
            i--;
        }
        while (i >= 0) {
            printf("%d", res[i]);
            i--;
        }
    }
    
    void reverse(char *a) {
        int size = strlen(a);
        char temp[LENGHT + 1] = {'0'};
        for (int i = size - 1, j = 0; i >= 0; --i, ++j) {
            temp[j] = a[i];
        }
        memcpy(a, temp, size);
    //    a[size] = '';
    }

    #include <stdio.h>
    #include <string.h>
    #define MAX_LEN 1001
    void removeZero(char *result, char *input)
    {
        int len = strlen(input);
        for(int i = 0; i < len; ++i) {
            if(input[i] == '0') {
                continue;
            }
            strncpy(result, input+i, len-i);
            result[len-i] = '';
            return;
        }
        strcpy(result, "0");
    }
    int compare(char *numA, char *numB) 
    {
        int lenA = strlen(numA);
        int lenB = strlen(numB);
        if(lenA != lenB) {
            return lenA - lenB;
        } 
        for(int i = 0; i < lenA; ++i) {
            if(numA[i] < numB[i]) {
                return -1;
            }
            if(numA[i] > numB[i]) {
                return 1;
            }
        }
        return 0;
    }
    void sub(char *numA, char *numB, char *result) 
    {
        int lenA = strlen(numA);
        int lenB = strlen(numB);
        int offset = lenA - lenB;
        char numBCopy[MAX_LEN];
        strcpy(numBCopy, numB);
        if(offset) {
            for(int i = lenB-1; i >= 0;--i) {
                numB[i+offset] = numB[i];
            }
            for(int i = 0;i < offset;++i) {
                numB[i] = '0';
            }
        }
        // calculate
        int carry = 0;
        for(int i = lenA-1; i >= 0; --i) {
            int value = numA[i] - numB[i] - carry;
            if(value < 0) {
                result[i] = '0' + (10 + value);
                carry = 1;
            } else {
                result[i] = '0' + value;
                carry = 0;
            }
        }
        result[lenA] = '';
        strcpy(numB, numBCopy);
    }
    void minus(char *numA, char *numB, char *result) 
    {
        char number[MAX_LEN];
        int ret = compare(numA, numB);
        if(ret == 0) {
            strcpy(result, "0");
            return;
        }
        char removeZeroStr[MAX_LEN];
        if(ret < 0) {
            sub(numB, numA, number);
            removeZero(removeZeroStr, number);
            sprintf(result, "-%s", removeZeroStr);
        } else {
            sub(numA, numB, number);
            removeZero(removeZeroStr, number);
            strcpy(result, removeZeroStr);
        }
    }
    int main() 
    {
        int N;
        char a[MAX_LEN], b[MAX_LEN];
        while(scanf("%d", &N) != EOF) {
            for(int i = 0; i < N; ++i) {
                scanf("%s", a);
                scanf("%s", b);
                char result[MAX_LEN+1];
                minus(a, b, result);
                printf("Case %d:
    ", i+1);
                printf("%s - %s = %s
    
    ", a, b, result);
            }
        }
        return 0;
    }

    #include <stdio.h>
    #include <string.h>
    
    #define LENGTH 1001
    #define RESLENGTH 2002
    void multiply(char *a, char *b, int length_a, int length_b);
    
    int main() {
        char a[LENGTH] = "";
        char b[LENGTH] = "";
        int len_a, len_b;
        while (scanf("%s %s", a, b) != EOF) {
            len_a = strlen(a);
            len_b = strlen(b);
            multiply(a, b, len_a, len_b);
            printf("
    ");
        }
        return 0;
    }
    
    void multiply(char *a, char *b, int length_a, int length_b) {
        int res_len = length_a + length_b;
        int res[RESLENGTH] = {0};
        for (int i = 0; i < length_a; ++i) {
            for (int j = 0; j < length_b; ++j) {
                res[i + j] += (a[length_a - i - 1] - '0') * (b[length_b - j - 1] - '0');  //字符转整数
            }
        }
        for (int k = 0; k < res_len; ++k) {
            if (res[k] >= 10) {
                res[k + 1] += res[k] / 10;
                res[k] = res[k] % 10;
            }
        }
    
        //去掉头部的0
        int index = res_len;
        while(res[index]==0 && index>=0){
            index--;
        }
        if(index<0){
            printf("0");
            return;
        }
        for (int i = index; i >= 0; --i) {
            printf("%d", res[i]);
        }
    
    
    }
  • 相关阅读:
    在 TB1 机器上编译并调试 TB 自带sample的方法
    C++编译错误 fatal error C1189: #error : The C++ Standard Library forbids macroizing keywords. Enable warning
    c++生成随机数
    Unity调用讯飞做语音听写(Android Studio版)
    TouchDesigner 编译FlexChop
    Behavior Designer知识点
    清除Unity缓存
    UnityEngine.UI.dll is in timestamps but is not known in assetdatabase
    Unity插件学习记录 -- SW Actions
    Unity3D 使用LineRenderer制作画板功能
  • 原文地址:https://www.cnblogs.com/ustc-anmin/p/11635901.html
Copyright © 2020-2023  润新知