首页 > 代码库 > c语言笔记

c语言笔记

以下,只是将个人觉得有点动脑儿的代码给存了下来。

练习2-1:

#include <stdio.h>
int main()
{
    int n1, n2;
    puts("请输入两个整数:");
    printf("整数A:"); scanf("%d", &n1);
    printf("整数B:"); scanf("%d", &n2);

    printf("A的值是B的值的%d%%\n", (n1*100)/n2);
}

练习2-5:

#include <stdio.h>
int main()
{
    int n1, n2;
    puts("请输入两个整数:");
    printf("整数A:"); scanf("%d", &n1);
    printf("整数B:"); scanf("%d", &n2);

    printf("A是B的%f%%。\n", (double) (n1*100) / n2);  
    return 0;
}

练习3-1:

#include <stdio.h>
int main()
{
    int a, b;
    puts("请输入两个整数。");
    printf("整数A:"); scanf("%d", &a);
    printf("整数B:"); scanf("%d", &b);

    if (a % b)
        puts("B不是A的约数。");
    else
        puts("B是A的约数。");

    return 0;   
}

练习3-3:

#include <stdio.h>
int main()
{
    int n1;

    printf("请输入一个整数:");
    scanf("%d", &n1);

    if (n1 >= 0)
        printf("绝对值是%d。\n", n1);
    else
        printf("绝对值是%d。\n", -n1);

    return 0;
}

练习3-8:

#include <stdio.h>
int main()
{
    int a, b, c, min;

    puts("请输入3个整数。");
    printf("整数A:"); scanf("%d", &a);
    printf("整数B:"); scanf("%d", &b);
    printf("整数C:"); scanf("%d", &c);

    min = a;
    min = b < min ? b : min;
    min = c < min ? c : min;

    printf("最小值是%d。\n", min);

    return 0;
}

练习3-9:

#include <stdio.h>
int main(void)
{
    int a, b, c;

    puts("请输入三个整数。");
    printf("整数A:"); scanf("%d", &a);
    printf("整数B:"); scanf("%d", &b);
    printf("整数C:"); scanf("%d", &c);

    if (a == b == c)
        puts("三个值都相等。");
    else if (a == b || a == c || b == c)
        puts("有两个值相等。");
    else
        puts("三个值各不相同。");

    return 0;
}  

练习3-11:

#include <stdio.h>
int main(void)
{
    int no;

    printf("请输入一个整数:");
    scanf("%d", &no);

    switch (no % 2) {
        case 0 : puts("该整数是偶数。"); break;
        default : puts("该整数是奇数。"); break;
    }

    return 0;
}

练习3-12:

#include <stdio.h>
int main(void)
{
    int month;

    printf("请输入月份:");
    scanf("%d", &month);

    switch (month) {
        case 3 : puts("是春天。"); break;
        case 4 : puts("是春天。"); break;
        case 5 : puts("是春天。"); break;
        case 6 : puts("是夏天。"); break;
        case 7 : puts("是夏天。"); break;
        case 8 : puts("是夏天。"); break;
        case 9 : puts("是秋天。"); break;
        case 10 : puts("是秋天。"); break;
        case 11 : puts("是秋天。"); break;
        case 12 : puts("是冬天。"); break;
        case 1 : puts("是冬天。"); break;
        case 2 : puts("是冬天。"); break;
        default : puts("输入的月份不存在!!\a"); break;
    }

    return 0;
}

练习4-1:

#include <stdio.h>
int main()
{
    int num1;
    int num2;

    do {
        printf("请输入一个非负整数:");
        scanf("%d", &num1);
        if (num1 < 0)
            puts("请不要输入负数。");
    } while(num1 < 0);

    num2 = num1;
    printf("%d逆向显示的结果是", num2);
    do {
        printf("%d", num1 % 10);
        num1 = num1 / 10;
    } while(num1 > 0);
    puts("。");

    return 0;
}

练习4-2:

#include <stdio.h>
int main()
{
    int a;  /* 输入的非负整数 */
    int b;  /* 用来显示输入的整数 */
    int c;  /* 位数 */

    do {
        printf("请输入一个非负整数:");
        scanf("%d", &a);
        if (a < 0)
            puts("请不要输入负数。");
    } while (a < 0);

    b = a;
    c = 0;
    printf("%d的位数是", b);
    do {
        a = a / 10;
        c = c + 1;
    } while (a > 0);
    printf("%d。 \n", c);

    return 0;
}

练习4-3:

#include <stdio.h>
int main()
{
    int n1;
    int n2;
    int a;
    int b;
    int sum = 0;

    puts("请输入两个整数。");
    printf("整数1:"); scanf("%d", &n1);
    printf("整数2:"); scanf("%d", &n2);

    a = n1;
    b = n2;
    
    printf("大于等于%d", b);
    printf("小于等于%d的所有整数的和是", a);
    do {
        sum = sum + n2;
        n2 = n2 + 1;
    } while (n2 <= n1);
    printf("%d。 \n", sum);

    return 0;
}

