01.
더보기
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int input;
printf("입력할 실수의 갯수를 입력 >> ");
scanf("%d", &input);
double* dynable = (double*)malloc(sizeof(double) * input);
if (dynable == NULL) {
printf("메모리 할당에 문제가 있습니다.\n");
exit(1);
}
printf("%d개의 실수 입력 >> ", input);
for (int i = 0; i < input; i++)
{
scanf("%lf", (dynable + i));
}
printf("입력한 실수\n");
for (int i = 0; i < input; i++)
{
printf("%.2lf ", *(dynable + i));
}
free(dynable);
return 0;
}
02.
더보기
#include <stdio.h>
#include <stdlib.h>
struct point {
double x;
double y;
};
struct circle {
struct point point;
double radius;
};
int main(void)
{
struct circle* mycircle = (struct circle*)malloc(sizeof(struct circle));
if (mycircle == NULL) {
printf("구조체 메모리 할당에 문제가 있습니다.\n");
}
mycircle->point.x = 1.24;
mycircle->point.y = 3.82;
mycircle->radius = 4.93;
printf("원 중심좌표(%.2lf, %.2lf), 반지름: %.2lf\n", mycircle->point.x, mycircle->point.y, mycircle->radius);
printf("원면적: %.2lf\n", 3.14 * 3.14 * mycircle->radius);
free(mycircle);
return 0;
}
03.
더보기
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node {
int cnt;
char name[20];
double middle;
double final;
struct node* next;
}node;
node* createNode(int, char*, double, double);
int main(void)
{
FILE* fp;
if (fopen_s(&fp, "input.txt", "r") != 0) {
printf("파일을 여는데 문제가 생겼습니다.\n");
exit(1);
}
char buff[50];
int cnt;
char name[20];
double middle;
double final;
node* head = NULL;
node* cur;
while (fgets(buff, 50, fp) != NULL) {
//while (fscanf_s(fp, "%d %s %lf %lf", &cnt, name, 20, &middle, &final) == 4) { 해도 됨, 이렇게 하면 buff에 저장 할 필요도 없고 좋네..
sscanf_s(buff, "%d %s %lf %lf", &cnt, name, 20, &middle, &final);
if (head == NULL) {
head = createNode(cnt, name, middle, final);
cur = head;
}
else {
cur = head;
while (cur->next != NULL)
{
cur = cur->next;
}
cur->next = createNode(cnt, name, middle, final);
}
}
//순회하면서 프린트하고 free해 줌
cur = head;
while (cur != NULL) {
node* deletion = cur;
printf("%8d %10s %10.1lf %10.1lf %10.1lf\n", cur->cnt, cur->name, cur->middle, cur->final, (cur->middle + cur->final));
cur = cur->next;
free(deletion);
}
fclose(fp);
return 0;
}
node* createNode(int count, char* name, double mid, double fin)
{
node* cur = (node*)malloc(sizeof(node));
if (cur == NULL) {
printf("동적 메모리 할당에 실패했습니다.\n");
exit(1);
}
cur->cnt = count;
strcpy(cur->name, name);
cur->middle = mid;
cur->final = fin;
cur->next = NULL;
return cur;
}
04.
더보기
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node {
int cnt;
char name[20];
double middle;
double final;
struct node* next;
}node;
node* createNode(int, char*, double, double);
int main(void)
{
FILE* fp;
if (fopen_s(&fp, "input.txt", "r") != 0) {
printf("파일을 여는데 문제가 생겼습니다.\n");
exit(1);
}
int cnt;
char name[20];
double middle;
double final;
node* head = NULL;
node* cur;
while (fscanf_s(fp, "%d %s %lf %lf", &cnt, name, 20, &middle, &final) == 4) {
if (head == NULL) {
head = createNode(cnt, name, middle, final);
cur = head;
}
else {
cur = head;
while (cur->next != NULL)
{
cur = cur->next;
}
cur->next = createNode(cnt, name, middle, final);
}
}
fclose(fp);
FILE* fp2;
if (fopen_s(&fp2, "out.txt", "w") != 0) {
printf("파일을 여는데 문제가 생겼습니다.\n");
exit(1);
}
//순회하면서 프린트하고 free해 줌
cur = head;
while (cur != NULL) {
node* deletion = cur;
printf("%8d %10s %10.1lf %10.1lf %10.1lf\n", cur->cnt, cur->name, cur->middle, cur->final, (cur->middle + cur->final));
fprintf(fp2,"%8d %10s %10.1lf %10.1lf %10.1lf\n", cur->cnt, cur->name, cur->middle, cur->final, (cur->middle + cur->final));
cur = cur->next;
free(deletion);
}
fclose(fp2);
return 0;
}
node* createNode(int count, char* name, double mid, double fin)
{
node* cur = (node*)malloc(sizeof(node));
if (cur == NULL) {
printf("동적 메모리 할당에 실패했습니다.\n");
exit(1);
}
cur->cnt = count;
strcpy(cur->name, name);
cur->middle = mid;
cur->final = fin;
cur->next = NULL;
return cur;
}
05.
더보기
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main(void) {
srand((long)time(NULL));
int* ary = (int*)malloc(sizeof(int) * 10);
if (ary == NULL) {
printf("메모리 할당에 문제 발생\n");
exit(1);
}
printf("정렬 전 배열값: -->\n");
for (int i = 0; i < 10; i++) {
*(ary + i) = rand() % 101;
printf("%d",ary[i]);
if (i != 9)
printf(", ");
else
printf("\n");
}
//sequential sort
for (int i = 0; i < 10; i++) {
for (int j = 1 + i; j < 10; j++) {
if (ary[i] > ary[j])
swap(&ary[i], &ary[j]);
}
}
printf("정렬 후 배열값: -->\n");
for (int i = 0; i < 10; i++) {
printf("%d", ary[i]);
if (i != 9)
printf(", ");
else
printf("\n");
}
free(ary);
return 0;
}
06.
더보기
#include <stdio.h>
#define print(exp) printf(#exp" = %d\n", exp)
int main(void)
{
int a = 2;
print(3 * 4 + 3 / a);
return 0;
}
07.
더보기
#include <stdio.h>
#define print(x, y) printf(#x "와 " #y"의 곱은 %d 입니다.\n",x*y)
int main(void)
{
int a = 7, b = 2;
print(a, b);
return 0;
}
08.
더보기
#include <stdio.h>
#define PRINTMAX(a,b) printf(#a"와 "#b"의 최대값: %d\n", a>b?a:b)
int main(void)
{
int a = 6, b = 5;
PRINTMAX(a++, b);
return 0;
}