markiemrboo Posted June 1, 2005 Posted June 1, 2005 main.c #include <stdio.h> #include <conio.h> #include "qa.h" int main(char **argv, int argc) { struct LinkedList list; int choice; // zero memory memset(&list, 0, sizeof(struct LinkedList)); // how to use functions in qa.c // print a question!!! Question("Hello World?"); // add answers to a very simple linked list AddAnswer(&list, "No"); AddAnswer(&list, "Yes"); AddAnswer(&list, "Grapefruit"); // display answers in list and return the user choice. the true specifies whether to free() each answer after displaying it or not. choice = Answer(&list, TRUE); printf("Option %d selected\n", choice); getch(); return 0; } qa.c #include "qa.h" void Question(char *msg) { int strLen = strlen(msg); int i; for (i = 0; i < strLen; i++) printf("="); printf("\n%s\n",msg); for (i = 0; i < strLen; i++) printf("="); printf("\n"); } void AddAnswer(struct LinkedList *list, char *anAnswer) { // pointless really, but it gives me an excuse to have linkedlist.c AddToList(list, anAnswer); } int Answer(struct LinkedList *list, BOOLEAN autoFree) { int i, choice = 0; BOOLEAN choiceValid = TRUE; // don't use `static char *input` here if you free it at the end of the // function. i'm a stupid dumb arse!!!! char *input = (char *)malloc(3 * sizeof(char)); // check malloc for input buffer assert(input != NULL); memset(input, 0, 3 * sizeof(char)); // test if the list has been initialized if (list->head != NULL) { // loop through list list->curr = list->head; i = 1; while (list->curr) { printf("%d. %s\n", i, list->curr->data); if (autoFree) { list->tmp = list->curr; } list->curr = list->curr->next; if (autoFree) { if (list->tmp != NULL) { free(list->tmp); } } i++; } printf("\n> "); // user choice do { if (!choiceValid) { printf("(Invalid) > "); } fgets(input, 3, stdin); choice = atoi(input); choiceValid = FALSE; } while (choice < 1 || choice > list->length); free(input); // reset list for next answers if (autoFree) { // dont free head here either, retard. it's already been free'd in the // while loop memset(list, 0, sizeof(struct LinkedList)); } } else { printf("ERROR: Answers haven't been initialized\n"); } return choice; } qa.h #ifndef QA_H #define QA_H #include <stdio.h> #include <malloc.h> #include <string.h> #include <assert.h> #include "linkedlist.h" typedef enum {FALSE = 0, TRUE = 1} BOOLEAN; struct Answers { int next; int max; char **answers; }; void Question(char *); int Answer(struct LinkedList *, BOOLEAN); void AddAnswer(struct LinkedList *, char *); #endif linkedlist.c #include "linkedlist.h" void AddToList(struct LinkedList *list, char *data) { if (list->head == NULL) { list->head = (struct LinkedListNode *)calloc(1, sizeof(struct LinkedListNode)); assert(list->head != NULL); memset(list->head, 0, sizeof(struct LinkedListNode)); list->tail = list->head; list->tail->data = data; } else { list->tail->next = (struct LinkedListNode *)calloc(1, sizeof(struct LinkedListNode)); assert(list->tail->next != NULL); memset(list->tail->next, 0, sizeof(struct LinkedListNode)); list->tail = list->tail->next; list->tail->data = data; } list->length++; } linkedlist.h #ifndef LINKEDLIST_H #define LINKEDLIST_H #include <stdio.h> #include <assert.h> #include <malloc.h> #include <string.h> struct LinkedListNode { char *data; struct LinkedListNode *next; }; struct LinkedList { struct LinkedListNode *curr; struct LinkedListNode *head; struct LinkedListNode *tail; struct LinkedListNode *tmp; int length; }; void AddToList(struct LinkedList *, char *); #endif I hate having to zero the memory to begin with, but i can't figure a way round that. Might be handy for menu's (that's what I used it for). See main() for usage.. Any pointers or... suggestions how to do it all better... orrr whatever, i'd appreciate it. I can't help but think it's a giant mess that just happens to work reasonably well Quote Share this post Link to post Share on other sites More sharing options...
Bleeble Posted June 1, 2005 Posted June 1, 2005 I don't really have any suggestions because I get lost after "Today, 10:10 PM", but I was just wondering what it does. Quote Share this post Link to post Share on other sites More sharing options...
Kamikaze_Badger Posted June 1, 2005 Posted June 1, 2005 Bored, are we? Quote Share this post Link to post Share on other sites More sharing options...
markiemrboo Posted June 1, 2005 Posted June 1, 2005 Bored, are we? 485301[/snapback] A little bit! I finished Uni till September so... I used the above to make simple text menus Quote Share this post Link to post Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.