งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

หน่วยที่ 17 แอเรย์ของสตรัคเจอร์. แอเรย์ของข้อมูลสตรัคเจอร์ student_info student[30]; Student[0]Student[0].Name Student[0].Midterm Student[0].Assignment.

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "หน่วยที่ 17 แอเรย์ของสตรัคเจอร์. แอเรย์ของข้อมูลสตรัคเจอร์ student_info student[30]; Student[0]Student[0].Name Student[0].Midterm Student[0].Assignment."— ใบสำเนางานนำเสนอ:

1 หน่วยที่ 17 แอเรย์ของสตรัคเจอร์

2 แอเรย์ของข้อมูลสตรัคเจอร์ student_info student[30]; Student[0]Student[0].Name Student[0].Midterm Student[0].Assignment Student[0].Final Student[0].TotalScore Student[1]Student[1].Name Student[1].Midterm Student[1].Assignment Student[1].Final Student[1].TotalScore Student[2] Student[3] ….. …… Student[29]

3 product_info product[10]; Product[0]Product[0].Code Product[0].Name Product[0].Price Product[0].Quantity Product[1]Product[1].Code Product[1].Name Product[1].Price Product[1].Quantity Product [2] Product [3] …… …… Product [9]

4 ถ้าต้องการใช้งานข้อมูล code ของนักศึกษาคนที่ 10 student[10].code การใช้แอเรย์ของสตรัคเจอร์ก็จะต้องกำหนดตัวชี้แอเรย์ตามหลังชื่อสตรัคเจอร์ เช่น สมมติว่ามีการประกาศแอเรย์ student_info student[30];

5 #include typedef struct{ char Name[20]; int Code, Quantity; float Price; }product_info; void main () { product_info product[20]; int i=0, NumberOfItems; printf("Enter code, name, price and quantity of item %d :",i+1); scanf("%d %s %f %d",&product[i].Code, product[i].Name, &product[i].Price,&product[i].Quantity); while(product[i].Code!=0) { i++; printf("Enter code, name, price and quantity of item %d :",i+1); scanf("%d %s %f %d",&product[i].Code, product[i].Name, &product[i].Price,&product[i].Quantity); } NumberOfItems=i; for (i=0; i< NumberOfItems; i++) printf("%4d %-15s %5.2f %4d\n",product[i].Code, product[i].Name, product[i].Price,product[i].Quantity); } Enter code, name, price and quantity of item 1 : 104 Notebook 18.00 200 Enter code, name, price and quantity of item 2 : 105 Ruler 15.50 100 Enter code, name, price and quantity of item 3 : 0 0 0 0 104 Notebook 18.00 200 105 Ruler 15.50 100 สตรัคเจอร์ของสต็อคสินค้า

6 #include typedef struct{ char Name[20]; int Code, Quantity; float Price; }product_info; int GetInputs(product_info p[]) { printf("Enter code, name, price and quantity of item %d :",i+1); scanf("%d %s %f %d",&p[i].Code, p[i].Name, &p[i].Price,&p[i].Quantity); while(product[i].Code!=0) { i++; printf("Enter code, name, price and quantity of item %d :",i+1); scanf("%d %s %f %d",&p[i].Code, p[i].Name, &p[i].Price,&p[i].Quantity); } return i; } void main () { product_info product[20]; int i=0, NumberOfItems; NumberOfItems=GetInputs(product); for (i=0; i< NumberOfItems; i++) printf("%4d %-15s %5.2f %4d\n",product[i].Code, product[i].Name, product[i].Price,product[i].Quantity); } Enter code, name, price and quantity of item 1 : 104 Notebook 18.00 200 Enter code, name, price and quantity of item 2 : 105 Ruler 15.50 100 Enter code, name, price and quantity of item 3 : 0 0 0 0 104 Notebook 18.00 200 105 Ruler 15.50 100 สตรัคเจอร์ของสต็อคสินค้า

7 สมมติว่าเรามีไฟล์ชื่อ input.txt ในไดรว์ C: ซึ่งมีข้อมูลดังนี้ 100 Pencil 25.0 20 101 Pen 90.0 5 103 Eraser 12.0 10 /* A program to read and write stock’s information */ #include /* structure of stock information */ typedef struct{ char Name[20]; int Code, Quantity; float Price; }product_info; void main () { product_info product[20]; int i, NumberOfItems; char InputFileName[20], OutputFileName[20]; FILE *infile, *outfile; /* Input filenames for input and output */ printf("Good Morning, Please Enter the Input File: "); scanf("%s", InputFileName); printf("Good Morning, Please Enter the Output File: "); scanf("%s", OutputFileName); /* Open files for input and output */ infile = fopen(InputFileName, "r"); outfile = fopen(OutputFileName, "w");

8 if (infile == NULL) printf("Error Opening File\n"); else { /* read product data to arrays until the end of input file */ i = 0; while(fscanf(infile, "%d %s %f %d",&product[i].Code,product[i].Name, &product[i].Price,&product[i].Quantity) != EOF) i++; fclose(infile); printf("The table has %d items\n", i); printf("Enter code, name, price and quantity of item %d :",i+1); fscanf("%d %s %f %d",&product[i].Code, product[i].Name, &product[i].Price,&product[i].Quantity); while(product[i].Code!=0) { i++; printf("Enter code, name, price and quantity of item %d :",i+1); scanf("%d %s %f %d",&product[i].Code, product[i].Name, &product[i].Price,&product[i].Quantity); } NumberOfItems=i; /* write all data in arrays to output files */ for (i=0; i< NumberOfItems; i++) fprintf(outfile, "%4d %-15s %5.2f %4d\n",product[i].Code, product[i].Name, product[i].Price,product[i].Quantity); fclose(outfile); }

9 Good Morning, Please Enter the Input File: C:input.txt Good Morning, Please Enter the Output File: C:output.txt The table has 3 items Enter code, name, price and quantity of item 4 : 104 Notebook 18.00 200 Enter code, name, price and quantity of item 5 : 105 Ruler 15.50 100 Enter code, name, price and quantity of item 6 : 0 0 0 0 ซึ่งเมื่อจบโปรแกรมแล้วจะทำให้ไฟล์ output.txt มีข้อมูลดังนี้ 100 Pencil 25.00 20 101 Pen 90.00 5 103 Eraser 12.00 10 104 Notebook 18.00 200 105 Ruler 15.50 100


ดาวน์โหลด ppt หน่วยที่ 17 แอเรย์ของสตรัคเจอร์. แอเรย์ของข้อมูลสตรัคเจอร์ student_info student[30]; Student[0]Student[0].Name Student[0].Midterm Student[0].Assignment.

งานนำเสนอที่คล้ายกัน


Ads by Google