if (min>a[i]) min=a[i];
}
}
void main()
{
int a[10]={2,5,2,9,0,8,6,1,7,4};
int max,min;
f(a,10,max,min);
cout << "Max:" << max << endl;
cout << "Min:" << min << endl;
system("pause");
}
p230求一元二次方程的根
#include "iostream"
#include "math.h"
using namespace std;
int solve(double a,double b,double c,double &x1,double &x2) { double d;
int count;
d=b*b-4*a*c;
if (d>0)
{
count=2;
x1=(-b+sqrt(d))/(2*a);
x2=(-b-sqrt(d))/(2*a);
}
else if (d==0)
{ count=1;
x1=(-b)/(2*a);
}
else count=0;
return count;
}
void disp(double a,double b,double c)
{ double x1,x2;
cout << "(" << a << "," << b << "," << c << "):";
switch (solve(a,b,c,x1,x2))
{
case 0:
cout<< "无实根"<< endl;
break;
case 1:
cout << "x=" << x1 << endl;
break;
case 2:
cout << "x1=" << x1 << ",x2=" <break;
}
}
void main()
{ disp(2,6,4);
disp(2,4,2);
disp(5,2,3);
system("pause");
}
p237将成绩转化为等级
#include"iostream"
#include"string"
#include"iomanip"
using namespace std;
class stud
{
char name[10];
int deg;
char level[7];
public:
stud(char na[],int d){strcpy(name,na);deg=d;}
char *getname(){return name;}
friend void trans(stud&s)
{
if(s.deg >=90)
strcpy(s.level,"优");
else if(s.deg >=80)
strcpy(s.level,"良");
else if(s.deg >=70)
strcpy(s.level,"中");
else if(s.deg >=60)
strcpy(s.level ,"及格");
else strcpy(s.level,"不及格");
}
void disp()
{cout<<" "<}
};
void main()
{
stud st[]={stud("王华",78),stud("李明",92),stud("张伟",62),stud("孙强",88)};
cout<<"输出结果"<for(int i=0;i<4;i++)
{
trans(st[i]);
st[i].disp();
}
system("pause");
}
p242银行例题
#include"iostream"
using namespace std;
class BBank; //这里预先说明,类BBank在后面定义
class GBank; //这里预先说明,类GBank在后面定义
class CBank //说明中国银行类CBank
{
private:
int balance;
public:
CBank() {balance=0;}
CBank(int b) {balance=b;}
void getbalance(){
cout<<"输入中国银行存款数:";
cin>>balance;
}
void disp(){
cout<<"中国银行存款数:"<}
friend void total(CBank,BBank,GBank);
};
class BBank //说明工商银行类Bbank {
private:
int balance;
public:
BBank() { balance=0; }