반응형
1. 상자의 체적을 구하는 프로그램.
#include <iostream> using std::cout; using std::endl; using std::cin; #include <math.h> //상자의 부피를 구하는 프로그램 //조건 1. 상자의 크기는 200 * 200 * 200을 넘지 않는다. //조건 2. 사용할 수 있는 가장 작은 변수를 사용하라. int main() { unsigned short length, width, height; bool sw = true; while (sw) { cout << "길이, 너비, 높이를 입력하세요" << endl; cin >> length >> width >> height; if (length > 200 || width > 200 || height > 200) { cout << "값이 범위를 벗어났습니다." << endl; } else { sw = false; } } cout << "상자의 부피는 " << length * width * height << "입니다" << endl; }
2. 평을 평방미터로 환산하는 프로그램
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
#include <math.h>
//평을 평방미터로 환산하는 프로그램
//조건1 : 기호상수를 이용하여 1평당 평방미터를 나타내라.
int main()
{
const double pyeong = 3.3058;
int p;
cout << "평수를 입력하세요 : "; cin >> p;
cout << p << "평은 " << p * pyeong << "평방미터입니다." << endl;
}
3. cm를 피트와 인치로 변환하는 프로그램
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
#include <math.h>
//cm를 피트와 인치로 변환하는 프로그램
int main()
{
const double inch = 2.54;
const double feet = 30.48;
int height;
int ft;
double ih;
cout << "키를 입력하세요 : "; cin >> height;
ft = height / feet;
ih = height - ft*feet;
cout << height << "cm는 " << ft << "피트 " << ih << "인치입니다." << endl;
}
4. 시,분,초로 표현된 시간을 초 단위로 변환하는 프로그램.
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
#include <math.h>
// 시, 분, 초로 표현된 시간을 초 단위로 변환하는 프로그램.
int main()
{
const int hour = 3600;
const int min = 60;
int h, m, s;
cout << "시간을 입력하세요. (시 분 초)" << endl;
cin >> h >> m >> s;
printf("%d시 %d분 %d초는 %d초입니다.", h, m, s, h*hour + m * min + s);
}
5. 구의 표면적과 체적을 구하는 프로그램.
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
#define _USE_MATH_DEFINES
#include <math.h>
//구의 표면적과 부피를 구하는 프로그램
int main()
{
double r;
cout << "구의 반지름을 입력하세요 : "; cin >> r;
cout << "구의 표면적 : " << 4 * M_PI * r << endl;
cout << "구의 부피 : " << 4 / 3 * M_PI * r << endl;
}
6. (문제 생략)
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
#include <string>
using std::string;
//구의 표면적과 부피를 구하는 프로그램
int main()
{
string name;
string doing;
cout << "당신의 이름을 입력하시오 : "; cin >> name;
cout << name << "씨 무엇을 해드릴까요?" << endl;
cout << "할일을 입력하시오 : "; cin >> doing;
cout << doing << "은 할 수 없습니다." << endl;
}반응형