4、简单程序求运算

一、

/**************************************************************************************
输入三角形的三边长,求三角形面积。

已知三角形的三边长a、b、c,则该三角形的面积公式为:
    area=(s(s-a)(s-b)(s-c))1/2
其中s = (a+b+c)/2。
***************************************************************************************/

#include<stdio.h>
#include<math.h>          //调用了开根号的sqrt函数,所以要添加库
int main()
{
   float a,b,c,s,area;
   printf(" please input the number of three sides for the triangle :\n");
   scanf("%f,%f,%f",&a,&b,&c);
   s=(a+b+c)/2.0;
   area=sqrt(s*(s-a)*(s-b)*(s-c));          //开根号sqrt,而且需要调用库函数math.h
   printf("the number of s is:%7.2f\nthe number of area is:%7.2f\n",s,area); //%7.2表示取值整数部分7位(不足7位的少的加空格,超过7位的只取7位,小数部分2位)
   return 0;
}
View Code

 

二、

/***********************************************************************************
         求ax2+bx+c=0方程的根,a、b、c由键盘输入,设b2-4ac>0。
***********************************************************************************/
#include<stdio.h>
#include<math.h>
int main()
{
  float a,b,c,d,e,x1,x2;
  printf("please input the three number a, b, c in ax^2+bx+c=0 :\n");
  scanf("%f,%f,%f",&a,&b,&c);
  if(b*b-4*a*c<0)                   //注意,C语言中求平方没有直接的^这样,可以pow(x,y)就是x的y次方,需要调用math.h库
     {
       d=-b/(2.0*a);
       e=sqrt(4*a*c-b*b)/(2.0*a);
       printf("the root is: x1=%7.2f+%7.2fi, x2=%7.2f-%7.2fi\n",d,e,d,e);
     }
  else if(b*b-4*a*c==0)
     {
       d=-b/(2.0*a);
       printf("the root is: x1=x2=%7.2f\n",d);
     }
  else
     {
       d=-b/(2.0*a);
       e=sqrt(b*b-4*a*c)/(2.0*a);
       printf("the root is: x1=%7.2f, x2=%7.2f\n",d+e,d-e);
     }
  return 0;
}
View Code

 

三、

1、putchar 函数是字符输出函数,其功能是在显示器上输出单个字符。其一般形式为:putchar(字符变量);  例如:(使用本指令前必须在前面加   #include<stdio.h>  )
putchar('A'); /* 输出大写字母A */                              putchar(x); /* 输出字符变量x的值 */                              putchar('\101'); /* 也是输出字符A */          putchar('\n'); /* 换行 */

2、getchar函数的功能是从键盘上输入一个字符。其一般形式为:getchar();    例如:(使用本指令前必须在前面加   #include<stdio.h>  ,getchar函数只能接受单个字符,输入数字也按字符处理。输入多于一个字符时,只接收第一个字符。)

 char c;      c=getchar();

3、格式字符串的一般形式为:%[*][输入数据宽度][长度]类型,如:

%5d

4、math.h包含的库函数

 

1、 三角函数
double sin(double);正弦
double cos(double);余弦
double tan(double);正切
2 、反三角函数
double asin (double); 结果介于[-PI/2,PI/2]
double acos (double); 结果介于[0,PI]
double atan (double); 反正切(主值),结果介于[-PI/2,PI/2]
double atan2 (double,double); 反正切(整圆值),结果介于[-PI,PI]
3 、双曲三角函数
double sinh (double);
double cosh (double);
double tanh (double);
4 、指数与对数
double frexp(double value,int *exp);这是一个将value值拆分成小数部分f和(以2为底的)指数部分exp,并返回小数部分f,即f*2^exp。其中f取值在0.5~1.0范围或者0。
double ldexp(double x,int exp);这个函数刚好跟上面那个frexp函数功能相反,它的返回值是x*2^exp
double modf(double value,double *iptr);拆分value值,返回它的小数部分,iptr指向整数部分。
double log (double); 以e为底的对数
double log10 (double);以10为底的对数
double pow(double x,double y);计算x的y次幂
float powf(float x,float y); 功能与pow一致,只是输入与输出皆为浮点数
double exp (double);求取自然数e的幂
double sqrt (double);开平方
5 、取整
double ceil (double); 取上整,返回不比x小的最小整数
double floor (double); 取下整,返回不比x大的最大整数,即高斯函数[x]
6 、绝对值
int abs(int i); 求整型的绝对值
double fabs (double);求实型的绝对值
double cabs(struct complex znum);求复数的绝对值
7 、标准化浮点数
double frexp (double f,int *p); 标准化浮点数,f = x * 2^p,已知f求x,p (x介于[0.5,1])
double ldexp (double x,int p); 与frexp相反,已知x,p求f
8 、取整与取余
double modf (double,double*); 将参数的整数部分通过指针回传,返回小数部分
double fmod (double,double); 返回两参数相除的余数
9 、其他
double hypot(double x,double y);已知直角三角形两个直角边长度,求斜边长度
double ldexp(double x,int exponent);计算x*(2的exponent次幂)
double poly(double x,int degree,double coeffs []);计算多项式
int matherr(struct exception *e);数学错误计算处理程序
source: 《C & C++ Code Capsules》
View Code

 

 

 

 

 

posted on 2015-01-28 08:55  放.逐  阅读(241)  评论(0)    收藏  举报

导航