One day 回顾C++语法(杂乱的)
1.输出格式
int s = 10;
printf("%d\n",s);
int a,b;
scanf("%d%d",&a,&b);
printf("%d",a+b);
2.字符串操作
char str1[20];
char str2[5][20];
gets(str1); //输入字符串
for(int i = 0;i<3;i++){
gets(str2[i]);
}
puts(str1); //输出字符串
for(int i = 0;i<3;i++){
puts(str2[i]);
}
3.定义别名
#include<stdio.h>
typedef long long LL;
int main(){
LL a = 12346797987;
printf("%lld",a);
return 0;
}
4.符号常量和const常量
#include<stdio.h>
//符号常量和const常量
#define PI 3.14
const double pi = 3.14;
#define ADD(a,b) ((a+b))
int main(){
int num = 1234567891LL;
printf("%d\n",num);
char a[25] = "I Love You";
char c[25] = "Me Too";
printf("%s\n%s\n",a,c);
printf("%.
5.向上向下取整
#include<stdio.h>
#include<cmath>
int main(){
double a = 2.5,v = -2.5;
printf("%.0f\t%.0f\n",floor(a),ceil(a));
printf("%.0f\t%.0f\n",floor(v),ceil(v));
//返回double类型
double c = round(3.55);
printf("%d\n",int(c));
return 0;
}
6.结构体
#include<stdio.h>
struct Point{
int x,y;
Point(){}
Point(int _x,int _y){
x = _x;
y = _y;
}
//或者 Point(int _x,int_y):x(_x),y(_y){}
} pt[10];
int main(){
int num = 0;
for(int i = 1;i<=3;i++)
for(int j = 1;j<=3;j++){
pt[num++] = Point(i,j);
}
for(int i = 0;i<num;i++){
printf("%d,%d\n",pt[i].x,pt[i].y);
}
//指针调用 p->x,p->y或者(*p).x
return 0;
}
7.浮点数比较
#include<stdio.h>
#include<math.h>
const double eps = 1e-8;
//等于
#define Equ(a,b) ((fabs((a)-(b)))<(eps))
//小于
#define Less(a,b) (((a)-(b))<(-eps))
//大于
#define More(a,b) (((a)-(b))>(eps))
//大于等于
#define MoreEqu(a,b) (((a)-(b))>(-eps))
//小于等于
#define MoreEqu(a,b) (((a)-(b))<(eps))
const double PI = acos(-1.0 );
int main(){
double db = 1.23;
if(Equ(db,1.23)){
printf("true");
}else{
printf("false");
}
return 0;
}
2019.12.23