//编程练习1
#include <iostream>
double har_average(double ,double );
using namespace std;
int main()
{
cout << "Enter two numbers:" << endl;
double x,y;
cin >> x >> y;
while (cin)
{
if(x==0||y==0 )
{
cout << "quit.\n";
break;
}
else
{
cout << "the harmonic average is : " ;
cout << har_average(x,y) << endl;
cin >> x >>y;
}
}
return 0;
}
double har_average(double x,double y)
{
return 2.0*x*y/(x+y);
}
//编程练习2
#include <iostream>
const int Size = 10;
int fill_arr(double *,int n);
void show_arr(double * ,int n);
double calculate(double [],int n);
using namespace std;
int main()
{
double arr[Size];
cout << "Please input the grade of golf: \n";
int N = fill_arr(arr,Size);
cout << "Output the grade: \n";
show_arr(arr ,N);
cout << "\nThe average is : \n";
cout << calculate( arr,N)<<endl;
cout << "Done!\n";
return 0;
}
int fill_arr(double *ar,int n)
{
double temp;
cin >> temp;
int i =0;
int count = 0;
while (cin)
{
ar[i] = temp;
i++;
cin>>temp;
count++;
}
return count;
}
void show_arr(double *ar ,int n)
{
for(int i = 0;i<n;i++)
{
cout << ar[i] << " ";
}
}
double calculate(double ar[],int n)
{
double sum =0.0;
for( int i=0 ;i<n;i++)
{
sum += ar[i];
}
return sum/n;
}
//编程练习3
#include <iostream>
struct box
{
char make[40];
float height;
float width;
float length;
float volume;
};
void show(box);
void vo(box * );
using namespace std;
int main()
{
box tp = {"square",20,34.5,34.6,0};
vo(&tp);
show(tp);
cout << "Done!" << endl;
return 0;
}
void show(box temp)
{
cout << "The name of box: ";
cout << temp.make << endl;
cout << "height: " << temp.height << endl;
cout << "width: " << temp.width <<endl;
cout << "length: " << temp.length << endl;
cout << "volume: " << temp.volume << endl;
}
void vo(box * temp)
{
temp->volume = temp->height * temp->length * temp->width;
}
// 编程练习4
#include <iostream>
long double probability(unsigned numbers1, unsigned picks1,unsigned numbers2, unsigned picks2);
int main()
{
using namespace std;
double total1, choices1,total2, choices2;
cout << "Enter the total number of choices on the game card and\n"
<<"the number of picks allowed:\n";
while ((cin >> total1 >> choices1) && choices1 <= total1)
{
cout << "Enter the total number of second choices on the game card and\n"
<< "the second number of picks allowed:\n";
while ((cin >> total2 >> choices2) && choices2 <= total2)
{
cout << "You have one chance in ";
cout << probability(total1, choices1,total2, choices2);
cout << " of winning.\n";
cout << "Next two numbers (q to quit): ";
}
}
cout << "bye\n";
return 0;
}
long double probability(unsigned numbers1, unsigned picks1,unsigned numbers2, unsigned picks2)
{
long double result1 = 1.0;
long double result2 = 1.0;
long double n;
unsigned p;
for (n = numbers1, p = picks1; p > 0; n--, p--)
result1 = result1 * n / p ;
//这里算阶层的方法要记住!分母先乘上一个数,再除一个分子,再乘分母除分子。。。
for (n = numbers2, p = picks2; p > 0; n--, p--)
result2 = result2 * n / p ;
return 1/(result1*result2);
}
// 编程练习5
#include <iostream>
long double factorial(unsigned int n);
int main()
{
using namespace std;
unsigned int n;
cout << "Enter number: ";
while ( cin >> n)
{
cout << n << "! = " << factorial(n);
cout <<"\nEnter the number again:(q to quit): ";
}
cout << "Done!\n";
return 0;
}
long double factorial(unsigned int n)
{
long double total = 1;
if(n>0)
{
total = n*factorial(n-1);
}
return total;
}
// 编程练习6
#include <iostream>
using namespace std;
const int Size = 10;
int Fill_array(double ar[],int n);
void Show_array(double ar[],int n);
void Reverse_array(double *ar,int n);
int main()
{
double arr[Size];
int N = Fill_array(arr,Size);
cout << "The array is : \n";
Show_array(arr,N);
Reverse_array(arr,N);
cout << "The reverse array is : \n";
Show_array(arr,N);
Reverse_array(arr+1,N-2);
cout << "The reverse array is : \n";
Show_array(arr,N);
return 0;
}
int Fill_array(double ar[],int n)
{
cout << "Please input double number: \n";
double temp;
int i = 0;
while( cin >> temp && i<n )
{
ar[i] = temp;
i++;
}
return i;
}
void Show_array(double ar[],int n)
{
for (int i = 0; i<n;i++)
{
cout << ar[i] << endl;
}
}
void Reverse_array(double *ar,int n)
{
double temp;
for (int i = 0;i < n/2;i++)
{
temp = ar[i];
ar[i] = ar[n-1-i];
ar[n-1-i] = temp;
}
}
// 编程练习7
#include <iostream>
const int Max = 5;
double * fill_array(double ar[], double arr[]);//函数原型里的参数变量可写可不写,且与定义里的可不相同。
void show_array(const double ar[],double *end);
void revalue(double r, double ar[],double arr[]);
int main()
{
using namespace std;
double properties[Max];
double * size = fill_array(properties, properties + Max);
show_array(properties, size);
if (size > 0)
{
cout << "Enter revaluation factor: ";
double factor;
while (!(cin >> factor))
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input; Please enter a number: ";
}
revalue(factor, properties, size);
show_array(properties, size);
}
cout << "Done.\n";
return 0;
}
double * fill_array(double begin[], double end[])
{
using namespace std;
double temp;
int i;
for (i = 0; i <(end-begin); i++)
{
cout << "Enter value #" << (i + 1) << ": ";
cin >> temp;
if (!cin)
{
cin.clear();
while (cin.get() != '\n')
continue;
cout << "Bad input; input process terminated.\n";
break;
}
else if (temp < 0)
break;
begin[i] = temp;
}
return &begin[i-1];
}
void show_array(const double ar[], double *end)
{
using namespace std;
for (int i = 0; &ar[i]<= end; i++) //判断语句也要做相应的改变
{
cout << "Property #" << (i + 1) << ": $";
cout << ar[i] << endl;
}
}
void revalue(double r, double ar[], double * end)
{
for (int i = 0; &ar[i]<= end; i++)
ar[i] *= r;
}
//编程练习8(a)
#include <iostream>
const int Seasons = 4;
const char * Snames[Seasons] = //数组指针,里面存放地址
{"Spring", "Summer", "Fall", "Winter"};
void fill(double * pa); //这里没有传递数组的长度void fill(double * da, int n)
void show(double * da);//因为有全局变量Seasons,在一般情况下最好采用传递参数的形式
int main()
{
double expenses[4];
fill(expenses);
show(expenses);
return 0;
}
void fill(double * pa)
{
for (int i = 0; i < Seasons; i++)
{
std::cout << "Enter " << Snames[i] << " expenses: ";
std::cin >> pa[i]; //在用arra时void fill(std::array<double, Seasons> * pa)
//std::cin >> (*pa)[i] (暂时理解为这里的pa指向对象)
}
}
void show(double * da)
{
double total = 0.0;
std::cout << "\nEXPENSES\n";
for (int i = 0; i < Seasons; i++)
{
std::cout << Snames[i] << ": $" << da[i] << '\n';
total += da[i];
}
std::cout << "Total: $" << total << '\n';
}
//编程练习8(b)
#include <iostream>
struct pay
{
double expenses[4];
};
const int Seasons = 4;
const char * Snames[4] =
{"Spring", "Summer", "Fall", "Winter"};
void fill(pay * pa);
void show(pay da);
int main()
{
pay exp;
fill(&exp);
show(exp);
return 0;
}
void fill(pay * pa) //往结构里面写数据的函数,要采用传址!
{
for (int i = 0; i < Seasons; i++)
{
std::cout << "Enter " << Snames[i] << " expenses: ";
std::cin >> pa->expenses[i];
}
}
void show(pay da)
{
double total = 0.0;
std::cout << "\nEXPENSES\n";
for (int i = 0; i < Seasons; i++)
{
std::cout << Snames[i] << ": $" << da.expenses[i] << '\n';
total += da.expenses[i];
}
std::cout << "Total: $" << total << '\n';
}
//编程练习9
#include <iostream>
using namespace std;
const int SLEN = 30;
struct student
{
char fullname[SLEN];
char hobby[SLEN];
int ooplevel;
};
int getinfo(student pa[],int n);//结构数组
void display1(student st);
void display2(const student * ps );
void display3(const student pa[],int n);
int main()
{
cout << "Enter the class size: ";
int class_size;
cin >> class_size;
while (cin.get() != '\n')
continue;
student * ptr_stu = new student [class_size];
int entered = getinfo(ptr_stu, class_size);
for(int i = 0;i<entered;i++)//两种显示结构数组元素的方法
{
display1(ptr_stu[i]);
display2(&ptr_stu[i]);
}
display3(ptr_stu,entered);
delete [] ptr_stu;
cout << "Done\n";
return 0;
}
int getinfo(student pa[],int n)
{
int count = 0 ;
for (int i = 0;i<n;i++)
{
cout << "#"<<(i+1)<<": \n";
cout << "Fullname: ";
cin.get(pa[i].fullname ,SLEN);//容易出错,向数组输入时,记得带上数组本来的长度
if(cin)
{
cin.get();
cout << "Hobby: ";
cin.get(pa[i].hobby,SLEN);
cout << "Opplevel: ";
cin >> pa[i].ooplevel;
cin.get();
count ++;
}
else
{
break;
}
}
return count;
}
void display1(student st)
{
cout << "The student's fullname is:";
cout << st.fullname<<endl;
cout << "hobby: " << st.hobby <<endl;
cout << "opplevel: " << st.ooplevel <<endl;
}
void display2(const student * ps )
{
cout << "The student's fullname is:";
cout << ps->fullname<<endl;
cout << "hobby: " << ps->hobby <<endl;
cout << "opplevel: " << ps->ooplevel <<endl;
}
void display3(const student pa[],int n)
{
for (int i = 0;i<n;i++)
{
cout << "#" << (i+1) << ":"<<endl;
cout << "The student's fullname is:";
cout << pa[i].fullname<<endl;
cout << "hobby: " << pa[i].hobby <<endl;
cout << "opplevel: " << pa[i].ooplevel <<endl;
}
}
// 编程练习10(运用指针数组)先写简单的,再写复杂的
#include <iostream>
using namespace std;
double add(double x,double y);
double multiply(double x,double y);
double calculate(double a,double b,double (*pa)(double x,double y));
int main()
{
double a ,b,result;
cout << "Enter two number: \n";
double (*pf[2])(double,double) = {add,multiply} ;//指针数组
while(cin>>a>>b)
{
for(int i = 0; i<2;i++)
{
result = calculate(a,b,pf[i]);
cout << result << endl;
}
}
cout << "Done!\n";
return 0;
}
double add(double x,double y)
{
cout << "add: ";
return x + y;
}
double multiply(double x,double y)
{
cout << "mult: ";
return x * y;
}
double calculate(double a,double b,double (*pa)(double x,double y))
{
double q = pa(a,b);
return q;
}