实验1 C语言开发环境使用和数据类型、运算符、表达式

实验任务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

源代码

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

 

运行截图

 

实验任务3

源代码

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

 

运行截图

 思考

改后运行截图

很显然,去掉第9行后对运行有很大影响。那为什么会这样呢?查阅资料后发现,getchar()的作用是从标准输入(通常是键盘)读取一个字符,并返回该字符的ASCII码值,然后赋值给变量。在输入字符后,按下回车键相当于又输入了一个换行符,换行符是有ASCll码值的,y被赋值给了ans1,还残留一个换行符,第9行的目的就是清空残余的换行符,如果没有第9行,则换行符被赋值给了ans2,就出现了后面的结果。

实验任务4

源代码

#include<stdio.h>

int main()
{
    double x, y;
    char c1, c2, c3;
    int a1, a2, a3;
    
    scanf("%d%d%d", &a1, &a2, &a3);
    printf("a1 = %d, a2 = %d, a3 = %d\n", a1,a2,a3);
    
    scanf("%c%c%c", &c1, &c2, &c3);
    printf("c1 = %c, c2 = %c, c3 = %c\n", c1, c2, c3);
    
    scanf("%lf%lf", &x, &y);
    printf("x = %f, y = %lf\n",x, y);

    return 0;
}

 

运行截图

 

实验任务5

源代码

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

 

运行截图

 

实验任务6

源代码

#include <stdio.h>
#include <math.h>
int main()
{
double x, ans;
while(scanf("%lf", &x) != EOF)
{
ans = pow(x, 365);
printf("%.2f的365次方: %.2f\n", x, ans);
printf("\n");
}
return 0;
}

 

运行截图

 

实验任务7

源代码

#include <stdio.h>
#include <math.h>
int main()
{
double c, F;
while(scanf("%lf", &c) != EOF)
{
F = 9*c/5+32;
printf("摄氏度c=%.2f时,华氏度F= %.2f\n", c, F);
printf("\n");
}
return 0;
}

 

运行截图

 

实验任务8

源代码

#include <stdio.h>
#include <math.h>
int main()
{
double a,b,c,s,area;
while(scanf("%lf%lf%lf", &a,&b,&c) != EOF)
{
s = (a+b+c)/2;
area =sqrt(s*(s-a)*(s-b)*(s-c));
printf("a=%.0f,b=%.0f,c=%.0f,area=%.3f\n", a, b, c,area);
printf("\n");
}
return 0;
}

 

运行截图

 

posted @ 2025-03-04 17:32  奕珩  阅读(27)  评论(0)    收藏  举报