繁体   English   中英

如何一步步分解得到的每个数字值,并用for循环将得到的每个数字写入push function?

[英]How do I break down each number value I get, step by step, and write each obtained number into the push function with a for loop?

我必须将收到的每个输入分成单独的数字。 然后我必须打印我进入推送 function 的所有值。

我必须将收到的每个输入分成单独的数字。 然后我必须打印我进入推送 function 的所有值。

int main(void)
{
    struct Node* first = NULL;
    struct Node* second = NULL;
    //Take the input from the user
    scanf("%d", &b);
    int n1;
    scanf("%d", &n1);
    
    // I must the use push function with input by one to one
    // push(&first, n1);
    printf("First List is: ");
    printList(first);

    // Multiply the two lists and see result
    struct Node* result = multiplyTwoLists(first, second);
    printf("Resultant list is: ");
    printList(result);

    return 0;

}

我们必须问的第一个问题是:

有多少输入,或者输入将持续多长时间?

列表应该有最小尺寸吗? 如果某些列表大于其他列表,我们应该怎么办?

这是我做过的事情。

希望能帮助到你

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

typedef struct STRUCT_INPUT_LIST {
    int inputValue;
    struct STRUCT_INPUT_LIST *nextList;
}STRUCT_INPUT_LIST;

void pushInputToList(STRUCT_INPUT_LIST *inputList){
    char read[32];
    
    memset(read, 0, sizeof(read));
    printf("Give a input\n");
    if ( fgets(read, sizeof(read), stdin) == NULL )
        return;

        
    strtok(read, "\n\r");
    if (strlen(read) > 0 && isdigit(read[0]))
        inputList->inputValue = atoi(read);

}
STRUCT_INPUT_LIST *getLastUsedNode(STRUCT_INPUT_LIST *inputList){
    STRUCT_INPUT_LIST* wrkList = NULL;
    if ( inputList == NULL ) return NULL;
    for( wrkList = inputList; wrkList->nextList != NULL; wrkList = wrkList->nextList );

    return wrkList;
}
void freeList(STRUCT_INPUT_LIST *inputList){
    STRUCT_INPUT_LIST* wrkList = NULL;
    for ( wrkList = inputList; wrkList != NULL; ){
        STRUCT_INPUT_LIST* lastList = NULL;  
        lastList = wrkList;
        wrkList = wrkList->nextList;
        free(lastList);
    }

}
int getMinMultipliableSize(STRUCT_INPUT_LIST *listOne, STRUCT_INPUT_LIST *listTwo){
    STRUCT_INPUT_LIST* wrkList= NULL;
    STRUCT_INPUT_LIST* wrkListTwo= NULL;
    int iSize = 0;
    for( wrkList = listOne,
         wrkListTwo = listTwo;
          wrkList != NULL && 
          wrkListTwo != NULL; 
         wrkList = wrkList->nextList,
         wrkListTwo = wrkListTwo->nextList ){
        iSize++;
    }

    return iSize;
}
void multiplyLists(STRUCT_INPUT_LIST *listOne, STRUCT_INPUT_LIST *listTwo,STRUCT_INPUT_LIST *resultList, int times){
    STRUCT_INPUT_LIST* wrkList= NULL;
    STRUCT_INPUT_LIST* wrkListTwo= NULL;
    STRUCT_INPUT_LIST* wrkRsl= NULL;
    int i = 0;
    for( wrkList = listOne,
         wrkListTwo = listTwo,
         wrkRsl = resultList;
          i < times; 
         wrkList = wrkList->nextList,
         wrkListTwo = wrkListTwo->nextList,
         wrkRsl = wrkRsl->nextList, i++ ){
        wrkRsl->inputValue = wrkList->inputValue * wrkListTwo->inputValue;
    }

    return ;
}

void outputList(STRUCT_INPUT_LIST *inputList, char *listName){
    STRUCT_INPUT_LIST* wrkList= NULL;
    printf("==========================\n");
    printf("+++ Listing Inputs of %s:\n", listName);
    for( wrkList = inputList; wrkList != NULL; wrkList = wrkList->nextList ){
        printf("[%d] ", wrkList->inputValue);
    }
    printf("+++ End of Inputs\n");
    printf("==========================\n");
}

int main(void)
{
    STRUCT_INPUT_LIST* firstList = NULL;
    STRUCT_INPUT_LIST* wrkList = NULL;
    STRUCT_INPUT_LIST* secondList = NULL;
    STRUCT_INPUT_LIST* rslList = NULL;
    int stop = 0;
    int invalidanswer = 0;
    int minMultipliable = 0;
    char read[32];

    firstList = (STRUCT_INPUT_LIST *) malloc (sizeof(STRUCT_INPUT_LIST));
    secondList = (STRUCT_INPUT_LIST *) malloc (sizeof(STRUCT_INPUT_LIST));
    
    memset(firstList, 0, sizeof(STRUCT_INPUT_LIST));  firstList->nextList  = NULL;
    memset(secondList, 0, sizeof(STRUCT_INPUT_LIST)); secondList->nextList = NULL;
    wrkList = firstList;
    do{
        
        pushInputToList(wrkList);
        memset(read, 0, sizeof(read));
        do{
            invalidanswer = 0;
            printf("Read more inputs to the (first) list, to the (second) or (stop) ?\n");
            if ( fgets(read, sizeof(read), stdin) == NULL )
                return -1;

            strtok(read, "\n\r");
            if ( !strcasecmp(read, "first") ){
                wrkList = getLastUsedNode(firstList);
            }
            else if ( !strcasecmp(read, "second") ){
                wrkList = getLastUsedNode(secondList); 
            }
            else if ( !strcasecmp(read, "stop") ){
                stop = 1;
                break;
            }
            else{
                printf("invalid input\n\n");
                invalidanswer = 1;
                continue;
            }
            if ( wrkList->inputValue == 0 )
                break;
            
            wrkList->nextList = (STRUCT_INPUT_LIST *) malloc (sizeof(STRUCT_INPUT_LIST));
            wrkList = wrkList->nextList;
            memset(wrkList, 0, sizeof(STRUCT_INPUT_LIST));  wrkList->nextList  = NULL;
        } while ( invalidanswer );

    } while ( !stop );

    minMultipliable = getMinMultipliableSize(firstList, secondList);
   
    {
        int i;
        STRUCT_INPUT_LIST *wrkRsl;
        rslList = (STRUCT_INPUT_LIST *) malloc (sizeof(STRUCT_INPUT_LIST));
        memset(rslList, 0, sizeof(STRUCT_INPUT_LIST)); 
        wrkRsl = rslList;
        for ( i = 0 ; i < (minMultipliable -1) ; i++){
            wrkRsl->nextList = (STRUCT_INPUT_LIST *) malloc (sizeof(STRUCT_INPUT_LIST));
            wrkRsl = wrkRsl-> nextList;
            memset(wrkRsl, 0, sizeof(STRUCT_INPUT_LIST)); 
        }
        wrkRsl->nextList  = NULL;
    }

    multiplyLists(firstList, secondList, rslList, minMultipliable);

    outputList(firstList, "firstList");
    outputList(secondList, "secondList");
    outputList(rslList, "multipliedList");

    freeList(firstList);
    freeList(secondList);
    freeList(rslList);

    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM