编程作业 第一、二章【合】

2.4
2.2 2.3
13 14
10
8
8
7

1.7

#include<iostream>
using namespace std;
int max(int x,int y,int z=3)
{
 if(x>y) y=x;
 if(y>z) z=y;
 return z;
}
int main()
{
 int a,b,c;
 cin>>a>>b>>c;
// max(a,b,c);
// cout<<c<<endl;输出的不是c,而是函数返回值
    cout<<max(a,b,c);
}

1.8

#include<iostream>
using namespace std;
int sort(int &x,int &y)
{
 int temb;
 if(y>x)
 {
  temb=x;
  x=y;
  y=temb;
    }//     if只管一个分号
}
int main()
{
 int a,b;
 cin>>a>>b;
 sort(a,b);
 cout<<a<<b<<endl;
 return 0;
}

1.9

#include<iostream>
using namespace std;
void swap(int &x,int &y,int &z)
{
 int temb;
 if(y<x)
 {
      temb=x;
      x=y;
      y=temb; 
    }
    if(z<y)
    {
    temb=z;
    z=y;
    y=temb;
 } 
 if(y<x)
 {
  temb=y;
  y=x;
  x=temb;
    }
}
int main()
{
 int a,b,c;
 cin>>a>>b>>c;
 swap(a,b,c);
 cout<<a<<b<<c<<endl;
 return 0;
}

1.10

#include<iostream>
#include<string>
using namespace std;
int main()
{
 string str1,str2;
 cin>>str1>>str2;
 str1=str1+str2;
 cout<<str1;
 return 0; 
}

1.11

#include<iostream>
#include<string>
using namespace std;
int main()
{
 string str;
 cin>>str;
 int i,j;
 int length=sizeof(str);
    for(i=length-1;i>=0;i--)
    {
     cout<<str[i];
    }
 
 return 0;
}

1.12

#include<iostream>
#include<string>
using namespace std;
void bubble_sort(string a[],int n)
{
 int i,j;
 string temb;
 for(i=0;i<n-1;i++)
 {
  for(j=0;j<n-i-1;j++)
  {
    if(a[j]>=a[j-1])
    {
   temb=a[j];
   a[j]=a[j-1];
   a[j-1]=temb;
    }
     }
    }
}
int main()
{
 int n;
 string str[5];
 for(n=0;n<5;n++)
 {
  cin>>str[n];
 }

 bubble_sort(str,5);
 for(n=0;n>5;n++)
 {
  cout<<str[n];
 }
 return 0;
}
//http://blog.csdn.net/cs_zlg/article/details/8332622

1.13

#include<iostream>
using namespace std;
int sort(int a[],int n)//为什么不用引用???????????
{
    int i,j,temb;
    for(i=0;i<n-1;i++)
    {
     for(j=0;j<n-j-1;j++)
     {
      if(a[i]<a[i-1])
   {
    temb=a[i];
    a[i]=a[i-1];
    a[i-1]=temb;
   }     
     }
    }
}
double sort(double a[],int n)
{
    int i,j;
    double temb;
    for(i=0;i<n-1;i++)
    {
     for(j=0;j<n-j-1;j++)
     {
      if(a[i]<a[i-1])
   {
    temb=a[i];
    a[i]=a[i-1];
    a[i-1]=temb;
   }     
     }
    }
}
float sort(float a[],int n)
{
    int i,j;
    float temb;
    for(i=0;i<n-1;i++)
    {
     for(j=0;j<n-j-1;j++)
     {
      if(a[i]<a[i-1])
   {
    temb=a[i];
    a[i]=a[i-1];
    a[i-1]=temb;
   }     
     }
    }
}
int main()//能否自动判断类型 ,能否根据需要的空间自动分配
{
     int i,n;
  cout<<"请输入n为数组中元素个数"<<endl;
  cin>>n;
  int d[n];
  double b[n];
  float c[n];
  for(i=0;i<n;i++)
  {
   cout<<"输入第"<<i+1<<"个整型元素"<<endl;
   cin>>d[i];
  }
  for(i=0;i<n;i++)
  {
   cout<<"输入第"<<i+1<<"个浮点型元素"<<endl;
   cin>>b[i];
  }
  for(i=0;i<n;i++)
  {
   cout<<"输入第"<<i+1<<"个长浮点型元素"<<endl;
   cin>>c[i];
  }
 d[n]=sort(d,n);//直接放数组名??????????
 b[n]=sort(b,n);
 c[n]=sort(c,n);
 for(i=0;i<n;i++)
  {
   cout<<d[i]<<"   ";
  }
 for(i=0;i<n;i++)
  {
   cout<<b[i]<<"   ";
  }
 for(i=0;i<n;i++)
  {
  cout<<c[i]<<"   ";
  }
 return 0; 
}

