第三章 程序控制结构
第三章程序控制结构:
3.1使用cin和cout输入输出数据
cout << 格式控制器<<表达式;
cin >>格式控制器>>变量;
格式控制器:
boolalpha : 逻辑型用文字true或false输入输出
noboolalpha:逻辑型用整数0或1输入输出
#include <iostream> //标准输入输出流类库
#include <iomanip> // 输入输出操纵器库
using namespace std; //标准输入输出需要std
int main()
{
bool v;
string tell;
cin >> boolalpha >> v;
cin >> tell;
cout << v << '\t' << boolalpha << v << '\t' << noboolalpha << v << endl;
cout << tell << '\t';
cout << "Press Enter to exit...";
system("pause");
return 0;
}
3.2计算三角形面积
已知三角形三边长,计算三角形面积:
面积: s = ( t ( t − a ) ( t − b ) ( t − c ) ) 面积:s=\sqrt{(t(t-a)(t-b)(t-c))} 面积:s=(t(t−a)(t−b)(t−c))
但是计算面积之前,首先要确定所输入的三角形三边是否能构成三角形,完整代码如下
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double a, b, c;
cin >> a >> b >> c;
if (a + b > c && b + c > a && a + c > b) {
double s, t;
t = (a + b + c) / 2.0;
s = sqrt(t * (t - a) * (t - b) * (t - c));
cout << s;
cout << "边长为" << a <<',' << b <<"和" << c << "的三角形面积为" << s << endl;
}
system("pause");
return 0;
}
3.3输入成绩,将成绩分类
90以上A;
80-90:B;
70-80:C;
60-70:D;
60以下:E
#include <iostream>
using namespace std;
int main()
{
int score;
cin >> score;
if (score >= 90) {
cout << "A" << endl;}
else if (score >= 80) {
cout << "B" << endl; }
else if (score >= 70) {
cout << "C" << endl; }
else if (score >= 60) {
cout << "D"; }
else {
cout << "E"; }
system("pause");
return 0;
}
3.4四个整数排序
先输入四个整数,然后自动从小到大排序:
#include <iostream>
using namespace std;
int main()
{
int a, b, c, d, t;
cout << "请输入四个整数"