c和指针阅读笔记
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c和指针阅读笔记相关的知识,希望对你有一定的参考价值。
第四章:语句
要点记录
????
课后答案及编程练习
- 问题
- 合法,但是语句计算结果不影响变量的原值
- 左值必须可修改
- 合法
- 使用分号
- 打印 0~9
- while不确定到底要执行多少次,for适用于明确执行次数
- 两个错误 while循环要加括号
- 如果要先执行一次在判断循环是否满足,使用do while
- 缺少break;
- 代码如下
#include <stdio.h> ? void main() { int x; int i; scanf("%d",&x); ? for(i = 0; i <x;i++) { printf("\n"); } } |
?
- if(( x>y) || (a>b))
printf(" WRONG");
else
????printf(" RIGHT");
- if((year/4==0&&year/100!=0)||(year/400==0)) year_leap=1; else year_leap =0;
- switch case
- while( hungry() ) eat_hamburger();
- do while
- 略
- 编程练习
- 代码如下
#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; } |
- 代码如下
? #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); } |
- 代码如下
#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"); } |
?
- 代码如下
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]; ????????} ????} } |
- 代码如下
#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("========"); } |
?
- 代码如下
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; } |
?
- 代码如下
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和指针阅读笔记的主要内容,如果未能解决你的问题,请参考以下文章