1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | /* 두 수 바꾸기 - 포인터 두 수를 입력받아 값을 바꾸어 출력하는 프로그램을 작성하시오 단, 포인터를 이용하시오. Input 두 값은 정수이다. Output 두 수를 바꾸어 출력한다. Sample Input 100 30 Sample Output 30 100 */ #include <iostream> void swap( int *a, int * b) { int temp = *a; *a = *b; *b = temp; } int main( void ) { int a = 0, b = 0; std::cin >> a >> b; swap(&a, &b); std::cout << a << " " << b; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | /* 단어 뒤집기 - 포인터 단어를 입력받아 단어를 뒤집어 출력하는 프로그램을 작성하시오 단, 포인터를 이용하시오. Input 단어는 공백을 포함하지 않는다. Output 포인터를 이용하여 단어를 뒤집어 출력한다. Sample Input silent Sample Output tnelis */ #include <iostream> #include <string> using namespace std; int main( void ) { string str; cin >> str; char *s = &str[str.size()-1]; while (cout << s[0]) if (--s < &str[0]) break ; return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | /* 도형의 넓이와 둘레 구하기 직사각형을 나타내는 Rectangle 클래스와 원을 나타내는 Circle 클래스를 작성하시오. 이 두 클래스는 넓이를 구하는 기능과 둘레를 구하는 기능을 지녀야 한다. 다음에 제공되는 main 함수와 출력 결과를 통해서 요구되는 Rectangle 클래스와 Circle클래스를 작성하시오. ※ 직사각형의 넓이 : 가로 × 세로 둘레 : (가로 + 세로) × 2 ※ 원의 넓이 : 반지름2 × 3.14 둘레 : 2 × 반지름 × 3.14 int main(void) { int x, y, r; cin >> x >> y >> r; //(가로, 세로, 반지름을 입력받음) Rectangle rec(x, y); //Rectangle rec(가로길이, 세로길이) cout << rec.GetArea() << endl; cout << rec.GetGirth() << endl; Circle cir(r); //Circle cir(원의 반지름) cout << cir.GetArea() << endl; cout << cir.GetGirth() << endl; return 0; } Input 가로, 세로, 반지름 순으로 입력 받는다. Output 사각형의 넓이, 사각형의 둘레, 원의 넓이, 원의 둘레 순으로 출력한다. 원의 넓이와 둘레는 소수점 둘째자리 까지 출력한다. 소수점 첫번째 자리까지 출력하기 cout.setf(ios::fixed, ios::floatfield); cout.precision(2); Sample Input 3 4 5 Sample Output 12 14 78.50 31.40 */ #include <iostream> using namespace std; #define pi 3.14 class Rectangle { private : int width; int height; public : Rectangle( int w, int h ); int GetArea( ); int GetGirth( ); }; Rectangle::Rectangle( int w = 0, int h = 0 ) { this ->width = w; this ->height = h; } int Rectangle::GetArea( ) { return ( this ->width * this ->height); } int Rectangle::GetGirth( ) { return ( this ->width * 2 + this ->height * 2 ); } class Circle { private : double radius; public : Circle( int r); double GetArea( ); double GetGirth( ); }; Circle::Circle( int r = 0 ) { this ->radius = ( double )r; } double Circle::GetArea( ) { return ( pi * this ->radius * this ->radius ); } double Circle::GetGirth( ) { return (2.0 * pi * this ->radius); } int main( void ) { int x, y, r; cin >> x >> y >> r; //(가로, 세로, 반지름을 입력받음) Rectangle rec(x, y); //Rectangle rec(가로길이, 세로길이) cout << rec.GetArea() << endl; cout << rec.GetGirth() << endl; cout.setf(ios::fixed, ios::floatfield); cout.precision(2); Circle cir(r); //Circle cir(원의 반지름) cout << cir.GetArea() << endl; cout << cir.GetGirth() << endl; return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | /* 시, 분, 초 출력하기 시(hour), 분(minute), 초(second) 정보를 지닐 수 있는 Time 클래스를 작성하시오. 이 클래스는 멤버 변수가 지니고 있는 데이터를 적절히 출력하는 기능을 지녀야 한다. 출력방식은 두 가지로 제공한다. 하나는 [시, 분, 초]의 형식을 띄며, 또 하나는 초 단위로 계산한 출력 결과를 보여준다. 제시되는 main 함수와 출력결과를 참조하시오. int main() { int hour, min, sec; int choice; cin >> choice; if(choice == 1) { cin >> hour; Time time(hour); time.ShowTime(); time.ShowTimeinSec(); } else if(choice == 2) { cin >> hour >> min; Time time(hour, min); time.ShowTime(); time.ShowTimeinSec(); } else if(choice == 3) { cin >> hour >> min >> sec; Time time(hour, min, sec); time.ShowTime(); time.ShowTimeinSec(); } return 0; } Input 입력 형식을 입력 받는다. 1. 시 2. 시, 분 3. 시, 분, 초 형식에 맞게 시, 분, 초를 입력받는다. Output 시(h), 분(m), 초(s)를 단위와 함께 출력한다. 없는 단위는 0으로 출력한다. 다음, 입력받은 수를 초단위로 환산하여 출력한다. Sample Input1 1 10 Sample Output1 10h 0m 0s 36000s Sample Input2 2 5 10 Sample Output2 5h 10m 0s 18600s Sample Input3 3 7 30 20 Sample Output3 7h 30m 20s 27020s */ #include <iostream> using namespace std; class Time { private : int hour, min, second; public : Time( int hour, int min, int sec); void ShowTime( ); void ShowTimeinSec(); }; Time::Time( int hour = 0, int min = 0, int sec = 0) { this ->hour = hour; this ->min = min; this ->second = sec; } void Time::ShowTime( ) { cout << this ->hour << "h " << this ->min << "m " << this ->second << "s" << endl; } void Time::ShowTimeinSec( ) { cout << this ->hour * 3600 + this ->min * 60 + this ->second << "s" << endl; } int main( void ) { int hour, min, sec; int choice; cin >> choice; if (choice == 1) { cin >> hour; Time time (hour); time .ShowTime(); time .ShowTimeinSec(); } else if (choice == 2) { cin >> hour >> min; Time time (hour, min); time .ShowTime(); time .ShowTimeinSec(); } else if (choice == 3) { cin >> hour >> min >> sec; Time time (hour, min, sec); time .ShowTime(); time .ShowTimeinSec(); } return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | /* Student_info 맴버 함수 다루기 Student_info 객체를 다루기 위해서는, 프로그래머들이 사용할 수 있는 인터페이스를 정의해야 합니다. 레코드 하나를 읽고(read), 전체 성적(grade)을 계산하는 프로그램을 작성하세요. S tudent_info 구조체는 이렇게 정의 됩니다. struct Student_info { string name; double midterm, final; vector< double > homework; istream& read(istream&) // 추가된 맴버함수 double grade() const; //추가된 맴버함수 } 맴버 함수들은 입력 스트림으로부터 한 레코드를 읽어드리고(read), 모든 Student_info 객체에 대해 최종 성적(grade)을 계산합니다. grade의 선언 시 사용한 const는 grade 함수가 Student_info 객체의 데이터 맴버를 전혀 변경하지 않을 것이라는 것을 나타냅니다. 전체 성적의 계산방식은 0.2 * 중간점수 + 0.4 * 기말점수 + 0.4 * 과제 입니다. 과제를 계산 할때 4.1.1절의 중앙값 찾기를 통해 중앙값을 찾아서 계산해 줍니다. 단, C++의 형식에 맞추어 COUT을 이용해 출력하셔야 합니다. EOF처리는 while(입력) 와 같이 하시면 됩니다. 참고 : 맴버 함수의 이름은 보통의 read, grade가 아니라 Student_info::read, Student_info::grade입니다. 중앙값을 계산할때 나오는 sort함수는 algorithm헤더에 정의 되어있습니다. INPUT 학생들의 성적을 입력합니다. 4.2.2절의 read함수를 이용해 이름과 시험점수를 입력받고, 4.1.3절의 read_hw함수를 이용해 과제점수를 입력받습니다. 입력순서는 이름 >> 중간시험점수 >> 기말시험점수 >> 과제 입니다. 참고 : read_hw함수는 read함수에 포함되어 있습니다. 과제점수를 여러개 입력할때 EOF처리는 while(입력)입니다. 학생의 성적과 과제 점수 입력을 마치고난뒤 EOF처리를 두 번 해줘야 프로그램의 결과가 출력됩니다. OUTPUT 학생들의 이름과 최종성적이 출력 됩니다. 최종성적은 소수점 첫째자리까지 출력합니다. 출력이 끝나면 개행처리를 해줍니다. 입력 예제 1 joo 100 70 10 5 4 1 0 park 100 60 10 5 4 1 0 lee 87 32 3 2 1 8 8 출력 예제 1 joo 49.6 park 45.6 lee 31.4 */ #include <iostream> #include <istream> #include <string> #include <vector> #include <algorithm> using namespace std; struct Student_info { string name; double midterm, final; vector< double > homework; double grade() const ; istream& read(istream& in); istream& read_hw( istream& in); }; // 0.2 * 중간점수 + 0.4 * 기말점수 + 0.4 * 과제 double Student_info::grade() const { vector< double >::size_type size = this ->homework.size(); vector< double >::size_type mid = size / 2; double hw_mid = (size%2 == 0) ? ( this ->homework[mid] + this ->homework[mid-1]) / 2 : this ->homework[mid]; return ((0.2 * this ->midterm) + (0.4 * this ->final) + (0.4 * hw_mid)); } istream& Student_info::read(istream& in) { if (!(in >> this ->name)) // 이름에서 eof가 들어오면 학생 입력 종료 처리 return in; in >> this ->midterm; in >> this ->final; read_hw( in ); return in; } istream& Student_info::read_hw( istream& in) { if (in) { double x; while (in >> x) this ->homework.push_back(x); } sort( this ->homework.begin(), this ->homework.end()); in.clear(); return in; } int main( void ) { vector<Student_info> student_list; while (1) { Student_info st; st.read(cin); if (cin.eof()) // 만약 eof bit 가 설정이 되어 있으면 학생 입력 종료 break ; student_list.push_back(st); } cout.setf( ios::fixed, ios::floatfield ); cout.precision( 1 ); for (vector<Student_info>::iterator i = student_list.begin(); i != student_list.end(); i++ ) cout << i->name << ' ' << i->grade() << endl; return 0; } |
Posted by LaLuna