实验1

实验任务1

编程源代码:

 

//打印俩垂直字符小人

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

printf(" 0 \n");
printf("<H>\n");
printf("I I\n");

system("pause");

return 0;
} 
    //打印一个字符小人

#include <stdio.h>
#include <stdlib.h>
int main()
{
     printf(" 0       0 \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,先算“/2”于是因为输入“int”为整型,51/2=25,造成运算不正确。

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

运行结果:

分析功能:设立一个t为介质将a与b的值对调。

实验任务4

编程源代码:

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

int main()
{
   int x, t, m;

   x=456;
   printf("x=%d\n",x);

   t=0;

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

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

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

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

运行结果:

 

 

 实现的功能是:依次取输入数字的个,十,百位,再分别通过一系列运算分别求出百,十,个的反位数据。

实验任务5

编程源代码:

 

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

int main()
{
   float a,b,c;

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

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

    system("pause");
   return 0 ;
}

运行结果:

 

 

实验任务6 

编程源代码:

#include <stdio.h>
#include <math.h>

int main()
{
   int year,k,s;

   long double k;
   s=1e+9;
   k=s/60/60/24/365;
   year=round(k+0.5);
   printf("十亿秒约等于%d年\n", year);


   return 0 ;
}

运行结果:

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

运行结果:

 

 

实验任务8

编程源代码:

 

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

运行结果:

 

 

 若去掉第九行,则会出现这样的问题:系统会将“Enter”键当成第二个输入问题的答案,如下图

 

 

posted on 2023-03-09 15:03  随客111  阅读(16)  评论(0编辑  收藏  举报

导航