Beispiel #1
0
int main(int argc, char** argv) 
{
    char *userChoice, *computerChoice, *outcome; //pointers to specific functions
    char* r = malloc(sizeof(char) * 128); //create string
    do
    {
        userChoice = getUserChoice();
        printf("You picked %s.\n", userChoice);
        
        computerChoice = getComputerChoice();
        printf("Computer picked %s.\n", computerChoice);
        
        outcome = compare(userChoice, computerChoice);
        printf("%s\n", outcome);
        printf("Play again? ");
        scanf("%s",r);
    }
    while(strcmp(r, "yes") == 0 || strcmp(r, "Y") == 0 || strcmp(r, "YES") == 0 || strcmp(r, "y") == 0); //to repeat press one the of following options
    free(r); //free malloc memory in r
    free(userChoice); //free malloc memory in the userChoice function
    free(computerChoice); //free malloc memory in the computerChoice function
    free(outcome); //free malloc memory in main; outcome.
    return 0;
}
Beispiel #2
0
int main()
{
    int looper = 1;

	while(looper)
    {
        userIO();
		switch(getUserChoice())
		{
			case 1: head = addNodeUlti(head);
			break;
			case 2: deleteNode();
			break;
			case 3: traverseChain();
			break;
            case 4: looper = 0;
			break;
			default: printf("Only 1-4 permitted\n");
			break;
		}
    }
	getchar();
	return 0;
}
Beispiel #3
0
void deleteNode()
{
    if(head == 0)
    {
        printf("You don't have any nodes to delete.\n");
        return;
    }

    traverseChain();
    printf("which nodes do you want to delete,\n"
            "choose between 1 and %d\n", getCount());
    int choice;
    choice = getUserChoice();

    if(choice < 1 || choice > getCount())
    {
        printf("You must choose between 1 and %d\n", getCount());
        return;
    }
    else
    {
        node* tempNode = head;
        int i = 1;

        for(i; i < choice; i++)
            tempNode = tempNode->next;

        printf("Node %i: %f Deleted\n",i, tempNode->data);

        if(getCount() == 1)
        {
            printf("\nVid en ensam\n");
            free(head);
            head = NULL;
        }
        else if(getCount() > 1 && choice == 1)
        {
            printf("\nVid första men fler i listan\n");
            node *temp = head;
            head = head->next;
            head->previous = 0;
            free(temp);
            temp = NULL;
            //Ta bort första
            //Sätt prev på 2 till 0
            //sätt head som nummer två
        }
        else if(choice == getCount())
        {
            printf("\nTa bort sista noden\n");
            node* temp = tempNode->previous;
            free(tempNode);
            tempNode = NULL;
            temp->next = 0;
            //Ta bort sista
            //Sätt next på näst sista som noll
        }
        else
        {
            printf("\nI mitten någonstans\n");
            node* temp = tempNode->previous;
            temp->next = tempNode->next;
            temp = tempNode->next;
            temp->previous = tempNode->previous;
            free(tempNode);
        }
    }
}