c和指针阅读笔记

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c和指针阅读笔记相关的知识,希望对你有一定的参考价值。

第四章:语句

要点记录

????

课后答案及编程练习

  • 问题
  1. 合法,但是语句计算结果不影响变量的原值
  2. 左值必须可修改
  3. 合法
  4. 使用分号
  5. 打印 0~9
  6. while不确定到底要执行多少次,for适用于明确执行次数
  7. 两个错误 while循环要加括号
  8. 如果要先执行一次在判断循环是否满足,使用do while
  9. 缺少break;
  10. 代码如下

#include <stdio.h>

?

void main()

{

int x;

int i;

scanf("%d",&x);

?

for(i = 0; i <x;i++)

{

printf("\n");

}

}

?

  1. if(( x>y) || (a>b))

    printf(" WRONG");

    else

    ????printf(" RIGHT");

  2. if((year/4==0&&year/100!=0)||(year/400==0)) year_leap=1; else year_leap =0;
  3. switch case
  4. while( hungry() ) eat_hamburger();
  5. do while
  • 编程练习
  1. 代码如下

#include <math.h>

?

const double DIFF = 0.00001;

?

int main(int argc, char* argv[])

{

????float n, s,s0;

????s = 1;

????s0 = 0;

?

????scanf("%f", &n);

?

????while (fabs((double)(s - s0)) >= DIFF)

????{

????????s0 = s;

????????s = (s + n / s) / 2;

????????printf(" %f ", s);

????}

????printf("\n Result: %f ", s);

?

????return 0;

}

  1. 代码如下

?

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

#include <math.h>

?

void main()

{

????int n = 2;

????int i = 0;

????int flag = 0;

????int num = 1;

?

????printf(" 2 ");

?

????while (n <= 100)

????{

????????for (i = 2; i < (sqrt(n)+1);i++)

????????{

????????????if ((n%i) == 0)

????????????{

????????????????flag = 1;

????????????????break;

????????????}

????????}

?

????????if (flag == 0)

????????{

????????????printf(" %d ", n);

????????????num++;

????????}

????????????

????????n++;

????????flag = 0;

????}

?

????printf("num :\n",num);

}

  1. 代码如下

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

?

void main()

{

????int a, b, c;

????scanf("%d,%d,%d", &a, &b, &c);

?

????if ((a >= (b + c)) || (b >= (c + a)) || (c >= (a + b)))

????????printf(" Input Error");

?

????if (a == b)

????{

????????if (a != c)

????????????printf("Isosceles triangle");

????????else

????????????printf("An equilateral triangle");

????}

????else

????{

????????if (a == c)

????????{

????????????if (a != b)

????????????????printf("Isosceles triangle");

????????????else

????????????????printf("An equilateral triangle");

????????}

????????else

????????{

????????????if (b == c)

????????????{

????????????????if (a != b)

????????????????????printf("Isosceles triangle");

????????????????else

????????????????????printf("An equilateral triangle");

????????????}

????????????else

????????????????printf("triangle");

????????}

????}

?

????printf("\n the end \n");

}

?

  1. 代码如下

void My_strcpy(char dst[], const char str[],const int number)

{

????int srclength = strlen(str);

????int i = 0;

?

????if (srclength <= number)

????{

????????strcpy(dst, str);

????????for (i = srclength; i < number;i++)

????????{

????????????dst[i] = ‘\0‘;

????????}

????}

????else

????{

????????for (i = 0; i < number;i++)

????????{

????????????dst[i] = str[i];

????????}

????}

}

  1. 代码如下

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

?

const int MAX_NUM = 128;

?

void main()

{

????char buff[MAX_NUM];

????char buff2[MAX_NUM];

?

????while (gets_s(buff, MAX_NUM) != NULL)

????{

????????if (strcmp(buff, buff2) != 0)

????????????puts(buff);

?

????????strcpy(buff2, buff);

????}

?

????printf("========");

}

?

  1. 代码如下

int substr(char dst[], const char src[], const int offset, const int len)

{

????int srclength = strlen(src);

????int i,reallength,j;

????if ((srclength < offset) || (offset <0) ||(len <0))

????????return 0;

?

????reallength = ((offset + len) > srclength) ? srclength : (offset + len);

????for (i = offset; i < reallength;i++)

????????dst[i-offset] = src[i];

?

????j = reallength - offset;

????dst[i] = ‘\0‘;

????return j;

}

?

  1. 代码如下

void debblank(char string[])

{

????int length = strlen(string);

????int i, j, flag;

????flag = 0;

????/*遍历数组*/

????for (i = 0; i <(length-1);i++)

????{

????????/*防止数组越界,将长度-1 ,判断当前数组值与下一个值都是空格,则i存入下一数组下表,flag标志置1*/

????????if ((string[i] == ‘ ‘) && (string[i + 1] == ‘ ‘))

????????{

????????????flag = 1;

????????????i++;

????????}

????????????

????????/*如果flag标志位1 说明连续两个空格存在,且下标标志位在第二个空格*/

????????if (flag == 1)

????????{

????????????/*移动数组,覆盖第一个空格*/

????????????for (j = i; j < (length-1) ;j++)

????????????????string[j-1] = string[j];

?

????????????/*确保数组最后一位被移动*/

????????????string[j] = string[length];

?

????????????/*覆盖后长度-1*/

????????????length--;

????????}

?

????????/*如果移动过数组,光标跳入下一数组下标,且当前还是空格,则回退至上一层,否则回档当前位置*/

????????if (flag == 1)

????????{

????????????if (string[i] == ‘ ‘)

????????????{

????????????????/*-2 是由于 for循环含有-1 */

????????????????i = i - 2;

????????????}

????????????else

????????????{

????????????????

????????????????flag = 0;

????????????????i--;

????????????}

????????}

????}

}

以上是关于c和指针阅读笔记的主要内容,如果未能解决你的问题,请参考以下文章

阅读笔记《C程序员 从校园到职场》第七章 指针和结构体

C语言学习笔记--指针阅读技巧

C和指针笔记

C语言进阶笔记深入了解进阶指针

C语言进阶笔记深入了解进阶指针

C语言进阶笔记深入了解进阶指针