2016-11-17 117 views
5

因此我理解如何對字符串中表示的整數執行計算,然後將結果打印到字符串中。但我迷失在如何用字符串中表示的數字中的小數表示同樣的事情。在C中添加十進制數字作爲字符串

下面是我用整數做的。

int answer = 0; 
char str1[100]; 
int count = 0; 
int total = 0; 
int k = 0; 
int diff = 0; 

if (ele == ele2) { 
    for (k = strlen(op1) - 1; k > -1; k--) { 
     if ((strspn(operand, "+") == strlen(operand))) { 
      answer = (op1[k] - '0') + (op2[k] - '0'); 
     } else if ((strspn(operand, "-") == strlen(operand))) { 
      answer = (op1[k] - '0') - (op2[k] - '0'); 
     } 

     total += (pow(10, count) * answer); 
     count++;   
    } 
    sprintf(str1, "%d", total); 
    printf("Answer: %s ", str1);  
} 

輸出

// 12 + 14 
Answer: 26 // Answer given as a string 

12.2 + 14.5 // Three strings 
Answer: 16.7 // Answer as string 

當前的嘗試:

for (k = strlen(argv[1]) - 1; k > -1; k--) { 
     if (argv[1][k] == '.') { 
      dec = k; 
     } else { 
      answer = (argv[1][k] - '0') + (argv[3][k] - '0'); 
      total += (pow(10, count) * answer); 
      count++; 
     } 


    } 

    // needs to be converted to a long? 
    // ele is the length of the operand 
    total = total/pow(10, ele - dec); 

    sprintf(str1, "%d", total); 
    printf("Answer: %s ", str1); 
+0

那麼......你是如何做整數的? – George

+0

「整數作爲字符串」是什麼意思? 「數字串中的小數點」是什麼意思?你能否請嘗試創建一個[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)並向我們展示?請包括輸入和實際以及預期的輸出。 –

+0

@George我會用我的代碼片斷重新編輯我的帖子,我的道歉。 – Jasmine

回答

0

共享一個簡單的算法中,開始代碼的這一部分在一起的兩個整數添加與(並假設你的添加整數funciton工作正常)。

十進制數基本上是由「。」分隔的兩個整數。

  • 確定「。」的位置。並抓取整數的兩邊作爲整數部分,decimalPart
  • 獲取decimalPart的一個警告是所有decimalParts的長度應該相同,如果不是,則在後綴中加上「0」。
  • 添加integerPart,在decimalPart中添加decimalPart並處理carryForwards。

所以,

12.2 + 14.95 
= (12 + 14) (20 + 95) 
= 26 115 
= 26+1 15 
= 27.15 
+0

這很有幫助,謝謝。然而,我的問題是,我將整個答案作爲一個字符串打印出來。而我這樣做的方式是將每列數字添加到最終結果中。那麼如何獲得小數點呢?我希望我的解釋清楚。 – Jasmine

+0

爲什麼不給你的函數調用兩次,得到兩者的輸出,處理一切,然後打印答案。你真的想一口氣做到嗎?如果是的話,還有其他方法 – thepace

+0

我認爲如果我像我對其餘工作一樣做了它會更簡單,但如果有更好的方法,我會接受其他建議。 – Jasmine

0

這是一個快速和骯髒的實施:無參數檢查,沒有深厚的測試只有你應如何處理的想法。

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

    typedef struct 
    { 
      int total_digits;; 
      int decimal_points; 
      int *number; 
    } NUMBER, *DECIMALNUMBER; 

    DECIMALNUMBER initilize(char *str) 
    { 
      DECIMALNUMBER result = calloc(1, sizeof(NUMBER)); 
      int in_decimal = 0; 
      char *s; 
      int i; 
      for (s = str; *s; s++) 
      { 
        if (isdigit(*s)) 
        { 
          result->total_digits++; 
          if (in_decimal) 
          { 
            result -> decimal_points++; 
          } 
        } 
        else if (*s == '.') 
        { 
          in_decimal = 1; 
        } 
        else 
        { 
          return NULL; 
        } 
      } 
      result->number = calloc(result->decimal_points, sizeof(int)); 
      i=0; 
      for (s = str; *s; s++) 
      { 
        if (isdigit(*s)) 
        { 
          result->number[i++] = (int)(*s - '0'); 
        } 
      } 
    //  printf("result->total_digits is %d\n",result->total_digits); 
    //  printf("result->decimal_points is %d\n",result->decimal_points); 
    //  printf("result is %d\n",result->number[--i]); 
    //  printf("result is %d\n",result->number[--i]); 
    //  printf("result is %d\n",result->number[--i]); 
      return result; 
     } 

    void print_number(DECIMALNUMBER p) 
    { 
      int i; 
      for (i=0; i<p->total_digits; i++) 
      { 
        if (i==p->total_digits - p->decimal_points) { 
          printf("."); 
        } 

        printf("%d", p->number[i]); 
      } 
      printf("\n"); 
    } 

    DECIMALNUMBER sum(DECIMALNUMBER a, DECIMALNUMBER b) 
    { 
      int max_decimals = a->decimal_points>b->decimal_points ? a->decimal_points : b->decimal_points; 

      int max_digits_count = a->total_digits>b->total_digits ? a->total_digits : b->total_digits; 
      DECIMALNUMBER result = calloc(1, sizeof(NUMBER)); 
      result->total_digits = max_digits_count; 
      result->decimal_points = max_decimals; 
      result->number = calloc(max_digits_count, sizeof(int)); 

      int i1 = a->total_digits-1; 
      int i2 = b->total_digits-1; 
      int i3 = result->total_digits-1; 
      int remainder = 0; 
      int summed; 
      while (i1 >= 0 || i2 >=0) 
      { 
        int aa = i1 < 0 ? 0 : a->number[i1]; 
        int bb = i2 < 0 ? 0 : b->number[i2]; 
        summed = aa + bb + remainder; 
        result->number[i3] = summed % 10; 
        remainder = summed/10; 
        i1--; 
        i2--; 
        i3--; 
      } 
      return result; 
    } 

    int main() 
    { 
      DECIMALNUMBER a = initilize("12.2"); 
      DECIMALNUMBER b = initilize("16.7"); 

      print_number(a); 
      print_number(b); 

      DECIMALNUMBER c = sum (a,b); 
      print_number(c); 

      return 0; 
    } 
相關問題