实验1

 

Task1

<实验结论>

#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     0 \n");
    printf("<H>   <H>\n");
    printf("I I   I I\n");

    system("pause");
    return 0;
}

 

Task2

<实验结论>

复制代码
#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;
}
复制代码

** 能实现题目要求写法:

    sum = n*(n+1)/2

    sum = (n+1)*n/2

    sum = (n+1)/2*n

   不能实现题目要求写法:

    sum = n/2*(n+1)

    原因,在进行除法运算时,会因为精度问题导致的计算结果小数部分的省去导致运算结果出错。

 

Task3

 

<实验结论>

复制代码
#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功能:

    将a和b的值进行置换。

<实验总结>

将变量值进行置换代码写法

eg.

t = a;
a = b;
b = t;
 
 
 Task4
 

<实验结论>

复制代码
#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;
}
复制代码

** line10-22功能:

    在一轮又一轮计算中,不断给m,t,x赋予新值,产生新的运算结果。

 

Task5

 

<实验结论>

复制代码
#include <stdio.h>
#include <stdlib.h>

int main()
{
    float a,b,c;
    printf("请输入三角形三边:");
    scanf("%f%f%f",&a,&b,&c);

    if(a+b>c || a+c>b || b+c>a)
        printf("能构成三角形\n");
    else
        printf("不能构成三角形\n");

    system("pause");
    return 0;
}
复制代码

 

Task6

 <实验结论>

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

int main()
{
    int year;
    year = 10^9 / (60*60*24*30*12);

    printf("10亿秒约等于%d年\n",year);

    system("pause");
    return 0;
}

 

 

Task7

<实验结论>

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

int main()
{
    int n;
    srand((unsigned)time(NULL));
    n = rand()%100 + 60;
    printf("n = %d\n",n);

    system("pause");
    return 0;
}

 

 

 

 

 

  

 

Task8

<实验结论>

保留getchar();

#include <stdio.h>
#include <stdlib.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");

    system("pause");
    return 0;
}

 

 

 

 

 

 去掉getchar();

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

int main()
{
    char ans1,ans2;

    printf("每次课前认真预习、课后复习了没?(输入y或Y表示有,输入n或N表示没有):");
    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;
}

 

 **结论:去掉会有影响,

            因为getchar();这一行起声明函数的作用。如果去掉,函数getchar()就只能被调用一次(如上图)。

 

<实验总结>

C语言中,函数不仅要调用,同时在调用前还需要声明。

否则会出现函数无法多次调用的情况。

posted on 2023-03-02 16:54  负熵宝子  阅读(35)  评论(0)    收藏  举报