第一章节

来源:互联网 发布:怎么优化群排名 编辑:程序博客网 时间:2024/04/18 18:35


第一章

7.
#include<iostream>
using namespace std;
int main()
{ int max(int x=4,int y=9);
 int a,b,c;
 c=max();
 cout<<c<<endl;
 return 0;
}
 int max(int x,int y)
{
 int z;
 if (x>y)z=x;
 else z=y;
 return(z);
}
8.
#include<iostream>
using namespace std;
int main()
{  int a,b;
    cin>>a>>b;
int &c=a;
int &d=b;
if(a>b)
cout<<c<<d;
else
cout<<d<<c;
}
9.
#include<iostream>
using namespace std;
int main()
{  int a,b,c;
    cin>>a>>b>>c;
 int &l=a;
 int &m=b;
 int &s=c;
 int temp;
 if (a<b) {temp=a;a=b;b=temp;}
 if(c>a) cout<<s<<l<<m;
 else if(c>b) cout<<l<<s<<m;
 else   cout<<l<<m<<s;
}
10.
#include<iostream>
#include<string>
using namespace std;
int main()
{
 string str1,str2;
 cin>>str1>>str2;
 str1=str1+str2;
 cout<<str1;
}
11.
#include<iostream>
#include<string>
using namespace std;
int main()
{
 string str1;
 cin>>str1;
 int i=5;
 for(i;i<6;i--)
  cout<<str1[i];
}

12.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int i;
string str[5];
void sort(string s[]);
cout<<"Please input string:"<<endl;
for(i=0;i<=5;i++) cin>>str[i];
cout<<"The sorted string is:"<<endl;
for(i=0;i<=5;i++)
cout<<str[i]<<endl;
return 0;
}
void sort(string s[])
 {
int i,j;
 string temp;
 for(i=0;i<4;i++)
for(j=0;j<4-i;j++)
if(s[j]>s[j+1])
{
temp=s[j];
 s[j]=s[j+1];
s[j+1]=temp;
}
}
14..#include<iostream>
using namespace std;
template<typename T>   
paixu(T*a,int n)    
{     for(int i=0;i<n;i++)
{         
 for(int j=0;j<n;j++)
{
if(a[i]<a[j])
{  T temp=a[i];
 a[i]=a[j];
 a[j]=temp;
}
}
}
for(int i=0;i<n;i++)
 cout<<a[i]<<endl; 
}
int main()
{
int a[5],n; 
n=sizeof(a)/sizeof(int); 
for(int i=0;i<n;i++)
cin>>a[i];
 paixu(a,n);
}

 

 

 

第三章

4.
#include<iostream>
using namespace std;
class student
{
 public:
  int num;
  int score;
  void display();
  void get();
};
void student::get()
{
 cin>>num;
 cin>>score;
}
void student::display()
{
 cout<<num<<" "<<score<<endl;
 
}
int main()
{
 student stud[5];
 for(int i=0;i<5;i++)
 {
  stud[i].get();
 }
 student *p;
 p=&stud[0];
 p->display();
 p+=2;
 p->display();
 p+=2;
 p->display();
}


5.
#include<iostream>
using namespace std;
class student
{
 public:
  int num;
  int score;
  void get();
};
void student::get()
{
 cin>>num;
 cin>>score;
}
int max(student *p)
{    int max;
 for(int i=0;i<5;i++)
 {
  if(p->score<++p->score)
   max=++p->score;
   else max=p->score;
   p++;
 }
   cout<<max;
}
int main()
 {   student stud[5];
  student *p;
     p=&stud[1];
 for(int i=0;i<5;i++)
 {
  stud[i].get();
 } 
 max(p);


8.
#include <iostream>
using namespace std;
class Student
{public:
    Student(int n,float s):num(n),score(s){}
    void change(int n,float s)
 {num=n;score=s;}
 void display()
 {cout << num <<" " << score <<endl;}
private:
     int num;
  float score;
};
void fun(Student &stud)
{
 stud.display();
 stud.change(101,80.5);
 stud.display();
}
int main()
{
 Student stud(101,78.5);
 fun(stud);
 return 0;
}

9.
#include <iostream>
using namespace std;
class salesman
{
public:
salesman()//构造函数

  quantity=0; //初始化
  price=0;
}
static float average();
static void display();
void total();
void set();
private:
static float discount;
static float sum;
static int n;
int quantity;
float price;
};
void salesman::set()
 {
 cout<<"销售件数"<<endl;
 cin>>quantity;
 cout<<"售货单价"<<endl;
 cin>>price;
 }
