在C语言中使用堆栈添加2个大数

时间:2018-03-10 07:51:07

标签: c stack push add pop

我试图使用堆栈编写一个用于大数字添加的程序。 我的步骤包括将字符串1(第一个数字)推入堆栈1,将str2推入堆栈2,执行添加并推送到第三个堆栈(我还使用变量' carry'然后将它们添加到下一个堆栈和)。之后,我将堆栈3弹出一个字符串。 我不知道为什么我的代码在某些情况下运行良好,例如,num1 =' 123',num2 =' 124'。 谢谢你的帮助!

     #include<stdio.h>
     #include<string.h>
     #include<stdlib.h>
     #define MAX_LEN 100

     typedef struct stack{
       int top;
       unsigned length;
       char* arr;
     }Stack;

     Stack* makeNew(unsigned length){
      Stack* stack = (Stack*)malloc(sizeof(Stack));
      stack->length = length;
      stack->top = -1;
      stack->arr = (char*)malloc(stack->length * sizeof(char));
      return stack;
     }

     int isEmpty(Stack* stack){
       if(stack->top == -1) return 1;
       else return 0;
       }

       int isFull(Stack* stack){
           if(stack->top == stack->length - 1) return 1;
           else return 0;
       }

       void push(Stack* stack,char each_char){
           if(isFull(stack)){
            printf("Stack Overflow\n");
           }
           stack->arr[++stack->top] = each_char;
       }

       char pop(Stack* stack){
           if(isEmpty(stack)){
               printf("Stack Empty\n");
           }
           return stack->arr[stack->top--];
       }

       void swap(char *str1, char *str2){
         char *temp = (char *)malloc((strlen(str1) + 1) * sizeof(char));
         strcpy(temp, str1);
         strcpy(str1, str2);
         strcpy(str2, temp);
         free(temp);
       }

       void add(char str1[MAX_LEN],char str2[MAX_LEN]){
           char result[strlen(str1)];
           Stack* num1 = makeNew(strlen(str1));    
           Stack* num2 = makeNew(strlen(str2));
           Stack* stack = makeNew(strlen(str1));
           int i;
           for(i=0;i<strlen(str1);i++) push(num1,str1[i]);                          
           for(i=0;i<strlen(str2);i++) push(num2,str2[i]);

           int sum = 0;
           int carry = 0;

           for(i=0;i<strlen(str2);i++){
               sum = (pop(num1)-'0')+(pop(num2)-'0')+ carry;
               push(stack,sum%10 + '0');
               carry = sum/10;
           }
           for(i=strlen(str2);i<strlen(str1);i++){
               sum = (pop(num1)-'0') + carry;
               push(stack,sum%10 + '0');
               carry = sum/10;
           }
           if(carry>0) push(stack,carry);
                   //reverse the string

           for(i=0;i<strlen(str1);i++)
               result[i] = pop(stack);
           printf("The result is: %s",result);
       }

       int main(){
           char str1[MAX_LEN], str2[MAX_LEN];
           printf("Enter the first number: ");
           scanf("%s",&str1);
           printf("Enter the second number: ");
           scanf("%s",&str2);
           if((strlen(str1))<(strlen(str2))) swap(str1,str2);
           add(str1,str2);
           return 0;
       }

1 个答案:

答案 0 :(得分:0)

这解决了结果长于最长输入字符串的问题。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX_LEN 100

typedef struct stack{
    int top;
    unsigned length;
    char* arr;
}Stack;

Stack* makeNew(unsigned length) {
    Stack* stack = (Stack*)malloc(sizeof(Stack));
    stack->length = length;
    stack->top = -1;
    stack->arr = (char*)malloc(stack->length * sizeof(char));
    return stack;
}

int isEmpty(Stack* stack)
{
    if(stack->top == -1) return 1;
    else return 0;
}

int isFull(Stack* stack)
{
    if(stack->top == stack->length - 1) return 1;
    else return 0;
}

void push(Stack* stack, char each_char)
{
    if(isFull(stack)) {
        printf("Stack Overflow\n");
    }
    stack->arr[++stack->top] = each_char;
}

char pop(Stack* stack)
{
    if(isEmpty(stack)) {
        printf("Stack Empty\n");
    }
    return stack->arr[stack->top--];
}

void swap(char *str1, char *str2)
{
    char *temp = (char *)malloc((strlen(str1) + 1) * sizeof(char));
    strcpy(temp, str1);
    strcpy(str1, str2);
    strcpy(str2, temp);
    free(temp);
}

void add(char str1[MAX_LEN],char str2[MAX_LEN])
{
    int resultLength = strlen(str1)+1;
    char result[resultLength];

    Stack* num1 = makeNew(strlen(str1));    
    Stack* num2 = makeNew(strlen(str2));

    Stack* stack = makeNew(resultLength);

    int i;
    for(i = 0; i < strlen(str1); i++) push(num1,str1[i]);                          
    for(i = 0; i < strlen(str2); i++) push(num2,str2[i]);

    int sum = 0;
    int carry = 0;

    for(i = 0; i < strlen(str2); i++) {
        sum = (pop(num1) - '0') + (pop(num2) - '0') + carry;
        push(stack, sum % 10 + '0');
        carry = sum / 10;
    }

    for(i = strlen(str2); i < strlen(str1); i++) {
        sum = (pop(num1) - '0') + carry;
        push(stack, sum % 10 + '0');
        carry = sum / 10;
    }

    if(carry > 0) {
        push(stack, carry + '0');
    }

    //reverse the string
    for(i = 0; i < resultLength; i++) {
        result[i] = pop(stack);
    }
    printf("The result is: %s", result);
}

int main()
{
    char str1[MAX_LEN], str2[MAX_LEN];
    printf("Enter the first number: ");
    scanf("%s",&str1);
    printf("Enter the second number: ");
    scanf("%s",&str2);
    if((strlen(str1))<(strlen(str2))) swap(str1,str2);
    add(str1,str2);
    return 0;
}