c plus plus 第一章练习

来源:互联网 发布:kafka集群 端口 编辑:程序博客网 时间:2024/05/16 15:27


//P35第七题

#include<iostream>
using namespace std;

int max(int a,int b,int c=0)
{
 if(b>a||a==b)
  a=b;
 if(c>a||a==c)
 a=c;
 return a;
}
int main()
{
 int a,b,c,n;
 cout<<"请输入进行比较的整数的个数"<<endl;
 cin>>n;


 if(n==3)
 {
  cout<<"请输入这3个数"<<endl;
  cin>>a>>b>>c;
 cout<<"这3个数中最大值为"<<max(a,b,c)<<endl;
 }


 if(n==2)
 {
  cout<<"请输入这2个数"<<endl;
  cin>>a>>b;
 cout<<"这个数中最大值为"<<max(a,b)<<endl;
 }


 if(!(n==2||n==3))
  cout<<"ERROR"<<endl;


 return 0;


}

运行的3种结果





//P35第八题

#include<iostream>
using namespace std;
void sort(int &a,int &b)

{    int temp;
 if(b>a)
 {
  temp=a;
  a=b;
  b=temp;
 }
 else
  return;
   


}
int main()
{
int a,b;
cout<<"请输入两个数进行由大到小排序"<<endl;
cin>>a>>b;
sort(a,b);
cout<<"排序完成"<<endl;
cout<<a<<','<<b<<endl;

}

结果:



//第9题

#include <iostream>
 using namespace std;
 int main()
 {void sort(int x,int y,int z);
 int x,y,z;
  cin>>x>>y>>z;
  sort(x,y,z);
  return 0;
 }


 void sort(int x, int y, int z)
 {
  int temp;
  if (x>y) {temp=x;x=y;y=temp;}      //{ }内3个语句的作用是将x和y的值互换)
  if (z<x)  cout<<z<<','<<x<<','<<y<<endl;
   else if (z<y) cout<<x<<','<<z<<','<<y<<endl;
        else cout<<x<<','<<y<<','<<z<<endl;
 }




//第10题

#include <iostream>
 #include <string>
 using namespace std;
 int main()
 { string s1,s2;
 cin>>s1>>s2;
   cout<<"s1="<<s1<<endl;
   cout<<"s2="<<s2<<endl;
   s1=s1+s2;
   cout<<"The new string is:"<<s1<<endl;
   return 0;
 }


//第11题

#include <iostream>
 #include <string>
 using namespace std;
 int main()
 {
  int i;
  string s;
  cout<<"请输入一个字符串"<<endl;
  cin>>s;
  int l=s.length();
  for(i=l-1;i>=0;i--)
   cout<<s[i];
  cout<<endl;
  return 0;

 
 }

//第12题

#include <iostream>

#include <string>

using namespace std;

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;}

}

int main()

{ int i;

 string str[5];
 cout<<"请输入5个字符串"<<endl;
 for(i=0;i<5;i++)
  cin>>str[i];

 void sort(string []);

 sort(str);

 cout<<"the sorted strings :"<<endl;

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

   cout<<str[i]<<" ";

 cout<<endl;

 return 0;

}

 



//第13题


#include <iostream>

#include <string>

using namespace std;

int main()

{

 long  c[5]={10100,-123567,1198783,-165654, 3456};

  int a[5]={1,9,0,23,-45};

 float b[5]={2.4, 7.6, 5.5, 6.6, -2.3 };

 void sort(int []);

void sort(float []);

 void sort(long []);

 sort(a);

 sort(b);

 sort(c);

 return 0;

}

 

void sort(int a[])

{int i,j,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<<"thesorted numbers :"<<endl;

 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<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]<<" ";

 cout<<endl<<endl;

}

 

void sort(float a[])

{int i,j;

 float 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]<<" ";

 cout<<endl<<endl;

}


//第14题

#include <iostream>

#include <string>

using namespace std;

template <typename T>

void sort(T a[])                   

{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;

         }

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

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

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

 cout<<endl<<endl;

}

 

int main()

{ int a[5]={1,8,0,22,-66};

 float b[5]={3.1, 6.6, 5.5, 9.6, -2.3 };

 long c[5]={10166,-123567, 119999,-165654, 9999};

 sort(a);

 sort(b);

 sort(c);

 return 0;

}




0 0