"Guide Job"--product record

Source: Internet
Author: User
Tags rewind

Topic

description of the problem: There is a shop, in order to see what the store, the quantity and the price of each item, you need to prepare a catalog of products. Each product consists of 4 attributes: Product ID, trade name, quantity, and price. The Product ID is an integer between 0-99, each product has a different ID number, so there are only 100 products by default; The product name contains only uppercase and lowercase letters and spaces, the length of which is not more than 35 characters; The range of goods is 0-99999; the price of a commodity is a non-negative floating-point number.

You want to help the store design a binary file that stores all of the store's merchandise situation. The following table is a sample of the store's merchandise list.

Record number (item ID)

Product Name

Number

Price

5

Lamp

23

5.99

8

Screwdriver

9

7.99

10

Hammer

10

12.00

13

Bicycle

5

100.99

16

Optical Mouse

21st

11.50

25

Keyboard

32

59.99

Requirements:

1. Design a structure to represent a commodity.

2. Create a binary file named "Commodity.dat", store the store commodity information, initialize it to 100 empty records, use the commodity ID as the record number.

3. Implementation of the "Commodity.dat" in the initial entry of commodity information.

4. Update the product information in "Commodity.dat", where the record number is not updated and the other three items can be updated.

5. Implement the deletion of the product information in "Commodity.dat".

6. Implement the output of the product information in "Commodity.dat".

7. The functions of initialization of files, deletion and update of records, and output of file information in the above 2~6 are defined as separate functions.

Program Run Effect:

Please enter your choice:

Create a file with 100 empty records

2--Enter a product record

3--Update Product Records

4--Delete a product record

5--Output Product record

[Select]1

100 Empty Records created

Please enter your choice:

Create a file with 100 empty records

2--Enter a product record

3--Update Product Records

4--Delete a product record

5--Output Product record

[Select]2

Please enter product information [record number is-1 for input end]:

Product Id:5

Product Name: Lamp

Quantity: 23

Price: 5.99

Please enter product information [record number is-1 for input end]:

Product Id:8

Product Name: Screwdriver

Quantity: 9

Price: 7.99

Please enter product information [record number is-1 for input end]:

Product Id:10

Product Name: Hammer

Quantity: 10

Price: 12.00

Please enter product information [record number is-1 for input end]:

-1

Product information is as follows:

Record number (item id) Commodity quantity price

5 Lamp 23 5.99

8 Screwdriver 9 7.99

Hammer 10 12.00

Please enter your choice:

Create a file with 100 empty records

2--Enter a product record

3--Update Product Records

4--Delete a product record

5--Output Product record

[Select]3

Please enter the item ID to be updated:

8

The original product information is as follows:

Record number (item id) Commodity quantity price

8 Screwdriver 9 7.99

Please enter the update information [if an item does not update please press ENTER directly]:

Product Name:

Quantity: 7

Price: 9.99

Updated product information is as follows:

Record number (item id) Commodity quantity price

8 Screwdriver 7 9.99

Please enter your choice:

Create a file with 100 empty records

2--Enter a product record

3--Update Product Records

4--Delete a product record

5--Output Product record

[Select]5

Product information is as follows:

Record number (item id) Commodity quantity price

5 Lamp 23 5.99

8 Screwdriver 7 9.99

Hammer 10 12.00

Please enter your choice:

Create a file with 100 empty records

2--Enter a product record

3--Update Product Records

4--Delete a product record

5--Output Product record

[Select]3

Please enter the item ID to be updated:

90

Sorry, the item does not exist and the update operation cannot be performed

Please enter your choice:

Create a file with 100 empty records

2--Enter a product record

3--Update Product Records

4--Delete a product record

5--Output Product record

[Select]4

Please enter the item ID you want to delete:

8

Delete Succeeded

Please enter your choice:

Create a file with 100 empty records

2--Enter a product record

3--Update Product Records

4--Delete a product record

5--Output Product record

[Select]5

Product information is as follows:

Record number (item id) Commodity quantity price

5 Lamp 23 5.99

Hammer 10 12.00

Please enter your choice:

Create a file with 100 empty records

2--Enter a product record

3--Update Product Records

4--Delete a product record

