C++ 语法概括总结

本文概述了C++中的输入输出注意事项,包括C风格与C++风格的区别,以及使用scanf和printf的情况。同时,详细介绍了条件语句、数组、指针、函数的使用,并提到了其他关键知识点,如数据类型转换、逻辑运算符、浮点数比较和typedef的运用。
摘要由CSDN通过智能技术生成

1 输入输出

1.1 注意事项

  • 一个.cpp文件,最好不要 printf 和 cout 交杂,要么全用C,要么全用C++
  • 除了string的输入用cin外,其余任何都用scanf、printf
  • 涉及浮点数,都用double

1.2 输入

1.2.1 C

#include<cstdio>

输入停止符(不能读)可读输出举例
scanf空格、/n当读入%c,可读空白(空格、/n、/t)printf
getchar读一个字符【任何】putcharchar ch;
ch = getchar();
putchar(ch);
gets\n读一行char数组【可读 空格,不读 \n】putschar chs[10];
gets(chs);
puts(chs);
// 用getchar 输入字符串数组
char str[20];
for(int i=0;i<5;i++){
	str[i] = getchar();
}
str[5] = '\0';  		// 关键

1.2.2 C ++

#include<iostream>

输入停止符(不能读)可读输出举例
cin>>xxx>>…空格、/ncout<<xxx<<…
getline/n读一行char数组 或 string【可读 空格,不读 \n】char chs[10]; string str;
cin.getline(chs,10);
getline(cin, str);
	int a;
	char b[20];
	double db;
	string str;
	cin>>a>>b>>db>>str;
	cout<<a<<endl<<b<<endl<<setiosflags(ios::fixed)<<setprecision(2)<<db<<endl<<str<<endl;
	//
	cin.getline(b,5); //只能读4个+\0
	cout<<b<<endl;
	getline(cin,str);
	cout<<str<<endl;

1.2.3 C/C++ 字符串输入小结

字符串类型是否含有空格输入
char*没有scanf
gets(char*);
string没有cin
getline(cin, string);

1.3 scanf / printf

#include<cstdio>
#include<iostream>
#include<cmath>

using namespace std;

const double PI = acos(-1.0);
const int INF = 0x3fffffff;     //无穷大, 7个f

int main(){
	int a;
	scanf("%d",&a);
	printf("%d",a);
	printf("%6d",a);        // 右对齐,不足6位的补空格
	printf("%06d",a);       // 右对齐,不足6位的补0

	long long b;
	scanf("%lld",&b);
	printf("%lld",b);       // 保留小数

	char c;
	scanf("%c",&c); 	    // 可读空白(空格、/n、/t)
	printf("%c",c);

	double d;
	scanf("%lf",&d);	    // %lf
	printf("%f",d);
	printf("%.2f",d);


	char s[20];
	scanf("%s",s);          // 没有 &
	printf("%s",s);


	string str;
	cin>>str;
	printf("%s",str.c_str());

	return 0;
}

2 条件、数组

2.1 if

	if(n){		// n != 0
		... 
	} 
	if(!n){		// n == 0
		... 
	}

2.2 switch

	int a = 1 , b=1;
	int c = 5;
	switch(c){
		case 2:
		case 3:
		case 4:
			printf("%d",a+b);       //c=2 || c=3 || c=4
			break;
		case 5:
			printf("%d",a-b);
			break;
		default:
			printf("end");
	}

2.3 数组

二维初始化:

	int arr[3][6] = {{1,2,3},{},{4,5}};

当数组较大( 1 0 6 10^6 106)时, 设置全局变量


3 指针

unsigned类型的整数

3.1 指针变量

	int *a;
	int b;
	a = &b;		//a指向b

a的内容:b的地址

  • a 等价于 &b
  • *a 等价于 b

扩展:
搞懂以下代码

    int *a;
    int b;
    a = &b;		//a指向b


    //
    b = 100;
    printf("*a = %d  b = %d  a = %d  &b = %d\n",*a,b,a,&b);
    *a = 200;   //改变b
    printf("*a = %d  b = %d  a = %d  &b = %d\n",*a,b,a,&b);
    //
    a ++;                       // 跨越了一个int存储单元(4B)
    printf("a = %d\n",a);

在这里插入图片描述

3.2 指针数组

	int a[5];
	for(int i=0; i<5; i++){
		scanf("%d",a+i);            // 相当于 scanf("%d", &a[i]);
	}
	for(int i=0;i<5;i++){
		printf("%d ",*(a+i));       // 相当于 printf("%d ",a[i]);
	}
	//
	int *p;
	p = a;			                //相当于 p = &a[0];
	for(; p<a+5; p++){
		scanf("%d", p);
	}
	for(p=a; p<a+5; p++){
		printf("%d ",*p);
	}


