IT공부/C언어
1/3 난생처음 C프로그래밍
공부하는 라라
2024. 1. 3. 15:14
LAB 13-3
내답: 왜케.. 어렵냐.. 에휴
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include <errno.h>
#include <string.h>
int main()
{
FILE* f = fopen("C:\\Windows\\win.ini", "rb");
char str[40] = { 0 };
int end = 0;
int count = 1;
if (f)
{
while (1)
{
fseek(f, end, SEEK_SET);
fgets(str, sizeof(str), f);
printf("read: %s ", str);
end = ftell(f);
count++;
if(count == 6 )
break;
}
fclose(f);
}
else
printf("Error: %d, %s", errno, strerror(errno));
}
해답: 내 코드 구려
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include <errno.h>
#include <string.h>
int main()
{
FILE* f = fopen("C:\\Windows\\win.ini", "rb");
if (f)
{
char str[128];
while (1)
{
int r = fgets(str, sizeof(str), f);
if (r)
{
printf("%s", str);
}
else
{
break;
}
}
fclose(f);
}
else
printf("Error: %d, %s", errno, strerror(errno));
}
근데 이렇게 하면 결괏값이 책이랑 다른뎅..... ㅠㅠ
LAB 13-4
내답: 굉장히 원시적임
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main()
{
#ifndef ENG
printf("%s", "난생처음 C 프로그래밍");
#else
printf("%s", "C programming for the first time in my life");
#endif
}
해답:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main()
{
#ifdef ENG
const char* title = "C programming for the first time in my life";
#else
const char* title = "난생처음 C 프로그래밍";
#endif
printf("%s", title);
}
chapter12 구조체 연습문제 454p
1. struct [tagname] , typedef
2. 피연산자
3. 구조체 객체, 구조체 포인터, 멤버
4. 패딩, sizeof
5. 공용체, 공용체, union
6. 일반변수는 타입한개만을 정의할수 있고, 구조체는 각각 다른 여러개의 타입을 한번에 정의할 수 있다. 또한 일반변수는 타입만큼이 크기인데 구조체는 패딩이 있어 타입들을 다 더한 크기보다 크다.
7. 20 8
8. 4, 구조체는 비교연산자의 피연산자로 불가
9. 2, 구조체는 함수의 인자로 사용될경우 값전달방식으로 동작해서 멤버들의 값이 그대로 복사되어 별개의 객체가 됨.
10. 2,3,4,7
11
(1).
(2).
(3). 내 코드 구리겠지..? 구릴거야...
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<malloc.h>
typedef enum
{
TAEKWONV,
MAZINGER,
MECHANDV,
GRANDIZER
}RType;
typedef struct
{
RType type;
int height;
int weight;
int hpower;
}Robot;
void ShowRobot(Robot* robot)
{
for (int i = 0; i < 4; i++)
{
switch ((robot + i)->type)
{
case TAEKWONV:
printf("이름: 태권브이\r\n"); break;
case MAZINGER:
printf("이름: 마징가\r\n"); break;
case MECHANDV:
printf("이름: 메칸더브이\r\n"); break;
case GRANDIZER:
printf("이름: 그랜다이저\r\n"); break;
}
printf("신장: %d\r\n", (robot + i)->height);
printf("무게: %d\r\n", (robot + i)->weight);
printf("마력: %d\r\n", (robot + i)->hpower);
printf("---------------\r\n");
}
}
void ShowAvg(Robot arr[4])// 포인터 Robot* arr 임
{
double Ave_Height = (double)(arr->height + arr[1].height + arr[2].height + arr[3].height) / 4;
double Ave_Weight = (double)(arr->weight + arr[1].weight + arr[2].weight + arr[3].weight) / 4;
double Ave_Hpower = (double)(arr->hpower + arr[1].hpower + arr[2].hpower + arr[3].hpower) / 4;
printf("평균 신장: %f\r\n", Ave_Height);
printf("평균 무게: %f\r\n", Ave_Weight);
printf("평균 마력: %f\r\n", Ave_Hpower);
}
void main()
{
Robot* pack = malloc(sizeof(Robot) * 4);
pack->type = TAEKWONV;
pack->height = 18;
pack->weight = 80;
pack->hpower = 3000;
(pack+1)->type = MAZINGER;
(pack + 1)->height = 17;
(pack + 1)->weight = 70;
(pack + 1)->hpower = 2500;
(pack + 2)->type = MECHANDV;
(pack + 2)->height = 20;
(pack + 2)->weight = 120;
(pack + 2)->hpower = 3500;
(pack + 3)->type = GRANDIZER;
(pack + 3)->height = 22;
(pack + 3)->weight = 150;
(pack + 3)->hpower = 5000;
ShowRobot(pack);
ShowAvg(pack);
free(pack);
}