练习4-8:

#include <stdio.h>
int main(void)
{
    int i, no;

    printf("请输入一个正整数:");
    scanf("%d", &no);

    i = 1;
    while (i <= no){
        printf("%d ", i++);
    }

    return 0;
}

练习4-9:

#include <stdio.h>
int main(void)
{
    int n1, n2;

    printf("请输入一个整数:");
    scanf("%d", &n1);

    n2 = 2;
    while (n2 < n1){
        printf("%d ", n2);
        n2 += 2;
    }
    putchar(‘\n‘);

    return 0;
}

练习4-10:

#include <stdio.h>
int main(void)
{
    int n1, n2;

    printf("请输入一个整数:");
    scanf("%d", &n1);

    n2 = 2;
    while (n2 < n1){
        printf("%d ", n2);
        n2 *= 2;
    }
    putchar(‘\n‘);

    return 0;
}

练习4-11:

#include <stdio.h>
int main(void)
{
    int no;

    printf("请输入一个正整数:");
    scanf("%d", &no);

    while(no-- > 0){
        putchar(‘*‘);
        printf("\n");
    }

    return 0;
}

练习4-12:

# include <stdio.h>
int main()
{
    int no, sum = 0;

    for (no = 1; no <= 5; no++){
        sum += no;
    }

    printf("1到5的合计值是%d. \n", sum);

    return (0);
}

练习4-13:

# include <stdio.h>
int main()
{
    int i; /*输入的整数小于10时的循环次数*/
    int num; /*输入的整数*/
    int l; /*输入的整数大于9时,整除10,所得到的「1234567890」的循环次数*/
    int o; /*输入的整数大于9时,除余10,所得到的个位数的显示位数*/
    int v;
    int e;

    printf("please input a integer: ");
    scanf("%d", &num);

    if (num <= 9)
    {
        for (i = 0; i < num; i++)
            printf("%d", i + 1);
    }
    else {
        l = num / 10;
        o = num % 10;
        for (v = 0; v < l; v++)
            printf("1234567890");
        for (e = 0; e < o; e++)
            printf("%d", e + 1);
    }
    printf("\n");

    return (0);
}

练习4-14:

#include <stdio.h>
int main()
{
    int start;
    int end;
    int interval;

    printf("开始数值(cm): "); scanf("%d", &start);
    printf("结束数值(cm): "); scanf("%d", &end);
    printf("间隔数值(cm): "); scanf("%d", &interval);

    for (start = start;start <= end; start += interval ) {
        printf("%dcm %.2fkg \n", start, (double)(start - 80) * 0.7); 
    }
    /*世卫计算方法 男性:(身高(cm)-80)*70%*/

    return (0);
}

练习4-15:

#include <stdio.h>
int main()
{
    int i, j;
    int a; /*横标题*/
    int b; /*竖标题*/

    printf("  |");

    for (a = 1; a <= 9; a++) {
        printf("  %d", a);
    }

    printf("\n");

    printf("------------------------------\n");

    for (i = 1; i <= 9; i++) {
        printf("%d ", i);
        printf("|");
        for (j = 1; j <= 9; j++)
            printf("%3d", i * j);
        putchar(‘\n‘);
    }

    return (0);
}

练习4-17:

(1)

# include <stdio.h>
int main()
{
    int i, j, ln;

    printf("三角形有几层: ");
    scanf("%d", &ln);

    for (i = ln; i > 0; i--) {
        for (j = 1; j <= i; j++)
            putchar(‘*‘);
        putchar(‘\n‘);
    }

    return (0);
}

(2)

# include <stdio.h>
int main()
{
    int i, j, ln;

    printf("三角形有几层:");
    scanf("%d", &ln);

    for (i = 0; i < ln; i++) {
        for (j = 1; j <= i; j++)
            putchar(‘ ‘);
        for (j = 1; j <= ln - i; j++)
            putchar(‘*‘);
        putchar(‘\n‘);
    }

    return (0);
}

练习4-18:

# include <stdio.h>
int main()
{
    int n;

    printf("让我们来描绘一个金字塔。\n");
    printf("金字塔有几层:"); scanf("%d", &n);

    int i, j, k;/*, n = 5;*/
    char c1 = ‘ ‘;
    char c2 = ‘*‘;

    for (i = 0; i < n; i++) {               /*最外层的循环语句还是控制三角形有几层呀。。。*/
        for (j = 0; j < n - i; j++)
            printf("%c", c1);
        for (k = 0; k <= 2 * i; k++)       /*根据左边的空格数来安排「*」的位置,输入「*」后直接换行了!!!*/
            printf("%c", c2);

        printf("\n");
    }

    return (0);
}

