sample project - studentGrade

main
Peace 6 days ago
parent b65491612f
commit d38063dd9d
  1. 71
      .vscode/tasks.json
  2. 19
      sampleProject/studentGrade/main.c
  3. 40
      sampleProject/studentGrade/student.c
  4. 20
      sampleProject/studentGrade/student.h

71
.vscode/tasks.json vendored

@ -1,5 +1,71 @@
{ {
"version": "2.0.0",
"runner": "terminal",
"type": "shell",
"echoCommand": true,
"presentation": {
"reveal": "always"
},
"tasks": [ "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", "type": "cppbuild",
"label": "C/C++: gcc.exe 활성 파일 빌드", "label": "C/C++: gcc.exe 활성 파일 빌드",
@ -7,7 +73,7 @@
"args": [ "args": [
"-fdiagnostics-color=always", "-fdiagnostics-color=always",
"-g", "-g",
"${file}", "${fileDirname}\\*.c",
"-o", "-o",
"${fileDirname}\\bin\\${fileBasenameNoExtension}.exe" "${fileDirname}\\bin\\${fileBasenameNoExtension}.exe"
], ],
@ -23,6 +89,5 @@
}, },
"detail": "디버거에서 생성된 작업입니다." "detail": "디버거에서 생성된 작업입니다."
} }
], ]
"version": "2.0.0"
} }

@ -0,0 +1,19 @@
#include <stdio.h>
#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;
}

@ -0,0 +1,40 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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);
}

@ -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
Loading…
Cancel
Save