实验1-实验报告

实验1:C语言简单程序编写

任务1

  • 源代码task1_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;
}
  • 运行测试截图
    image

  • 源代码task1_2.c

#include <stdio.h>
int main(){
    printf(" O\t O \n");
    printf("<H>\t<H>\n");
    printf("I I\tI I\n");
    
    return 0;
}
  • 运行测试截图
    image

任务2

  • 源代码
//从键盘上输入三个数据作为三角形边长,判断其能否构成三角形
//构成三角形的条件:任意两边之和大于第三边

#include <stdio.h>

int main(){
    double a, b, c;
    //输入三边边长
    scanf ("%lf%lf%lf",&a, &b, &c);
    //判断能否构成三角形
    if (a+b>c && a+c>b && b+c>a)
        printf("能构成三角形\n");
    else
        printf("不能构成三角形\n");
    return 0;
}
  • 运行测试截图
    image

任务3

  • 源代码
#include <stdio.h>
int main(){
    char ans1, ans2;  // 保存用户输入的答案

    printf("每次课及时预习/复习了没?(是-输入y或Y,否-输入n或N):");
    ans1 = getchar();  // 从键盘输入一个字符,赋值给ans1

    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;
}
  • 运行测试截图
    image
    image
    image
    image
  • 回答问题:当把getchar()去掉后,重新编译运行,结果是什么样的?你的分析和结论。
    重新编译运行得到的结果如图所示。原因是getchar()获取了“回车键”,所以ans2可以正常获取到'Y/y/N/n',如果去掉了getchar()这行,ans2获取到的是“回车键”,不满足if条件,则输出else语句。
    屏幕截图 2026-10-23 112241

任务4

  • 源代码
//计算10亿秒约等于多少年,并打印输出
#include <stdio.h>
#include <math.h>

int main(){
    int year;
    year = (int)round(1000000000.0 / (365 * 24 * 3600));
    printf("10亿秒约等于%d年\n", year);
       
    return 0;
}
  • 运行测试截图
    image

任务5

  • 源代码
//摄氏温度转换成华氏温度
#include <stdio.h>

int main(){
    double c, f;

    scanf("%lf", &c);
    f = (9.0 / 5.0) * c + 32;
    printf("摄氏度c = %.2f时,华氏温度f = %.2f\n\n", c, f);

    return 0;
}
  • 运行测试截图
    image
    image
    image

任务6

  • 源代码
#include <stdio.h>
#include <math.h>

int main(){
    int a, b, c;
    double s, area;

    scanf("%d %d %d", &a, &b, &c);
    s = (a + b + c) / 2.0;
    area =  sqrt(s * (s-a) * (s-b) * (s-c));

    printf("a = %d, b = %d, c = %d, area = %.3lf",a, b, c, area);
}
  • 运行测试截图
    image
    image
posted on 2026-10-23 11:40  ASa_08  阅读(6)  评论(1)    收藏  举报