Home > Enterprise >  How do I break down each number value I get, step by step, and write each obtained number into the p
How do I break down each number value I get, step by step, and write each obtained number into the p

Time:11-18

I have to separate each input I receive into individual digits. Then I have to print all the values ​​I get into the push function.

I have to separate each input I receive into individual digits. Then I have to print all the values ​​I get into the push 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;

}

CodePudding user response:

The first questions we must ask are:

How many inputs goes, or for how long the inputs will last?

Should the lists have a minimun size? In the case some lists are greater than other, what should we do?

Here goes something i Did.

Hope it helps

#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;
}
  • Related