void salesman::total()
{ if(quantity>=10) 
  price=price*0.98;
  sum+=quantity*price*discount; //打折折扣后价钱
  n+=quantity;
 }
float salesman::average()
 {
 return(sum/n);
 }
float salesman::discount=0.9;
int salesman::n=0;
float salesman::sum=0;
void salesman::display()
 {
 cout<<"总销售款为"<<sum<<endl;
 cout<<"平均售价为"
 <<salesman::average()
 <<endl;
 }
  int main()
 {
 salesman sal[3];
 sal[0].set();
 sal[0].total();
 sal[1].set();
 sal[1].total();
 sal[2].set();
 sal[2].total();
 salesman::display();
 }


10.
#include <iostream>
using namespace std;
class Date;
class Time{
 public:
  Time(int ,int ,int);
  friend void display(Time &);
 private:
  int hour;
  int minute;
  int sec;
};
class Date{
 public:
  Date(int ,int ,int);
  friend void display(Date &);
 private:
  int mouth;
  int day;
  int year;
};
Time::Time(int h,int m,int s)
{
 hour=h;
 minute=m;
 sec=s;
}
void display(Date &d)
{
 cout<<d.mouth<<"/"<<d.day<<"/"<<d.year<<"/"<<endl;
}
void display(Time &d)
{
 cout<<d.hour<<":"<<d.minute<<":"<<d.sec<<endl;
}
Date::Date(int m,int d,int y)
{
 mouth=m;
 day=d;
 year=y;
}
int main()
{
 Time t1(10,13,56);
 Date d1(12,25,2004);
 display(t1);
 display(d1);
 return 0;
}


11.
#include <iostream>
using namespace std;
class Date;
class Time{
 friend Date;
 public:
  Time(int ,int ,int);
  void display(Date &);
 private:
  int hour;
  int minute;
  int sec;
};
class Date{
 public:
  Date(int ,int ,int);
  friend void Time::display(Date &);
 private:
  int mouth;
  int day;
  int year;
};
Time::Time(int h,int m,int s)
{
 hour=h;
 minute=m;
 sec=s;
}
void Time::display(Date &d)
{
 cout<<d.mouth<<"/"<<d.day<<"/"<<d.year<<"/"<<endl;
 cout<<hour<<":"<<minute<<":"<<sec<<endl;
}
Date::Date(int m,int d,int y)
{
 mouth=m;
 day=d;
 year=y;
}
int main()
{
 Time t1(10,13,56);
 Date d1(12,25,2004);
 t1.display(d1);
 return 0;
}
12..
#include <iostream>
using namespace std;
template<class numtype>            
class compare//声明类模板
{public: 
compare(numtype,numtype); 
numtype max();
 numtype min();
private:
numtype x,y;
};
template<class numtype>
 compare<numtype>::compare( numtype a,numtype b)
{
 x=a;
 y=b;
}
template<class numtype>
numtype compare<numtype>::max()
{
 return (x>y)?x:y;
}
template<class numtype>
numtype compare<numtype>::min()
{
 return (x<y)?x:y;
}

int main( )
{compare <int> cmp1(3,7); 
cout<<cmp1.max( )<<"is the Maximum of two integer numbers." <<endl;
cout<<cmp1.min( )<<"is the Minimum of two integer numbers."<<endl<<endl;
compare<float> cmp2(45.78,93.6); 
cout<<cmp2.max( )<<" is the Maximum of two float numbers."<<endl;
cout<<cmp2.min( )<<"is the Minimum of two float numbers."<<endl<<endl;
compare<char> cmp3('a','A');     
cout<<cmp3.max( )<<"is the Maximum of two characters." <<endl;
cout<<cmp3.min( )<<"is the Minimum of two characters."<<endl;
return 0;
}

 


 


0 0
原创粉丝点击