今天网课听的有点少,些许烦心事hh
主要是结构体,联合体,枚举,位域
皮毛理解起来还是很轻松的,脑子里有一些内存的概念,想着在内存中怎么存怎么读取
这些掌握的还不是很扎实,只是听了一遍琢磨了一遍,等过段时间复习之后理论清晰了再分享出来。
今天学着做了个通讯录,才做了一半,还别说还是得亏错的多,看起来挺容易上手再写的话还是有很多小地方是糊涂的,写不出来,然后再跟着看一遍就会有一种豁然开朗的感觉,真不错hh,去食堂吃饭都是香的
记录复习一下昨天和今天的代码
//自己实现的memmove函数
//比如数组1,2,3,4,5,6,7,8,9
//想把1234复制到2345,这时候就要考虑从前拷贝,还是从后拷贝
//如果从前面开始拷贝1复制到到2的时候2已经被改变了
//这时候要从后赋值,这是几种情况,画个图琢磨琢磨
void* my_memmove(const void* dest,const void* src, size_t count)
{
assert(dest);
assert(src);
void* ret = dest;
if (dest > src)
//如果目的地的首地址比源头的首地址要大
{
while (count--)
//因为是void型指针,+count要强制类型转换
{
*((char*)dest + count) = *((char*)src + count);
--(char*)dest;
--(char*)src;
}
}
else
{
while (count--)
{
*(char*)dest = *(char*)src;
++(char*)dest;
++(char*)src;
}
}
return ret;
}
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
my_memmove(arr + 2, arr, 20);
return 0;
}
void* my_memmove(void* dest,void* src, size_t count)
{
assert(dest);
assert(src);
void* ret = dest;
if (dest > src)
{
while (count--)
{
*((char*)dest + count) = *((char*)src + count);
--(char*)dest;
--(char*)src;
}
}
else
{
while (count--)
{
*(char*)dest = *(char*)src;
++(char*)dest;
++(char*)src;
}
}
return ret;
}
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
my_memmove(arr + 2, arr, 20);
return 0;
}
//一瓶汽水一块钱 两个空瓶可以换一瓶汽水 问可以喝多少瓶汽水
int main()
{
int count = 0;
int total = 0;
int n = 0;
scanf_s("%d", &n);
count = n;
total = count;
while (total >= 2)
{
//空瓶÷2就是能喝到的,这只是÷2,空瓶数并没有变
count += total / 2;
//这时候空瓶再÷2,当是奇数的时候还要加上一个
//空瓶÷2是换的瓶数,因为上面已经加上了所以这时候是剩下的瓶子数量,然后再往上循环
total = total / 2 +total % 2;
}
printf("%d\n", count);
return 0;
}
其实还有一个更简单的
×2-1就是能喝多少瓶,把一个空瓶看成五毛钱
20块钱可以买40个,最后手里还剩下一个空瓶
这个是打印星星
上下头都有,下面一层自己琢磨了两个方法,听老师讲的时候才发现自己想的第一种跟老师的一样,但是我觉得那种太麻烦了应该有更简单的,结果第二种琢磨出来之后更麻烦了
#include<math.h>
int main()
{
int x = 0;
int y = 0;
int i = 0;
printf("please input a number :");
scanf_s("%d%d", &x, &y);
int sum = 0;
int ret = 0;
while(i < y)
{
//ret = x * pow(10 ,i) + ret;//
ret = ret * 10 + x;
sum += ret;
++i;
}
printf("%d ", sum);
return 0;
}
int main()
{
int line = 0;
scanf_s("%d", &line);
int i = 0;
for (i = 0; i < line; ++i)
{
int j = 0;
for (j = 0; j < line - 1 - i; ++j)
{
printf(" ");
}
for (j = 0; j < 2 * i + 1; ++j)
{
printf("*");
}
printf("\n");
}
for (i = 0; i < line - 1; ++i)
{
int j = 0;
for (j = 0; j < i + 1; ++j)
{
printf(" ");
}
for (j = 0; j < 2 * (line -1 - i) - 1; ++j)
//这个是j从0开始,相当于从小到大
{
printf("*");
}
printf("\n");
//}
for (i = 0; i < line - 1; ++i)
{
int j = 0;
for (j = 0; j < i + 1; ++j)
{
printf(" ");
}
for (j = 2 * (line - 1); j - (2 * i + 1 )>0; --j)
//这个相当于从大到小,可读性还低,第一种比较好理解
//中间的取值范围要深入理解一下
{
printf("*");
}
printf("\n");
}
return 0;
}
等明天把通讯录写出来,过两天闲了再把扫雷敲一遍,这个还是比三字旗代码量小的,复习一下加深理解,同时多熟悉熟悉写代码的方式
,睡觉,年轻人身体重要😌