Adding decimal numbers as strings in C

So, I understand how to perform calculations on integers represented in strings, and then print the result in a string. But I am lost in how to do the same with the decimal digit in the quantity represented in the string.

Here's how I did it with integers. This piece of code combines two integers:

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); } 

Output

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

Example

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

Current attempt:

 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); 
+5
source share
2 answers

Sharing a simple algorithm to get you started (and assuming adding integer funciton works fine).

The decimal number basically consists of two integers, separated by the symbol ".".

  • Define the position ".". and grab both sides of the integer as integerPart, decimalPart
  • One caveat to getting the decimal part is that the length of all decimal parts should be the same, if not, add “0” to the suffix.
  • Add integerPart, add decimalPart and handle carryForwards in decimalPart.

So,

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

This is a quick and dirty implementation: without checking parameters, without deep testing, only the idea of ​​how you should handle it.

 #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; } 
0
source

Source: https://habr.com/ru/post/1259890/


All Articles