Cpp class exercise 1:修订间差异
		
		
		
		跳转到导航
		跳转到搜索
		
| 无编辑摘要 | |||
| 第132行: | 第132行: | ||
| == 分支结构(1) == | == 分支结构(1) == | ||
| <syntaxhighlight lang="C++" line> | |||
| #include <iostream> | |||
| using namespace std; | |||
| float BMI(float h, float w); | |||
| int main() | |||
| { | |||
|     float h,w,bmi; | |||
|     cout << "请输入身高(米)" <<endl; | |||
|     cin >> h; | |||
|     cout << "请输入体重(公斤)" <<endl; | |||
|     cin >> w; | |||
|     bmi = BMI(h, w); | |||
|     cout << "您的身高体重指数是:" << bmi <<endl; | |||
|     if(bmi < 18.5) | |||
|     { | |||
|     	cout << "您的体重过轻。" <<endl; | |||
|     } | |||
|     if(bmi >= 18.5 && bmi <= 23.9) | |||
|     { | |||
|     	cout << "您的体重正常。" <<endl; | |||
|     }    | |||
|     if(bmi > 23.9) | |||
|     { | |||
|     	cout << "您的体重过重。" <<endl; | |||
|     } | |||
|     return 0; | |||
| } | |||
| float BMI(float h, float w) | |||
| { | |||
| 	return w / (h*h) ; | |||
| } | |||
| </syntaxhighlight> | |||
| == 循环结构(1) == | == 循环结构(1) == | ||
2019年3月21日 (四) 19:23的版本
安装编程开发环境
1、下载Dev-C++ 5.3.0.2 临时
百度下载
2、安装并设置 Dev-C++
Hello World 程序
#include <iostream>
using namespace std;
int main()
{
    cout << "Hello, World!" <<endl;
    return 0;
}
四则运算题
#include <iostream>
using namespace std;
int main()
{
    cout << 1 + 2 <<endl;
    cout << 3 - 1 <<endl;
    cout << 9 * 9 <<endl;
    cout << 9 / 9 <<endl;
    return 0;
}
带输入输出的四则运算题
#include <iostream>
using namespace std;
int main()
{
    int i,j,k;
    cout << "请输入第1个数" <<endl;
    cin >> i;
    cout << "请输入第2个数" <<endl;
    cin >> j;
    k = i + j;
    cout << i << "+" << j << "=" << k <<endl;
    return 0;
}
扩展,如果写成了i,会怎么样? 试着运行一下,看看输出结果是什么。想一想为什么?应该注意什么?
简单函数
#include <iostream>
using namespace std;
int add(int i, int j);
int main()
{
    int i,j,k;
    cout << "请输入第1个数" <<endl;
    cin >> i;
    cout << "请输入第2个数" <<endl;
    cin >> j;
    k = add(i, j);
    cout << i << "+" << j << "=" << k <<endl;
    return 0;
}
int add(int i, int j)
{
	return i + j;
}
注意观察第3行和第17行有什么不同? 为什么需要写第3行的内容? 不写会怎么样?
数据类型(1)
试着运行以下代码,计算身高体重指数(BMI)
#include <iostream>
using namespace std;
int BMI(int h, int w);
int main()
{
    int h,w,bmi;
    cout << "请输入身高(米)" <<endl;
    cin >> h;
    cout << "请输入体重(公斤)" <<endl;
    cin >> w;
    bmi = BMI(h, w);
    cout << "您的身高体重指数是:" << bmi <<endl;
    return 0;
}
int BMI(int h, int w)
{
	return w / (h * h);
}
运行结果会是多少呢? 对不对? 应该怎么改呢?
分支结构(1)
#include <iostream>
using namespace std;
float BMI(float h, float w);
int main()
{
    float h,w,bmi;
    cout << "请输入身高(米)" <<endl;
    cin >> h;
    cout << "请输入体重(公斤)" <<endl;
    cin >> w;
    bmi = BMI(h, w);
    cout << "您的身高体重指数是:" << bmi <<endl;
    
    if(bmi < 18.5)
    {
    	cout << "您的体重过轻。" <<endl;
    }
    
    if(bmi >= 18.5 && bmi <= 23.9)
    {
    	cout << "您的体重正常。" <<endl;
    }   
    
    if(bmi > 23.9)
    {
    	cout << "您的体重过重。" <<endl;
    }
    
    
    return 0;
}
float BMI(float h, float w)
{
	return w / (h*h) ;
}