xhw354405545

三月二日实验

1.实验任务

1.1实验一

1.1.1 task1_1.c

#include<stdio.h>
#include<Windows.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;
}

1.1.2 task1_2.c

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


system("pause");
return 0;
}

1.2 实验二

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

实验结论:写法一与写法二正确,写法三与写法四错误。原因是n、n+1为int型,在除以2时会忽略小数部分,导致误差发生。

1.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;
}

问题回答:line11-13合起来实现的功能是将a,b取值互相转换。

1.4 实验四

#include<stdio.h>
#include<Windows.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;
 
 printf("t=%d\n",t);
 system("pause");
    return 0;
}

问题回答:代码line10-22组合起来实现的功能是将x的数字排列反序。

1.5实验五

#include<stdio.h>
#include<Windows.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("triangel is available");
else
printf("triangel is unavailable");
system("pause");
return 0;
}

1.6实验六

#include<stdio.h>
#include<Windows.h>
int main()
{
int year, t=1e9;

year = t / (60*60*24*365);


printf("a trillion seconds is %d years",year);
system("pause");
return 0;

}

1.7实验七

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
int n;

srand((unsigned)time(NULL));
n = rand() % 41 + 60;

printf("n = %d\n",n);
system("pause");
return 0;
}

1.8实验八

#include<stdio.h>
#include<Windows.h>
int main()
{
int ans1, ans2;
printf("每次课前认真预习、课后及时复习了没?(输入N或n表示没有,输入Y或y表示有)");
ans1 = getchar();

getchar();

printf("\n动手实践敲代码了没?(输入N或n表示没有,输入Y或y表示有)");
ans2 = getchar();

if((ans1=='y'&&ans2=='y')||(ans1=='Y'&&ans2=='Y'))
printf("\n罗马不是一天建成的,继续保持哦");
else
printf("\n罗马不是一天毁灭的,我们来建设吧");
system("pause");
return 0;
}

 

 

 

 

posted on 2023-03-03 11:27  果胶纤维素  阅读(16)  评论(0编辑  收藏  举报

导航