실습문제
C언어 복습
1. 조건문
[1] if
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
//조건문-if문 형식1 : if... else....
int age = 20 ; // 20세
if(age<=19) //19세 이하일경우 출력
{
cout << "미성년자입니다.\n";
}
else // 19세 이하가 아닐경우
{
cout << "성인입니다\n";
}
}
<소스코드>
<실행결과>
<주석>
[1]-1 if문 형식2
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
int age=19;
//조건문-if문 형식2 : if... elseif....else...
if (age<=18){
cout <<"미성년자입니다\n"; // 18세 이하
}
else if(age==19){
cout <<"아쉽네요\n"; // 18세 초과인 경우중 19세인경우
}
else
{
cout<<"성인입니다\n"; // 20세 이상
}
}
<소스코드>
<실행결과>
<주석>
[2] switch
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
int score = 2; //score선언
switch( score )
{
case 0: //score=0
cout << "밀려 쓰셨군요~\n";
break;
case 1: //score=1
cout << "조금 더 노력하세요~\n";
break;
case 2: //score=2
cout << "안타깝네요\n";
break;
case 3: //score=3
cout << "멋있어요~\n";
break;
}
}
<소스코드>
<실행결과>
<주석>
2. 반복문
[1]for
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
int sum=0;
int i;
//반복문 -for 문
for (i=1; i<=10; i++)
sum+=i;
cout << "현재 i의 값 = " << i << endl;
cout << "1~10까지의 합 = " << sum << endl;
<소스코드>
<실행결과>
<주석>
[2]while
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
int i=1; //1. 초기값
int sum=0;
while (i<=10) //2. 조건식 (i가 10이하일때까지)
{
sum+=i;
i++; //3. 증감식
}
cout << "현재 i의 값 = " << i << endl;
cout << "1~10까지의 합 = "<< sum << endl;
}
<소스코드>
<실행결과>
<주석>
[3]do-while
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
int i=1; //초기값
int sum=0; //초기값
do
{
sum+=i; //sum값에 i를 더함
i++; // i 1씩 증가
} while(i<=10); //i가10이하일때까지 실행
cout << "현재 i의 값 = " << i << endl;
cout << "1~10까지의 합 = " << sum << endl;
}
<소스코드>
<실행결과>
<주석>
3. 기타제어문
[1]continue문
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char** argv) {
int i;
for (i=1; i<=10; i++)
{
if(i==5) continue;
cout << "i의 값 = " << i << endl;
}
}
<소스코드>
<실행결과>
<주석>
[2]break문
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char** argv) {
for (int i= 1; i<=10; i++)//i가 1부터 10이 될때까지 실행
{
if (i==5)
break; //i가 5인 경우는 반복문을 빠져나감.
cout << " i의 값 = " << i << '\n'; //결과출력
}
}
<소스코드>
<실행결과>
<주석>
2장 실습문제
1. 1~100까지의 정수출력
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
//1번
int i;
for(i=1; i<=100; i++){
cout << i;
if(i%10 == 0)
cout << endl;
else
cout << '\t';
}
return 0;
}
<소스코드>
<주석>
2. 구구단 출력
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
//2번
int i,j;
for(i=1;i<10;i++) {
for(j=1;j<10;j++){
cout << j << 'x' << i << '=' << j*i << '\t';
}
cout << endl;
}
return 0;
}
<소스코드>
<실행결과>
<주석>
3. 두 개의 정수중 큰 수 출력
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
//3번
int a, b;
cout << "두 수를 입력하라>>";
cin >> a >> b;
cout << "큰 수 = ";
if(a>b)
cout << a << endl;
else
cout << b << endl;
return 0;
}
<소스코드>
<실행결과>
<주석>
4.입력받은 두 개의 문자열이 같으면 "같습니다", 아니면 "같지않습니다."를 출력
#include <iostream>
#include <cstring>
using namespace std;
int main(int argc, char** argv) {
//4번
char password1[100];
char password2[100];
cout << "새 암호를 입력하세요>> ";
cin >> password1;
cout << "새 암호를 다시 한 번 입력하세요>> ";
cin >> password2;
if (strcmp(password1, password2) == 0)
cout << "같습니다. \n";
else
cout << "같지 않습니다. \n";
cout << endl;
return 0;
}
<소스코드>
<실행결과>
<주석>
5. yes가 입력되면 종료되는 프로그램
#include <iostream>
#include <cstring> //strcmp
using namespace std;
int main(int argc, char** argv) {
//5번
char yes[100];
while(true){
cout << "종료하고 싶으면 yes를 입력하세요>> ";
cin.getline(yes, 100); //cin.getline(yes, 100, '\n');
if (strcmp(yes, "yes")==0)
break;
}
cout << "종료합니다...." << endl;
return 0;
}
<소스코드>
<실행결과>
<주석>
6. 이름,주소,나이 입력받아 출력
#include <iostream>
#include <cstring>
using namespace std;
int main(int argc, char** argv) {
//6번
char name1[100];
char address[100] ;
int age;
cout << "이름은? ";
cin.getline(name1,100);
cout << "주소는? ";
cin.getline(address,100);
cout << "나이는? ";
cin >> age;
cout << name1 << "," << address << "," << age << "세" << endl;
return 0;
}
<소스코드>
<실행결과>
<주석>
7. 5칙연산(+,-,*,/,%)을 하는 프로그램
#include <iostream>
#include <cstring>
#include <cstdlib> // system("cls"); 화면지우기
using namespace std;
int main(int argc, char** argv) {
//7번
char opcode;
int left, right, result;
while (true){
cout << "부호를 입력하세요 (종료:q) : ";
cin >> opcode;
if(opcode == 'q')
break;
cout << "숫자1입력 : ";
cin >> left;
cout << "숫자2입력 : ";
cin >> right;
switch (opcode)
{
case '+':
result = left + right;
break;
case '-':
result = left - right;
break;
case '*':
result = left * right;
break;
case '/':
if(right==0){
cout << "0으로 나눈 오류발생!" << endl;
return -1; // -1은 0으로 나누는 오류임을 표시함
}
else
result = left / right;
break;
case '%':
result = left % right;
break;
default:
cout << "잘못된 연산자" << endl;
}
cout << result << endl;
system("pause"); //화면을 멈추고
system("cls"); //화면정리
}
}
<소스코드>
<실행결과>
<주석>
댓글
댓글 쓰기