C++程序设计(谭浩强) 第一章课后习题8~13题

8.

#include<iostream>
using namespace std;
int main()
{
	void change(int &,int &);
	int a,b;
	cin>>a>>b;
	if(a<b) change(a,b);
	cout<<a<<" "<<b<<" "<<endl;
	return 0;
 } 
 void change(int &a,int &b)
 {
 		int temp;
 		temp=a;
 		a=b;
 		b=temp;
 }

9.

#include<iostream>
using namespace std;
int main()
{
	int a,b,c;
	cin>>a>>b>>c;
	void change(int &,int &);
	if(a>b) change(a,b);
	if(a>c) change(a,c);
	if(b>c) change(b,c);
	cout<<a<<" "<<b<<" "<<c<<" "<<endl;
	return 0; 
}
void change(int &a,int &b)
{
 	int temp;
 	temp=a;
 	a=b;
 	b=temp;
}

10.

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string s1,s2;
	s1="week";
	s2="end";
	cout<<"s1 = "<<s1<<endl;
	cout<<"s2 = "<<s2<<endl;
	s1=s1.append(s2);
	cout<<"s1 = "<<s1<<endl;
	return 0;
}

11.

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str;
	int i,n;
	char temp;
	cin>>str;
	n=str.size();
	for(i=0;i<n/2;i++)
	{
		temp=str[i];
		str[i]=str[n-i-1];
		str[n-i-1]=temp;
	}
	cout<<str<<endl;
	return 0;
} 

12.

#include<iostream>
#include<string>
using namespace std;
int main()
{
	int i;
	string str[5]={"BASIC","C","C++","FORTRAN","PASCAL"};
	void sort(string[]);
	sort (str);
	for(i=0;i<5;i++)
	  cout<<str[i]<<" ";
	cout<<endl;
	return 0;
}
void sort(string s[])
{
	int i,j;
	string t;
	for(j=0;j<5;j++)
	  for(i=0;i<5-j;i++)
	if(s[i]>s[i+1])
	{
		t=s[i];
		s[i]=s[i+1];
		s[i+1]=t;
	}

13.

#include<iostream>
#include<string>
using namespace std;
int main()
{
	int a[5]={1,10,-3,7,2};
	float b[5]={1.7,4.9,3.6,7.9,-9.1};
	long c[5]={110221,-123574,1190876,-12980,2643};
	void sort(int a[]);
	void sort(float b[]);
	void sort(long c[]);
	sort(a);
	sort(b);
	sort(c);
	return 0;
}
void sort(int a[])
{
	int i,j,t;
	for(j=0;j<4;j++)
	  for(i=0;i<4;i++)
	    if(a[i]>a[i+1])
	     {
	     	t=a[i];
	     	a[i]=a[i+1];
	     	a[i+1]=t;
		 }
	cout<<"i_sort= ";
	for(i=0;i<5;i++)
	  cout<<a[i]<<" ";
	cout<<endl<<endl;
}
void sort(float a[])
{
	int i,j;
	float t;
	for(j=0;j<4;j++)
	  for(i=j+1;i<5;i++)
	    if(a[j]>a[i])
	    {
	    	t=a[j];
	     	a[j]=a[i];
	     	a[i]=t;
		}
	cout<<"f_sort= ";
	for(i=0;i<5;i++)
	  cout<<a[i]<<" ";
	cout<<endl<<endl;
}
void sort(long a[])
{
	int i,j;
	long t;
	for(j=0;j<4;j++)
	  for(i=0;i<4-j;i++)
	    if(a[i]>a[i+1])
	    {
	    	t=a[i];
	     	a[i]=a[i+1];
	     	a[i+1]=t;
		}
	cout<<"l_sort= ";
	for(i=0;i<5;i++)
	  cout<<a[i]<<" ";
	cout<<endl<<endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值