5--Output Product record

[Select]4

Please enter the item ID you want to delete:

8

Sorry, the item does not exist and cannot be deleted

Please enter your choice:

Create a file with 100 empty records

2--Enter a product record

3--Update Product Records

4--Delete a product record

5--Output Product record

1

100 Empty Records created

Please enter your choice:

Create a file with 100 empty records

2--Enter a product record

3--Update Product Records

4--Delete a product record

5--Output Product record

2

Please enter product information [record number is-1 for input end]:

Record number: 5

Product Name: Lamp

Quantity: 23

Price: 5.99

Please enter product information [record number is-1 for input end]:

Record number: 8

Product Name: Screwdriver

Quantity: 9

Price: 7.99

Please enter product information [record number is-1 for input end]:

Record number: 10

Product Name: Hammer

Quantity: 10

Price: 12.00

Please enter product information [record number is-1 for input end]:

-1

Code
#include <stdio.h> #include <string.h>typedef struct commodity{int id;    Char name[40];    int num; Double Price;}    CD;CD a[105];void Init (FILE *f) {int i;    Rewind (f);    for (I=1; i<=100; i++) a[i].id=i;    Fwrite (A, sizeof (CD), +, f); printf ("100 Empty records created \ n");}    void Add () {int i,id[105],cnt=0;        while (1) {printf ("Please enter product information [record number 1 for input end]:\n");        printf ("Product ID:");        scanf ("%d", &id[++cnt]);        if (id[cnt]==-1) break;        printf ("Product Name:");              scanf ("%s", &a[id[cnt]].name);              printf ("Quantity:");              scanf ("%d", &a[id[cnt]].num);              printf ("Price:");    scanf ("%lf", &a[id[cnt]].price);    } printf ("Product information as follows: \ n");    printf ("Record number (Product ID) \t\t Product name \t\t quantity \t\t price \ n"); for (I=1; i<cnt; i++) printf ("%-16d\t\t%-6s\t\t%-4d\t\t%-4lf\n", Id[i],a[id[i]].name,a[id[i]].num,a [Id[i]].price);}    void Update (FILE *f) {int id;        while (1) {printf ("Please enter the item to be updated id:\n"); ScaNF ("%d", &id);        if (id==-1) break;        if (strlen (a[id].name) ==0) printf ("Sorry, the item does not exist, cannot be updated \ n");            else {fseek (f, (ID-1) *sizeof (CD), Seek_set);            Fwrite (a,sizeof (CD), 1,f);        printf ("Update%d succeeded \ n", id);    }}}void Delete (FILE *f) {int id;    Rewind (f);        while (1) {printf ("Please enter the item to be deleted id:\n");        scanf ("%d", &id);        if (id==-1) break;        if (strlen (a[id].name) ==0) printf ("Sorry, the item does not exist, cannot be updated \ n");            else {memset (a[id].name,0,sizeof (a[id].name));            a[id].num=0;            a[id].price=0;            Fseek (f, (ID-1) *sizeof (CD), Seek_set);            Fwrite (a,sizeof (CD), 1,f);        printf ("Delete%d succeeded \ n", id);    }}}void output () {int i;    printf ("Product information as follows: \ n");    printf ("Record number (Product ID) \t\t Product name \t\t quantity \t\t price \ n");               for (I=1; i<=100; i++) {if (strlen (a[i].name)!=0) {printf ("%-16d\t\t%-6s\t\t%-4d\t\t%-4lf\n", I,a[i].name,a[i].nUm,a[i].price);    }}}int Main () {FILE *f=fopen ("Commodity.dat", "rb+");//Read and write, do not empty, overwrite write.        while (1) {int i;        printf ("Please enter your choice: \ n");        printf ("To create a file with 100 empty records \n2--Enter the product record \n3--update the product record \n4--delete the product record \n5--output the product record \ n");        printf ("[select]");        scanf ("%d", &i);        if (i==1) init (f);        else if (i==2) add ();        else if (i==3) update (f);        else if (i==4) Delete (f);        else if (i==5) output ();        GetChar ();    printf ("\ n");    } fclose (f); return 0;}

Still, welcome the students to find out the bug

"Guide Job"--product record

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.