葛才璟

实验1 C语言开发环境使用和编程初体验

1. 实验任务1

#include <stdio.h>
int main() {
    printf(" O \n");
    printf("<H>\n");
    printf("I I\n");
    printf(" O \n");
    printf("<H>\n");
    printf("I I\n");
    return 0;
}

 

#include <stdio.h>
int main() {
    printf(" O    O\n");
    printf("<H>  <H>\n");
    printf("I I  I I\n");
    return 0;
}

 

2. 实验任务2

 

#include <stdio.h>
int main() {
    int x, y;
    int r1, r2, r3, r4;
    x = 1;
    y = 2;
    r1 = x + y;
    r2 = x - y;
    r3 = x * y;
    r4 = x / y;
    printf("r1 = %d\n", r1);
    printf("r2 = %d\n", r2);
    printf("r3 = %d\n", r3);
    printf("r4 = %d\n", r4);
    return 0;
}

 

 

 

 

#include <stdio.h>
int main() {
    float x, y;
    float r1, r2, r3, r4;
    x = 1;
    y = 2;
    r1 = x + y;
    r2 = x - y;
    r3 = x * y;
    r4 = x / y;
    printf("r1 = %f\n", r1);
    printf("r2 = %f\n", r2);
    printf("r3 = %f\n", r3);
    printf("r4 = %f\n", r4);
    return 0;
}

 r4值不同原因:

int属于整型,故1/2取整等于0;

而float属于浮点型,故1/2等于0.5.

3. 实验任务3

#include <stdio.h>
int main() {
    int n, r1, r2, r3, r4;
    n = 100;
    r1 = n * (n + 1) / 2;
    r2 = (n + 1) * n / 2;
    r3 = n / 2 * (n + 1);
    r4 = (n + 1) / 2 * n;
    printf("n = %d\n", n);
    printf("r1 = %d\n", r1);
    printf("r2 = %d\n", r2);
    printf("r3 = %d\n", r3);
    printf("r4 = %d\n", r4);
    return 0;
}

 

#include <stdio.h>
int main() {
    int n, r1, r2, r3, r4;
    n = 101;
    r1 = n * (n + 1) / 2;
    r2 = (n + 1) * n / 2;
    r3 = n / 2 * (n + 1);
    r4 = (n + 1) / 2 * n;
    printf("n = %d\n", n);
    printf("r1 = %d\n", r1);
    printf("r2 = %d\n", r2);
    printf("r3 = %d\n", r3);
    printf("r4 = %d\n", r4);
    return 0;
}

 

line7          n*(n+1)/2

line8          (n + 1) * n / 2     

line9          n / 2 * (n + 1)

line10        (n+1)/2*n

结果不同的原因:乘法和除法的优先级相同,故运算起来都是从左往右计算,而整型计算中碰到小数要向下取整,导致结果出现差异.

4. 实验任务4

 

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
int main()
{
    double x, y;
    char c1, c2, c3;
    int a1, a2, a3;
    scanf("%d%d%d", &a1, &a2, &a3);
    printf("%d,%d,%d\n", a1, a2, a3);
    scanf("%c%c%c", &c1, &c2, &c3);
    printf("%c%c%c\n", c1, c2, c3);
    scanf("%lf%lf", &x, &y);
    printf("%lf,%lf\n", x, y);
    system("pause");
    return 0;
}

 

 

 

5. 实验任务5

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
    int a1, a2;
    char c1, c2;
    scanf("%d%c%d%c", &a1, &c1, &a2, &c2);
    printf("a1 = %d, a2 = %d\n", a1, a2);
    printf("c1 = %c, c2 = %c\n", c1, c2);
    return 0;
}

 

 6. 实验任务6

 

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main() {
    int answer;
    char words[5000];
    printf("网课学习让一些人欢喜一些人忧.\n");
    printf("1. 作为一个宅,社恐,觉得这样很好. 有网络,自主学习,很ok.\n");
    printf("2. 不喜,不喜,很不喜. 眼睛快瞎了. 脑壳有点卡...\n");
    printf("3. 中间派. 不知道. 说不清.\n");
    printf("4. 其它...\n");
    printf("你的选择: ");
    scanf("%d", &answer);
    if (answer == 4) {
        printf("请补充说明你的想法: \n");
        getchar();
    }
    printf("\n");
    printf("当遇到问题时, 你会选择:\n");
    printf("1. 遇到问题, 就解决问题。不懂的,搜索式学习,逐个攻破.\n");
    printf("2. 遇到问题, 不高兴。然后,...,没有了.\n");
    printf("3. 其它...\n");
    printf("你的选择: ");
    scanf("%d", &answer);
    if (answer == 1)
        printf(":)\n");
    else if (answer == 2)
        printf("这样不能解决问题啊...我们选择1好不好:)\n");
    else if (answer == 3) {
        printf("请补充说明你的想法: \n");
        getchar();
    }
    return 0;
}

 

 

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main() {
    int answer;
    char words[5000];
    printf("今天充实吗\n");
    printf("1. 充实\n");
    printf("2.不充实\n");
    printf("3.说不清.\n");
    printf("4. 其它...\n");
    printf("你的选择: ");
    scanf("%d", &answer);
    if (answer == 4) {
        printf("请补充说明你的想法: \n");
        getchar();
    }
    printf("\n");
    printf("在最美年华做最喜欢的事情,别辜负了美好时光,借时光之手,暖一处花开,借一方晴空,拥抱梦想吧 :\n");
    printf("1. 大师我悟了\n");
    printf("2. 我没有梦想.\n");
    printf("3. 其它...\n");
    printf("你的选择: ");
    scanf("%d", &answer);
    if (answer == 1)
        printf(":)\n");
    else if (answer == 2)
        printf("你还太年轻...我们选择1好不好:)\n");
    else if (answer == 3) {
        printf("请补充说明你的想法: \n");
        getchar();
    }
    return 0;
}

 

 

posted on 2021-10-31 16:59  葛才璟  阅读(31)  评论(0编辑  收藏  举报

导航