//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", ×); char operator[2][LENGHT] = {{'0'}, {'0'}}; for (int i = 0; i < times; ++i) { scanf("%s %s", operator[0], operator[1]); printf("Case %d:\n", i + 1); printf("%s + %s = ", operator[0], operator[1]); sumPlus(operator[0], operator[1]); if (i != times - 1) { printf("\n"); printf("\n"); } } 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] == '\0') flag_a = 1; if (b[i] == '\0') 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] = '\0'; }
#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] = '\0'; 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] = '\0'; 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:\n", i+1); printf("%s - %s = %s\n\n", 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] = "\0"; char b[LENGTH] = "\0"; 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("\n"); } 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]); } }