Vấn đề
Cách lưu trữ thông tin liên quan đến sách của thư viện bằng lập trình C.
Thuật toán
Step 1: Declare a structure which holds data members Step 2: declare variables which are used for loop Step 3: use switch case to work on each module Step 4: case 1- for Adding book information Case 2- for Display book information Case 3- for Finding number for books in library Case 4- for EXIT
Chương trình
#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<string.h> struct library{ char bookname[50]; char author[50]; int noofpages; float price; }; int main(){ struct library lib[100]; char bookname[30]; int i,j, keepcount; i=j=keepcount = 0; while(j!=6){ printf("\n1. Add book information\n"); printf("2.Display book information\n"); printf("3. no of books in the library\n"); printf("4. Exit"); printf ("\n\nEnter one of the above : "); scanf("%d",&j); switch (j){ /* Add book */ case 1: printf ("Enter book name = "); scanf ("%s",lib[i].bookname); printf ("Enter author name = "); scanf ("%s",lib[i].author); printf ("Enter pages = "); scanf ("%d",&lib[i].noofpages); printf ("Enter price = "); scanf ("%f",&lib[i].price); keepcount++; i++; break; case 2: printf("you have entered the following information\n"); for(i=0; i<keepcount; i++){ printf ("book name = %s\n",lib[i].bookname); printf ("\t author name = %s\n",lib[i].author); printf ("\t pages = %d\n",lib[i].noofpages); printf ("\t price = %f\n",lib[i].price); } break; case 3: printf("\n No of books in library : %d", keepcount); break; case 4: exit (0); } } return 0; }
Đầu ra
1. Add book information 2.Display book information 3. no of books in the library 4. Exit Enter one of the above : 1 Enter book name = HarryPotter Enter author name = hp Enter pages = 250 Enter price = 350.6 1. Add book information 2.Display book information 3. no of books in the library 4. Exit Enter one of the above : 2 you have entered the following information book name = HarryPotter author name = hp pages = 250 price = 350.600006 1. Add book information 2.Display book information 3. no of books in the library 4. Exit Enter one of the above : 3 No of books in library : 1 1. Add book information 2.Display book information 3. no of books in the library 4. Exit Enter one of the above : 4