4 函数

4.1 传递参数:变量

一共有5种情形,对应如下5个swap函数

// 不改变main中变量(函数中的变量是main中的副本)
void swap1(int a, int b){
	int temp;
	temp = a;
	a = b;
	b = temp;
}

//引用:改变main中的变量(函数中的变量是main中的别名)
void swap2(int &a, int &b){
	int temp;
	temp = a;
	a = b;
	b = temp;
}

// 地址传值/指针传值:改变main中的变量
void swap3(int * a, int * b){
	int temp;			// int *temp;
	temp = *a;			// *temp = *a;  运行错误,理由:temp未初始化,存放随机
	*a = *b;
	*b = temp;
}

// 不改变main中变量
void swap4(int * a, int * b){	
	int *temp;
	temp = p;
	p = q;
	q = temp;
}

// 指针引用:不改变main中变量
void swap5(int * &p, int * &q){	
	int *temp;
	temp = p;
	p = q;
	q = temp;
}

int main(){
	int a=1, b=2;
	swap1(a, b);
	swap2(a, b);		// 交换
	swap3(&a, &b);		// 交换
	swap4(&a, &b);	
	int *p = &a, *q = &b;
	swap5(p, q);
	return 0;
}

4.2 传递参数:数组

void fun(int a[], int b[][3]){
	...
}

int main(){
	int a[10];
	int b[5][3];
	fun(a, b);
	return 0;
}

5 其他

5.1 int / long long(空间大小)、OJ最大运算次数(时间大小)

一般比赛平台,最大运算次数: 1 0 7 10^7 107

类型位数可表示范围
int32 − 2 31 -2^{31} 231 − 2 ∗ 1 0 9 -2*10^9 2109) ~~~ 2 31 − 1 2^{31}-1 2311 2 ∗ 1 0 9 2*10^9 2109
long long64 − 2 63 -2^{63} 263 − 9 ∗ 1 0 18 -9*10^{18} 91018) ~~~ 2 63 − 1 2^{63}-1 2631 9 ∗ 1 0 18 9*10^{18} 91018
总结:
  • x <= 1 0 9 10^9 109 或 32位,用int
  • x <= 1 0 18 10^{18} 1018 或 64位,用long long

5.2 ASCII码

小写 = 大写 + 32

字符ASCII码
‘0’48
‘A’65
‘a’97
	char c = 117;
	printf("%c",c);
	char c = '9';
	printf("%d",c);

5.3 数据类型的转换

  1. char*(字符串数组)<——> int、double、 char*(字符串数组)
    sscanf()和sprintf()
    详情见:此文章:3 cstring

  2. char*(字符串数组) <——> string
    详情见:此文章:2 string

  3. int <——> double
    int a; double db; a = (int)db; db = (double)a;

5.4 逻辑运算符

类型代码
&&
||
!
位与a & b
位或a | b
位异或a ^ b
取反~ a

1左移8位:
1<<8 = 2 8 2^{8} 28

5. 5 条件运算符

int max(int a, int b){
	return a>b?a:b ;
}

5.6 typedef

当复杂数据类型出现太多,起别名

typedef 数据类型 别名
如:typedef long long LL;

#include<cstdio>
using namespace std;

typedef long long LL;  

int main(){
	LL a = 23LL;
	printf("%lld",a);
	return 0;
}

5.7 浮点数的比较

#include<cstdio>
#include<cmath>

using namespace std;

const double eps = 1e-8;                            // 1*10^(-8)

// define中定义的函数:变量带(),返回bool
#define Equ(a,b) ( (fabs((a)-(b))) < (eps))         // a==b:  b-eps< a <b+eps
#define More(a,b) ( ((a)-(b)) > (eps))              // a>b:   a > b+eps
#define Less(a,b) ( ((a)-(b)) < (-eps))             // a<b:   a < b-eps
#define MoreEqu(a,b) ( ((a)-(b)) > (-eps))          // a>=b:  a > b-eps
#define LessEqu(a,b) ( ((a)-(b)) > (eps))           // a<=b:  a < b+eps

int main(){
	double db1 = 4 * asin(sqrt(2.0)/2);      // 4 * pi/4
	double db2 = 3 * asin(sqrt(3.0)/2);	     // 3 * pi/3
	// 浮点数的比较
	printf("%d\n",db1==db2);                // 不精确
	printf("%d\n",Equ(db1,db2));            // 精确

	// 修正
	double res = -0.00;                     //某个结果
    printf("%f\n",res+eps);                 // 修正

	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值