C++
-
[C++] 함수가 여러 개의 값을 반환해야하는 경우C++/class 2022. 6. 15. 15:01
#include #include using namespace std; // ================================================= // #1. 합(a+b)는 return, 나머지는 주소로 전달 int sum_diff_mult(int a, int b, int c, int *x, int *y){ *x = a - b; *y = a * b; return a + b; } // #2. 합, 차, 곱 3개의 값을 모두 포인터로 반환 void sum_diff_mult2(int a, int b, int c, int *x, int *y, int *z){ *x = a + b; *y = a - b; *z = a * b; } // #3. 합(a+b)는 return, 나머지는 레퍼런스로 전달 i..
-
[C++] 질문 후 답변이 긍정이면 1, 부정이면 0 반환하는 함수C++/문제풀이 2022. 6. 15. 13:00
power C++ p.297 19번 사용자에게 질문을 제시하고 답변을 받아서 긍정이면 1 반환, 부정이면 0 반환하는 함수 작성하기 strcmp() 이용 #include #include using namespace std; // p.297 19번) // 사용자에게 질문을 제시하고 답변을 받아서 긍정이면 1 반환, 부정이면 0 반환하는 함수 작성하기 // strcmp() 이용 int get_response(); //0 또는 1 반환 int main(){ cout
-
[C++] 문자열이 회문인지 알아보는 프로그램C++/문제풀이 2022. 6. 15. 12:44
Power C++ p.257 문자열이 회문인지 알아보는 프로그램 #include #include using namespace std; // p.296 16번) // 문자열이 회문인지 알아보는 프로그램 // 1. i번째 원소와 length - 1 - i이 같은지 비교 int main(){ cout 공백 문자 처리 불가(cin 객체) cin.getline(sentence, 81); //1. 문자열 맨 끝의 길이: 문자열의 length 구하기 int length = strlen(sentence); cout
-
[C++] 문자열 안에 있는 영단어의 개수 계산C++/문제풀이 2022. 6. 15. 08:37
Power C++ p.296 14번) 영문 문자열 안에 포함된 영단어의 개수를 계산하여 화면에 출력하는 프로그램 작성 조건 1. isalpha()를 사용하여 공백문자와 단어 구분 가능 2. 영어 알파벳이 연속되다가 공백 문자가 나오면 단어가 끝나는 것으로 간주 #include #include using namespace std; // p.296 14번) // 영문 문자열 안에 포함된 영단어의 개수를 계산하여 화면에 출력하는 프로그램 작성 // 1. isalpha()를 사용하여 공백문자와 단어 구분 가능 // 2. 영어 알파벳이 연속되다가 공백 문자가 나오면 단어가 끝나는 것으로 간주 int main(){ cout 공백 문자 처리 불가(cin 객체) cin.getline(sentence, 81); //1...
-
[C++] 문자열을 입력받아서 문자열에 포함된 모든 공백 문자를 삭제하는 프로그램C++/문제풀이 2022. 6. 15. 08:16
Power C++ p.296 12번 문자열을 입력으로 받아서 문자열에 포함된 모든 공백 문자를 삭제하는 코드를 작성해보자. #include #include using namespace std; // p.296 12번) 문자열을 입력으로 받아서 문자열에 포함된 모든 공백 문자를 삭제하는 코드를 작성해보자. int main(){ // 루핑을 돌며 맨 뒤에서부터 감소하다가 공백 문자를 만나면 앞으로 땡겨진다. cout 공백 문자 처리 불가(cin 객체) cin.getline(sentence, 81); //1. 문자열 맨 끝의 길이: 문자열의 length 구하기 int length = strlen(sentence); cout =0; i--){ if (sentence[i] == ' ') for(int j = i;..
-
[C++] 외부 파일에서 데이터 읽어오기(fstream, infile)C++/class 2022. 6. 14. 21:02
#include // open(), close() #include using namespace std; int main(){ ifstream infile; // 입력파일스트림 infile.open("data1.txt"); // ifstream infile("data1.txt"); // 선언과 open 한줄로 // 파일에서 12개의 데이터를 읽어서 배열(inumbers[4][3])에 저장 if (infile.is_open()) { for (int row = 0; row > inumbers[row][col]; } else { cout inumbers[row][col];
-
[C++] 두 행렬의 곱 구하기(3x3)C++/문제풀이 2022. 6. 14. 20:53
실습 9-4번 문제 두 행렬의 곱 구하기(3x3) void randomNumbers(int inumbers[][3], int rowSize); void multiplyMatrix(int a[][3], int b[][3], int c[][3], int rowSize); void printArray(int num[][3], int rowSize); // // main.cpp // // Created by yeonsu on 2022/06/14. // // 난수(0~9)발생 시키기, 숫자의 출현 빈도수 세기(4행 3열) #include #include //rand 함수 #include using namespace std; const int ROW = 4; const int COL = 3; void randomN..
-
[C++] 난수 발생 시킨 뒤, 숫자의 출현 빈도수 세기(이차원 배열)C++/문제풀이 2022. 6. 14. 20:10
실습 9-2번 난수(0~9)발생 시키기, 숫자의 출현 빈도수 세기(4행 3열) const int ROW = 4; const int COL = 3; void randomNumbers(int inumbers[][3], int rowSize); void randomCounts(int inumbers[][3], int rowSize, int counts[]); void graph(int counts[], int size); // // main.cpp // // Created by yeonsu on 2022/06/14. // // 난수(0~9)발생 시키기, 숫자의 출현 빈도수 세기(4행 3열) #include #include //rand 함수 #include using namespace std; const int..