练习5-4:

#include <stdio.h>
int main()
{
    int i;
    int va[5] = {15, 20, 30};
    int vb[5];

    for (i = 0; i < 5; i++) {
        vb[0] = va[4];
        vb[1] = va[3];
        vb[2] = va[2];
        vb[3] = va[1];
        vb[4] = va[0];
    }

    puts(" va vb");
    puts("------");
    for (i = 0; i < 5; i++) {
        printf("%3d%3d\n", va[i], vb[i]);
    }

    return 0;
}

练习5-5:

#include <stdio.h>
int main()
{
    int i;
    int vx[8];

    for (i = 0; i < 8; i++) {
        printf("vx[%d]:", i);
        scanf("%d", &vx[i]);
    }

    for (i = 0; i < 8; i++) {
        int temp = vx[i];
        vx[i] = vx[7 - i];
        vx[7 - i] = temp;
    }

    for (i = 0; i < 8; i++) {
        printf("vx[%d] = %d\n", i, vx[7 - i]);
    }

    return 0;
}

练习5-7:

#include <stdio.h>
int main(void)

/*
当矩阵A的列数等于矩阵B的行数时,A与B可以相乘。
矩阵C的行数等于矩阵A的行数,C的列数等于B的列数。
乘积C的第m行第n列的元素等于矩阵A的第m行的元素与矩阵B的第n列对应元素乘积之和。
*/

{
    int x[2][3], y[3][2], z[2][2], i, j, k;

    puts("请输入x[2][3]的值:");
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 3; j++) {
            scanf("%d", &x[i][j]);
        }
    }

    puts("请输入y[3][2]的值:");
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 2; j++) {
            scanf("%d", &y[i][j]);
        }
    }
// 将x*y的值存入z中
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 2; j++) {
            z[i][j] = 0;
            for (k = 0; k < 3; k++)
                z[i][j] += x[i][k] * y[k][j];
        }
    }
// 将矩阵z的值输出,注意换行
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 2; j++) 
            printf("%d ", z[i][j]);
            printf("\n");   
    }

    return (0);
}

练习6-2:

#include <stdio.h>

int min3(int x, int y, int z)
{
    int min = x;

    if (y < min) min = y;
    if (z < min) min = z;

    return (min);
}

int main(void)
{
    int a, b, c;

    puts("please input three integers.");
    printf("integer 1:"); scanf("%d", &a);
    printf("integer 2:"); scanf("%d", &b);
    printf("integer 3:"); scanf("%d", &c);

    printf("The minimal integer is %d. \n", min3(a, b, c));

    return (0);
}

练习6-3:

#include <stdio.h>

int cube(int x)
{
    return (x * x * x);
}

int main(void)
{
    int a;

    printf("请输入一个整数:"); scanf("%d", &a);

    printf("该整数的立方为:%d\n", cube(a));

    return (0);
}

练习6-4:

#include <stdio.h>

int sqr(int x)
{
    return (x * x);
}

int pow4(int x)
{
    return (sqr(x) * sqr(x));
}

int main(void)
{
    int a;

    printf("请输入一个整数。"); scanf("%d", &a);

    printf("该数的四次幂为:%d\n", pow4(a));

    return (0);
}

练习6-5:

#include <stdio.h>

void alert(int no)
{
    while (no-- > 0)
        putchar(‘\a‘);
}

int main(void)
{
    int i, a;
    printf("请输入一个整数:"); scanf("%d", &a);

    for (i = 1; i <= a; i++) {
        alert(i);
    }

    return (0);
}

练习6-6:

#include <stdio.h>

void hello(void)
{
    printf("你好。\n");
}

int main(void)
{
    hello();  /*若无输入语句,可直接通过写下函数名来调用函数。*/
    return (0);
}

练习6-7:

#include <stdio.h>

#define  NUMBER 5

int min_of(const int vc[], int no)
{
    int i;
    int min = vc[0];

    for (i = 1; i < no; i++)
        if (vc[i] < min)
            min = vc[i];
        return (min);
}

int main(void)
{
    int i;
    int ary1[NUMBER];

    printf("请输入%d个整数。 \n", NUMBER);
    for (i = 0; i < NUMBER; i++){
        printf("[%d]:", i + 1); scanf("%d", &ary1[i]);
}
    
    printf("这些数中最小的一位是:%d\n", min_of(ary1, NUMBER));

    return (0);
}

练习6-8:

#include <stdio.h>

void rev_intary(int vc[], int no)
{
    int i, temp;

    for (i = 0; i < no / 2; i++) {
        temp = vc[i];
        vc[i] = vc[no - i - 1];
        vc[no - i - 1] = temp;
    }     
}

