碎片知识点整理

常量

1. 常量

1整形常量赋值给布尔型时自动转化为true或false(文件扩展名必须为.cpp否则加上头文件

       2常量两种定义方式:

        #define ADD(a,b) ((a)+(b))  //每个都加括号防止出错

      const double PI =3.14;//记得不要漏分号 主推荐

     typedef long long LL;

2. 数据类型与格式化输出

数据类型:绝对值在1e9以内的整数都可以定义成int类型,整数超过2147483647(例如1e10或1e18)用long long 

     long long a ;   scanf("%lld",&a);  printf("%d",a);  

     double a ;   scanf("%lf",&a);  printf("%f",a);

     float  a ;   scanf("%f",&a);  printf("%f",a);

     

格式化输出:%md以m位进行右对齐

    %0md以m位进行右对齐,不足的地方补0
    %.mf,四舍六入五成双,保留m位小数,四舍六入用round

#include<stdio.h>
#include<math.h>
int main()
{
    
    printf("%.2f,%.2f\n",3.125,3.135);//四舍六入五成双 
    printf("%.2f,%.2f\n",round(3.135),round(3.125));// 四舍五入到整数部分 
    printf("%.2f,%.2f\n",round(3.135*100+0.5)/100,round(3.125*100+0.5)/100);//四舍五入到小数后2两位 
    return 0;
    
} 

结果为:

3.13,3.13
3.00,3.00
3.14,3.13

3. 常用函数

1.math类

四舍五入到整数位:double round(double x)
向下取整:floor(double x)
向上取整:ceil(double x)
求绝对值:fabs(double x)
指数函数:double(double a,dpuble b)
以e为底的对数:double log(double x)
三角函数:sin(double x),cos(double x),tan(double x)
asin(double x),acos(double x),atan(double x)

2.其他

memset--数组中每个元素赋相同值0或者-1(速度快)
memset(数组名,值,sizeof(数组名))
注意点:加头文件 string.h,赋除了0和-1之外的其他值用fill函数 ,实践表明用memset赋值1出错

4. 字符数组

1.注意点:

  • 数组较大要将其定义在函数外面,否则可能会异常退出
  • 字符数组可以通过直接赋值字符串来初始化,但是仅限于初始化,其他位置不允许直接赋值字符串
  • gets,sanf赋值字符数组末尾自动添加\0;因此开数组要比实际长度多1

2.几种常见字符处理方法

  • scanf对字符类型有%c,%s两种,%c可以识别空格和换行并读入,而%s以空格和换行为读入的结束标志
  • getchar(),putchar()输入输出当个字符,可以识别换行符
  • gets(),puts()用来输入一行字符串,可以包含空格,以换行符\n作为结束输入标志,注意!!!

  (因此在使用gets之前先使用了scanf(),要用getchar()先接收换行符)

  • string 类型不可以读入空格,空格作为结束输入的标志,但是初始化中有空格可以输出,使用要包含#include<string>头文件

     需要读入空格使用getline()如:string s1; getline(cin,s1); cout<<s1;

  • 4
#include<stdio.h>
int main()
{

char b[10];
char a[10];
scanf("%s",a);//注意无取地址符
getchar();//如果没有此句,输入a回车键之后程序结束 
gets(b);
puts(a);
puts(b);
return 0;

} 

3.处理字符串相关函数

  • strlen()
  • strcmp()字符串比较
  • strcpy(b,a),将a复制给b
  • strcat(a,b)b连接到a后面
  • scannf与sprintf
#include<stdio.h>
int main()
{
    
    char str1[20]="2010:1.50,happy";
    char str2[20];
    int a;
    double b;
    char s[10];
    sscanf(str1,"%d:%lf,%s",&a,&b,s);//从左至右,将str中的字符按格式写到a,b,c中 
    printf("%d %.2f %s\n",a,b,s);
    sprintf(str2,"%d:%.2f,%s",a,b,s);//从右至左,将a,b,s中的内容按格式写道str2中 
    puts(str2);
    return 0;
    
} 

5. 函数,指针,引用

1函数:

数组作为参数时,在函数中对数组元素的修改相当于对原数组元素的修改
数组可以作为参数,但是不可以作为返回类型出现


2指针:

int *p ;*是类型的一部分,p才是变量名,
a+i等同于a[i]

3引用

#include<stdio.h>
void change(int &a,int &b)
{
    int c;
    c=a;
    a=b;
    b=c;
}
int main()
{
    int a=1,b=2;
    change(a,b);
    printf("a=%d ,b=%d",a,b);
    return 0;
    
} 

6. 浮点数比较

//按照区间图理解记忆
#include<stdio.h>
#include<math.h>
const double eps=1e10-8;
const double pi=acos(-1.0);
#define Equ(a,b) ((fabs((a)-(b)))<(eps))//a==b
#define Mor(a,b) (((a)-(b))>(eps))//a>b
#define Les(a,b) (((a)-(b))<(-eps))//a<b
#define MorEqu(a,b) (((a)-(b))>(-eps))//a>=b
#define LesEqu(a,b) (((a)-(b))<(eps))//a<=b

7. 多组输入输出

详见https://blog.csdn.net/qq_40763929/article/details/83096221

  • while(scanf("%s",str)!=EOF){}
  • while(get(s)!=NULL){}
  • while(scanf("%s",str)!=EOF)

       {

if(a==0&&b==0) break;

  }

  • while(scanf("%s",str)!=EOF,a||b)

  {..}

// 逐个读取字符 ,直至按下回车键停止 
    while((a=getchar())!='\n'){        
        sum+=a-'0';    
    } 

 








原文地址:https://www.cnblogs.com/wml2018/p/12153155.html