《C和指针》学习笔记[第四章 语句]

4.13问题

1.

它是合法的,执行了加减乘除操作,但没有赋值

2.

x =  expression

3.

合法

4.

if 后面用;空语句,或者else的条件该到用if的, 用!就可以.

5.

0

1

2

3

4

5

6

7

8

9

6.

个人感觉都差不多。水平有限

7.

少了{},while的执行体就是第一个语句

正确的

while ((ch = getchar()) != EOF) {
        checksum += ch;
        putchar(ch);
    }
    
    printf("chechsum = %d\n", checksum);

8.

无论条件是否成立,都需要执行一次的环境下

9.

odd
even
odd
odd
even
odd

10.

int main(void)
{
    int i;
    
    scanf("%d", &i);
    
    for (; i > 0; i--) {
        printf("\n");
    }
    
    
    return EXIT_SUCCESS;
}

11.

int main(void)
{
    int x, y, a, b;
    
    scanf("%d %d %d %d", &x, &y,&a,&b);
    
    if (x < y || a >= b) {
        printf("WRONG\n");
    }
    else
        printf("RIGHT\n");
    
    
    return EXIT_SUCCESS;
}

12.

int main(void)
{
    int in_year;
    int leap_year;
    
    scanf("%d", &in_year);
    if ((in_year % 4 == 0 && in_year % 100 != 0) || in_year % 400 == 0) {
        leap_year = 1;
    }
    else
        leap_year = 0;
    
    printf("leap_year is %d\n", leap_year);
    
    
    return EXIT_SUCCESS;
}

13.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "t_c.h"




int main(void)
{
    int in_num;
    
    while (scanf("%d", &in_num) != 0) {
        switch (in_num) {
            case 1:
                printf("who\n");
                break;
            case 2:
                printf("waht\n");
                break;
            case 3:
                printf("when\n");
                break;
            case 4:
                printf("where\n");
                break;
            case 5:
                printf("why\n");
                break;
            default:
                printf("don't know");
                break;
        }
    }
    
    
    return EXIT_SUCCESS;
}

14.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "t_c.h"

static int full_num = 10;


void
eat_hamberger(void)
{
    printf("eat one hamberger\n");
    full_num--;
}

int
hungry(void)
{
    return full_num;
}


int main(void)
{
    while (hungry()) {
        eat_hamberger();
    }
    
    
    return EXIT_SUCCESS;
}

15.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "t_c.h"

static int full_num = 0;


void
eat_hamberger(void)
{
    printf("eat one hamberger\n");
    full_num--;
}

int
hungry(void)
{
    if (full_num > 0)
        return 1;
    else
        return 0;
}


int main(void)
{
    
    do {
        eat_hamberger();
    } while (hungry());
    
    return EXIT_SUCCESS;
}

16.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "t_c.h"

int main(void)
{
    
    int precipitsting;
    int temperature;
    
    scanf("%d%d", &precipitsting, &temperature);
    
    if (precipitsting == 1) {
        if (temperature < 32)
            printf("snowing\n");
        else
            printf("raining\n");
    }
    else
    {
        if (temperature < 60) {
            printf("cold\n");
        }
        else
            printf("warm\n");
    }

    
    return EXIT_SUCCESS;
}

 

4.14编程练习

1.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "t_c.h"

int main(void)
{
    int n = 1;
    double i = 1, j;
    while (1) {
        j = (i + n / i)/2;
        printf("%d %lf\n",n, j);
        if ((j - i) < 0.001 && i != 1) {
            break;
        }
        i = j;
        n++;
    }
    
    return EXIT_SUCCESS;
}

2.

int main(void)
{
    int n;
    int i;
    int is_prime;
    for (n = 2; n <= 100; n++) {
        is_prime = 1;
        for (i = 2; i <= n/2; i++) {
            if (n % i == 0) {
                is_prime = 0;
                break;
            }
        }
        if (is_prime) {
            printf("%d ", n);
        }
    }
    printf("\n");
    
    
    return EXIT_SUCCESS;
}

3.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "t_c.h"

int main(void)
{
    int o, p, q;
    scanf("%d %d %d", &o, &p, &q);
    if (o + p > q && o + q > p && p + q >o) {
        if (o == p || p == q || o == q) {
            if (o == p && p == q) {
                printf("等边三角形\n");
            }
            else
                printf("等腰三角形\n");
        }
        else
            printf("是三角形\n");
    }
    else
        printf("无法组成三角形\n");
    
    
    
    return EXIT_SUCCESS;
}

4.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "t_c.h"

void
copy_n(char dst[], char src[], int n)
{
    int i;
    for (i = 0; i < n; i++) {
        if (src[i] != '\0') {
            dst[i] = src[i];
        }
        else
        {
            dst[i] = '\0';
        }
            
    }
}


int main(void)
{
    char dst[100];
    char *src = "hello world";
    copy_n(dst, src, 5);
    printf("%s\n", dst);
    
    
    return EXIT_SUCCESS;
}

5.

This is the first line.
Another line.
And another.
And another.
And another.
And another.
Still more.
Almost done now --
Almost done now --
Another line.
Still more.
Finished!
//
//  t_c.c
//  pointers_on_c
//
//  Created by sidian on 2022/5/27.
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "t_c.h"

#define MAX_CHAR 128



int main(void)
{
    char in_line[MAX_CHAR];
    char first_line[MAX_CHAR];
    char second_line[MAX_CHAR];
    
    
    
    while (gets(in_line) != NULL) {
        if ( !strcmp(second_line, in_line) && strcmp(in_line, first_line)) {
            printf("%s\n", in_line);
        }
        strcpy(first_line, second_line);
        strcpy(second_line, in_line);
    }
    
    return EXIT_SUCCESS;
}

6.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "t_c.h"

int substr(char dst[], char src[], int start, int len){
    int i = 0;
    if ((int) strlen(src) < start - 1 || len < 0) {
        dst[0] = '\0';
    }
    else{
        for (; i < len; i++) {
            if (src[start + i] != '\0') {
                dst[i] = src[start +i];
            }
            else{
                break;
            }
        }
        dst[i] = '\0';
    }
    return i;
}



int main(void)
{
    int n;
    char dst[100];
    char *src = "hello";
    
    n = substr(dst, src, 4, 5);
    printf("%s\nn = %d\n", dst, n);
    
    return EXIT_SUCCESS;
}

7.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "t_c.h"

void deblank(char string[])
{
    int i = 0;
    int move = 0;
    
    for (; i < strlen(string); i++) {
        if (string[i] == ' ' && string[i+1] == ' ') {
            move++;
        }
        else
        {
            string[i - move] = string[i];
        }
    }

    string[i- move] = '\0';
    }



int main(void)
{

    char src[] = "he     l l    o    e  s";
   
    deblank(src);
    
    printf("%s\n", src);
    
    return EXIT_SUCCESS;
}

 

posted @ 2022-05-31 15:42  就是想学习  阅读(31)  评论(0编辑  收藏  举报