图书管理 c

#ifndef _LIST1_H_
#define _LIST1_H_
#include <stdbool.h>
#define TMAX 50
struct item
{
	char title[TMAX];
	float price;
};
typedef struct item Item;
struct node
{
	Item item;
	struct node * next;
};
typedef struct node Node;
typedef Node * List;

void InitList (List * plist);
bool IsEmpty (List * plist);
bool IsFull (List * plist);
bool Insert (Item item, List * plist, int num);
bool Add (Item item, List * plist);
bool Delete (int num, List * plist);
bool DeleteAll (List * plist);
int Count (List * plist);
Node * FindNode (char * ptitle, List * plist);
bool ModiNode (char * ptitle, List * plist, void (* pfun) (Node * pnode));
void PrintList (List * plist, void (* pfun) (Item item, int num));
bool Backup (List * plist);

#endif
 
操作函数
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "list1.h"

static void ItemToNode (Item item, Node * pnode);
void InitList (List * plist)
{
	*plist = NULL;
}

bool IsEmpty (List * plist)
{
	if (*plist == NULL)
		return true;
	else
		return false;
}
bool IsFull (List * plist)
{
	Node * pnew;

	pnew = malloc (sizeof (Node));
	if (pnew == NULL)
	{
		fprintf (stderr, "memory is full\n");
		return true;
	}
	
	free (pnew);
	
	return false;
}

bool Insert (Item item, List * plist, int num)
{
	Node * pn;
	Node * pt = *plist;
	int i;
	i = Count (plist);
	
	if (IsFull (plist))
	{
		return false;
	}

	pn = malloc (sizeof (Node));
	ItemToNode (item, pn);

	if (i == 0) //when the list is empty
		*plist = pn;
	else
	{
		if (FindNode (item.title, plist) != NULL)
		{
			printf ("there are the same item in the list.\n");
			return false;
		}

		if (num > i)
		{
			printf ("the list only has %d item, it will insert to the last\n", i);
			while (pt->next != NULL)
				pt = pt->next;
			pt->next = pn;
		}
		else
		{
			if (num <= 1)
			{
				pn->next = pt;
				*plist = pn;
			}
			else
			{
				while (--num > 1)
					pt = pt->next;
				pn->next = pt->next;
				pt->next = pn;
			}
		}
	}
	return true;
}

bool Add (Item item, List * plist)
{	
	Node * pn;
	Node * pt = *plist;
	char * ptitle = item.title;
	
	if (IsFull (plist))
		return false;
	pn = malloc (sizeof (Node));
	ItemToNode (item, pn);
	if (IsEmpty (plist))
		*plist = pn;
	else
	{
		if (FindNode (ptitle, plist) != NULL)
		{
			printf ("there are the same item in the list.\n");
			return false;
		}
	
		while (pt->next != NULL)
			pt = pt->next;
		pt->next = pn;
	}
	return true;
}

bool Delete (int num, List * plist)
{
	Node * pn = *plist;
	Node * pt;
	int i = Count (plist);
	if (num > i)
	{
		printf ("it has only %d books in the list\n", i);
		return false;
	}
	else if (num <= 0)
	{
		printf ("enter the 1 - %d\n", i);
		return false;
	}
	else if (num == 1)
	{
		*plist = pn->next;
		free (pn);
		pn = NULL;
	}
	else
	{
		while (--num > 1)
			pn = pn->next;
		pt = pn->next;
		pn->next = pt->next;
		free (pt);
		pt = NULL;
	}
	return true;
}

bool DeleteAll (List * plist)
{
	Node * pt;
	if (*plist == NULL)
		printf ("List is Empty");
	else
	{
		while (*plist != NULL)
		{
			pt = (*plist)->next;
			free (*plist);
			*plist = pt;
		}
	}
	return true;
}

int Count (List * plist)
{
	int count = 0;
	Node * pt = *plist;
	while (pt != NULL)
	{
		++count;
		pt = pt->next;
	}

	return count;
}

