c++简单程序第二章

/****************************************************/

//eg.2-1

#include <iostream.h>

void main()

{

    cout<<"Hello!"<<endl;

    cout<<"Welcome to C++!"<<endl;

}

/****************************************************/

 //eg.2-2 输入一个年份,判断是否闰年。

#include <iostream.h>

void main()

{

   int year;

   bool IsLeapYear;

   cout<<"Enter the year:"<<endl;

   cin>>year;

   IsLeapYear=((year%4==0&&year%100!=0)||year%400==0);

   if (IsLeapYear)

       cout<<year<<"is a leap year!"<<endl;

   else

       cout<<year<<"is not a leap year!"<<endl;

}

/***************************************************/

 //eg.2-3 比较两个数的大小。

#include <iostream.h>

void main()

{

   int a,b;

   cout<<"Enter a and b:"<<endl;

   cin>>a>>b;

   if(a>b)cout<<"a>b"<<endl;

   else if (a<b)cout<<"a<b"<<endl;

   else cout<<"a=b"<<endl;

}

 

/*  if(a!=b)

      if(a>b)cout<<"a>b"<<endl;

      else cout<<"a<b"<<endl;

    else cout<<"a=b"<<endl;*/

/***************************************************/

//eg.2-4 输入一个0-6的整数,转换成星期输出。

#include <iostream>

using namespace std;

void main()

{

  int day;

  cin>>day;

  switch (day)

  {

  case 0: cout<<"Sunday"<<endl;break;

  case 1: cout<<"Monday"<<endl;break;

  case 2: cout<<"Tuesday"<<endl;break;

  case 3: cout<<"Wednesday"<<endl;break;

  case 4: cout<<"Thursday"<<endl;break;

  case 5: cout<<"Friday"<<endl;break;

  case 6: cout<<"Saturday"<<endl;break;

  default:cout<<"Day out of rang Sunday...Saturday!";break;

  }

}

/**************************************************************/

//eg.2-5 求自然数1~10之和。

#include <iostream.h>

void main()

{

   int i=1,sum=0;

   do

   {   sum+=i;

       i++;

   }while(i<=10);

   cout<<"sum="<<sum<<endl;

}

/***************************************************************/

//eg.2-6输入一个整数,将各位数字反转后输出

#include <iostream.h>

void main(void)

{

  int n,right_digit;

  cout<<"Enter the number:";

  cin>>n;

  cout<<"The number in reverse order is:";

  do

  {

    right_digit=n%10;

    cout<<right_digit;

    n/=10;

  }

  while(n!=0);

  cout<<endl;

}

/************************************************************/

//eg.2-8 输入一个整数,求出它的所有因子

#include <iostream.h>

void main(void)

{

   int n,k;

   cout<<"Enter a positive integer:";

   cin>>n;

   cout<<"Number"<<n<<"Factors";

   for(k=1;k<=n;k++)

       if(n%k==0)

          cout<<k<<" ";

   cout<<endl;

}

/*eg.2-8 *********************************

循环结构的嵌套

#include <iostream.h>

void main()

{

   int i(1),a(0);

   for(;i<=5;i++)

   { do{

        i++;

        a++;

       }while(i<3);cout<<i<<a<<endl;

       i++;

   }

   cout<<a<<","<<i<<endl;

}

**************************************/

/*eg.2-9 编写程序输出以下图案

                     *

                   ***                   

                 *****

               *******

               *****

               ***

               *

*/

#include <iostream.h>

void main()

{ int i,j,n=4;

  for(i=1;i<=n;i++)           //输出前4行图案

  { for(j=1;j<=30;j++)

      cout<<" ";             //在图案左侧空30列

    for(j=1;j<=8-2*i;j++)

      cout<<" ";

    for(j=1;j<=2*i-1;j++)

      cout<<"*";

    cout<<endl;

  }

  for(i=1;i<=n-1;i++)        //输出后3行图案

  { for(j=1;j<=30;j++)

      cout<<" ";             //在图案左侧空30列

    for(j=1;j<=7-2*i;j++)

       cout<<"*";

    cout<<endl;

  }

}

/**************************************************/

//eg.2-10 读入一系列整数,统计出正整数个数i和负整数j,读入0则结束

#include <iostream.h>

void main()

{int i=0,j=0,n;

 cout<<"请输入若干整数(输入0则结束):";

 cin>>n;

 while(n!=0)

 { if(n>0)i+=1;

   if(n<0)j+=1;

   cin>>n;

 }

 cout<<"正整数个数:"<<i<<"负整数个数:"<<j<<endl;

}

