当前位置: 首页 > news >正文

C++ 语法基础课1 —— 变量、输入输出、顺序语句

文章目录

  • 1. 变量的定义
  • 2. 输入输出
    • 2.1 整数的输入输出
    • 2.2 字符串的输入输出
    • 2.3 输入输出多个不同类型的变量
  • 3. 表达式
    • 3.1 整数的加减乘除四则运算
    • 3.2 浮点数(小数)运算
    • 3.3 整数变量的自增自减
    • 3.4 变量的类型转换
  • 4. 顺序语句
    • 4.1 输出第二个整数
    • 4.2 计算 (a + b) * c 的值
    • 4.3 带余除法
    • 4.4 求反三位数
    • 4.5 输出菱形
    • 5. 其他
    • 5.1 浮点数的比较
    • 5.2 A + B
    • 5.3 hello world

1. 变量的定义

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

#include<iostream>
using namespace std;

int main()
{
	int a = 5;
	int b = a, d = 10/2;
	return 0;
}
类型关键字
布尔型bool
字符型char
整型int
浮点数型float
双浮点型double

2. 输入输出

2.1 整数的输入输出

#include<iostream>
using namespace std;

int main()
{
	int a,b;
	cin >> a >> b;
	count << a+b << endl;
	return 0;
}

2.2 字符串的输入输出

#include<iostream>
#include<string>

using namespace std;

int main()
{
	string str;
	cin >> str;
	cout << str;
	return 0;
}

2.3 输入输出多个不同类型的变量

#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. 表达式

3.1 整数的加减乘除四则运算

#include<iostream>
#include<string>
using namespace std;

int main()
{
	int a = 6 + 3 * 4 / 2 - 2;
	cout << a << endl;
	int b = a * 10 + 5 / 2;
	cout << b << endl;
	cout << 23 * 56 - 78 / 3 << endl;
	
	return 0;
}
运算符描述实例
+把两个操作数相加A + B 得到 30
-操作数1减去操作数2A - B 得到 -10
*两个操作数相乘A * B 得到 200
/分子除以分母B / A 得到 2
%取模运算符,整数后的余数B % A 得到 0

3.2 浮点数(小数)运算

#include<iostream>
#include<string>
using namespace std;

int main()
{
	float x = 1.5, y = 3.2;
	cout << x * y << ' ' << x + y << endl;
	cout << x - y << ' ' << x / y << endl;
	return 0;
}

3.3 整数变量的自增自减

#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;
}

3.4 变量的类型转换

#include<iostream>
#include<string>
using namespace std;

int main()
{
	float x = 123.12;
	int y = (int)x;
	cout << x << ' ' << y << endl;
	return 0;
}

4. 顺序语句

4.1 输出第二个整数

#include<iostream>
#include<string>
using namespace std;

int main()
{
	int a, b, c;
	cin >> a >> b >> c;
	cout << b << endl;
	return 0;
}

4.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;
}

4.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.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;
}

4.5 输出菱形

#include<iostream>
#include<string>
using namespace std;

int main()
{
	char c;
	cin >> c;
	cout << "  " << c << endl;
	cout << " " << c << c << c << endl;
	cout << c << c << c << c << c << endl;
	cout << "  " << c << endl;
	return 0;
}

5. 其他

5.1 浮点数的比较

#include<iostream>
#include<cmath>

using namespace std;

const double eps=1e-6

int main()
{
    double x = 1.23456789;
    double a = x*x;
    double b =sqrt(a);
    
    printf("%.10f\n",b);
    if(fabs(x-b)<eps) puts("相等");
    
    return 0;
}

5.2 A + B

#include<cstdio>
#include<iostream>

using namespace std;

int main()
{
    int a,b;
    scanf("%d%d",&a,&b);
    printf("a+b=%d\na*b=%d\n",a+b,a*b);
    
    char a,b;//会读入空格,所以输入中间不能有空格
    scanf("%c%c",&a,&b);
    printf("%c %c\n",a,b);
    
    // int:%d
    // float:%f
    // double:%lf
    // char:%c
    
    return 0;
}

