0% found this document useful (0 votes)
31 views18 pages

Bao Cao Lap Trinh C

The document is a report on C programming and its application in object-oriented programming, particularly in a student management system. It covers the key features of C, the principles of object-oriented programming, and the design of classes for managing students, classes, and faculties. The report emphasizes the importance of C in software development and how its principles can be applied to create structured and maintainable code.

Uploaded by

23145017
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views18 pages

Bao Cao Lap Trinh C

The document is a report on C programming and its application in object-oriented programming, particularly in a student management system. It covers the key features of C, the principles of object-oriented programming, and the design of classes for managing students, classes, and faculties. The report emphasizes the importance of C in software development and how its principles can be applied to create structured and maintainable code.

Uploaded by

23145017
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

TRƯỜNG ĐẠI HỌC SƯ PHẠM KỸ THUẬT TP.

HỒ CHÍ MINH
KHOA ĐIỆN ĐIỆN TỬ
BỘ MÔN KỸ THUẬT MÁY TÍNH - VIỄN THÔNG

BÁO CÁO MÔN HỌC LẬP TRÌNH C

ĐỀ TÀI:

NGÀNH CÔNG NGHỆ KỸ THUẬT MÁY TÍNH

Sinh viên: Nguyen Van A

MSSV:
Nguyen Van B

MSSV:
GVHD:

TP. HỒ CHÍ MINH – 12/2024

MỤC LỤ
C
MỤC LỤC..............................................................................................................................................i
Chapter 1: INTRODUCTION TO THE C PROGRAMMING LANGUAGE........................1
1.1. Key Features of C.................................................................................................................1
1.2. Impact on Other Programming Languages..............................................................2
1.3. Why Learn C...........................................................................................................................2
Chapter2: INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING IN C.............4
2.1. The Essence of Object-Oriented Programming..........................................................4
2.2. Object-Oriented Programming in C................................................................................5
2.3. Advantages of Object-Oriented Programming Concepts in C...............................6
2.4. Limitations of OOP in C.....................................................................................................7
Chapter 3: BUILDING CLASSES IN THE STUDENT MANAGEMENT PROGRAM ...8
3.1. Faculty Class...........................................................................................................................8
3.2. Classes.......................................................................................................................................9
3.3. Student Class.......................................................................................................................10
3.4. Relationships between classes....................................................................................10
Chapter 4: APPLICATION ILLUSTRATION..........................................................................12
4.1. Chapter Objectives ............................................................................................................12
4.2. Application Structure.........................................................................................................12
4.3. Application Illustration..................................................................................................12
4.4. Conclusion............................................................................................................................16

i
Chapter 1: Introduction to the C Programming Language
The C programming language, developed in the early 1970s by Dennis
Ritchie at Bell Labs, has become one of the most influential and widely used
programming languages in the world. It was initially designed to implement the
Unix operating system, but its simplicity, efficiency, and portability quickly made it
a staple in software development across various platforms. Over the decades, C has
remained relevant in both academic settings and professional software engineering
due to its power, versatility, and foundational role in the evolution of modern
programming languages.
1.1. Key Features of C
 Efficiency and Performance: C is known for its efficiency in both
execution and memory usage. Unlike high-level programming languages, C
provides low-level access to memory via pointers, allowing developers to
write optimized code for resource-constrained environments, such as
embedded systems and operating systems.
 Portability: One of C's hallmark features is its portability. Code written in C
can be compiled and run on virtually any machine with minimal
modifications, making it an ideal choice for developing cross-platform
applications.
 Structured Programming: C supports structured programming principles,
meaning that it allows developers to write code in a logical, modular way.
This encourages the use of functions, loops, and conditionals, which makes
code more readable, reusable, and maintainable.
 Rich Set of Operators: C provides a wide variety of operators that allow
developers to perform mathematical, logical, relational, and bitwise
operations, which makes it an extremely flexible language for handling
different types of tasks.
 Standard Library: C comes with a comprehensive standard library that
provides useful functions for managing memory, handling input/output,

1
manipulating strings, and performing other common tasks. The availability
of such a library reduces the need for developers to reinvent the wheel.
 Low-Level Access: C allows direct manipulation of hardware resources,
