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

本文介绍了C++编程的基础知识,包括变量的定义、整数和字符串的输入输出、多种类型变量的混合输入输出、基本的数学运算、自增自减操作以及类型转换。此外,还涉及到了顺序语句的使用,如输出特定位置的整数、计算表达式值、带余除法和输出菱形图案。最后讨论了浮点数的比较、处理空格的输入输出以及‘HelloWorld’程序。
摘要由CSDN通过智能技术生成

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(scanf中间会入读空格)

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

NqqGOGO

你的鼓励是我学习的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值