链表中传递的数据错误

时间:2013-08-15 13:08:12

标签: c

假设我输入以下内容:

maximum weight number of containers the ship can carry: 10
maximum weight:5
filename: containerlist.txt

输出:

Weight: 1.82 Shipping Price:2537.76  <---- total exceed 5
Weight: 2.98 Shipping Price:5526.86
Weight: 6.22 Shipping Price:8444.84
Weight: 6.77 Shipping Price:4698.15

Total weight: 5.96 <----- Wrong 

我试图把我的体重打印在

之下
totalweight = totalweight + cnr_getWeight(node_getData(current));

看看我得到了什么价值,结果证明是2.98和2.98。

typedef struct Node {
    void *data;
    struct Node *next;
} Node;

typedef struct{
    char *id;
    double weight;
    double shippingPrice;
}Container;

typedef struct{
    Node *head;      
    Node *tail; 
    Node *iterator;  
    int size;       
} List;

/* Main */

int main()
{
    int maxCnr;
    int file = 0;
    double maxWeight;
    char filename[50]; 
    char choice;
    FILE *input;
    List *list = lst_new();

    printf("********************** WELCOME TO SHIPMANAGER **********************\n\n");
    printf("Please enter the maximum number of containers the ship can carry: ");
    scanf("%i", &maxCnr);

    printf("\nPlease enter the maximum weight the ship can carry: ");
    scanf("%lf", &maxWeight);

    do {
        printf("\nEnter the filename (.txt): ");
        scanf("%s", &filename);

        input = fopen(filename, "r");
        if (input == NULL)
        {
            file = 0;
            printf("File does not exist. \n");
        }
        else
        {
            printf("File located.\n");
            file = 1;
        }
    }while(file == 0);

    readContainer(list,filename);
    mxCnr(&list, maxCnr);
    mxWeight(&list, maxWeight); 
    printContainers(list);
}
/* ensure that the container does not exceed the maximum number */

void mxCnr(List **list, int mCnr)
{
   Container *tmpContainer;
   List *max = lst_new();
   int i = 0;

   for (tmpContainer = lst_first(*list); i<mCnr; tmpContainer = lst_next(*list))
   {
        Node *previous = NULL;
        Node *current = max->head;

        if (max->size == 0)
            lst_add(max, tmpContainer);     
        else
        {               
            Node *newNode = node_new(tmpContainer, NULL);
            current = max->head;
            while (node_getNext(current) != NULL)
            {
                previous = current;
                current = node_getNext(current);
            }

           if (previous == NULL)
           {
              node_setNext(newNode, max->head);
              max->head = newNode;    
           }
           else
           {
              max->tail->next = newNode;
              max->tail = newNode;
           }
           max->size++;
        }
        i++;
    }    
    lst_delete(*list); 
    *list = max;    
}

/* function to calculate the total weight and to ensure that the weight does not exceed the maximum weight */

void mxWeight(List **list, double mWeight)
{
    Container *tmpContainer;
    List *maxW = lst_new();
    double totalweight= 0;
    printf("maxweight :%lf ", mWeight);

   for (tmpContainer = lst_first(*list); totalweight<=mWeight; tmpContainer = lst_next(*list))
    {
        Node *current = maxW->head;
        Node *previous = NULL;


        if (maxW->size == 0)
        {
            lst_add(maxW, tmpContainer);

        }
        else
        {               
            Node *newNode = node_new(tmpContainer, NULL);
            current = maxW->head;        
            while (node_getNext(current) != NULL && totalweight <= mWeight)
            {
                previous = current;
                current = node_getNext(current);    
                totalweight = totalweight + cnr_getWeight(node_getData(current));    
                printf("%lf ", cnr_getWeight(node_getData(current)));              
            }

            if (previous == NULL)
            {
                node_setNext(newNode, maxW->head);
                maxW->head = newNode;                                           
            }
            else
            {
                maxW->tail->next = newNode;
                maxW->tail = newNode;
            }
            maxW->size++;
        }

    }
    printf("Total weight: %lf", totalweight);
    lst_delete(*list);
    *list = maxW;
}

1 个答案:

答案 0 :(得分:0)

如果您不想超过总重量,则需要在执行总重量之前进行检查。 (简单...)

 while (node_getNext(current) != NULL && totalweight<= mWeight)
        {
            Node *old_previous = previous;
            previous = current;
            current = node_getNext(current);    
            totalweight = totalweight + cnr_getWeight(node_getData(current));    
            printf("%lf ", cnr_getWeight(node_getData(current)));
            if (totalweight > mWeight){
               totalweight -= cnr_getWeight(node_getData(current));
               current = previous;
               previous = old_previous;
               break;
            }  
        }
相关问题