From d38063dd9d471b647beb9faab147e1f47bd29eff Mon Sep 17 00:00:00 2001 From: Peace Date: Thu, 24 Apr 2025 12:29:03 +0900 Subject: [PATCH] sample project - studentGrade --- .vscode/tasks.json | 71 ++++++++++++++++++++++++++-- sampleProject/studentGrade/main.c | 19 ++++++++ sampleProject/studentGrade/student.c | 40 ++++++++++++++++ sampleProject/studentGrade/student.h | 20 ++++++++ 4 files changed, 147 insertions(+), 3 deletions(-) create mode 100644 sampleProject/studentGrade/main.c create mode 100644 sampleProject/studentGrade/student.c create mode 100644 sampleProject/studentGrade/student.h diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 88af757..a5beda6 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -1,5 +1,71 @@ { + "version": "2.0.0", + "runner": "terminal", + "type": "shell", + "echoCommand": true, + "presentation": { + "reveal": "always" + }, "tasks": [ + { + "label": "save and compile for C++", + "command": "g++", + "args": [ + "-g", + "${fileDirname}/*.cpp", + "-o", + "${fileDirname}/bin/${fileBasenameNoExtension}" + ], + "group": "build", + "problemMatcher": { + "fileLocation": [ + "relative", + "${workspaceRoot}" + ], + "pattern": { + "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning error):\\s+(.*)$", + "file": 1, + "line": 2, + "column": 3, + "severity": 4, + "message": 5 + } + } + }, + { + "label": "save and compile for C", + "command": "gcc", + "args": [ + "-g", + "${fileDirname}/*.c", + "-o", + "${fileDirname}/bin/${fileBasenameNoExtension}" + ], + "group": "build", + "problemMatcher": { + "fileLocation": [ + "relative", + "${workspaceRoot}" + ], + "pattern": { + "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning error):\\s+(.*)$", + "file": 1, + "line": 2, + "column": 3, + "severity": 4, + "message": 5 + } + } + }, + { + "label": "execute", + "command": "cmd", + "group": "test", + "args": [ + "/C", + "${fileDirname}\\bin\\${fileBasenameNoExtension}" + ] + }, { "type": "cppbuild", "label": "C/C++: gcc.exe 활성 파일 빌드", @@ -7,7 +73,7 @@ "args": [ "-fdiagnostics-color=always", "-g", - "${file}", + "${fileDirname}\\*.c", "-o", "${fileDirname}\\bin\\${fileBasenameNoExtension}.exe" ], @@ -23,6 +89,5 @@ }, "detail": "디버거에서 생성된 작업입니다." } - ], - "version": "2.0.0" + ] } \ No newline at end of file diff --git a/sampleProject/studentGrade/main.c b/sampleProject/studentGrade/main.c new file mode 100644 index 0000000..85f02c2 --- /dev/null +++ b/sampleProject/studentGrade/main.c @@ -0,0 +1,19 @@ +#include +#include "student.h" + +int main() +{ + Student* student1 = createStudent("Bill Gates", 1001, 95.5); + Student* student2 = createStudent("Steve Jobs ", 1002, 88.0); + + printStudentInfo(student1); + printStudentInfo(student2); + + freeStudent(student1); + freeStudent(student2); + + printStudentInfo(student1); + printStudentInfo(student2); + + return 0; +} \ No newline at end of file diff --git a/sampleProject/studentGrade/student.c b/sampleProject/studentGrade/student.c new file mode 100644 index 0000000..70b4d14 --- /dev/null +++ b/sampleProject/studentGrade/student.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include "student.h" + +// 학생생성 함수 +Student* createStudent(const char* name, int id, float grade) +{ + Student* newStudent = (Student*)malloc(sizeof(Student)); + if (!newStudent) + { + printf("Fail to student malloc!\n"); + exit(1); + } + + strncpy(newStudent->name, name, sizeof(newStudent->name) - 1); // 가용한 마지막 문자까지 복사하고 + newStudent->name[sizeof(newStudent->name) - 1] = '\0'; // 마지막\0 null문자 추가로 문자열 종료 보장 + newStudent->id = id; + newStudent->grade = grade; + + return newStudent; +} + +// 학생정보 출력 함수 +void printStudentInfo(const Student* student) +{ + if (!student) + { + printf("Invalid student info."); + return; + } + + printf("Name: %s, ID: %d, Grade: %.2f\n", student->name, student->id, student->grade); +} + +void freeStudent(Student* student) +{ + if (student) + free(student); +} \ No newline at end of file diff --git a/sampleProject/studentGrade/student.h b/sampleProject/studentGrade/student.h new file mode 100644 index 0000000..4567c86 --- /dev/null +++ b/sampleProject/studentGrade/student.h @@ -0,0 +1,20 @@ +#ifndef STUDENT_H +#define STUDENT_H + +typedef struct +{ + char name[50]; + int id; + float grade; +} Student; + +// 학생 생성 함수 +Student* createStudent(const char* name, int id, float grade); + +// 학생정보 출력 함수 +void printStudentInfo(const Student* student); + +// 학생 메모리 해제 함수 +void freeStudent(Student* Student); + +#endif \ No newline at end of file