/* Диалог создания записи о книге */
void new_dialog(BookDB* db) {
	Book* book =  new_book(db);
	int id;
	if (!book) return;
	output("Введите данные новой книги\n");
	output("ID: \n");
	input("%d", &id);
	getc(stdin);
	book->id = id;
	output("Название\n<< ");
	fgets(book->title, MAX_STRING_LENGTH, stdin); delns(book->title);
	output("Автор\n<< ");
	fgets(book->author, MAX_STRING_LENGTH, stdin); delns(book->author);
	output("Жанр\n<< ");
	fgets(book->ganre, MAX_STRING_LENGTH, stdin); delns(book->ganre);
	db->saved = 0;
}
示例#2
0
文件: adbook.c 项目: sduncan/Code
int main() {

  struct link **book;
  
  book = new_book();
  book = add_entry(book, "Eddie", "*****@*****.**");
  book = add_entry(book, "Ed", "*****@*****.**");
  book = add_entry(book, "HughJazz", "*****@*****.**");
  book = add_entry(book, "SeymorButz", "*****@*****.**");
  book = add_entry(book, "Fry", "*****@*****.**");
  book = add_entry(book, "Bender", "*****@*****.**");

  //OPEN FILE
  int fd = open("book.dat",  O_CREAT | O_RDWR, 0644);

  //WRITE TO FILE

  int b;
  char n[256];
  char e[256];
  int a;
  struct link *p;
  p = (struct link *)malloc(sizeof(struct link));
  for (a=0; a<26; a++){
    p = book[a];
    while(p){
      strncpy(n, return_name(p), 256);
      strncpy(e, return_email(p), 256);
      b = write(fd, n, 256);
      b = write(fd, e, 256);
      p = p->next;
    }
  }

  book = free_book( book );
  free(book);
 
  return 0;
}
示例#3
0
文件: main.c 项目: canvural/homeworks
int main()
{
    FILE *file = fopen("inputbookdb.txt", "r");
    if(NULL == file) {
        fprintf(stderr, "Can't open '%s' for reading!", "inputbookdb.txt");
        return 1;
    }

    // One line from commands file
    char *line;

    // One of the of publication type
    char op;

    // Rest of line
    char rline[256];

    char *name;
    char *author;
    int price;
    float p_price;
    int vol_no;
    Boolean in_stock;

    // Create new Binary Search Tree
    Node *root = NULL;



    while((line = read_line(file)) != NULL) {
        sscanf(line, "%c %255[^\n]", &op, rline);

        switch(op) {
            case 'B':
                name     = strtok(rline, "#");
                author   = strtok(NULL, "#");
                price    = strtol(strtok(NULL, "#"), NULL, 10);
                in_stock = strcmp(strtok(NULL, "#"), " T") == 0 ? true : false;

                root = insert(root, new_node(Book_t, new_book(name, author, price, in_stock)));
                break;
            case 'P':
                name     = strtok(rline, "#");
                author   = strtok(NULL, "#");
                p_price  = strtof(strtok(NULL, "#"), NULL);
                in_stock = strcmp(strtok(NULL, "#"), " T") == 0 ? true : false;
                vol_no   = strtol(strtok(NULL, "#"), NULL, 10);

                root = insert(root, new_node(Periodical_t, new_periodical(name, author, p_price, in_stock, vol_no)));
                break;
            case 'E':
                name     = strtok(rline, "#");
                author   = strtok(NULL, "#");
                price    = strtol(strtok(NULL, "#"), NULL, 10);
                in_stock = strcmp(strtok(NULL, "#"), " T") == 0 ? true : false;

                root = insert(root, new_node(Encyclopedia_t, new_encyclopedia(name, author, price, in_stock)));
                break;
            default:
                break;
        }
    }

    int i;
    Node *current_node = root;
    Node *parent = NULL;
    char i_name[61];
    char i_author[41];
    int i_price;
    float ip_price;
    int i_vol_no;
    int stock;
    Boolean i_in_stock;

    do {
        printf("\n");
        printf("1) Display\n");
        printf("2) Go Left\n");
        printf("3) Go Right\n");
        printf("4) Go up\n");
        printf("5) Delete\n");
        printf("6) Insert\n");
        printf("7) Save\n");
        printf("8) Quit\n");
        printf("\nEnter your choice:");
        scanf("%d", &i);

        switch(i) {
            case 1:
                display_node(current_node);
                break;
            case 2:
                if(NULL != current_node->left) {
                    parent = current_node;
                    current_node = current_node->left;
                }
                else
                    printf("Current node does not have a left node!\n");
                break;
            case 3:
                if(NULL != current_node->right) {
                    parent = current_node;
                    current_node = current_node->right;
                } else
                    printf("Current node does not have a right node!\n");
                break;
            case 4:
                if(NULL == parent) {
                    printf("Current node does not have a parent node!\n");
                } else {
                    current_node = parent;
                }
                break;
            case 5:
                delete_node(root, current_node);
                current_node = root;
                break;
            case 6:
                printf("1)Book\n2)Periodical\n3)Encyclopedia\nEnter the publication type: ");
                int pt;
                scanf("%d", &pt);
                switch(pt){
                    case 1:
                        printf("Enter the book name: ");
                        scanf("%s", i_name);
                        printf("Enter the book author: ");
                        scanf("%s", i_author);
                        printf("Enter the book price: ");
                        scanf("%d", &i_price);
                        printf("Enter the book is in store: ");
                        scanf("%d", &stock);
                        root = insert(root, new_node(Book_t, new_book(i_name, i_author, i_price, stock == 1 ? true: false)));
                        break;
                    case 2:
                        printf("Enter the Periodical name: ");
                        scanf("%s", i_name);
                        printf("Enter the Periodical editor: ");
                        scanf("%s", i_author);
                        printf("Enter the Periodical price: ");
                        scanf("%f", &ip_price);
                        printf("Enter the Periodical is in store: ");
                        scanf("%d", &stock);
                        printf("Enter the Periodical vol no: ");
                        scanf("%d", &i_vol_no);
                        root = insert(root, new_node(Periodical_t, new_periodical(i_name, i_author, ip_price, stock == 1 ? true: false, i_vol_no)));
                        break;
                    case 3:
                        printf("Enter the Encyclopedia name: ");
                        scanf("%s", i_name);
                        printf("Enter the Encyclopedia publisher: ");
                        scanf("%s", i_author);
                        printf("Enter the Encyclopedia price: ");
                        scanf("%d", &i_price);
                        printf("Enter the Encyclopedia is in store: ");
                        scanf("%d", &stock);
                        root = insert(root, new_node(Encyclopedia_t, new_encyclopedia(i_name, i_author, i_price, stock == 1? true: false)));
                        break;
                    default:
                        break;
                }
                break;
            case 8:
                break;
            default:
                break;
        }
    } while(i != 8);

    return 0;
}