第一章 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<iostream>

using namespace std;

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

F10,保存并运行,查看结果。恭喜你!全世界所有程序员的第一个程序编译测试完成!
在这里插入图片描述

三、语法基础

1.变量的定义

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

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

常用变量类型及范围:
在这里插入图片描述

2.输入输出

整数的输入输出:
#include<iostream>
using namespace std;
int main()
{
	int a, b;
	cin >> a >> b ;
	cout << a + b << endl;	
	return 0;
}
字符串的输入输出:
#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str;
	cin >> str;
	cout << str << endl;	
	return 0;
}
输入输出多个不同类型的变量:
#include<iostream>
#include<string>
using namespace std;
int main()
{
	int a, b;
	string str;
	cin >> a;	
	cin >> b >> str;
	cout << str <<"!!!" << a + b << endl;	
	return 0;
}

3.表达式

整数的加减乘除四则运算:
#include<iostream>
#include<string>
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<iostream>
#include<string>
using namespace std;
int main()
{
	float x = 1.3, y = 3.4;
	cout << x * y << ' ' << x + y <<endl;
	cout << x - y << ' ' << x / y <<endl;	
	return 0;
}
整型变量的自增、自减:
#include<iostream>
#include<string>
using namespace std;
int main()
{
	int a = 1;
	int b = a ++;	
	cout << a << ' ' << b <<endl;
	int c = ++ a;
	cout << a << ' ' << c <<endl;	
	return 0;
}
变量的类型转换:
#include<iostream>
#include<string>
using namespace std;
int main()
{
	float x = 133.56;
	int y = (int)x;	
	cout << x << ' ' << y <<endl;	
	return 0;
}

4.顺序语句

(1)输出第二个整数:
#include<iostream>
#include<string>
using namespace std;
int main()
{
	int a, b, c;
	cin >> a >> b >> c;
	cout << b << endl;	
	return 0;
}
(2)计算 (a + b) * c的值
#include<iostream>
#include<string>
using namespace std;
int main()
{
	int a, b, c;
	cin >> a >> b >> c;
	cout << (a + b) * c << endl;	
	return 0;
}
(3)带余除法
#include<iostream>
#include<string>
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<iostream>
#include<string>
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<iostream>
#include<string>
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<iostream>
#include<string>
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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李老师信息奥赛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值