第一章作业

来源:互联网 发布:中国域名注册商 编辑:程序博客网 时间:2024/06/10 06:33

第七题

#include <iostream>

using namespace std;
int max(int a,int b,int c)
{if(b>a) a=b;
 if(c>a) a=c;
 return a;
}
int max(int a,int b)
{if(a>b)
 return a;
 else return b;
}
int main()
{
    int a,int b,int c;
    cin>>a>>b>>c;
    cout<<"max_3="<<max(a,b,c)<<endl;
    cout<<"max(a,b)="<<max(a,b)<<endl;

    return 0;

}

}

第八题

#include  <iostream>
using namespace std;
int main()

  void change(int &,int & );
  int a,b;
  cin>>a>>b;
  if(a<b) change(a,b);
  cout<<"max="<<a<<" min="<<b<<endl;
  return 0;
}


void change(int &r1,int &r2)

  int temp;


  temp=r1;
  r1=r2;
  r2=temp; 
}


第九题

#include <iostream>
using namespace std;
int main()
{void sort(int &,int &,int &);
 int a,b,c,a1,b1,c1;
 cout<<"请输入3个数:";
 cin>>a>>b>>c;
 a1=a;b1=b;c1=c;
 sort(a1,b1,c1);
 cout<<a<<" "<<b<<" "<<c<<" in sorted order is ";
 cout<<a1<<" "<<b1<<" "<<c1<<endl;
 return 0;
}
void sort(int &x,int &y,int &z)
{ void jiaohuan(int &,int &);
if (x>y) jiaohuan(x,y);
  if (x>z) jiaohuan(x,z);
  if (y>z) jiaohuan(y,z);
}
void jiaohuan(int &x,int &y)
{ int temp;
  temp=x;
  x=y;
  y=temp;
}


第十题

#include <iostream>
#include <string>
using namespace std;
int main()
{ string s1="Sun",s2="day";
  cout<<"第一个字符串是"<<s1<<endl;
  cout<<"第二个字符串是"<<s2<<endl;
  s1=s1+s2;
  cout<<"新的字符串是"<<s1<<endl;
  return 0;
}


第十一题

#include <iostream>
#include <string>
using namespace std;
int main()
{ string str;
  int i,n;
  char temp;
  cout<<"请输入一个字符串:";
  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;
}


第十二题

#include <iostream>
#include <string>
using namespace std;
int main()
{ int i;
  string str[5]={"Q","C","D","V","M"};
  void sort(string []);
  sort(str);
  cout<<"the sorted strings :"<<endl;
  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;}
 }


第十三题

 #include <iostream>
#include <string>
using namespace std;
int main()

  long  c[5]={45435,-456765, 588893,-12344, 3897090};
  int a[5]={1,65,7,83,90};
  float b[5]={4.4, 8.6, 5.3, 6.0, -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<<"the sorted 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;
}

0 0