Sunday, March 20, 2016

C Programming Tutorial - 3

Tutorial 9 Linked List Full example
struct product{
    float price;
    char productname[20];

    struct product *next;
};
struct product *pFirstNode = NULL;
struct product *pLastNode = NULL;
void createNewList(){
    struct product *pNewstruct = (struct product *) malloc(sizeof(struct product));
    pNewstruct->next = NULL;
    printf("Enter Product Name:");
    scanf("%s", &(pNewstruct)->productname);
    printf("Enter Product Price:");
    scanf("%f", &(pNewstruct)->price);
    pFirstNode = pLastNode = pNewstruct;
}
void inputData(){
    if(pFirstNode == NULL){
        createNewList();
    } else {
    struct product *pNewstruct = (struct product *) malloc(sizeof(struct product));
    printf("Enter Product Name:");
    scanf("%s", &(pNewstruct)->productname);
    printf("Enter Product Price:");
    scanf("%f", &(pNewstruct)->price);
    if(pFirstNode == pLastNode){
        pFirstNode->next =  pNewstruct;
        pLastNode = pNewstruct;
        pNewstruct->next = NULL;
    } else {
        pLastNode->next = pNewstruct;
        pNewstruct->next = NULL;
        pLastNode = pNewstruct;
    }
    }
}
void outputData(){
    struct product *pProducts = pFirstNode;
    printf("Products Entered\n");
    while(pProducts!=NULL){
        printf("%s costs %.2f\n\n", pProducts->productname, pProducts->price);
        pProducts = pProducts->next;
    }
}
struct product *pProductbeforeProducttoDelete = NULL;

struct product *searchForProduct(char * productname){
    struct product *pProductIterator=pFirstNode;
    while(pProductIterator!=NULL){
        int areThey = strncmp(pProductIterator->productname, productname, 30);
        if(!areThey){
            printf("%s was found and it costs %.2f\n\n",pProductIterator->productname, pProductIterator->price);
            return pProductIterator;
        }
        pProductIterator = pProductIterator->next;
    }
    pProductbeforeProducttoDelete = pProductIterator;
    printf("%s wasnt found\n\n", productname);
    return NULL;
}
void removeProduct(char *productname){
    struct product *pProductToDelete = NULL;
    pProductToDelete = searchForProduct(productname);
    if(pProductToDelete!=NULL){
        printf("%s was deleted\n\n", productname);
        if(pProductToDelete==pFirstNode){
            pFirstNode = pProductToDelete->next;
        } else {
            pProductbeforeProducttoDelete->next = pProductToDelete->next;
        }
        free(pProductToDelete);
    } else {
        printf("%s was not found", productname);
    }
}
int main(){
    inputData();
    inputData();
    inputData();
    outputData();
    removeProduct("Tomato");
    outputData();
    return 0;
}
Tutorial 10 File I/O
int main()
{
    char buffer[1000];
    int num, i;
    FILE *pFile;
    pFile = fopen("randomnumbers.txt","w");
    if(!pFile){
        printf("Error: Couldn't write to file");
        return 1;
    }
    for(i=0; i<10; i++){
        num = rand()%100;
        fprintf(pFile, "%d\n", num);
    }
    printf("Success Writing to File\n");
    if(fclose(pFile)!=0){
        printf("Error: File not Closed\n");
    }
    return 0;
}

No comments:

Post a Comment