实验1

实验任务1

程序源码:

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

  

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

    printf(" O \n");
    printf("<H>\n");
    printf("I I\n");
    system("pause");

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

程序运行截图:

实验任务2

程序源码:

#include<stdio.h>
#include<stdlib.h>

int main()
{
    int n, sum;

    scanf("%d", &n);
    sum = n*(n+1)/2;
    printf("sum = %d\n", sum);
    system("pause");
    return 0;
}

1和2都可以

讨论:代码3和4不可以,由运算符的运算等级可知3和4均从左至右开始运算,当在代码3中,n为奇数或者代码4中,n为偶数,且数据类型又为int类型,均不可达到运算目的。

实验任务3

程序源码:

#include<stdio.h>
#include<stdlib.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);
    system("pause");
    return 0;
}

程序运行截图:

 

分析line11-13:由t实现a和b值的互换,j将a赋值给t后,又将b值赋值给a,再将t值赋值b啊,以达到目的

实验任务4

程序源码:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int x, t, m;

    x=123;
    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;

    m = x % 10;
    t = t * 10 + m;
    x = x / 10;

    printf("t = %d\n", t);
    system("pause");

    return 0;
}

程序运行截图:

回答问题:得到一个数的逆序,并赋值给t

实验任务5

程序源码:

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

    //输入三边边长
    scanf("%f%f%f", &a, &b, &c);

    //判断能否构成三角形
    if(((a+b)>c)&&((b+c)>a)&&((a+c)>b))
        printf("能构成三角形\n");
    else
        printf("不能构成三角形\n");
    system("pause");

    return 0;
}

程序运行截图:

 

 

试验任务6

程序源码:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
    double year;
    year=1e+9;
    year=year/31536000+0.5;
        printf("10亿秒约等于%d年\n",(int)year);
    system("pause");
    return 0;

}

程序运行截图:

 

 实验任务7

程序源码:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
    int n;
    srand(time(0));
    n=rand()%41+60;
    printf("n = %d\n", n);
    system("pause");
    return 0;
}

 

 程序运行截图:

实验任务8

程序源码:

#include<stdio.h>
#include<stdlib.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");
    system("pause");
    return 0;
}

注意注意,if的条件不加分号不加分号不加分号啊啊啊啊

程序运行截图:

 

posted @ 2023-03-03 21:04  zlzliz  阅读(13)  评论(0编辑  收藏  举报