您的位置:首页 > 其它

第五堂课后作业

2013-04-07 22:29 309 查看
作业1.1:C语言标准库的目的是什么?编写一个小程序,说明如何使用标准库里的数学函数;

编译器提供的可在c源程序中调用的函数。可分为两类,一类是c语言标准规定的库函数,一类是编译器特定的库函数。库函数库函数:顾名思义是把函数放到库里..是别人把一些常用到的函数编完放到一个文件里,供别人用.别人用的时候把它所在的文件名用#include<>加到里面就可以了.一般是放到lib文件里的.函数库是由系统建立的具有一定功能的函数的集合。库中存放函数的名称和对应的目标代码,以及连接过程中所需的重定位信息。用户也可以根据自己的需要建立自己的用户函数库。

作业1.2:用一个程序计算下面两个表达式的值;





我的程序:

#include "stdafx.h"
int main()
{
float a,b;
a=234.0/(1.0+257.0);
b=1065.0+(24.0+13.0);
printf("%f\n",a);
printf("%f\n",b);
}
运行程序截图:






作业1.3:利用C标准库里的数学函数<math.h>,用一个程序计算下面两个表达式的值;





我的程序:

#include "stdafx.h"  
#include"math.h"  
void main()  
{  
    double PI=3.1415926;  
    printf("a=%f\n",log(sqrt (2*PI-1))/(log(5.0)));  
    printf("b=%f\n",exp(sqrt(PI+1)));  
}  运行程序截图:



------------------------------------题目分割线------------------------------------

作业2:请根据你对三种模式的理解,在一个程序中分别写出三段代码模拟这三种模式。



顺序执行

我的程序:#include "stdafx.h"
#include "math.h"
void main()
{
double a=1.0,b=2.0,c,d,e,f;
c=sin(a*b);
d=asin(sin(a*b));
e=log10(a*b);
f=exp(a*b);
printf("c=%lf\nd=%lf\ne=%lf\nf=%lf\n",c,d,e,f);
}运行程序截图:





选择执行

我的程序:

double dmax (double x, double y)  
{   
  if (x > y)   
      return x;   
  else   
      return y;   
 }   
  
int main()  
{  
  double a,b;  
  printf("Input 2 number:\n");  
  scanf_s("%lf %lf",&a,&b);  
  printf("The max is:%f \n",dmax(a,b));  
}  运行程序截图:







重复执行

#include<stdio.h>  
  
void main()  
{  
    int a =1;  
while (a <=10)  
{ printf("%d\n",a);  
a++;  
}  
}运行程序截图:





------------------------------------题目分割线------------------------------------

作业3.1:下面例子有两个if,请说明
else 部分属于哪个 if ? 为什么?
if (x > 0) 
if (y > 1) z = 1; 
else z = 2;  


答:else属于第二个if,因为C语言规定,每个else
部分总属于前面最近的那个缺少对应的 else 部分的 if语句。

作业3.2:上面的例子没问题,但容易造成误解,请修改写法,使其易理解、更容易体现嵌套关系。

我的程序:#include "stdafx.h"  
void main()  
{  
    int x,y,z;  
    printf("Input two number x,y:\n");  
    scanf_s("%d%d",&x,&y);  
    if(x>0)  
        {if(y>1)  
            z=1;  
        else  
            z=2;}  
    printf("%d\n",z);  
}  ------------------------------------题目分割线------------------------------------
作业4:请根据你对循环的理解,在一个程序中先写一段while循环代码;再用for语句重写“while循环”代码;



while程序:

#include<stdio.h>  
  
void main()  
{  
    int a =1;  
while (a <=10)  
{ printf("%d\n",a);  
a++;  
}  
}  运行程序截图:



for
程序:

#include"stdafx.h"  
void main()  
{  
    for(int i=1;i<=10;i++);    
        printf("%d\n",i);   
}

------------------------------------题目分割线------------------------------------

作业5.1:定义一个计算圆面积的函数,并在主函数中调用两次,从而分别计算两个不同圆的面积;

作业5.2:下面的例子程序可以正常通过编译,但执行时却得不到正确结果。也就是说,这个程序有语义错误。为什么呢?




[cpp] view
plaincopy

#include <stdio.h>   

#include <math.h>   

int main () {   

    printf("%f\n", sin(1) + sin(1/2) + sin(1/3) + sin (1/4) );   

    return 0;   

}  

我的程序:
#include "stdafx.h"  
double cycle(double r)  
{  
    double area,PI=3.1415926;  
    area=PI*r*r;  
    return area;  
}  
void main()  
{  
    double   r1,r2;  
    printf("请输入圆的半径r1,r2:");  
    scanf_s("%lf%lf",&r1,&r2);  
    printf("圆的面积为:r1=%lf,r2=%lf\n",cycle(r1),cycle(r2));  
}  运行程序截图:



------------------------------------题目分割线------------------------------------
作业6:定义一个三角形面积函数,在主函数中调用5次,要求程序运行类似下图:


.
我的程序:
#include "stdafx.h"  
#include"math.h"  
double triangle(double a,double b,double x)  
{  
    double area;  
    area=a*b*sin(x/180.0*3.14)/2.0;  
    return area;  
}  
void main()  
{  
    int i;  
    for(i=0;i<=4;i++)  
    {  
    double a,b,x;  
    printf("请输入第%d组三角形的参数:",i);  
    scanf_s("%lf%lf%lf",&a,&b,&x);  
    printf("第%d组三角形的实参为,A边长%.2lf;B边长%.2lf;夹角%.2lf;\n",i,a,b,x);  
    printf("第%d组三角形的面积为:%.2lf\n",i,triangle(a,b,x));  
    printf("----------分割线-----------\n");  
    }  
  
}  
运行程序截图:



------------------------------------题目分割线------------------------------------
作业7 阅读博客及其评论
一个大学生从堕落走向编程之路的感想 http://blog.csdn.net/java4found/article/details/8654196
要求:发表感慨、必须真实,感慨字数大于100,小于200!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: