栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

第一章 C++入门及简单的顺序结构

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

第一章 C++入门及简单的顺序结构

一、软件环境

1.编译软件的安装与使用
直接下载安装版,然后点击程序进入,建议选择白色主题(不伤眼),下面图示说明,当然也可以直接在在线平台上学习操作(如ACWING:https://www.acwing.com/)
https://royqh1979.gitee.io/redpandacpp/

2.作业的评测与提交
推荐北大闫学灿学长的ACWING网站(你也可以报他的课程,性价比极高)
在线练习地址:www.acwing.com
作业提交地址:https://www.acwing.com/activity/content/21/

二、最简单的C++程序:Hello World
#include

using namespace std;

int main()
{
	cout << "Hello World" << endl;
	return 0;
}

F10,保存并运行,查看结果。恭喜你!全世界所有程序员的第一个程序编译测试完成!

三、语法基础 1.变量的定义

变量必须先定义,才可以使用。不能重名。
变量定义的方式:

#include
using namespace std;
int main()
{
	int a = 5;
	int b, c = a,d = 10 / 2;
	
	return 0;
}

常用变量类型及范围:

2.输入输出 整数的输入输出:
#include
using namespace std;
int main()
{
	int a, b;
	cin >> a >> b ;
	cout << a + b << endl;	
	return 0;
}
字符串的输入输出:
#include
#include
using namespace std;
int main()
{
	string str;
	cin >> str;
	cout << str << endl;	
	return 0;
}
输入输出多个不同类型的变量:
#include
#include
using namespace std;
int main()
{
	int a, b;
	string str;
	cin >> a;	
	cin >> b >> str;
	cout << str <<"!!!" << a + b << endl;	
	return 0;
}
3.表达式 整数的加减乘除四则运算:
#include
#include
using namespace std;
int main()
{
	int a = 6 + 8 * 4 / 2 - 2;
	cout << a << endl;
	int b = a * 4 + 10 / 2;
	cout << b << endl;
	cout << 25 * 56 - 88 / 4 << endl;	
	return 0;
}

浮点数(小数)的运算:
#include
#include
using namespace std;
int main()
{
	float x = 1.3, y = 3.4;
	cout << x * y << ' ' << x + y < 
整型变量的自增、自减: 
#include
#include
using namespace std;
int main()
{
	int a = 1;
	int b = a ++;	
	cout << a << ' ' << b < 
变量的类型转换: 
#include
#include
using namespace std;
int main()
{
	float x = 133.56;
	int y = (int)x;	
	cout << x << ' ' << y < 
4.顺序语句 
(1)输出第二个整数: 
#include
#include
using namespace std;
int main()
{
	int a, b, c;
	cin >> a >> b >> c;
	cout << b << endl;	
	return 0;
}
(2)计算 (a + b) * c的值
#include
#include
using namespace std;
int main()
{
	int a, b, c;
	cin >> a >> b >> c;
	cout << (a + b) * c << endl;	
	return 0;
}
(3)带余除法
#include
#include
using namespace std;
int main()
{
	int a, b;
	cin >> a >> b;
	int c = a / b, d = a % b;
	cout <<  c << ' ' << d << endl;	
	return 0;
}
(4)求反三位数:
#include
#include
using namespace std;
int main()
{
	int n;
	cin >> n;
	int a = n % 10;
	n = n / 10;
	int b = n % 10;
	n = n / 10;
	int c = n ;
	cout << a << b << c << endl;	
	return 0;
}
(5)交换两个整数
#include
#include
using namespace std;
int main()
{
	int a=20;
	int b=30;
	int c = b;
	b = a;
	a = c;
	cout << a <<' '<< b << endl;	
	return 0;
}
(6)输出菱形
#include
#include
using namespace std;
int main()
{
	char a;
	cin >> a;
	cout << "  " << a << endl;	
	cout << " " << a << a << a << endl;	
	cout << a << a << a << a << a << endl;	
	cout << " " << a << a << a << endl;	
	cout << "  " << a << endl;	
	return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/916851.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号