Τα πάντα για την C

...του ubuntu και έργων ΕΛ/ΛΑΚ (Έργα-Οδηγοί-Προτάσεις)

Συντονιστής: konnn

Re: Τα πάντα για την C

Δημοσίευσηαπό mpekatsoula » 12 Απρ 2012, 01:27

Ilias95 έγραψε:
3. Το παρακάτω sample μου δίνει έξοδο 14:
Μορφοποιημένος Κώδικας: Επιλογή όλων
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int *a;

a = malloc(3 * sizeof(int));
a[0] = 4;
a[1] = 3;
a[7] = 14;
printf("%d\n", a[7]);

return 0;
}

Πως γίνεται να δουλεύει σωστά; Αφού υποτίθεται ότι η a έχει δεσμευμένο χώρο για 3 στοιχεία.
Δουλεύει κατά τύχη ή συμβαίνει κάτι άλλο;


Όταν καλείς malloc, και για τόσο μικρές τιμές, δεσμεύεται παραπάνω χώρος (αν δεν κάνω λάθος, γύρω στα 4kb) κυριώς για performance issues. Γιαυτό και δεν σου χτυπάει και δουλεύει "σωστά".
Volos LUG
stack0verflow
As a wise Chinese man once said: ‘do not anger one who has shell on your server’.
mpekatsoula
babeTUX
babeTUX
 
Δημοσιεύσεις: 28
Εγγραφή: 09 Ιουν 2010, 13:26
Τοποθεσία: Volos/Xalkida
IRC: mpekatsoula
Εκτύπωση

Re: Τα πάντα για την C

Δημοσίευσηαπό migf1 » 12 Απρ 2012, 18:42

Off topic:
mpekatsoula έγραψε:
...
stack0verflow

Respect! :)
Go under the hood with C: Pointers, Strings, Linked Lists
Άβαταρ μέλους
migf1
powerTUX
powerTUX
 
Δημοσιεύσεις: 2082
Εγγραφή: 03 Ιουν 2011, 16:32
Εκτύπωση

Re: Τα πάντα για την C

Δημοσίευσηαπό Ilias95 » 12 Απρ 2012, 19:05

Έτοιμη και η άσκηση με τα ζώα του ζωολογικού κήπου. :P

Χρησιμοποίησα μία 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!
Ilias95
saintTUX
saintTUX
 
Δημοσιεύσεις: 1548
Εγγραφή: 29 Απρ 2011, 23:26
Εκτύπωση

Re: Τα πάντα για την C

Δημοσίευσηαπό migf1 » 12 Απρ 2012, 19:12

Αυτά είναι!!!!!!!!!!!!!!!!!!!!!!!

Πρόταση: αντί για "Insert animal", "Capture animal" :D

Ωραίος Ηλία!! :)
Go under the hood with C: Pointers, Strings, Linked Lists
Άβαταρ μέλους
migf1
powerTUX
powerTUX
 
Δημοσιεύσεις: 2082
Εγγραφή: 03 Ιουν 2011, 16:32
Εκτύπωση

Re: Τα πάντα για την C

Δημοσίευσηαπό Ilias95 » 12 Απρ 2012, 19:16

migf1 έγραψε:Πρόταση: αντί για "Insert animal", "Capture animal" :D

:lol: :lol: :lol:
Ναι και θα τους σιχτιρίζω και σε κάθε επιτυχή αιχμαλώτιση!
Θα φοβάται κανείς να τρέξει το πρόγραμμα! :P :lol:
Ilias95
saintTUX
saintTUX
 
Δημοσιεύσεις: 1548
Εγγραφή: 29 Απρ 2011, 23:26
Εκτύπωση

Re: Τα πάντα για την C

Δημοσίευσηαπό migf1 » 12 Απρ 2012, 19:22

Βάλε κι ένα menu option "Γενοκτονία" που θα τα σκοτώνει όλα με τη μια :lol:
Go under the hood with C: Pointers, Strings, Linked Lists
Άβαταρ μέλους
migf1
powerTUX
powerTUX
 
Δημοσιεύσεις: 2082
Εγγραφή: 03 Ιουν 2011, 16:32
Εκτύπωση

Re: Τα πάντα για την C

Δημοσίευσηαπό migf1 » 12 Απρ 2012, 19:37

Λοιπόν, άκου ιδέα!

1. Shoot animal
2. Release animal
3. Display captured
4. Leave jungle

Το "Shoot animal" θα παράγει έναν τυχαίο αριθμό από το 0 έως το πλήθος γραμμάτων του ζώου. Αν ο παραγόμενος αριθμός είναι μεγαλύτερος ας πούμε από τα 2/3 των συνολικών γραμμάτων που αποτελούν το όνομα του ζώου, τότε θα γίνεται αυτόματα captured, γιατί και καλά το πλήγωσες πάρα πολύ ή/και το σκότωσες, οπότε μπορείς να το... συλλάβεις.

