C语言程序设计练习题1

所属学校:中央电大 科目:C语言程序设计A 2015-03-17 15:49:03

练习题1


1. 已知三角形的底和高,求出三角形的面积。


#include "stdio.h"

void main()

{

   int x,y;

   float s;

   x=4;

   y=5;

   s=x*y/2.0;

   printf("//n s=%f",s);

}



2. 已知三角形的三边长,求出三角形的面积。


#include "stdio.h"

#include "math.h"

void main()

{

   int a,b,c;

   float p,s;

   a=3;

   b=4;

   c=5;

   p=(a+b+c)/2.0;

   s=sqrt(p*(p-a) *(p-b) *(p-c));

   printf("//n s=%f",s);

}



3. 已知二元一次方程的三个系数,求方程的一个根。


#include "stdio.h"

#include "math.h"

void main()

{

   int a,b,c;

   float root;

   a=3;

   b=4;

   c=5;

   root=(-b-sqrt(pow(b,2)-4*a*c))/(2.0*a);

   printf("//n root=%f",root);

}



4. 编程实现符号函数。当x <0 ,则sgn(x)=-1, 当x >0 ,则sgn(x)=+1, 当x =0 ,则sgn(x)=0


#include "stdio.h"

void main()

{

   float x;

   int y;

   scanf("%f",&x);

   if (x>0)   y=1;

   if (x==0)  y=0;

   if (x<0)   y=-1;

   printf("//n x=%f ,sgn(x)=%d",x,y);

}


或者:


#include "stdio.h"

void main()

{

   float x;

   int y;

   scanf("%f",&x);

   if (x>0)  y=1;

   else if (x==0)   y=0;

       else   y=-1;

   printf("//n x=%f ,sgn(x)=%d",x,y);

}


或者:


#include "stdio.h"

void main()

{

   float x;

   int y;

   scanf("%f",&x);

   y=(x>0) ? 1: (x==0) ? 0 : -1;

   printf("//n x=%f ,sgn(x)=%d",x,y);

}


版权声明

声明:有的资源均来自网络转载,版权归原作者所有,如有侵犯到您的权益 请联系本站我们将配合处理!

分享: