实验1

实验任务1

程序源码tast1_1.c

#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;
 } 

 

运行截图

 

程序源码tast1_2.c

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

运行截图

 

实验任务2

完整源代码

#include <stdio.h>
int main()
{
    int n, sum;
    scanf("%d", &n);
    sum = n*(n+1)/2;
    printf("sum = %d\n", sum);
    
    return 0;
}

运行截图

问题:写法1,写法2可以满足

           /与*在同一优先级,为从左到右结合,n定义为整型变量,对于写法3,n为奇数时,n/2所得只能为整型,所得结果错误,对于写法4,n为偶数时,(n+1)/2所得只能为整型,所得结果错误

 

实验任务3

源代码

#include <stdio.h>
int main()
{
    int a, b, t;
    a = 3;
    b = 4;
    printf("a = %d, b = %d\n", a, b);
    
    t = a;
    a = b;
    b = t;
    printf("a = %d, b = %d\n", a, b);
    
    return 0;
}

运行截图

问题:调换a与b的值

 

实验任务4

完整源代码

#include <stdio.h>
int main()
{
    int x, t, m;
    x = 456;
    printf("x = %d\n", x);
    
    t = 0;
    m = x % 10;
    t = t*10 + m;
    x = x / 10;
    
    m = x%10;
    t = t*10+m;
    x = x/10;
    
    printf("t = %d\n", t);
    
    return 0;
}

运行截图

问题:将x的个位作为t的十位,十位作为t的个位

 

实验任务5

源代码

#include<stdio.h>
int main()
{
    float a, b, c;
    scanf("%f%f%f", &a, &b, &c);
    if(a+b>c && a+c>b && b+c>a)
        printf("能构成三角形\n");
    else 
        printf("不能构成三角形\n");
    
    return 0; 
}

运行截图

 

 

实验任务6

源代码

#include<stdio.h> 
int main()
{
    int year;
    long int second;
    second = 1000000000;
    year=(second*1.0)/(60*60*24*365)+0.5;
    printf("10亿秒约等于%d年\n", year);
    return 0;
}

运行截图

 

实验任务7

源代码

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
    int n;
    srand((unsigned)time(NULL));
    n=rand()%(100-60+1)+60;
    printf("n=%d\n", n);
    return 0;
}

运行截图

 

实验任务8

源代码

#include <stdio.h>
int main()
{
    char ans1, ans2;
    printf("每次课前认真预习、课后及时复习了没?(输入y或Y表示有,输入n或N表示没有):");
    ans1=getchar();
    
    getchar();
    
    printf("\n动手敲代码实践了没?(输入y或Y表示敲了,输入n或N表示木有敲) : ");
    ans2=getchar();
    
    if((ans1=='y'||ans1=='Y') && (ans2=='y'||ans2=='Y'))
        printf("\n罗马不是一天建成的,继续保持哦:)\n");
    else
        printf("\n罗马不是一天毁灭的,我们来建设吧\n");
    
    return 0;
}

运行截图

 

实验总结

通过本次实验,我对C语言有了更加深刻的认识,编程较之前而言变得熟练起来,但对于一些函数还没有掌握,不能理解其用法。

posted @ 2023-03-03 23:38  玥下秋风  阅读(12)  评论(0编辑  收藏  举报