Αν ο παραγόμενος τυχαίος αριθμός είναι μικρότερος από τα 2/3 των συνολικών γραμμάτων του ζώου, τότε απλά θα τραυματίζεται αλλά θα μπορεί να φύγει. Μπορείς να γράφεις κάτι σαν "το πέτυχες, αλλά αν και τραυματισμένο κατάφερε να τρέξει μακριά" :lol: και για εφέ, μπορείς για παράδειγμα να εμφανίζεις το όνομα του ζώου με τόσα λιγότερα γράμματα όσα ο τυχαίος αριθμός.

Π.χ. Shoot zebra
Τυχαίος παραγόμενος: 2
Z*b*a was hurt, but got away (τα αστέρια θα είναι μεν 2, αλλά θα μπαίνουν σε τυχαίες θέσεις).

Το πρόγραμμα ονόμασε το "Hunt" :lol:

ΥΓ. Ψιτ, είσαι να πάμε για κυνήγι :lol:
Go under the hood with C: Pointers, Strings, Linked Lists
Άβαταρ μέλους
migf1
powerTUX
powerTUX
 
Δημοσιεύσεις: 2082
Εγγραφή: 03 Ιουν 2011, 16:32
Εκτύπωση

Re: Τα πάντα για την C

Δημοσίευσηαπό Ilias95 » 12 Απρ 2012, 19:47

:lol:
Έγινε, θα το ετοιμάσω!
Οπότε άκυρη η επιλογή Genocide. :P

migf1 έγραψε:ΥΓ. Ψιτ, είσαι να πάμε για κυνήγι :lol:

Αφού το παίζω φιλόζωος και καλά ρε! :lol:

Off topic:
Καμιά ιδέα για το πως θα πούμε στα αγγλικά: "Ένα ελεύθερο ζώο λιγότερο.";
Ilias95
saintTUX
saintTUX
 
Δημοσιεύσεις: 1548
Εγγραφή: 29 Απρ 2011, 23:26
Εκτύπωση

Re: Τα πάντα για την C

Δημοσίευσηαπό migf1 » 12 Απρ 2012, 19:56

One free/unleashed animal less (αλλά δεν ακούγεται ωραία)

Αν είναι κάνε το σε στυλ game, μη βάλεις μενού... άφηνε τον χρήστη να πληκτρολογεί τις εντολές (όποιες του παρέχεις, π.χ. s ή shoot, c ή capture, h ή help, κλπ)... μπορείς επίσης να έχεις μια συγκεκριμένη λίστα από ζώα κι αν σου δώσει κάτι που δεν υπάρχει, να του γράφεις π.χ. no such animal in this jungle :lol:

Μπορείς επίσης να τον αφήνεις να πυροβολεί μέχρι ένα όριο φορών (και καλά μετά ξεμένει από πυρομαχικά).
Go under the hood with C: Pointers, Strings, Linked Lists
Άβαταρ μέλους
migf1
powerTUX
powerTUX
 
Δημοσιεύσεις: 2082
Εγγραφή: 03 Ιουν 2011, 16:32
Εκτύπωση

Re: Τα πάντα για την C

Δημοσίευσηαπό migf1 » 12 Απρ 2012, 20:06

Μου θυμίζει text-adventure της δεκαετίας του '80 :lol: ... πλάκα-πλάκα τι ωραία που ήταν;

Μου 'ρθε κι άλλη ιδέα, μπορείς να μην τον αφήνεις να γράφει ότι ζώο θέλει, μπορείς για παράδειγμα κάθε φορά που τρέχει το πρόγραμμα να βγάζεις έναν τυχαίο αριθμό από ζώα της λίστας σου (ίσως με κάποιο όριο, δηλαδή αν έχεις 10 ζώα συνολικά στη λίστα σου, να βγάζει ξέρω γω μέχρι 3) και να του γράφεις...

"there is 1 zebra and 2 rabbits in the scene "

και να του δίνεις 3 shots, μετά να ξεκινάει νέος γύρος, κλπ... κι όταν βαρεθεί να γράφει π.χ. "leave" και να τερματίζει το πρόγραμμα.
Go under the hood with C: Pointers, Strings, Linked Lists
Άβαταρ μέλους
migf1
powerTUX
powerTUX
 
Δημοσιεύσεις: 2082
Εγγραφή: 03 Ιουν 2011, 16:32
Εκτύπωση

ΠροηγούμενηΕπόμενο

Επιστροφή στο Ανάπτυξη Λογισμικού / Αλγόριθμοι