Δημοσιεύτηκε: 12 Απρ 2012, 19:05
Έτοιμη και η άσκηση με τα ζώα του ζωολογικού κήπου. 
Χρησιμοποίησα μία ordered linked list για την υλοποίηση.
Για είσοδο απ' τον χρήστη χρησιμοποίησα την read_line() του King και την fgets().
Για τον έλεγχο αν η είσοδος είναι valid ή όχι έγραψα την δικά μου valid_input().
Να και μια session:
Χρησιμοποίησα μία ordered linked list για την υλοποίηση.
Για είσοδο απ' τον χρήστη χρησιμοποίησα την read_line() του King και την fgets().
Για τον έλεγχο αν η είσοδος είναι valid ή όχι έγραψα την δικά μου valid_input().
- Μορφοποιημένος Κώδικας: Επιλογή όλων
-
#include <stdio.h> // printf(), puts(), scanf(), fgets(), getchar(), putchar()
#include <stdlib.h> // malloc(), free(), strtol(), exit(), EXIT_SUCCESS, EXIT_FAILURE
#include <stdbool.h> // bool, true, false
#include <ctype.h> // isspace()
#include <string.h> // strcmp()
#define MAX_INPUT (255+1)
#define LEN_WORD (50+1)
typedef struct node {
char animal[LEN_WORD];
struct node *next;
} Node;
typedef struct {
Node *head;
int len;
} List;
/*******************************************************************
* read_line: Skips leading white-space characters, then reads the *
* remainder of the input line and stores it in str. *
* Truncates the line if its length exceeds n. *
* Returns the number of characters stored. *
*******************************************************************/
int read_line(char str[], int n)
{
int ch, i = 0;
while (isspace(ch = getchar()))
;
while (ch != '\n' && ch != EOF) {
if (i < n)
str[i++] = ch;
ch = getchar();
}
str[i] = '\0';
return i;
}
/*********************************************************************
* valid_input: Skips leading white-space characters, then reads the *
* first character and stores it in tmp. Returns whether *
* tmp is a valid input (char between '1' and '4'). *
*********************************************************************/
bool valid_input(char *s)
{
char tmp, *ch = s;
for (; *ch && isspace(*ch); ch++)
; // skip any blanks
tmp = *ch;
if (tmp < '1' || tmp > '4')
return false;
if (*++ch)
for (; *ch; ch++)
if (!isspace(*ch))
return false;
return true;
}
/*********************************************************************
* insert: Add a new node to a sorted list. List is sorted *
* alphabetically. Asks user for an animal name and checks *
* if it is already exists in the list. *
* Returns true if node stored properly, else false. *
*********************************************************************/
bool insert(Node **list)
{
Node *cur, *prev, *new_node;
new_node = malloc(sizeof(Node));
if (new_node == NULL) {
puts("The zoo is full; can't add more animals.");
return false;
}
printf("\nEnter animal: ");
read_line(new_node->animal, LEN_WORD);
for (cur = *list, prev = NULL;
cur != NULL && strcmp(new_node->animal, cur->animal) > 0;
prev = cur, cur = cur->next)
;
if (cur != NULL && (!strcmp(new_node->animal, cur->animal))) {
printf("%s already in the zoo.\n", new_node->animal);
free(new_node);
return false;
}
new_node->next = cur;
if (prev == NULL)
*list = new_node;
else
prev->next = new_node;
printf("%s was added.\n", new_node->animal);
return true;
}
/*********************************************************************
* delete: Delete a node from a sorted list. List is sorted *
* alphabetically. Asks user for an animal name and checks *
* if it is already exists in the list. *
* Returns true if node removed properly, else false. *
*********************************************************************/
bool delete(Node **list)
{
Node *cur, *prev;
char animal[LEN_WORD];
printf("\nEnter the name of the animal that you want to kill: ");
read_line(animal, LEN_WORD);
for (cur = *list, prev = NULL;
cur != NULL && strcmp(animal, cur->animal) > 0;
prev = cur, cur = cur->next)
;
if (cur == NULL || strcmp(animal, cur->animal)) {
puts("No animal with such a name. Fortunately!");
return false;
}
if (prev == NULL)
*list = cur->next;
else
prev->next = cur->next;
printf("You just killed the %s. Are you completely heartless?\n", cur->animal);
free(cur);
return true;
}
/********************************************************
* list_print: Prints the contents of a linked list. *
********************************************************/
void list_print(Node *list)
{
puts("\nAnimals sorted by name: ");
for (; list != NULL; list = list->next)
printf("%s, ", list->animal);
putchar('\n');
putchar('\n');
}
/*********************************************************************
* list_destroy: Deallocate memory of all members of a linked list. *
*********************************************************************/
void list_destroy(Node *list)
{
if (list == NULL)
return;
Node *dummy = NULL;
for (; list != NULL; list = dummy) {
dummy = list->next;
free(list);
}
}
/**************************************************************************
* main: Prints a list of available choices and asks user to select one. *
* User can insert or kill an animal, print a sorted list of all *
* animals or quit the program.
*************************************************************************/
int main(void)
{
char input[MAX_INPUT];
List zoo;
zoo.head = NULL;
zoo.len = 0;
puts("Welcome to the zoo!");
puts("-------------------");
for (;;) {
puts("\n1. Insert animal\n2. Kill animal\n"
"3. Print all animals\n4. Quit\n");
if (valid_input(fgets(input, sizeof(input), stdin)))
switch(strtol(input, NULL, 10)) {
case 1:
if (insert(&zoo.head))
zoo.len++;
break;
case 2:
if (delete(&zoo.head))
zoo.len--;
break;
case 3:
list_print(zoo.head);
printf("There are %d animals in total.\n", zoo.len);
break;
case 4:
puts("\nGoodbye!");
goto exit_success;
break;
}
else
puts("invalid option");
}
exit_success:
list_destroy(zoo.head);
exit(EXIT_SUCCESS);
}
Να και μια session:
- Κώδικας: Επιλογή όλων
Welcome to the zoo!
-------------------
1. Insert animal
2. Kill animal
3. Print all animals
4. Quit
1
Enter animal: Cheetah
Cheetah was added.
1. Insert animal
2. Kill animal
3. Print all animals
4. Quit
1
Enter animal: Cobra
Cobra was added.
1. Insert animal
2. Kill animal
3. Print all animals
4. Quit
1
Enter animal: Alligator
Alligator was added.
1. Insert animal
2. Kill animal
3. Print all animals
4. Quit
3
Animals sorted by name:
Alligator, Cheetah, Cobra,
There are 3 animals in total.
1. Insert animal
2. Kill animal
3. Print all animals
4. Quit
1
Enter animal: American Bison
American Bison was added.
1. Insert animal
2. Kill animal
3. Print all animals
4. Quit
3
Animals sorted by name:
Alligator, American Bison, Cheetah, Cobra,
There are 4 animals in total.
1. Insert animal
2. Kill animal
3. Print all animals
4. Quit
2
Enter the name of the animal that you want to kill: Cobra
You just killed the Cobra. Are you completely heartless?
1. Insert animal
2. Kill animal
3. Print all animals
4. Quit
2
Enter the name of the animal that you want to kill: Badger
No animal with such a name. Fortunately!
1. Insert animal
2. Kill animal
3. Print all animals
4. Quit
2
Enter the name of the animal that you want to kill: American Bison
You just killed the American Bison. Are you completely heartless?
1. Insert animal
2. Kill animal
3. Print all animals
4. Quit
3
Animals sorted by name:
Alligator, Cheetah,
There are 2 animals in total.
1. Insert animal
2. Kill animal
3. Print all animals
4. Quit
fda
invalid option
1. Insert animal
2. Kill animal
3. Print all animals
4. Quit
43
invalid option
1. Insert animal
2. Kill animal
3. Print all animals
4. Quit
4
Goodbye!