在C中将十进制数字添加为字符串

时间:2016-11-17 08:49:57

标签: c

所以我理解如何对字符串中表示的整数执行计算,然后将结果打印在字符串中。但是,如果用字符串中表示的数字中的小数来做同样的事情我就输了。

这是我如何用整数做的。这部分代码将两个整数相加:

    public static void main(String[] args) {
            timesRepeat("test",30);
        }
        public static void  timesRepeat(String input , int times )
        {
            char[] resultString = new char[times];
            //write in bouts 

            int fullPart = times/input.length();
            int partPart = times%input.length();
            for ( int i =0 ; i< fullPart; i++)
            {
                System.arraycopy(input.toCharArray(), 0, resultString, (i*input.length()), input.length());
            }

            System.arraycopy(input.toCharArray(), 0, resultString, (fullPart)*input.length(), partPart);
            System.out.println(resultString);
        }

输出

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

2 个答案:

答案 0 :(得分:0)

分享一个简单的算法开始(并假设你的添加整数功能正常)。

十进制数基本上是由“。”分隔的两个整数。

  • 确定“。”的位置。并将整数的两边抓取为integerPart,decimalPart
  • 获取decimalPart的一个警告是所有decimalPart的长度应该相同,如果不相同,则在后缀中添加“0”。
  • 添加integerPart,添加decimalPart并处理decimalPart中的carryForwards。

所以,

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

答案 1 :(得分: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;
    }