5.3 hello world

#include<cstido>
#include<iostream>

using namespace std;

int main()
{
    cout << "Hello World" << endl;
    
    bool false/true;    1byte 1字节   1Byte=8bit 1字节=8比特
    char 'a','b',' ','\n';  1byte
    int -2147483648~2147483647  4byte
    float 1.23,1.23e-2,6-7有效数字  4byte
    double 15-16有效数字    8byte
    
    long long -2^63~2^63-1  8byte
    long double 1819return 0;
}

相关文章:

  • M的编程备忘录之C++——map和set
  • 《Orange‘s 一个操作系统的实现》第六章
  • Spring Cloud 拉取 Nacos 中配置文件
  • python-中断time.sleep一种更优雅的办法:event.wait
  • 【毕业设计】大数据公交数据分析与可视化 - 大数据 python falsk
  • Hadoop与Spark中的Shuffle过程梳理
  • CH9101芯片应用—硬件设计指南
  • [NCTF2019]True XML cookbook
  • 湖仓一体电商项目(十二):编写写入DM层业务代码
  • 遥感生态指数(RSEI)——四个指数的计算
  • 9--RNN
  • JDBC的使用
  • 《Mycat分布式数据库架构》之数据切分实战
  • SpringBoot使用spring.config.import多种方式导入配置文件
  • 【框架】Spring Framework :SpringBoot
  • php的引用
  • 07.Android之多媒体问题
  • classpath对获取配置文件的影响
  • CNN 在图像分割中的简史:从 R-CNN 到 Mask R-CNN
  • interface和setter,getter
  • JavaScript创建对象的四种方式
  • JS正则表达式精简教程(JavaScript RegExp 对象)
  • PermissionScope Swift4 兼容问题
  • vue--为什么data属性必须是一个函数
  • 测试开发系类之接口自动化测试
  • 番外篇1:在Windows环境下安装JDK
  • 分布式熔断降级平台aegis
  • 个人博客开发系列:评论功能之GitHub账号OAuth授权
  • 开源中国专访:Chameleon原理首发,其它跨多端统一框架都是假的?
  • 前端每日实战:61# 视频演示如何用纯 CSS 创作一只咖啡壶
  • 驱动程序原理
  • 入门级的git使用指北
  • 设计模式 开闭原则
  • 什么是Javascript函数节流?
  • 使用 @font-face
  • 算法-插入排序
  • 应用生命周期终极 DevOps 工具包
  • 栈实现走出迷宫(C++)
  • AI又要和人类“对打”,Deepmind宣布《星战Ⅱ》即将开始 ...
  • 国内开源镜像站点
  • 曾刷新两项世界纪录,腾讯优图人脸检测算法 DSFD 正式开源 ...
  • ​​​​​​​​​​​​​​汽车网络信息安全分析方法论
  • ​什么是bug?bug的源头在哪里?
  • ${factoryList }后面有空格不影响
  • (22)C#传智:复习,多态虚方法抽象类接口,静态类,String与StringBuilder,集合泛型List与Dictionary,文件类,结构与类的区别
  • (env: Windows,mp,1.06.2308310; lib: 3.2.4) uniapp微信小程序
  • (Git) gitignore基础使用
  • (ZT)出版业改革:该死的死,该生的生
  • (二十一)devops持续集成开发——使用jenkins的Docker Pipeline插件完成docker项目的pipeline流水线发布
  • (附源码)spring boot公选课在线选课系统 毕业设计 142011
  • (附源码)ssm基于微信小程序的疫苗管理系统 毕业设计 092354
  • (附源码)ssm跨平台教学系统 毕业设计 280843
  • (更新)A股上市公司华证ESG评级得分稳健性校验ESG得分年均值中位数(2009-2023年.12)
  • (每日持续更新)信息系统项目管理(第四版)(高级项目管理)考试重点整理第3章 信息系统治理(一)
  • (篇九)MySQL常用内置函数