2020年1月13日 星期一

大數加減

題目:

  • Only add or minus two large number(+ or -).
  • All inputs are positive numbers.
  • The number may contain an decimal point, you need to handle it by your self.
  • The first input is how many testing data.
  • The answer you print should have it’s data number.

範例輸入:(intput.txt)
5
12345678991019231124+1234567890000000
12345678991019231124-1234567890000000
1-123456789123456789
123465789.123456789+987654321.987654321
1000000000-0.1

範例輸出:
#1:12346913558909231124
#2:12344444423129231124
#3:-123456788
#4:1111111111.11111111
#5:999999999.9


備註:
這題....我只寫整數加減而已,而且還寫得很醜.....反正我是放棄了。
根據助教,可以想成好幾個整數來代表,那只要處理接起來時的進位問題就好,但讀檔真的好難處理.......。

程式碼:
#include<stdio.h>
#include "FileHandler.h"
void add(char *a, char *b, char *c,int length_a,int length_b)
{
 int n;
 if (length_a > length_b)
 {
  n = length_b;
  for (int i = 0; i < n; i++)//clean char to int and add
   c[i] = a[length_a - 1 - i] - '0' + b[length_b - 1 - i] - '0';
  for (int i = n; i < length_a; i++)//add the rest
  {
   c[i] = a[length_a - 1 - i]-'0';
  }
  for (int i = 0; i < length_a-1; i++)
  {
   c[i + 1] += c[i] / 10;
   c[i] %= 10;
  }
  for (int i = 0; i < length_a; i++)//int to  char
   c[i] += '0';
 }
 else
 {
  n = length_a;
  for (int i = 0; i < n; i++)
   c[i] = a[length_a - 1 - i] - '0' + b[length_b - 1 - i] - '0';
  for (int i = n; i < length_b; i++)
   c[i] = b[length_b - 1 - i] - '0';
  for (int i = 0; i < length_b - 1; i++)
  {
   c[i + 1] += c[i] / 10;
   c[i] %= 10;
  }
  for (int i = 0; i < length_b; i++)
   c[i] += '0';
 }
}
void sub(char *a, char *b, char *c, int length_a, int length_b)
{
 int n;
 if (length_a > length_b) //when a>b, find a-b 
 {
  n = length_b;
  for (int i = 0; i < length_b; i++)
  {
   c[i] = (a[length_a - 1 - i] - '0') - (b[length_b - 1 - i] - '0');
  }
  for (int i = n; i < length_a ; i++)
   c[i] = a[length_a - 1 - i] - '0';
  for (int i = length_b - 1; i >= 0; i--)
  {
   if (c[i] < 0)
   {
    c[i + 1] -= 1;
    c[i] += 10;
   }
  }
  for (int i = 0; i < length_a; i++)
   c[i] += '0';
 }
 else //caution: when length_a==length_b but a>b
 {
  n = length_a;
  for (int i = 0; i < length_a; i++)
   c[i] = (b[length_b - 1 - i] - '0')-(a[length_a-1-i]-'0');
  for (int i = n; i < length_b; i++)
   c[i] = b[length_b - 1 - i] - '0';
  for (int i = length_a - 1; i >= 0; i--)
  {
   if (c[i] < 0)
   {
    c[i + 1] -= 1;
    c[i] += 10;
   }
  }
  for (int i = 0; i < length_b; i++)
   c[i] += '0';
  c[length_b] = '-';
 }
}
int main()
{
 char *str = readAllTextFromFile("./input.txt");
 int n = 0, i_now = 0, t = 1;
 while (str[i_now] != '\n')//read case number
  n = str[i_now++] - '0' + n * 10;
 i_now++;//skip new line
 while (n--)
 {
  char num1[200] = { 0 }, num2[200] = { 0 }, result[200] = { 0 };
  int length_1 = 0, length_2 = 0, length = 0, check1 = 0, check2 = 0;
  char command[1];
  while (str[i_now] != '+'&&str[i_now] != '-')//read num1 while '+' or '-' then stop
  {
   if (str[i_now] == '.')
    check1 = 1;
   num1[length_1++] = str[i_now++];
  }
  command[0] = str[i_now++];
  while (str[i_now] != '\n'&&str[i_now]!=NULL)//read num2 while '\n' then stop
  {
   if (str[i_now] == '.')
    check2 = 1;
   num2[length_2++] = str[i_now++];
  }
  i_now++;
  //for (int i = 0; i < length_1; i++)//print num1
  // printf("%c", num1[i]);
  //printf("%c", command[0]);//print '+' or '-'
  //for (int i = 0; i < length_2; i++)
  // printf("%c", num2[i]);//print num2
  //printf("\n");
  printf("#%d:", t++);//print result
  if (check1 == 0 && check2 == 0)
  {
   if (command[0] == '+')//add
    add(num1, num2, result, length_1, length_2);
   else if (command[0] == '-')//substract
    sub(num1, num2, result, length_1, length_2);
   for (int i = 199; i >= 0; i--)
   {
    if (result[i] != NULL)
     printf("%c", result[i]);
   }

  }
  else if(check1==1&&check2==1)
  {
   ;
  }
  
  
  printf("\n");
 }
 system("pause");
 return 0;
}

沒有留言:

張貼留言