Node * FindNode (char * title, List * plist)
{
	Node * pt = *plist;
	while (strcmp (title, pt->item.title) != 0)
	{
		pt = pt->next;
		if (pt == NULL)
			return pt;
	}
	printf ("<< %s >>  price: ( %.2f )  in the list!!\n",  pt->item.title, pt->item.price);
	return pt;
}

bool ModiNode (char * ptitle, List * plist, void (* pfun) (Node * pnode))
{
	Node * pt;
	pt = FindNode (ptitle, plist);

	if (pt == NULL)
	{
		printf ("can't find which you want to modify.\n");
		return false;
	}

	(* pfun) (pt);

	return true;
}

void PrintList (List * plist, void (* pfun) (Item item, int num))
{
	Node * pn = *plist;
	int num = 1;
	if (*plist == NULL)
		printf ("list is empty\n");
	else
	{
		while (pn != NULL)
		{	
			(* pfun) (pn->item, num++);
			pn = pn->next;
		}
	}
}

bool Backup (List * plist)
{
	FILE * fp;
	Node * pn = *plist;
	int num = 1;
	if (*plist == NULL)
		printf ("list is empty\n");
	else
	{
		if ((fp = fopen ("books.txt", "a+")) == NULL)
		{
			fprintf (stdout, "can't open \"books\" file.\n");
			return false;
		}
		while (pn != NULL)
		{
			
			fprintf (fp, "%d. << %s >>        price: ( %.2f$ )\n", num,  pn->item.title, pn->item.price);
			pn = pn->next;
			num++;
		}
		return true;
	}
}

static void ItemToNode (Item item, Node * pnode)
{
	pnode->item = item;
	pnode->next = NULL;
}
实现函数
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "list1.h"
#define SPACE printf("\n                         ****************************************************************\n")

void A (List * plist, int num);
void B (List * plist);
void C (List * plist);
void D (List * plist);
void E (List * plist);
void F (List * plist);
void G (List * plist);
void showmenu (void);
void showCount (List * plist);
void show (Item item, int num);
void changeitem (Node * pnode);

int main (void)
{
	List books;
	Item temp;
	char chose;

	SPACE;
	puts ("                                    this is a book list");
	showmenu ();

	InitList (&books);
	if (IsFull (&books) == true)
   {
 		fprintf (stderr, "the memory has no size\n");
		exit (1);
	}
	scanf ("%c", &chose);
	while (getchar () != '\n')
		continue;
	while ( chose != 'q')
	{
		switch (chose - 48)
		{
			case 1: puts ("wellcome to add books: ");
					  A (&books, 1);
 					  break;
			case 2: puts ("wellcome to add books where you want: ");
					  B (&books);
					  break;
			case 3: puts ("you can delete the book in the list");
					  C (&books);
					  break;
			case 4: puts ("are you sure to delete all the books? (y or n)");
					  D (&books);
					  break;
			case 5: E (&books);
					  break;
			case 6: F (&books);
					  break;
			case 7: SPACE;
					  PrintList (&books, show);
					  showCount (&books);
					  SPACE;
					  break;
			case 8: A (&books, 2);
					  break;
			case 9: showmenu();
					  break;
			case 0: G (&books);
					  break;
			default :puts ("please enter 0-10 or 'q' to quit: "); 
					  break;
		}

		puts ("if you want to quit please enter 'q':");
		scanf ("%c", &chose);
		while (getchar () != '\n')
			continue;
	}
	return 0;
}

/**************************************************************/
void A (List * plist, int select)
{
	Item temp;
	
	if (IsFull (plist))
		printf ("the list is full\n");
	else
	{
		puts ("enter the book name (less than 50 characters): ");
		gets (temp.title);
		if (select == 1)
		{
			if (temp.title[0] != '\0')
			{
				puts ("enter the book's price: ");
				scanf ("%f", &temp.price);
				while (getchar () != '\n')
					continue;
			}

			if (Add (temp, plist))
				printf ("add success\n");
			else
				puts ("you failse");
		}
		else
		{
			while (temp.title[0] != '\n' && temp.title[0] != '\0')
			{
				puts ("enter the book's price: ");
				scanf ("%f", &temp.price);
				while (getchar () != '\n')
					continue;

				if (Add (temp, plist))
					printf ("add success\n");
				else
					puts ("you failse");
				
				puts ("enter the next book's name,empty line to quit: ");
				gets (temp.title);
			}
		}
	}

	showmenu ();
}

