(二)
2.1
#include <iostream.h>
void main()
{  //本题原考虑在16位机器上实验目前多为32位机器,故已过时。
  int a = 42486;
  cout <<oct <<a <<endl
      <<hex <<a <<endl;
  unsigned b = 42486;
  cout << dec <<(signed)b <<endl;
}
2.2
#include <iostream.h>
#include <iomanip.h>
const double pi = 3.1415926;
void main()
{
  double radius1, radius2;
  cout <<"please input two numbers:\n";
  cin >>radius1 >>radius2;
  cout <<setw(10) <<pi <<setw(10) <<radius1
      <<setw(10) <<(pi*radius1*radius1) <<endl
      <<setw(10) <<pi <<setw(10) <<radius2
      <<setw(10) <<(pi*radius2*radius2) <<endl;
}
2.3
#include <iostream.h>
#include <iomanip.h>
const double e = 2.718281828;
void main()
{
  cout <<setprecision(10) <<e <<endl
      <<setiosflags(ios::fixed) <<setprecision(8) <<e <<endl
      <<setiosflags(ios::scientific) <<e <<endl;
}
2.4
#include <iostream.h>
void main()
{
  cout <<"\"How many students here?\"\n"
      <<"\"500\"\n";
}
2.5
#include <iostream.h>
void main()
{
  cout <<"size of char          " <<sizeof(char) <<" byte\n"
      <<"size of unsigned char  " <<sizeof(unsigned char) <<" byte\n"
      <<"size of signed char    " <<sizeof(signed char) <<" byte\n"
      <<"size of int            " <<sizeof(int) <<" byte\n"
      <<"size of unsigned      " <<sizeof(unsigned) <<" byte\n"
      <<"size of signed        " <<sizeof(signed) <<" byte\n"
      <<"size of short          " <<sizeof(short) <<" byte\n"
      <<"size of unsigned short " <<sizeof(unsigned short) <<" byte\n"
      <<"size of long          " <<sizeof(long) <<" byte\n"
      <<"size of signed long    " <<sizeof(signed long) <<" byte\n"
      <<"size of unsigned long  " <<sizeof(unsigned long) <<" byte\n"
      <<"size of float          " <<sizeof(float) <<" byte\n"
      <<"size of double        " <<sizeof(double) <<" byte\n"
      <<"size of long double    " <<sizeof(long double) <<" byte\n";
}
2.6
1)
please input 3 sides of one triangle:
6,6,8
a=  6.00,b=  6.00,c=  8.00
area of triangle is  17.88854
2)
该程序计算三角形的面积
前后分为三部分:输入,处理,输出。
3)
//#include <stdio.h>
#include <iostream.h>
#include <iomanip.h>
#include <math.h>
void main()
{
  float a,b,c,s,area;
  //printf("please input 3 sides of one triangle:\n");
  cout <<"please input 3 sides of one triangle:\n";
  //scanf("%f,%f,%f",&a,&b,&c);  //输入时以逗号作为数据间隔
  cin >>a >>b >>c;  //输入时以空格作为数据间隔
  s=(a+b+c)/2;
  area=sqrt(s*(s-a)*(s-b)*(s-c));
  //printf("a=%7.2f,b=%7.2f,c=%7.2f\n",a,b,c);
  cout <<setiosflags(ios::fixed) <<setprecision(2)
      <<"a=" <<setw(7) <<a
      <<",b=" <<setw(7) <<b
      <<",c=" <<setw(7) <<c <<endl;
  //printf("area of triangle is %10.5f",area);
  cout <<"area of triangle is " <<setw(10)
      <<setprecision(5) <<area <<endl;
}
4)
#include <iostream.h>
#include <iomanip.h>
#include <math.h>
printf怎么加endl
float area(float a, float b, float c); //函数声明
void main()
{
  float a,b,c;
  cout <<"please input 3 sides of one triangle:\n";
  cin >>a >>b >>c;  //输入时以空格作为数据间隔
  float result = area(a,b,c);          //函数调用
  cout <<setiosflags(ios::fixed) <<setprecision(2)
      <<"a=" <<setw(7) <<a
      <<",b=" <<setw(7) <<b
      <<",c=" <<setw(7) <<c <<endl;
  cout <<"area of triangle is " <<setw(10)
      <<setprecision(5) <<result <<endl;
}
float area(float a, float b, float c)  //函数定义
{
  float s=(a+b+c)/2;
  return sqrt(s*(s-a)*(s-b)*(s-c));
}
2.7
In main():
Enter two numbers:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。