which is particularly useful for system-level programming. This capability
has made C the language of choice for operating systems, device drivers, and
embedded systems development.
1.2. Impact on Other Programming Languages
C has had a profound influence on many modern programming languages.
Languages such as C++, C#, and Java owe much of their syntax and core concepts
to C. Even more recently, languages like Python and Go have been shaped by C’s
principles, emphasizing simplicity and performance.
C also serves as the foundation for more advanced topics in computer
science, such as data structures, algorithms, and compiler construction. By learning
C, developers gain a solid understanding of how computers work at a low level,
which is crucial for debugging, performance tuning, and mastering more advanced
programming techniques.
1.3. Why Learn C
Learning C is highly recommended for anyone serious about becoming a
proficient programmer. The language teaches essential concepts such as memory
management, pointers, and system-level programming. It serves as a stepping stone
to other, more complex programming languages, and provides a deep understanding
of how software interacts with hardware.
Moreover, mastering C can significantly enhance problem-solving skills, as
the language encourages developers to think logically and solve problems
efficiently with limited resources. It also allows programmers to develop
applications that are both fast and lightweight, making it indispensable for fields
like game development, operating systems, and embedded programming.
In conclusion, C remains a powerful and relevant language even after more
than four decades since its creation. Whether you're interested in building high-
performance applications, exploring system-level programming, or learning

2
foundational programming concepts, C is an excellent choice that provides the
building blocks for a successful career in software development.

3
Chapter2: Introduction to Object-Oriented Programming in C
Object-Oriented Programming (OOP) is a programming paradigm that is
based on the concept of "objects," which are instances of classes. It aims to organize
code in a more structured and modular way by using classes and objects, making it
easier to manage and maintain complex software systems. Although C itself is not
an object-oriented language, object-oriented concepts can still be implemented in C
through the use of structures, functions, and pointers.
2.1. The Essence of Object-Oriented Programming
OOP is built around four primary principles:
 Encapsulation: This principle involves bundling data (attributes) and
methods (functions) that operate on the data into a single unit, called a class
or structure in C. Encapsulation helps to restrict direct access to some of an
object's components, which helps prevent accidental interference and misuse
of the data.
 Abstraction: Abstraction means hiding the complex implementation details
of an object and only exposing the necessary parts. In C, this can be achieved
using functions and structures. By hiding the internal workings, abstraction
allows developers to focus on what an object does rather than how it does it.
 Inheritance: Inheritance allows one class or structure to inherit the attributes
and behaviors of another, promoting code reuse. While C does not natively
support inheritance like languages such as C++ or Java, we can simulate
inheritance by using structures and pointers to handle relationships between
different types of objects.
 Polymorphism: Polymorphism allows objects of different classes to be
treated as objects of a common superclass. In C, polymorphism can be
implemented through function pointers, enabling us to write functions that
can operate on different data types and objects.

2.2. Object-Oriented Programming in C

4
Though C is not inherently an object-oriented language, we can simulate the
principles of OOP using a combination of structures, function pointers, and data
encapsulation. Below is an example of how object-oriented principles can be
implemented in C.
Example: Simulating OOP in C
Let's consider a simple example where we define an object for Car, and simulate
basic OOP concepts like encapsulation, abstraction, and polymorphism.
#include <stdio.h>
#include <string.h>
// Define a structure to represent the 'Car' object.
typedef struct {
char brand[50];
int year;
float mileage;
// Function pointer for polymorphism (behavior of the object)
void (*printInfo)(void *);
} Car;
// Function to initialize the 'Car' object.
void initCar(Car *car, const char *brand, int year, float mileage) {
strcpy(car->brand, brand);
car->year = year;
car->mileage = mileage;
car->printInfo = &printCarInfo; // Assigning function for polymorphism
}
// Function to print information about the car (Encapsulation & Abstraction).
void printCarInfo(void *carObj) {
Car *car = (Car *)carObj; // Cast void pointer to Car pointer
printf("Car Brand: %s\n", car->brand);
printf("Year of Manufacture: %d\n", car->year);
printf("Mileage: %.2f km\n", car->mileage);

5
}
int main() {
// Create and initialize a 'Car' object.
Car myCar;
initCar(&myCar, "Toyota", 2020, 15000.0);
// Use polymorphism to print the car's info.
myCar.printInfo(&myCar);
return 0;
}
Explanation of Code:
 Encapsulation: The Car structure encapsulates the properties of the car, such
as brand, year, and mileage. The printInfo function pointer is used to
associate behavior with the Car structure, making it more flexible.
 Abstraction: The printCarInfo function abstracts the process of displaying
car information. The user of the Car object doesn’t need to know the details
of how the information is printed—only the result is exposed.
 Polymorphism: By using function pointers (printInfo), we simulate
polymorphism. This allows different car objects to have different behaviors
for printing their information, even though they all follow the same interface.
2.3. Advantages of Object-Oriented Programming Concepts in C
 Modularity: Code is organized into discrete objects (structures) that manage
their own data and functions. This makes it easier to debug, extend, and
maintain the code.
 Reusability: By simulating inheritance and polymorphism, we can reuse
code and create new objects based on existing ones. This improves
productivity and reduces redundancy.
 Maintainability: With the encapsulation and abstraction principles, changes
to one part of the code (e.g., internal implementation of an object) do not
affect the rest of the program, leading to easier maintenance.
2.4. Limitations of OOP in C

