02. C Pro 的一些基础小随笔
/* 求两个自然数的最大公约数和最小公倍数
int hcd(int u, int v) {
int a, b, tmp, result;
if (u > v) { tmp = u; u = v; v = tmp; }
a = u; b = v;
while ((result=b%a) != 0) { //此处的简写方式
b = a;
a = result;
}
return a;
}
int lcd(int u, int v, int l) {
return (u*v / l);
}
int main() {
int i, j;
printf("please input two numbers:");
scanf("%d %d", &i, &j);
int h = hcd(i, j);
printf("H.C.D=%d\t", h);
printf("L.C.D=%d", lcd(i, j, h));
}
*/
/* 字符函数
char mm[80];
int i, d = 0, l = 0, u = 0, p = 0, c = 0, o = 0, b = 0;
gets_s(mm); //获取字符串列表
for (i = 0; i < strlen(mm); i++) {
if (isprint(mm[i])) { //可打印
if (isalnum(mm[i])) { //alpha + number
if (isdigit(mm[i])) d++; //number
else if (isupper(mm[i])) u++; //upper
else if (islower(mm[i])) l++; //lower
}
else if (isspace(mm[i])) b++; //space
else if (ispunct(mm[i])) p++; //标点符号
else o++;
}
else if (iscntrl(mm[i])) c++; //control chars
else o++; //others
}
*/
/* 杨辉三角
int i, j, N=10, yy[11][12] = { 0 };
yy[1][1] = 1;
printf("%d\n", yy[1][1]);
for (i = 2; i <= N; i++) {
for (j = 1; j <= i; j++) {
yy[i][j] = yy[i - 1][j - 1] + yy[i - 1][j];
if (yy[i][j] == 0) continue;
printf("%d\t", yy[i][j]);
}
printf("\n");
}
*/
/* 下一跳指针与指针的参数传递
int * tmp, a[5];
tmp = a; //当数组名出现在表达式时,它和指向数组的第一个元素的指针是等价的,使用指针比数组下标快
for (int i = 0; i < 5; i++) {
*tmp = i;
printf("%d %d\n",*tmp,tmp);
tmp += 1; //自动跳到下一个数据类型的位置
}
等价于:
int a[5];
void update(int * j) {
for (int i = 0; i < 5; i++) {
*j = i;
j += 1;
}
}
int main(void) {
int * tmp;
tmp = a;
update(tmp);
for (int i = 0; i < 5; i++) printf("%d \t", a[i]);
}
*/
/*
int count=0;
char ch;
while ((ch = getchar()) != '\n') {
if (ch == ' ') break;
count++;
}
AAA:; //空语句
printf("input %d chars", count);
*/
/* %*3d 跳脱赋值
int a, b, c, d;
scanf("%2d%*3d%3d%d", &a, &b, &c, &d);
printf("%d %d %d %d", a, b, c, d);
*/
/* gets_s 和 scanf 的区别
char str[100];
puts(gets_s(str));
scanf("%s", &str);
printf("%s", str);
*/
/* 强制清空IOStream
putchar(getchar());
fflush(stdin);
putchar(getchar());
putchar('\n');
*/