Cpp class exercise 1:修订间差异
(未显示同一用户的27个中间版本) | |||
第4行: | 第4行: | ||
1、下载Dev-C++ 5.3.0.2 | 1、下载Dev-C++ 5.3.0.2 | ||
{{Clickable button 2|临时(直接)下载|url= http://class.hanyu123.cn/Dev-Cpp_5.3.0.2_Setup.zip|class=mw-ui-progressive}} | |||
{{Clickable button 2|百度云网盘下载|url=https://pan.baidu.com/s/1EcbDDE0dELZ-Dg96kFhTCg|class=mw-ui-progressive}} | |||
2、安装Dev-C++ | |||
3、设置中文界面 | |||
如果安装时不能选择,安装完成后启动Dev-C++,在【Tools】→【Environment Options】中选择界面语言为中文 | |||
http://static.hanyu123.cn/wiki/images/dev-cpp_config_language_cn.png | |||
== 练习说明 == | |||
初学时,不要复制示例代码,请全部都自己输入一遍。 | |||
至少把前3个示例代码(到“带输入输出的四则运算题”)运行一遍,其他部分可尝试。 | |||
C++ 入门知识手册 | |||
https://www.kancloud.cn/wizardforcel/w3school-cpp/100716 | |||
软件相关问题请附屏幕图片 | |||
(参考:屏幕抓图方法汇总 https://mp.weixin.qq.com/s/llhievU1S_Zw6IxCGGqqDg) | |||
== Hello World 程序 == | |||
启动Dev-C++, | |||
按 Ctrl + N (或点击【文件】→【新建】→【源代码】), | |||
新建一个源代码文件 | |||
原样输入(不要复制)以下代码: | |||
<syntaxhighlight lang="C++" line> | |||
#include <iostream> | |||
using namespace std; | |||
int main() | |||
{ | |||
cout << "Hello, World!" <<endl; | |||
return 0; | |||
} | |||
</syntaxhighlight> | |||
按F11键(或【运行】→【编译运行】)运行。 | |||
cout: 输出(out)内容 | |||
保存文件为 hello_world.cpp | |||
扩展: | |||
在第4行("Hello, World!")之后,增加一行,输出中文内容(例如“你好!世界!”)。 | |||
注意中英文引号的不同。 | |||
== 四则运算题 == | == 四则运算题 == | ||
<syntaxhighlight lang="C++" line> | |||
#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; | |||
} | |||
</syntaxhighlight> | |||
扩展:使用任意整数进行运算,观察运行效果。 | |||
如果 运算 9 / 0 会出现什么? 为什么? | |||
== 带输入输出的四则运算题 == | == 带输入输出的四则运算题 == | ||
<syntaxhighlight lang="C++" line> | |||
#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; | |||
} | |||
</syntaxhighlight> | |||
cin: 输入(in)内容 | |||
扩展:如果第9行仍然写成了cin >> i,会怎么样? | |||
试着运行一下,看看输出结果是什么。想一想为什么?应该注意什么? | |||
== 简单函数 == | == 简单函数 == | ||
函数: 函数都有参数(0到n个),并在函数内部运行计算后,返回一个数值。 | |||
观察main()函数和add()函数,各有几个参数,返回的是什么值? | |||
<syntaxhighlight lang="C++" line> | |||
#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; | |||
} | |||
</syntaxhighlight> | |||
注意观察第3行和第17行有什么不同? | |||
为什么需要写第3行的内容? 不写会怎么样? | |||
main() 里面使用了 i,j , add(int i, int j)里面也一定要使用i,j 吗? | |||
可以使用不一样的名称吗? | |||
== 数据类型(1) == | |||
试着运行以下代码,计算身高体重指数(BMI) | |||
<syntaxhighlight lang="C++" line> | |||
#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); | |||
} | |||
</syntaxhighlight> | |||
运行结果会是多少呢? 对不对? | |||
应该怎么改呢? | |||
(可以参考分支结构(1)中的代码) | |||
int:整数,没有小数点;float:实数,可以有小数点 | |||
== 分支结构(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) == | ||
<syntaxhighlight lang="C++" line> | |||
#include <iostream> | |||
using namespace std; | |||
float BMI(float h, float w); | |||
int main() | |||
{ | |||
float h,w,bmi; | |||
int flag = 1; | |||
do{ | |||
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; | |||
} | |||
cout << "是否继续计算?(1:继续;0: 退出)" <<endl; | |||
cin >> flag; | |||
}while(flag == 1); | |||
return 0; | |||
} | |||
float BMI(float h, float w) | |||
{ | |||
return w / (h*h) ; | |||
} | |||
</syntaxhighlight> |
2019年3月24日 (日) 17:58的最新版本
安装编程开发环境
1、下载Dev-C++ 5.3.0.2
Lua错误:无法创建进程:proc_open(/dev/null): Failed to open stream: Operation not permitted Lua错误:无法创建进程:proc_open(/dev/null): Failed to open stream: Operation not permitted
2、安装Dev-C++
3、设置中文界面
如果安装时不能选择,安装完成后启动Dev-C++,在【Tools】→【Environment Options】中选择界面语言为中文
练习说明
初学时,不要复制示例代码,请全部都自己输入一遍。
至少把前3个示例代码(到“带输入输出的四则运算题”)运行一遍,其他部分可尝试。
C++ 入门知识手册 https://www.kancloud.cn/wizardforcel/w3school-cpp/100716
软件相关问题请附屏幕图片
(参考:屏幕抓图方法汇总 https://mp.weixin.qq.com/s/llhievU1S_Zw6IxCGGqqDg)
Hello World 程序
启动Dev-C++, 按 Ctrl + N (或点击【文件】→【新建】→【源代码】), 新建一个源代码文件
原样输入(不要复制)以下代码:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!" <<endl;
return 0;
}
按F11键(或【运行】→【编译运行】)运行。
cout: 输出(out)内容
保存文件为 hello_world.cpp
扩展: 在第4行("Hello, World!")之后,增加一行,输出中文内容(例如“你好!世界!”)。 注意中英文引号的不同。
四则运算题
#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;
}
扩展:使用任意整数进行运算,观察运行效果。 如果 运算 9 / 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;
}
cin: 输入(in)内容
扩展:如果第9行仍然写成了cin >> i,会怎么样? 试着运行一下,看看输出结果是什么。想一想为什么?应该注意什么?
简单函数
函数: 函数都有参数(0到n个),并在函数内部运行计算后,返回一个数值。 观察main()函数和add()函数,各有几个参数,返回的是什么值?
#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行的内容? 不写会怎么样? main() 里面使用了 i,j , add(int i, int j)里面也一定要使用i,j 吗? 可以使用不一样的名称吗?
数据类型(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)中的代码) int:整数,没有小数点;float:实数,可以有小数点
分支结构(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) ;
}
循环结构(1)
#include <iostream>
using namespace std;
float BMI(float h, float w);
int main()
{
float h,w,bmi;
int flag = 1;
do{
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;
}
cout << "是否继续计算?(1:继续;0: 退出)" <<endl;
cin >> flag;
}while(flag == 1);
return 0;
}
float BMI(float h, float w)
{
return w / (h*h) ;
}