C++作业(第一二章)

C++第一次作业

第一章

7.求2个或3个正整数中的最大数,用带有默认参数的函数实现。

#include<iostream>
using namespace std;
int main()
{  
 int max(int a,int b,int c=0);
    int a,b,c;
 cout<<"please enter a b c:";
    cin>>a>>b>>c;
 cout<<"max(a,b,c)="<<max(a,b,c)<<endl;
    cout<<"max(a,b)="<<max(a,b)<<endl;
    return 0;
}
 
int max(int a,int b,int c)
{
   if(b>a)  a=b;
     if(c>a)  a=c;
   return a;
} 

10.编一程序,将两个字符串连接起来,结果取代第一个字符串。要求用string方法。

#include<iostream>
#include<string> 
using namespace std;
int main()
{  
	string string1,string2,string3;
	cout<<"please input two strings:";
    cin>>string1>>string2;
    string3=string1+string2;
    cout<<string3<<endl;
}

13.编一个程序,用同一个函数名对n 个数据进行从小到大排序,数据类型可以是 整型、单精度型、双精度型。用重载函数实现。

#include<iostream>
#include<string> 
using namespace std;
int main()
{  
	long long_num[5]={11542,54552,-262541,265687,-222245}; 
	int  int_num[5]={1,5,-8,4,-2}; 
	float float_num[5]={2.4,-5.9,5.1,-6.5,1.5};
	void sort(long[]);
	void sort(int[]);
	void sort(float[]);
	sort(long_num);
	sort(int_num);
	sort(float_num);
	return 0;
}

void sort(long long_num[])
{
 int i,j;
 long t; 
 for (j=0;j<4;j++) 
 	for( i=0;i<4-j;i++)
	  if(long_num[i]>long_num[i+1]) 
	  {
	  	t=long_num[i];
		long_num[i]=long_num[i+1];
		long_num[i+1]=t;
	  } 
	cout<<" the sorted numbers :"<<endl; 
		for(i=0;i<5;i++) 
		cout<<long_num[i]<<" ";
	cout<<endl<<endl;
}

void sort(int int_num[])
{
 int i,j;
 int t; 
 for (j=0;j<4;j++) 
 	for( i=0;i<4-j;i++)
	  if(int_num[i]>int_num[i+1]) 
	  {
	  	t=int_num[i];
		int_num[i]=int_num[i+1];
		int_num[i+1]=t;
	  } 
	cout<<" the sorted numbers :"<<endl; 
		for(i=0;i<5;i++) 
		cout<<int_num[i]<<" ";
	cout<<endl<<endl;
}

void sort(float float_num[])
{
 int i,j;
 int t; 
 for (j=0;j<4;j++) 
 	for( i=0;i<4-j;i++)
	  if(float_num[i]>float_num[i+1]) 
	  {
	  	t=float_num[i];
		float_num[i]=float_num[i+1];
		float_num[i+1]=t;
	  } 
	cout<<" the sorted numbers :"<<endl; 
		for(i=0;i<5;i++) 
		cout<<float_num[i]<<" ";
	cout<<endl<<endl;
}

14.对第13题改用函数模板实现。并与13题程序进行对比分析。

#include <iostream> 
#include <string> 
using namespace std; 
template <typename T > 
void sort(T num[]) 
{
	 int i, j, min; 
	 T t; 
     for(i= 0;i<5;i++) 
	    {
		 min=i; 
		 for(j=i+1;j<5;j++) 
		 if(num[min]>num[j]) 
		    min=j; 
		    t=num[i]; 
		 num[i]=num[min]; 
		 num[min]=t;
		}
	cout<<"wthe sorted numbers : "<<endl; 
	for(i=0;i<5;i++) 
	cout<<num[i]<<" ";
	cout<<endl<<endl;
}

int main()
{  
	long long_num[5]={11542,54552,-262541,265687,-222245}; 
	int  int_num[5]={1,5,-8,4,-2}; 
	float float_num[5]={2.4,-5.9,5.1,-6.5,1.5};

	sort(long_num);
	sort(int_num);
	sort(float_num);
	return 0;
}

第二章

6.需要求3 个长方柱的体积,请编一个基于对象的程序。数据成员包括length (长)、 width (宽)、height (高)。要求用成员函数实现以下功能:
(1) 由键盘分别输入3 个长方柱的长、宽、高;
(2) 计算长方柱的体积;
(3) 输出3 个长方柱的体积。
请编程序,上机调试并运行。

#include <iostream> 
using namespace std; 
class Box
{
	private:
		float length; 
		float height;
		float width;
	public:
		void get_value(); 
		float volume(); 
		void display(); 
};

void Box::get_value()
{
	cout<<"please input length,width,height:";
	cin>>length; 
	cin>>width; 
	cin>>height;
}

float Box::volume()
{
	return(length*width*height); 
} 

void Box::display() 
{
	cout<<volume()<<endl;
} 

int main()
{
	Box box1,box2,box3;
	box1.get_value();
	cout<<"volume of box1 is ";
	box1.display();
	
	box2.get_value();
	cout<<"volume of box2 is ";
	box2.display(); 
	
	box3.get_value();
	cout<<"volume of box3 is ";
	box3.display();
	return 0;
 } 
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值