6
Although it’s possible to implement OOP principles in C, the language itself
is not built to fully support OOP features like inheritance and polymorphism in the
way that C++ or Java does. Therefore, the implementation in C may feel less
intuitive and more cumbersome compared to languages that natively support OOP.
Conclusion
Object-Oriented Programming principles such as encapsulation, abstraction,
inheritance, and polymorphism can be simulated in C, allowing developers to
organize and manage complex systems more efficiently. While C does not have
built-in OOP features like C++ or Java, its flexibility allows programmers to
implement many OOP concepts using structures, function pointers, and careful
design. However, if you're looking for a language that natively supports full OOP
features, you may want to consider transitioning to C++ or another object-oriented
language.

Chapter 3: Building Classes in the Student Management


Program

7
In the student management program, organizing and classifying information
in a reasonable way is very important to ensure scalability and ease of management.
Therefore, in this chapter, we will build and deploy the main classes in the program,
including: Add Student Class, Display Student List Class, and Find Student Class,
Delete Student Class, Edit Student Class, and Enter Student Information Class.
3.1. Faculty Class
The Faculty class represents the faculties in the school. Each faculty can
have many classes and each class belongs to a certain faculty. This class will store
information about the faculty name, faculty code, and the list of classes belonging to
that faculty.
Faculty class structure:
#include <stdio.h>
#include <string.h>
#define MAX_CLASSES 10
// Faculty class
typedef struct {
char maKhoa[10]; // Faculty code
char tenKhoa[50]; // Faculty name
char danhSachLop[MAX_CLASSES][50]; // List of classes belonging to the faculty
int soLop; // Number of classes in the faculty
} Faculty;
Description:
maKhoa: Faculty identification code.
tenKhoa: Faculty name.
danhSachLop: Array to store the names of classes in the faculty.
soLop: Number of classes in the faculty.
This Faculty class will help you manage scientific information in the student
management system. Each faculty can have many classes, and these classes will be
associated with the corresponding faculty code.
3.2. Classes

8
Classes are part of the Faculty class and are used to manage information
about classes in the system. Each class will have a class name, a list of students
participating, and some other information such as the instructor teaching the class.
Class Structure:
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 50
// Lớp Học
typedef struct {
char maLop[10]; // Mã lớp
char tenLop[50]; // Tên lớp
char giangVien[50]; // Tên giảng viên
char danhSachSinhVien[MAX_STUDENTS][50]; // Danh sách sinh viên tham
gia lớp
int soSinhVien; // Số lượng sinh viên trong lớp
} LopHoc;
Description:
maLop: Class code.
tenLop: Class name.
giangVien: Name of the instructor teaching the class.
danhSachSinhVien: Array containing the names of students attending the class.
soSinhVien: Number of students in the class.
Classes will help you manage separate classes and link them with information about
the instructor and students attending.

3.3. Student Class


The Student Class is the most important class in the student management
program. Each student will have personal information, such as student code, full
name, age, average score, and classes that the student participates in.

9
Student Class Structure:
#include <stdio.h>
#include <string.h>
#define MAX_CLASSES 5
// Student Class
typedef struct {
char maSV[15]; // Student ID
char hoTen[100]; // Student's full name
int tuoi; // Student's age
float diemTrungBinh; // Average score
char danhSachLop[MAX_CLASSES][50]; // List of classes the student is taking
int soLop; // Number of classes the student is taking
} SinhVien;
Description:
maSV: Student ID.
hoTen: Student's full name.
tuoi: Student's age.
diemTrungBinh: Student's average score.
danhSachLop: Array to store the names of the classes that the student is taking.
soLop: Number of classes that the student is taking.
The Student class will be where all the student's personal information is stored,
including the classes that the student has registered for.
3.4. Relationships between classes
In the student management system, the classes Faculty, Class, and Student
have a close relationship with each other. A faculty can include many classes, and a
class can have many students participating. Each student can participate in many
different classes.
 A faculty can have many classes.
 A class can have many students.
 A student can participate in many classes.

10
These relationships will help you easily look up and update information
about students, classes, and faculties in the system.
Conclusion
In this chapter, we have built the basic classes to manage student, class, and
faculty information in the student management system. Implementing these classes
helps to organize information clearly and easily manage it. These classes will play
an important role in building the main functions of the program such as adding,
editing, deleting, and searching for information about students, classes, and
departments.

Chapter 4: Application Illustration


4.1. Chapter Objectives
This chapter will detail how to implement and apply the student management
program in practice, using the classes that have been designed in the program. The

11
goal of the chapter is to illustrate how the classes work together to provide an
efficient and easy-to-use student management system.
4.2. Application Structure
This application will include basic features such as:
 Add students
 Display student list
 Search students
 Update student information
 Remove students from the list
