Tuesday, September 30, 2014

[Delivery] Struct

Bài 1: Write a C Program to store student data in a structure. The data should include student ID, name, course registered for, and year of joining. Write a function to display the details of students enrolled in a specified academic year. Write another function to locate and display the details of a student based on a specified student ID.



To do this,

a.       Define the structure to store the student details.

b.      Declare and initialize the structure with details of 10 students.

c.       Set a loop to display a menu for the operations to be performed.

d.      Accept the menu choice and invoke appropriate functions with the structure array as parameter.
e.       In the function to display students for a year, accept the year. Set a loop to check each student’s enrollment year, and display if it matches. At the end, allow the user to specify another year.
In the function to locate student details, accept the student ID. Set a loop to check each student’s ID, and display if it matches. At the end, allow the user to specify another student ID.

Code:

#include <stdio.h> #include <stdlib.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ typedef struct { int id; char name[30]; char course[30]; int year; }Student; Student sv[3]; void printYear (int year); void printID (int id); int main(int argc, char *argv[]) { int i; int choice, id, year; printf("___________Enter 3 student information here___________\n"); for (i = 0; i < 3; i++) { printf("Enter student ID: "); scanf("%d", &sv[i].id); fflush(stdin); printf("Enter student name: "); gets(sv[i].name); fflush(stdin); printf("Enter student year: "); scanf("%d", &sv[i].year); fflush(stdin); printf("Enter student course: "); gets(sv[i].course); fflush(stdin); printf("\n");fflush(stdin); } do { printf("___________Menu___________\n"); printf("1. Search by ID"); printf("\n2. Search by year"); printf("\n3. Exit"); printf("\nYou entered: "); scanf("%d", &choice);fflush(stdin); if (choice == 1) { printf("Enter student ID: "); scanf("%d", &id);fflush(stdin); printID( id); printf("\n"); } else if(choice == 2) { printf("Enter student year: "); scanf("%d", &year);fflush(stdin); printYear(year); printf("\n"); } else { printf("End of program !"); printf("\n"); } } while(choice !=3); return 0; } void printYear (int year) { int i; for (i = 0; i < 3; i++) { if (year == sv[i].year) { printf("____________Student found____________\n"); printf("\nStudent ID: %d", sv[i].id); printf("\nStudent name: %s", sv[i].name); printf("\nStudent course: %s", sv[i].course); printf("\nStudent year: %d", sv[i].year); printf("\n"); } } } void printID (int id) { int i; for (i = 0; i < 3; i++) { if (id == sv[i].id) { printf("_______________Student found_______________\n"); printf("\nStudent ID: %d", sv[i].id); printf("\nStudent name: %s", sv[i].name); printf("\nStudent course: %s", sv[i].course); printf("\nStudent year: %d", sv[i].year); printf("\n"); } } }

----------------------------------------------------------------------------------------------------------------------------------------------

Result:



Result

Bài 2: Write a C program to store 5 lengths in a structure array. The lengths should be in the form of yards, feet and inches. Sort and display the lengths.


Code + result 

Code:


#include <stdio.h> #include <stdlib.h> struct length { float yard; float feet; float inch; }; int main(int argc, char *argv[]) { int i, j, temp; struct length caculate[5]; for (i = 0; i < 5; i ++ ){ printf("Enter yard: "); scanf("%f", &caculate[i].yard); } for (i = 0; i < 5; i++){ for (j = i; j < 5; j ++){ if (caculate[i].yard < caculate[j].yard) { temp = caculate[i].yard; caculate[i].yard = caculate[j].yard; caculate[j].yard = temp; } } } for (i = 0; i < 5; i++) { caculate[i].feet = caculate[i].yard * 3; caculate[i].inch = caculate[i].yard * 36; } printf("\nSort array descending: "); for (i = 0; i < 5; i++) { printf("\nYard: %.2f", caculate[i].yard); printf("\nFoot: %.2f", caculate[i].feet); printf("\nInchies: %.2f", caculate[i].inch); printf("\n"); } return 0; }