/*********************************************************/

/*自定义数据类型

  typedef double area,volume;

  typedef int natural;

  natural i1,i2;

  area a;

  volume v;  */

/*eg.2-11设某次体育比赛的结果有四种可能:胜(win)、负(lose)、

平局(tie)、比赛取消(cancel),编写程序顺序输出这四种情况*/

#include <iostream.h>

enum game_result{WIN,LOSE,TIE,CANCEL};

int main()

{ game_result result;                       //声明变量时,可以不写关键字enum

  //enum game_result omit=CANCEL;             //可以在类型名前写enum

  int count;

  for(count=WIN;count<=CANCEL;count++)

  {

      result=(game_result)count;

      if(result==CANCEL)cout<<"The game wad cancelled/n";

      else

      {

         cout<<"The game was played";

         if(result==WIN)cout<<"and we won!";

         if(result==LOSE)cout<<"and we lose!";

         cout<<endl;

      }

  }

  return 0;

}

***********************************************************************    

/*enum weekday{sun,monday,tuesday,wednesday,thursday,friday,saturday}{0,1,2,...}  sun=0;非法的语句

  enum weekday{sun=7,monday=1,tuesday,wednesday,thursday,friday,saturday}*/

//eg.2-12 有红、黄、蓝、白、黑的球若干,取3个不同颜色的球,问有多少种取法

#include <iostream.h>

void main()

{

    enum color{red,yellow,blue,white,black};

    enum color pri;

    int n=0,loop,i,j,k;

    for(i=red;i<=black;i++)

       for(j=red;j<=black;j++)

           if(i!=j)

           {  for(k=red;k<=black;k++)

              if((k!=i)&&(k!=j))

              {  n=n+1;

                 cout.width(4);

//将下一项输出内容的宽度设置为4个字符

                cout<<n;

                for(loop=1;loop<=3;loop++)

                {switch(loop)

                {  case 1:pri=(enum color)i;break;

                   case 2:pri=(enum color)j;break;

                   case 3:pri=(enum color)k;break;

                   default:break;

                }

                switch(pri)

                {  case red:cout<<"    red";break;

                   case yellow:cout<<"    yellow";break;

                   case blue:cout<<"    blue";break;

                   case white:cout<<"    white";break;

                   case black:cout<<"    black";break;

                   default:break;

                }

                }

                cout<<endl;

              }

           }

           cout<<"total:"<<n<<endl;

}

//eg.2-13结构体变量的初始化和使用

#include <iostream.h>

#include <iomanip.h>

struct student

{

    int num;

    char name[20];

    char sex;

    int age;

}stu={97001,"Linlin",'F',19};

void main()

{

    cout<<setw(7)<<stu.num<<setw(10)<<stu.name<<setw(3)<<stu.sex<<setw(3)<<stu.age<<endl;

}

//eg.2-14 有三只动物,都具有重量和身长两个属性,现在需要对它们的重量和身长赋值,并且输出它们的重量

#include <iostream.h>

struct animal

{

    int weight;

    int feet;

};

int main()

{

    animal dog1,dog2,chicken;

    dog1.weight =15;

    dog2.weight =37;

    chicken.weight =3;

    dog1.feet =4;

    dog2.feet =4;

    chicken.feet =2;

    cout<<"The weight of dog1 is:"<<dog1.weight <<endl;

    cout<<"The weight of dog2 is:"<<dog2.weight <<endl;

    cout<<"The weight of chicken is:"<<chicken.weight <<endl;

    return 0;

}

//适用描述战斗机、轰炸机、运输机的结构体

#include <iostream.h>

struct aircraft

{

    int wingspan;

    int passengers;

    union

    {

       float fuel_load;

       float bomb_load;

       int pallets;

    };

}fighter,bomber,transport;

int main()

{

    fighter.wingspan=40;

    fighter.passengers=1;

    fighter.fuel_load=12000.0;

    bomber.wingspan =90;

    bomber.passengers =12;

    bomber.bomb_load =140000.0;

    transport.wingspan =106;

    transport.passengers =4;

    transport.pallets =42;

    transport.fuel_load =18000.0;

    fighter.pallets =4;

    cout<<"the fighter carries:"<<fighter.pallets <<"pallets"<<endl;

    cout<<"the bomber bomb load is:"<<bomber.bomb_load <<endl;

    return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值