To ensure flexibility and easy extensibility, the application will use classes and
objects corresponding to the required data models. The main classes include:
Student class: Contains student attributes such as code, full name, date of birth,
average score, etc.
Faculty class: Manages information about faculties in the school, including
faculty name, faculty code, etc.
LopHoc class: Contains information about the classes that students attend,
including class name, number of students, etc.
DanhSachSinhVien class: Manages the student list and methods such as
adding, deleting, searching for students.
4.3. Application Illustration
Step 1: Initialize objects First, we will initialize objects of the SinhVien,
Khoa, LopHoc and DanhSachSinhVien classes. These classes will be used to
manage information about students and their respective faculties and classes.

#include <stdio.h>
#include <string.h>
// cau truc du lieu sinh vien
typedef struct {
char maSV[15];

12
char hoTen[50];
char khoa[50];
char nganhhoc[50];
int tuoi;
char gioiTinh[10];
float diemmon1;
float diemmon2;
float diemmon3;
float diemTB;
} SinhVien;
Step 2: Add Student When adding a new student, the program will ask to
enter the necessary information and store it in the student list.
void themSinhVien(SinhVien *danhSach, int *soLuong) {
if (*soLuong >= MAX) {
printf("Danh sach sinh vien da day!\n");
return;
}
SinhVien sv;
printf("Nhap Khoa: ");
gets(sv.khoa);
printf("Nhap Nganh hoc: ");
gets(sv.nganhhoc);
printf("Nhap ma sinh vien: ");
gets(sv.maSV);
printf("Nhap ho ten sinh vien: ");
gets(sv.hoTen);
printf("Nhap tuoi sinh vien: ");
scanf("%d", &sv.tuoi);
getchar();
printf("Nhap gioi tinh sinh vien: ");

13
gets(sv.gioiTinh);
printf("Nhap diem mon 1: ");
scanf("%f", &sv.diemmon1);
printf("Nhap diem mon 2: ");
scanf("%f", &sv.diemmon2);
printf("Nhap diem mon 3: ");
scanf("%f", &sv.diemmon3);
sv.diemTB = tinhDiemTrungBinh(sv.diemmon1, sv.diemmon2,
sv.diemmon3);
getchar();

danhSach[*soLuong] = sv;
(*soLuong)++;
printf("Sinh vien da duoc them thanh cong!\n");
}
Step 3: Display Student List The program will display information of all
students in the list. This is an important part of the system for users to track and
manage students.
void hienThiSinhVien(SinhVien *danhSach, int soLuong) {
if (soLuong == 0) {
printf("Danh sach sinh vien rong!\n");
return;
}
printf("\nDanh sach sinh vien:\n");
for (int i = 0; i < soLuong; i++) {
printf("Ma SV: %s, Ho Ten: %s, Tuoi: %d, Gioi Tinh: %s, Diem TB:
%.2f\n",
danhSach[i].maSV, danhSach[i].hoTen, danhSach[i].tuoi,
danhSach[i].gioiTinh, danhSach[i].diemTB);
}

14
}
Step 4: Search for Students The student search method will allow users to
look up student information based on student ID or student name.
void timSinhVien(SinhVien *danhSach, int soLuong, char *maSV) {
int found = 0;
for (int i = 0; i < soLuong; i++) {
if (strcmp(danhSach[i].maSV, maSV) == 0) {
printf("Sinh vien tim thay: Ma SV: %s, Ho Ten: %s, Tuoi: %d, Gioi
Tinh: %s, Diem TB: %.2f\n",
danhSach[i].maSV, danhSach[i].hoTen, danhSach[i].tuoi,
danhSach[i].gioiTinh, danhSach[i].diemTB);
found = 1;
break;
}
}
if (!found) {
printf("Khong tim thay sinh vien voi ma %s.\n", maSV);
}
}
Step 5: Update and Delete Students Finally, we can update information or
delete students from the list.
void xoaSinhVien(SinhVien *danhSach, int *soLuong, char *maSV) {
int found = 0;
for (int i = 0; i < *soLuong; i++) {
if (strcmp(danhSach[i].maSV, maSV) == 0) {
for (int j = i; j < *soLuong - 1; j++) {
danhSach[j] = danhSach[j + 1];
}
(*soLuong)--;
printf("Sinh vien da duoc xoa!\n");

15
found = 1;
break;
}
}
if (!found) {
printf("Khong tim thay sinh vien de xoa.\n");
}
}
4.4. Conclusion
This student management program is a good example of how to apply classes
in C programming to manage information. It helps users perform basic operations
such as adding, deleting, editing, searching and displaying student lists effectively.
The system can be extended and customized to serve other needs in student
management, such as calculating GPA, classifying students, or adding new features
related to departments and classes.

16

You might also like