-
[C++] 2장 실습문제C++ 2022. 4. 17. 21:30
- 2-1
#include <iostream> using namespace std; int main() { int i; for (i = 1; i <= 100; i++) { cout << i << '\t'; if (i % 10 == 0) cout << endl; } return 0; }
- 2-2
#include <iostream> using namespace std; int main() { int i, j; for (int i = 1; i <= 9; i++) { for (int j = 1; j <= 9; j++) { cout << j << "X" << i << "=" << i * j << '\t'; } cout << endl; } return 0; }
- 2-3
#include <iostream> using namespace std; int main() { int n1, n2,max; cout << "두 수를 입력하라>>"; cin >> n1 >> n2; max = n1 > n2 ? n1 : n2; cout << "큰 수 = " << max; return 0; }
- 2-4
#include <iostream> using namespace std; int main() { float n1, n2, n3, n4, n5, max=0.0; cout << "5개의 실수를 입력하라>>"; cin >> n1 >> n2 >> n3 >> n4 >> n5; if (max < n1) max = n1; if (max < n2) max = n2; if (max < n3) max = n3; if (max < n4) max = n4; if (max < n5) max = n5; cout << "제일 큰 수 = " << max; return 0; }
- 2-5
#include <iostream> using namespace std; int main() { char x[100]; int cnt = 0; cout << "문자들을 입력하라(100개 미만)."<<endl; cin.getline(x, 100, '\n'); for (int i = 0; i < 100; i++) { if (x[i] == 'x') cnt++; } cout << "x의 개수는 " << cnt; return 0; }
- 2-6
#include <iostream> #include <cstring> //c 헤어파일 using namespace std; int main() { char a[100], b[100]; cout << "새 암호를 입력하세요>>"; cin >> a; cout << "새 암호를 다시 한 번 입력하세요>>"; cin >> b; if (strcmp(a, b) == 0) { cout << "같습니다." << endl; } else { cout << "같지 않습니다." << endl; } return 0; }
#include <iostream> #include <string> using namespace std; int main() { string a, b; cout << "새 암호를 입력하세요>>"; getline(cin, a); cout << "새 암호를 다시 한 번 입력하세요>>"; getline(cin, b); if (a==b) { cout << "같습니다." << endl; } else { cout << "같지 않습니다." << endl; } return 0; }
strcmp 함수
https://blockdmask.tistory.com/391
[C언어/C++] strcmp, strncmp 함수(문자열 비교)에 대해서
안녕하세요 BlockDMask입니다. 오늘은 c/c++에서 두개의 문자열이 같은지, 다른지 다르면 어떤식으로 다른지 검사할 수 있는 strcmp 함수. 문자열 비교 함수인 strcmp 함수를 알아 보려고 합니다. 오늘은
blockdmask.tistory.com
- 2-7
#include <iostream> #include <cstring> //c 헤어파일 using namespace std; int main() { char yes[100]; while (1) { cout << "종료하고싶으면 yes를 입력하세요>>"; cin.getline(yes, 100, '\n'); if (strcmp(yes, "yes") == 0) { cout << "종료합니다..."; break; } } return 0; }
#include <iostream> #include <cstring> //c 헤어파일 #include <string> using namespace std; int main() { char cyes[100]; string syes="yes"; while (1) { cout << "종료하고싶으면 yes를 입력하세요>>"; cin.getline(cyes, 100, '\n'); if (cyes==syes) { cout << "종료합니다..."; break; } } return 0; }
- 2-8
#include <iostream> #include <cstring> //c 헤어파일 #include <string> using namespace std; int main() { char name[100], max[100]; //name : 이름 입력, max : 최대 길이 이름 저장 int cntmax = 0; //cntmax : 문자열 길이 저장 cout << "5명의 이름을 ';'으로 구분하여 입력하세요" << endl << ">>"; for (int i = 0; i < 5; i++) { cin.getline(name, 100, ';'); cout << i + 1 << " : " << name << endl; if (strlen(name) > cntmax) { //name 문자열의 길이가 최대 길이 (cntmax)보다 크다면 strcpy_s(max, name); //max 배열에 name을 넣어주고 cntmax = strlen(name); //가장 긴 문자열 길이에 name의 문자열 길이를 넣어줌 } } cout << "가장 긴 이름은 " << max; return 0; }
strlen 함수
https://blockdmask.tistory.com/381
[C언어/C++] strlen 함수(문자열 길이)에 대해서
안녕하세요. BlockDMask 입니다. 오늘은 char*, char [] 타입의 문자열 길이에 대해서 이야기 해보려 합니다. 즉. C언어 스타일 문자열의 길이를 구하는 함수에 대해서 알아보려합니다. > C언어 문자열
blockdmask.tistory.com
strcpy 함수
https://blockdmask.tistory.com/348
[C언어/C++] strcpy, strncpy 함수(문자열 복사)에 대해서
안녕하세요. BlockDMask 입니다. 오늘은 C 스타일의 문자열인 char*, char[] 타입의 문자열을 복사하는 함수 두가지에 대해서 알아 볼 것 입니다. 두 함수는 바로 strcpy, strncpy 입니다. 이 두함수가 무슨
blockdmask.tistory.com
- 2-9
#include <iostream> #include <string> using namespace std; int main() { string name, adr, old; cout << "이름은?"; getline(cin,name); cout << "주소는?"; getline(cin, adr); cout << "나이는?"; getline(cin, old); cout << name << ", " << adr << ", " << old << "세"; return 0; }
- 2-10
#include <iostream> #include <string> using namespace std; int main() { char str[100]; cout << "문자열 입력>>"; cin >> str; for (int i = 0; i < strlen(str); i++) { //줄바꿈 for (int j = 0; j <= i; j++) { //문자출력 cout << str[j]; } cout << endl; } return 0; }
- 2-11
#include <iostream> #include <string> using namespace std; int main() { int k, n = 0, sum = 0; cout << "끝 수를 입력하세요>>"; cin >> n; for (int k = 1; k <= n; k++) sum += k; cout << "1에서 " << n << "까지의 합은 " << sum << "입니다"; return 0; }
- 2-12
#include <iostream> #include <string> using namespace std; int sum(int a, int b) { int k, res = 0; for (k = a; k <= b; k++) { res += k; } return res; } int main() { int n = 0; cout << "끝 수를 입력하세요>>"; cin >> n; cout << "1에서 " << n << "까지의 합은 " << sum(1, n) << "입니다" << endl; return 0; }
- 2-13
#include <iostream> #include <string> using namespace std; int main() { int one, two, three, four, menu, num; //num : 인분 cout << "***** 승리장에 오신 것을 환영합니다. *****" <<endl; while (1) { cout << "짬뽕:1, 짜장:2, 군만두:3, 종료:4>> "; cin >> menu; if (menu==1 || menu ==2 || menu==3) { cout << "몇인분?"; cin >> num; } else if(menu==4) { cout << "오늘 영업은 끝났습니다." << endl; break; } else { cout << "다시 주문하세요!!" << endl; continue; } switch (menu) { case 1: cout << "짬뽕 " << num << "인분 나왔습니다." <<endl; break; case 2: cout << "짜장 " << num << "인분 나왔습니다." <<endl; break; case 3: cout << "군만두 " << num << "인분 나왔습니다." << endl; break; } } return 0; }
- 2-14
#include <iostream> #include <string> using namespace std; int main() { string coffee, e="에스프레소", a="아메리카노", k="카푸치노"; int num, price, sum=0; cout << "에스프레소 2000원, 아메리카도 2300원, 카푸치노 2500원입니다." << endl; while (1) { cout << "주문>> "; cin >> coffee >> num; if (coffee == e) { price = 2000 * num; sum += price; cout << price << "원입니다. 맛있게 드세요" << endl; } else if (coffee == a) { price = 2300 * num; sum += price; cout << price << "원입니다. 맛있게 드세요" << endl; } else if (coffee == k) { price = 2500 * num; sum += price; cout << price << "원입니다. 맛있게 드세요" << endl; } if (sum>=20000) { cout << "오늘 " << sum << "원을 판매하여 카페를 닫습니다. 내일 봐요~~~"; break; } } return 0; }
- 2-15
#pragma warning (disable:4996) #include <iostream> #include <string> #include <cstring> using namespace std; int main() { int n1, n2; char* op; while (1) { cout << "? "; char line[100]; cin.getline(line, 100, '\n'); n1 = atoi(strtok(line, " ")); op = strtok(NULL, " "); n2 = atoi(strtok(NULL, " ")); switch (*op) { case '+': cout << n1 << " " << op << " " << n2 << " = " << n1 + n2 << endl; break; case '-': cout << n1 << " " << op << " " << n2 << " = " << n1 - n2 << endl; break; case '*': cout << n1 << " " << op << " " << n2 << " = " << n1 * n2 << endl; break; case '/': cout << n1 << " " << op << " " << n2 << " = " << n1 / n2 << endl; break; case '%': cout << n1 << " " << op << " " << n2 << " = " << n1 % n2 << endl; break; default: break; } } return 0; }
atoi 함수
https://blockdmask.tistory.com/331
[C언어/C++] atoi, atof, atol 함수 (char* to int)
안녕하세요. BlockDMask 입니다. 오늘은 C, C++에서 문자열을 숫자(정수, 실수)로 변환하는 함수들에 대해서 알아보겠습니다. (C/C++ 에서 string -> char* -> int 로 변경? [바로가기]) (C++에서 int -> strin..
blockdmask.tistory.com
strtok 함수
https://blockdmask.tistory.com/382
[C언어/C++] strtok 함수(문자열 자르기)에 대해서.
안녕하세요. BlockDMask 입니다. 오늘 공부할 함수는 문자열을 일정 기준을 정해서 싹둑싹둑 자를 수 있는 strtok 함수입니다. C언어 strtok 함수에 대해서 한번 알아보러 가보겠습니다. <목차> 1. strtok
blockdmask.tistory.com
- 2-16
#include <iostream> #include <cstring> using namespace std; int main() { char text[10000]; // 10000 개의 문자열 배열 int histo[26] = { 0 }; // 영문자 26 글자의 누적수 저장. 초깃값은 모두 0 cout << "영문 텍스트를 입력하세요. 히스토그램을 그립니다." << endl; cout << "텍스트의 끝은 ; 입니다. 10000개까지 가능합니다. " << endl; cin.getline(text, 10000, ';'); int len = strlen(text); for (int i = 0; i < len; i++) { if (isalpha(text[i])) { //알파벳인지 검사 char c = tolower(text[i]); //대문자를 소문자로 변경 histo[c - 'a']++; } } int n = 0; for (int i = 0; i < 26; i++) n += histo[i]; // 전체 알파벳 수 더하기 cout << "총 알파벳 수 " << n << endl; cout << endl; for (int i = 0; i < 26; i++) { //히스토그램 그리기 출력 cout << char('a' + i) << " (" << histo[i] << ")" << '\t' << ": "; //('a'+i)번째 문자열, i번째 histo의 개수 for (int j = 0; j < histo[i]; j++) //j는 i번쨰 histo에 저장된 개수만큼 cout << '*'; //출력 cout << endl; } }
isalpha 함수
https://blockdmask.tistory.com/448
[C언어/C++] isalpha 함수 (알파벳을 확인하는 함수)
안녕하세요. BlockDMask 입니다. 오늘은 C언어 C++에서 알파벳인지 확인할 수 있는 isalpha 함수에 대해 알아보려합니다. 예전에 문자가 숫자인지 확인해주는 isdigit() 함수를 소개해드린적이 있는데요.
blockdmask.tistory.com
tolower 함수
https://blockdmask.tistory.com/452
[C언어/C++] tolower, toupper 대문자 소문자 변경
안녕하세요. BlockDMask 입니다. 오늘은 C언어, C++에서 알파벳을 소문자는 대문자로, 대문자는 소문자로 변경해주는 tolower, toupper 함수에 대해서 알아보려고 합니다. <목차> 1. toupper, tolower 함수 원형
blockdmask.tistory.com
'C++' 카테고리의 다른 글
[C++] 2차원 vector 생성 및 초기화 (0) 2024.01.08 [C++] 1장 실습문제 (0) 2022.04.17 [C++] CP 13 (0) 2022.04.17 [C++] CP 11, 12 (0) 2022.04.17 [C++] CP 09, 10 (0) 2022.04.14