Bài 3:  Write a C Program to store employee details in a structure array. The data should include emploee ID, name, salary, and date of joining. The date of joining should be stored in a structure. The program should perform the following operations based on a menu selection:




a.       Increase the salaries according to the following rules:

Salary Range
Percentage increase
<= 2000
15%
> 2000 and <= 5000
10%
>5000
No increase

b.      Display the details of employees who complete 10 years with the company.

Code

Code


Result

Code:

#include <stdio.h> #include <stdlib.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ typedef struct { int id; char name[50]; int salary; int date; }employee; employee data[3]; int main(int argc, char *argv[]) { int i, id, date; employee data[3] = { {1, "hoang", 1000, 2000}, {2, "jimmy", 3000, 2012}, {3, "bobby", 6000, 2014} }; for (i = 0; i < 3; i++) { if (data[i].salary < 2000) { data[i].salary = data[i].salary + (data[i].salary * 15/100); } else if (data[i].salary > 2000 && data[i].salary < 5000 ) { data[i].salary = data[i].salary + (data[i].salary * 10/100); } } printf("Enter your ID: "); scanf("%d", &id); for (i = 0; i < 3; i++){ if (data[i].id == id) { printf("Name: %s, salary: %d, year: %d", data[i].name, data[i].salary, data[i].date ); } } printf("\n\nwho finish 10 years of working ?"); for (i =0; i < 3; i++) { if ( data[i].date < 2004) { printf("\n%s complete 10 years of working ! Welldone !", data[i].name); } } return 0; }

Bài 4: Write a C program to implement an inventory system. Store the item number, name, rate and quantity on hand in a structure. Accept the details for five items into a structure array and display the item name and its total price. At the end, display the grand total value of the inventory.

Code + result

Code: 

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
typedef struct {
int id;
char name[50];
int quantity;
int price;
}Store;

Store item[3];

int main(int argc, char *argv[]) {
int i, sum =0 ;

printf("___________Enter 5 items here___________\n");
 for (i = 0; i < 3; i++) {
  printf("Enter items ID: "); scanf("%d", &item[i].id); fflush(stdin);
  printf("Enter items name: "); gets(item[i].name); fflush(stdin);
  printf("Enter items quantity: "); scanf("%d", &item[i].quantity); fflush(stdin);
  printf("Enter items price: "); scanf("%d", &item[i].price); fflush(stdin);
        printf("\n");fflush(stdin);

}


 for (i = 0; i < 3; i++) {
  item[i].price = item[i].price * item[i].quantity;
  printf("\n%s total price: %d", item[i].name, item[i].price);
 }

for (i = 0; i < 3; i++) {
    sum += item[i].price;
}
printf("\n\nTotal price of inventory: %d", sum);
return 0;
}


Bài 5: Write a C program to store the names and scores of 5 students in a structure array. Sort the structure array in descending order of scores. Display the top 3 scores.



Code + result

Code:

#include <stdio.h> #include <stdlib.h> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ typedef struct { char name[50]; int score; }student; student data[5]; int main(int argc, char *argv[]) { int i, j, temp; for (i =0; i < 5; i++) { printf("Enter name: "); gets(data[i].name); fflush(stdin); printf("Enter score: "); scanf("%d", &data[i].score); fflush(stdin); printf("\n"); } for (i = 0; i < 5; i++) { for (j = i; j < 5; j++) { if (data[i].score < data[j].score ) { temp = data[i].score; data[i].score = data[j].score; data[j].score = temp; } } } for (i = 0; i < 3; i++) { printf("\nDescending order of top 3 scores: %d", data[i].score); } return 0; }



Bài học rút ra:


  • Struct giúp ta quản lí dữ liệu, giúp ta không phải lặp lại 1 công việc nhập giá trị 1 nhiều lần.
  • Cấu trúc:

struct cat
{
char bk_name [25];
char author [20];
int edn;
float price;
};

No comments:

Post a Comment