1.14

#include<iostream>
using namespace std;
template<typename T>
T sort(T a[],T n,T temb)//为什么不用引用???????????
{
    int i,j;
    for(i=0;i<n-1;i++)
    {
     for(j=0;j<n-j-1;j++)
     {
      if(a[i]<a[i-1])
   {
    temb=a[i];
    a[i]=a[i-1];
    a[i-1]=temb;
   }     
     }
    }
}

int main()//能否自动判断类型 ,能否根据需要的空间自动分配
{
     int i,n;
  cout<<"请输入n为数组中元素个数"<<endl;
  cin>>n;
  int temb_int;
  float temb_float;
  double temb_double;
  int d[n];
  double b[n];
  float c[n];
  for(i=0;i<n;i++)
  {
   cout<<"输入第"<<i+1<<"个整型元素"<<endl;
   cin>>d[i];
  }
  for(i=0;i<n;i++)
  {
   cout<<"输入第"<<i+1<<"个浮点型元素"<<endl;
   cin>>b[i];
  }
  for(i=0;i<n;i++)
  {
   cout<<"输入第"<<i+1<<"个长浮点型元素"<<endl;
   cin>>c[i];
  }
 d[n]=sort(d,n,temb_int);//直接放数组名??????????
 b[n]=sort(b,n,temb_float);
 c[n]=sort(c,n,temb_double);
 for(i=0;i<n;i++)
  {
   cout<<d[i]<<"   ";
  }
 for(i=0;i<n;i++)
  {
   cout<<b[i]<<"   ";
  }
 for(i=0;i<n;i++)
  {
  cout<<c[i]<<"   ";
  }
 return 0; 
}

2.2

#include<iostream>
using namespace std;
class Time
{
 public:
 void cin_Time()
 {
  cin>>hour>>minute>>sec;
 }
 void cout_Time()
 {
  cout<<hour<<":"<<minute<<":"<<sec;
 }
 private:
 int hour;
    int minute;
    int sec;
   
};
int main()
{
 Time t;
 t.cin_Time();
 t.cout_Time();
 return 0;
}

2.3

#include<iostream>
using namespace std;
class Time
{
 public:
 void cin_Time();
 void cout_Time();
 private:
 int hour;
    int minute;
    int sec;
   
};
int main()
{
 Time t;
 t.cin_Time();
 t.cout_Time();
 return 0;
}
void Time::cin_Time()
{
    cin>>hour>>minute>>sec;
}
void Time::cout_Time()
{
 cout<<hour<<":"<<minute<<":"<<sec;
}
 

2.5

//array_max.h
#include<string>
using namespace std;
class array_max
{
 public:
 void set_value();
 void max_value();
 void show_value();
 private:
 int array[10];
 int max;
};
//array_max.cpp
#include<iostream>
#include"array_max.h"
using namespace std;
void array_max::set_value()
{
 int i;
 for(i=0;i<10;i++)
 {
  cin>>array[i];
 }
}
void array_max::max_value()
{
 int i;
 max=array[0];
 for(i=1;i<10;i++)
 {
  if(array[i]>max)max=array[i];
 }
}
void array_max::show_value()
{
 cout<<"max="<<max;
}
#include<iostream>
#include"array_max.h"
using namespace std;
int main()
{
 array_max arrmax;
 arrmax.set_value();
 arrmax.max_value();
 arrmax.show_value();
 return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值