/**************************************************************/
void B (List * plist)
{
	int num;
	Item temp;
	if (IsFull (plist))
		printf ("the list is full\n");
	else
	{
		puts ("enter the book name (less than 50 characters): ");
		gets (temp.title);
		if (temp.title[0] != '\0')
		{
			puts ("enter the book's price: ");
			scanf ("%f", &temp.price);
			while (getchar () != '\n')
				continue;
		}

		puts ("enter the num where you want to insert");	
		scanf ("%d", &num);
		while (getchar () != '\n')
			continue;
		if(Insert (temp, plist, num) == true)
			puts ("insert success");
	}
}

/**************************************************************/
void C (List * plist)
{
	int num;
	Node * delp;
	Node * pt = *plist;
	if (IsEmpty (plist) == true)
		printf ("the list is empty you should add some books in it.\n");
	else
	{		
		PrintList (plist, show);
		puts ("enter the book's num or the book's name you want to delete ");
		scanf ("%d", &num);
		while (getchar () != '\n')
			continue;
		if (Delete (num, plist) == true)
			printf ("delete success\n");
		else
			puts ("delete failed");
	}
}

/**************************************************************/
void D (List * plist)
{
	char ans;
	ans = getchar ();
	if (ans == 'y')
	{
		if (DeleteAll (plist) == true)
			printf ("delete all success\n");
		else
			puts ("you failed");
	}
	else
		puts ("ok we will return");
}

/**************************************************************/
void E (List * plist)
{
	char title[TMAX];
	char ans;
	if (IsEmpty (plist))
		printf ("the list is empty, you should enter some books\n");
	else
	{
		PrintList (plist, show);
		puts ("enter the book name which you want to alter:  ");
		scanf ("%s", title);
		if (ModiNode (title, plist, changeitem) == true)
		{
			puts ("you has change it!~ do you want to show the list (y or n)");
			while ((ans = getchar ()) == '\n')
				continue;
			if (ans == 'y')
				PrintList (plist, show);
		}
		else
			puts ("you failed");
	}
}

/**************************************************************/
void F (List * plist)
{
	char title[TMAX];
	if (IsEmpty (plist))
		printf ("the list is empty, you should enter some books\n");
	else
	{
		puts ("enter the book name:");
		gets (title);
		if (FindNode (title, plist) == false)
			printf ("%s is not in the list\n", title);
	}
}

/**************************************************************/
void G (List * plist)
{
	if (Backup (plist) == true)
		puts ("enter success");
	else
		puts ("enter failed");
}

/**************************************************************/
void showmenu (void)
{
	SPACE;
	puts ("                                1) add a book               2) add a book where you want");
	puts ("                                3) delete the book          4) delete all books");
	puts ("                                5) modify the book          6) find the book");
	puts ("                                7) show the book list       8) add some books");	
	puts ("                                9) showmenu                10) backup the list");
	puts ("                                q) quit");
	SPACE;

}

/**************************************************************/
void show (Item item, int num)
{
	printf ("                                       %d.  << %s >>      ( %.2f$ )\n", num, item.title, item.price);
}

/**************************************************************/
void showCount (List * plist)
{
	int num = Count (plist);
	printf ("                                           ~~~ there are %d books in the list ~~~\n", num);
}

/**************************************************************/
void changeitem (Node * pnode)
{
	puts ("enter the new book name: ");	
	scanf ("%s", pnode->item.title);
	puts ("enter the new price");
	scanf ("%f", &pnode->item.price);
}
 

 

你可能感兴趣的:(C++,c,C#,F#,FP)