2022-05-03
1, 2.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char author[30];
char title[50];
int pages;
} Book;
int main() {
Book books[3];
// Book * books = (Book *)malloc(sizeof(Book) * 3); // 2번 문제.
printf("도서 정보 입력\n");
for (int i = 0; i < 3; i++) {
printf("저자: ");
fgets(books[i].author, sizeof(books[i].author), stdin);
books[i].author[strlen(books[i].author) - 1] = 0;
printf("제목: ");
fgets(books[i].title, sizeof(books[i].title), stdin);
books[i].title[strlen(books[i].title) - 1] = 0;
printf("페이지 수: ");
scanf("%d", &books[i].pages);
getchar(); // 숫자 다음 엔터가 다음 책의 저자로 잡혀서 처리.
printf("\n");
}
for (int i = 0; i < 3; i++) {
printf("book %d\n", i+1);
printf("저자: %s\n", books[i].author);
printf("제목: %s\n", books[i].title);
printf("페이지 수: %d\n\n", books[i].pages);
}
}
2번의 장점 : 도서 관리용 프로그램에 도서가 늘어나면 쉽게 realloc
3.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
double real;
double imaginary;
} Complex;
int main() {
Complex *complexes = (Complex *)calloc(2, sizeof(Complex));
double res_real = 0, res_imaginary = 0;
for (int i = 0; i < 2; i++) {
printf("복소수 입력%d[실수 허수]: ", i + 1);
scanf("%lf %lf", &complexes[i].real, &complexes[i].imaginary);
}
// 합
for (int i = 0; i < 2; i++) {
res_real += complexes[i].real;
res_imaginary += complexes[i].imaginary;
}
printf("합의 결과] 실수: %lf, 허수: %lf\n", res_real, res_imaginary);
res_real = complexes[0].real * complexes[1].real - complexes[0].imaginary * complexes[1].imaginary;
res_imaginary = complexes[0].real * complexes[1].imaginary + complexes[0].imaginary * complexes[1].real;
printf("곱의 결과] 실수: %lf, 허수: %lf\n", res_real, res_imaginary);
}
4.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TRUE 1
#define FALSE 0
int countAandP(char str[]) {
int count = 0;
int startAorP = FALSE;
for(int i = 0; i < strlen(str); i++) {
if (i == 0) {
startAorP = (str[i] == 'A' || str[i] == 'P');
}
if (str[i] == ' ' || str[i] == '\t' || str[i] == '\n') {
if (startAorP) {
count++;
}
i++;
startAorP = (str[i] == 'A' || str[i] == 'P');
}
}
return count;
}
int main(int argc, char* argv[]) {
FILE *fp = fopen(argv[1], "rt");
// FILE *fp = fopen("mystory.txt", "rt");
char str[100];
int count = 0;
if (fp == NULL) {
printf("파일 열기 실패\n");
return -1;
}
while(!feof(fp)) {
fgets(str, sizeof(str), fp);
// str[strlen(str) - 1] = 0; // \n 은 함수에서 구분할 것이므로 이걸 할 필요가 없다.
count += countAandP(str);
}
printf("A 나 P로 시작하는 단어 개수 : %d\n", count);
}
5.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[]) {
FILE *f1 = fopen(argv[1], "rt");
FILE *f2 = fopen(argv[2], "rt");
char str1[100];
char str2[100];
if (f1 == NULL) {
printf("1 파일 읽기 실패!\n");
return - 1;
}
if (f2 == NULL) {
printf("2 파일 읽기 실패!\n");
return - 1;
}
while (!feof(f1) && !feof(f2)) {
fgets(str1, sizeof(str1), f1);
str1[strlen(str1) - 1] = 0;
fgets(str2, sizeof(str2), f2);
str2[strlen(str2) - 1] = 0;
// 한 줄씩 비교하다 다른게 있으면 다른거임.
if (strcmp(str1, str2) != 0) {
printf("두 파일은 다릅니다.\n");
return 0;
}
}
// 여기까진 같았지만 하나의 파일이 더 길면 다른거임.
if (!feof(f1) || !feof(f2)) {
printf("두 파일은 다릅니다.\n");
return 0;
}
// 이 모든 관문을 통과하면 같은거임.
printf("두 파일은 완전히 일치합니다.\n");
return 1;
}
6.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
char name[30];
char phoneNumber[15];
} PhoneElement;
typedef struct {
PhoneElement * numbers;
int n;
} PhoneBook;
void input(PhoneBook *book);
void delete(PhoneBook *book);
void search(PhoneBook *book);
void printAll(PhoneBook *book);
void exitPhoneBook(void);
void printPhoneBook() {
printf("***** MENU *****\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Search\n");
printf("4. Print All\n");
printf("5. Exit\n");
printf("Choose the item : ");
}
void input(PhoneBook *book) {
char name[30];
char phoneNum[15];
printf("[ INSERT ]\n");
printf("Input Name : ");
scanf("%s", name);
printf("Input Tel Number : ");
scanf("%s", phoneNum);
book->numbers = realloc(book->numbers, book->n + 1);
strcpy(book->numbers[book->n].name, name);
strcpy(book->numbers[book->n++].phoneNumber, phoneNum);
}
void delete(PhoneBook *book) {
char name[30];
int delete_index = -1;
printf("[ DELETE ]\n");
printf("Input Name : ");
scanf("%s", name);
for (int i = 0; i < book->n; i++) {
if (strcmp(book->numbers[i].name, name) == 0) {
delete_index = i;
}
}
if (delete_index == -1) {
printf("%s is not exist.\n\n", name);
return;
}
for(int i = delete_index; i < book->n; i++) {
book->numbers[i] = book->numbers[i+1];
}
book->n--;
printf("deleted %s\n\n", name);
}
void search(PhoneBook *book) {
char name[30];
printf("[ SEARCH ]\n");
printf("Input Name : ");
scanf("%s", name);
for (int i = 0; i < book->n; i++) {
if (strcmp(book->numbers[i].name, name) == 0) {
printf("Name: %s Tel: %s\n\n", book->numbers[i].name, book->numbers[i].phoneNumber);
return;
}
}
printf("%s is not exist.\n", name);
}
void printAll(PhoneBook *book) {
for (int i = 0; i < book->n; i++) {
printf("Name: %s Tel: %s\n\n", book->numbers[i].name, book->numbers[i].phoneNumber);
}
}
void exitPhoneBook(void) {
exit(1);
}
int main(int argc, char* argv[]) {
PhoneBook phoneBook;
int num = 0;
char name[30];
do {
printPhoneBook();
scanf("%d", &num);
switch(num) {
case 1:
input(&phoneBook);
break;
case 2:
delete(&phoneBook);
break;
case 3:
search(&phoneBook);
break;
case 4:
printAll(&phoneBook);
break;
case 5:
exitPhoneBook();
break;
default:
return 0;
}
} while(num != 5);
}
7.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NAME_LEN 50
#define NUM_LEN 20
typedef struct {
char name[NAME_LEN];
char phoneNumber[NUM_LEN];
} PhoneElement;
typedef struct {
PhoneElement * numbers;
int n;
} PhoneBook;
void input(PhoneBook *book);
void delete(PhoneBook *book);
void search(PhoneBook *book);
void printAll(PhoneBook *book);
void exitPhoneBook(void);
int getSpaceIdx(char str[]);
void readTxt(PhoneBook *book, FILE *fp);
void printPhoneBook() {
printf("***** MENU *****\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Search\n");
printf("4. Print All\n");
printf("5. Exit\n");
printf("Choose the item : ");
}
void input(PhoneBook *book) {
char name[NAME_LEN];
char phoneNum[NUM_LEN];
printf("[ INSERT ]\n");
printf("Input Name : ");
scanf("%s", name);
printf("Input Tel Number : ");
scanf("%s", phoneNum);
if (book->numbers > 100) {
book->numbers = realloc(book->numbers, book->n + 1);
}
strcpy(book->numbers[book->n].name, name);
strcpy(book->numbers[book->n++].phoneNumber, phoneNum);
}
void delete(PhoneBook *book) {
char name[NAME_LEN];
int delete_index = -1;
printf("[ DELETE ]\n");
printf("Input Name : ");
scanf("%s", name);
for (int i = 0; i < book->n; i++) {
if (strcmp(book->numbers[i].name, name) == 0) {
delete_index = i;
}
}
if (delete_index == -1) {
printf("%s is not exist.\n\n", name);
return;
}
for(int i = delete_index; i < book->n; i++) {
book->numbers[i] = book->numbers[i+1];
}
book->n--;
printf("deleted %s\n\n", name);
}
void search(PhoneBook *book) {
char name[NAME_LEN];
printf("[ SEARCH ]\n");
printf("Input Name : ");
scanf("%s", name);
for (int i = 0; i < book->n; i++) {
if (strcmp(book->numbers[i].name, name) == 0) {
printf("Name: %s Tel: %s\n\n", book->numbers[i].name, book->numbers[i].phoneNumber);
return;
}
}
printf("%s is not exist.\n", name);
}
void printAll(PhoneBook *book) {
for (int i = 0; i < book->n; i++) {
printf("Name: %s Tel: %s\n\n", book->numbers[i].name, book->numbers[i].phoneNumber);
}
}
void exitPhoneBook(void) {
exit(1);
}
int getSpaceIdx(char str[]) {
for (int i = 0; i < strlen(str); i++) {
if (str[i] == ' ') {
return i;
}
}
return -1;
}
void readTxt(PhoneBook *book, FILE *fp) {
char str[NAME_LEN + NUM_LEN];
int i = 0;
int index = 0;
int length = 0;
fp = fopen("phonebook.txt", "rt");
if (fp == NULL) {
printf("파일 오픈 실패");
return;
}
while(1) {
fgets(str, sizeof(str), fp);
str[strlen(str) - 1] = 0;
// !feof 로 while 에 쓰면 안됨..
// 내가 생각한 것보다 한줄이 더 읽힌다.
if (feof(fp)) {
break;
}
index = getSpaceIdx(str);
if (index != -1) {
book->n++;
strncpy(book->numbers[i].name, str, index);
strncpy(book->numbers[i].phoneNumber, &str[index + 1], NUM_LEN);
}
i++;
}
fclose(fp);
}
void storeTxt(PhoneBook *book, FILE *fp) {
char str[NAME_LEN + NUM_LEN];
fp = fopen("phonebook.txt", "wt");
for (int i = 0; i < book->n; i++) {
book->numbers[i].name[strlen(book->numbers[i].name)] = ' ';
strcpy(str, book->numbers[i].name);
book->numbers[i].phoneNumber[strlen(book->numbers[i].phoneNumber)] = '\n';
strcat(str, book->numbers[i].phoneNumber);
fputs(str, fp);
}
fclose(fp);
}
int main(int argc, char* argv[]) {
FILE *fp;
PhoneBook phoneBook;
phoneBook.n = 0;
phoneBook.numbers = (PhoneElement *)malloc(sizeof(PhoneElement) * 100);
readTxt(&phoneBook, fp);
int num = 0;
char name[NAME_LEN];
do {
printPhoneBook();
scanf("%d", &num);
switch(num) {
case 1:
input(&phoneBook);
break;
case 2:
delete(&phoneBook);
break;
case 3:
search(&phoneBook);
break;
case 4:
printAll(&phoneBook);
break;
case 5:
storeTxt(&phoneBook, fp);
exitPhoneBook();
break;
default:
return 0;
}
} while(num != 5);
}
여기까지 풀 줄 알면 탄탄한 기본기를 갖추었다고 자부해도 좋다고 책에 써있는데 과연...??? 그런지는 잘 모르겠지만, C언어에 printf, scanf 만 있는게 아니라는걸 알게 되었다.
'Language > C' 카테고리의 다른 글
[윤성우의 열혈 C프로그래밍] 도전 프로그래밍3 풀이 (1) | 2023.11.12 |
---|---|
[윤성우의 열혈 C프로그래밍] 도전 프로그래밍2 풀이 (0) | 2023.11.12 |
[윤성우의 열혈 C프로그래밍] 도전 프로그래밍1 풀이 (1) | 2023.11.12 |