C++第一章

<pre name="code" class="html">7. #include<iostream>

using namespace std;

int main()

{

 int max(int a,int b,int c=0);

 int a,b,c;

 cin>>a>>b>>c;

 cout<<"max(a,b,c)="<<a<<endl;

 cout<<"max=(a,b)="<<a<<endl;

 return 0;

}

int max(int a,int b,int c)

{

  if(a<b) a=b;

  if(a<c) a=c;

  return a;

}
8.<span style="font-family: Arial, Helvetica, sans-serif;">#include<iostream></span><pre name="code" class="html">using namespace std;
void sort(int &x,int &y)
{
if(x<y) 
cout<<y<<","<<x<<endl;
else 
cout <<x<<","<<y<<endl;
}
int main()
{
 int a,b;
 cout<<"请输入两个整数"<<endl; 
 cin>>a>>b;
 sort(a,b);
 return 0;
}

 
<pre name="code" class="html">9.#include<iostream> 
using namespace std;
void sort(int &a,int &b,int &c)
{
int t;
if(a<b) 
{
	t=a;a=b;b=t;
}
if(b>c)

	cout<<c<<","<<b<<","<<a<<endl;

else if(c>a)  
	cout<<b<<","<<a<<","<<c<<endl;

else 
      cout<<b<<","<<c<<","<<a<<endl;
}

int main()
{
 int x,y,z;
 cin>>x>>y>>z;
 sort(x,y,z);
 return 0;
}
 
<pre name="code" class="html">10.#include<iostream>

#include<string>

using namespace std;

int main()

{

 string s1="Thank",s2="you";

 s1=s1+s2;

 cout<<"The new stringis:"<<s1<<endl;

 return 0;

}
11.#include<iostream>

#include<string>

using namespace std;

int main()

{

string s;

int i,n;

char t;

cin>>s;

n=s.size();

for(i=0;i<n/2;i++)

{

 t=s[i];

 s[i]=s[n-i-1];

 s[n-i-1]=t;

}

cout<<s<<endl;

return 0;

}

12.#include <string>
#include <iostream>

using namespace std;

int main()

{

 int i;

 string str[5]={"China","American","Japan","Korea","Y"};

 void sort(string[]);

 sort(str);

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

       cout<<str[i]<<" "<<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>

using namespace std;

int main()

{

      long a[5]={10100,-123567.1198783,-165654,3456};

      int b[5]={1,9,8,3,4};

      float c[5]={2.0,1.0,3.0,2.0,4.0};

      void sort(long[]);

      sort(a);

      void sort(int[]);

      sort(b);

      void sort(float[]);

      sort(c);

      return 0;

}

void sort(long a[])

{

  int i,j;

  long t;

  for(j=0;j<5;j++)

       for(i=0;i<5-j;i++)

            if(a[i]>a[i+1])

            {

             t=a[i];

             a[i]=a[i+1];

             a[i+1]=t;

            }

            cout<<"The sorted numbers:"<<endl;

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

                   cout<<a[i]<<""<<endl;

}

void sort(int b[])

{

  int i,j;

  int t;

  for(j=0;j<5;j++)

       for(i=0;i<5-j;i++)

            if(b[i]>b[i+1])

            {

             t=b[i];

             b[i]=b[i+1];

             b[i+1]=t;

            }

            cout<<"The sorted numbers:"<<endl;

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

                   cout<<b[i]<<""<<endl;

}

void sort(float c[])

{

  int i,j;

  float t;

  for(j=0;j<5;j++)

       for(i=0;i<5-j;i++)

            if(c[i]>c[i+1])

            {

             t=c[i];

             c[i]=c[i+1];

             c[i+1]=t;

            }

            cout<<"The sorted numbers:"<<endl;

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

                   cout<<c[i]<<""<<endl;

}
14.#include <iostream>

using namespace std;

template <typename T>

void sort(T [])

{

 int a[5];

 int i,j,min;

 T t;

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

       {

        min=i;

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

             if(a[min]>a[j])

                    min=j;

             t=a[i];

             a[i]=a[min];

             a[min]=t;

    }

}

int main()

{

      int i;

    long a[5]={10100,-123567.1198783,-165654,3456};

      int b[5]={1,9,8,3,4};

      float c[5]={2.4,1.5,3.6,2.8,4.8};

      sort(a);

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

      cout<<a[i]<<" "<<endl;

      sort(b);

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

      cout<<b[i]<<" "<<endl;

    sort(c);

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

    cout<<c[i]<<" "<<endl;

    return 0;

}
<img src="https://img-blog.youkuaiyun.com/20150401141625668" alt="" />
 






                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值