int main(void)
{
    int i;
    const int n = 6;
    int vc[n] = {2, 3, 5, 7, 11, 13};
    
    rev_intary(vc, n);

    for (i = 0; i < n; i++)
        printf("vc[%d]:%d\n", i + 1, vc[i]);

    return (0);

}

练习6-9:

#include <stdio.h>

void intary_rcpy(int v1[], const int v2[], int no)
{
    int i;
    
    for (i = 0; i < no; i++) {
        v1[i] = v2[no - 1 - i];
    }
}

int main(void)
{
    int i;
    const int n = 6;
    const int vb[] = {2, 3, 5, 7, 11, 13};
    int va[n];

    intary_rcpy(va, vb, n);

    for (i = 0; i < n; i++)
        printf("v1[%d]:%d\n", i, va[i]);

    return (0);
}

练习6-10:

#include <stdio.h>

void mul(const int ma[2][3], const int mb[3][2], int mc[2][2])
{
    int i, j, k;

    for (i = 0; i < 2; i++)
        for (j = 0; j < 2; j++) {
            for (k = 0; k < 3; k++)
                mc[i][j] += ma[i][k] * mb[k][j]; 
        }
}

int main(void)
{
    int i, j;
    const int ma[2][3] = {{1, 2, 3}, {4, 5, 6}};
    const int mb[3][2] = {{1, 4}, {2, 5}, {3, 6}};
    int mc[2][2] = {0};

    mul(ma, mb, mc);

    for (i = 0; i < 2; i++) {
        for (j = 0; j < 2; j++) 
            printf("%d ", mc[i][j]);
            putchar(‘\n‘);
    }
        

    return (0);
}

练习6-11:

(1)

#include <stdio.h>
#define NUMBER 5

int max_of(int a[], int no) {
    int i;
    int max = a[0];

    for (i = 1; i < no; i++)
        if (a[i] > max)
            max = a[i];
        return (max);
}

int main(void)
{
    int i;

    int chi[NUMBER];
    int mat[NUMBER];
    int eng[NUMBER];

    int max_c;
    int max_m;
    int max_e;

    printf("请输入[%d]名学生的分数。\n", NUMBER);
    for (i = 0; i < NUMBER; i++) {
        printf("[%d]\n", i + 1);
        printf("chinese: "); scanf("%d", &chi[i]);
        printf("   math: "); scanf("%d", &mat[i]);
        printf("english: "); scanf("%d", &eng[i]);
        putchar(‘\n‘);
    }

    max_c = max_of(chi, NUMBER);
    max_m = max_of(mat, NUMBER);
    max_e = max_of(eng, NUMBER);

    printf("The max score of chinese is: %d\n", max_c);
    printf("The  max  score  of math is: %d\n", max_m);
    printf("The max score of english is: %d\n", max_e);

    return (0); 
}

(2)

#include <stdio.h>

#define NUMBER 5
#define COURSE 3

int i, j;

void calculate(const float score[][3]);

int main()
{
    float result[NUMBER][COURSE];

    puts("请输入以下5名同学各三科成绩。");
    for (i = 0; i < NUMBER; i++) {
        printf("第[%d]名同学: ", i + 1);
        for (j = 0; j < COURSE; j++)
            scanf("%f", &result[i][j]);
    }

    calculate(result);

    return (0);
}

void calculate(const float score[][COURSE])
{
    float average[NUMBER] = {0};
    float sum[NUMBER] = {0};

    for (i = 0; i < NUMBER; i++) {
        for (j = 0; j < COURSE; j++){
            sum[i] += score[i][j];
        }
            average[i] = sum[i] / COURSE; 
    }

    for (i = 0; i < NUMBER; i++) {
        printf("第%d名学生的总成绩为:%.2f,平均分为:%.2f。\n", i + 1, sum[i], average[i]);
    }
}

练习6-12:

#include <stdio.h>

#define NUMBER 5
int ma[NUMBER]; /*static storage duration*/

int main(void)
{
    int i;
    //static int ma[NUMBER]; /*static storage duration*/

    for (i = 0; i < NUMBER; i++)
        printf("[%d]:%d \n", i + 1, ma[i]);

    return (0);
}

练习8-1:

#include <stdio.h>

#define diff(x, y) ((x) - (y))

int main(void)
{
    int a, b;

    puts("请输入两个整数:");
    printf("integer 1:"); scanf("%d", &a);
    printf("integer 2:"); scanf("%d", &b);

    printf("两个整数之差为:%d。\n", diff(a, b));


    return (0);
}

练习8-3:

#include <stdio.h>

#define swap(int, x, y) (int = x)

int main(void)
{
    int a, b;
    int c = 0;

    puts("请输入2个整数:");
    printf("整数a:"); scanf("%d", &a);
    printf("整数b:"); scanf("%d", &b);

    swap(c, a, b);

    a = b;
    b = c;

    printf("a = %d, b = %d \n", a, b);

    return (0);
}

c语言笔记