C++ Primer 04 프로그래밍 연습
01
#include <iostream>
int main()
{
using namespace std;
cout << "이름을 입력해주세요 : ";
char f_name[20];
cin.getline(f_name, 20);
cout << "성을 입력해주세요 : ";
char l_name[20];
cin.getline(l_name, 20);
cout << "나이를 입력해주세요 : ";
int age;
cin >> age;
cout << "원하는 학점은 무엇입니까? : ";
char grade;
cin >> grade;
cout << "영문 퍼스트 네임(이름) : " << f_name << endl;
cout << "영문 라스트 네임(성) : " << l_name << endl;
cout << "학생이 원하는 학점 : " << grade << endl;
cout << "나이 : " << age
<< "\n성명 : " << l_name << ", " << f_name
<< "\n학점 : " << char (grade + 1)
<< "\n나이 : " << age << endl;
return 0;
}
02
#include <iostream>
#include <string>
int main()
{
using namespace std;
string f_name, l_name;
cout << "이름을 입력해주세요 : ";
getline(cin, f_name);
cout << "성을 입력해주세요 : ";
getline(cin, l_name);
cout << "나이를 입력해주세요 : ";
int age;
cin >> age;
cout << "원하는 학점은 무엇입니까? : ";
char grade;
cin >> grade;
cout << "영문 퍼스트 네임(이름) : " << f_name << endl;
cout << "영문 라스트 네임(성) : " << l_name << endl;
cout << "학생이 원하는 학점 : " << grade << endl;
cout << "나이 : " << age
<< "\n성명 : " << l_name << ", " << f_name
<< "\n학점 : " << (char)(grade + 1)
<< "\n나이 : " << age << endl;
return 0;
}
03
#include <iostream>
#include <cstring>
int main()
{
using namespace std;
cout << "이름을 입력해주세요 : ";
char f_name[20];
cin.getline(f_name, 20);
cout << "성을 입력해주세요 : ";
char l_name[20];
cin.getline(l_name, 20);
char full_name[40];
strcpy(full_name, l_name);
strcat(full_name, ", ");
strcat(full_name, f_name);
cout << "하나의 문자열로 만들면 : " << full_name << endl;
return 0;
}
04
#include <iostream>
#include <string>
int main()
{
using namespace std;
string f_name, l_name;
cout << "이름을 입력해주세요 : ";
getline(cin, f_name);
cout << "성을 입력해주세요 : ";
getline(cin, l_name);
string full_name = l_name + ", " + f_name;
cout << "하나의 문자열로 만들면 : " << full_name << endl;
return 0;
}
05
#include <iostream>
struct CandyBar
{
char name[20];
double weight;
int cal;
};
int main()
{
using namespace std;
CandyBar candy = {"Mocha Munch", 2.3, 350};
cout << "name is " << candy.name << endl;
cout << "weight is " << candy.weight << endl;
cout << "calorie is " << candy.cal << endl;
return 0;
}
06
#include <iostream>
struct CandyBar
{
char name[20];
double weight;
int cal;
};
int main()
{
using namespace std;
CandyBar candy[3] = {
{"Mocha Munch", 2.3, 350},
{"B", 4.6, 700},
{"C", 1.0, 100}
};
cout << "candy 1\n";
cout << "name is " << candy[0].name << endl;
cout << "weight is " << candy[0].weight << endl;
cout << "calorie is " << candy[0].cal << endl;
cout << "\ncandy 2\n";
cout << "name is " << candy[1].name << endl;
cout << "weight is " << candy[1].weight << endl;
cout << "calorie is " << candy[1].cal << endl;
cout << "\ncandy 3\n";
cout << "name is " << candy[2].name << endl;
cout << "weight is " << candy[2].weight << endl;
cout << "calorie is " << candy[2].cal << endl;
return 0;
}
07
#include <iostream>
struct pizza
{
char name[20];
double dia;
double weight;
};
int main()
{
using namespace std;
pizza piz;
cout << "피자 회사 이름을 입력하세요 : ";
cin.getline(piz.name, 20);
cout << "피자의 지름을 입력하세요 : ";
cin >> piz.dia;
cout << "피자의 중량을 입력하세요 : ";
cin >> piz.weight;
cout << "피자 회사의 이름은 " << piz.name << "입니다.\n"
<< "피자의 지름은 " << piz.dia << "입니다.\n"
<< "피자의 중량은 " << piz.weight << "입니다.\n";
return 0;
}
08
#include <iostream>
struct pizza
{
char name[20];
double dia;
double weight;
};
int main()
{
using namespace std;
pizza * piz = new pizza;
cout << "피자 회사 이름을 입력하세요 : ";
cin.getline(piz->name, 20);
cout << "피자의 지름을 입력하세요 : ";
cin >> piz->dia;
cout << "피자의 중량을 입력하세요 : ";
cin >> piz->weight;
cout << "피자 회사의 이름은 " << piz->name << "입니다.\n"
<< "피자의 지름은 " << piz->dia << "입니다.\n"
<< "피자의 중량은 " << piz->weight << "입니다.\n";
return 0;
}
09
#include <iostream>
#include <cstring>
struct CandyBar
{
char name[20];
double weight;
int cal;
};
int main()
{
using namespace std;
CandyBar * candy = new CandyBar[3];
strcpy(candy[0].name, "A");
candy[0].weight = 1.0;
candy[0].cal = 11.1;
strcpy(candy[1].name, "B");
candy[1].weight = 2.0;
candy[1].cal = 22.2;
strcpy(candy[2].name, "C");
candy[2].weight = 3.0;
candy[2].cal = 33.3;
cout << "candy 1\n";
cout << "name is " << candy[0].name << endl;
cout << "weight is " << candy[0].weight << endl;
cout << "calorie is " << candy[0].cal << endl;
cout << "\ncandy 2\n";
cout << "name is " << candy[1].name << endl;
cout << "weight is " << candy[1].weight << endl;
cout << "calorie is " << candy[1].cal << endl;
cout << "\ncandy 3\n";
cout << "name is " << candy[2].name << endl;
cout << "weight is " << candy[2].weight << endl;
cout << "calorie is " << candy[2].cal << endl;
return 0;
}
10
#include <iostream>
#include <array>
int main()
{
using namespace std;
array<double, 3> run;
cout << "첫번째 달리기 결과를 입력하시오 : ";
cin >> run[0];
cout << "두번째 달리기 결과를 입력하시오 : ";
cin >> run[1];
cout << "세번째 달리기 결과를 입력하시오 : ";
cin >> run[2];
cout << "총 세번의 달리기의 평균은 " << (run[0] + run[1] + run[2]) / 3 << "입니다.\n";
return 0;
}