C++ Primer Plus (第6版)编程练习 代码-----第七章

1.

double average(double x,double y);

int _tmain(int argc, _TCHAR* argv[])
{
    double input1,input2;
cout<<"Please input two numbers,if has 0,stop :"<<endl;
cin>>input1;

cin>>input2;
while(!(input1==0||input2==0))
{
 cout<<"the averrage is: "<<average(input1,input2)<<endl;
 cout<<"Please input two numbers,if has 0,stop :"<<endl;
 cin>>input1;
 cin>>input2;
}

    system("pause");  

return 0;
}


double average(double x,double y)
{
double av=0;
av=2.0*x*y/(x+y);
return av;

}


2.


const int Max=10;
int inputnum(double arr[],int n);
double average(double arr[],int n);
int _tmain(int argc, _TCHAR* argv[])
{
double score[Max];
cout<<"please enter scores by order;negative numbers to teminal! \n";
int number =inputnum(score,Max);
double av =average(score,number);
cout<<"\nthe average is : "<<av<<endl;

system("pause");
return 0;
}
 int inputnum(double arr[],int n)
{  int i;
   double temp;
for(i=0;i<n;i++)
{  
 cout<<"Please input score of round  "<<i+1<<" : ";
 while(!(cin>>temp))
{   cin.clear();
  cout<<"please input right number\n";
  cin.get();
  cout<<"Please input score of round  "<<i+1<<" : ";
}
 if(temp<0)
 break;
      arr[i]=temp;
}
return i;
}


double average(double arr[],int n)
 {
double sum=0;
   for(int i=0;i<n;i++)
   {
     cout<<arr[i]<<" ";
     sum=sum+arr[i];
   }
   cout<<"the sum is "<<sum<<endl;
   double avr=sum/n;
    return avr;
 }


3.

#include "stdafx.h"
#include<iostream>
using namespace std;
struct box
{
  char maker[40];
  float height;
  float width;
  float lenght;
  float volume;
};
void a(box t);
void b(const box *t);
int _tmain(int argc, _TCHAR* argv[])
{
box test ={"test factory",20,30,20,12000};
a(test);
b(&test);
system("pause");
return 0;
}
 
void a(box t)
{
  cout<<"the box's maker  is: "<<t.maker<<endl;
  cout<<"the box's height is: "<<t.height<<endl;
  cout<<"the box's width  is: "<<t.width<<endl;
  cout<<"the box's length is: "<<t.lenght<<endl;
  cout<<"the box's volume is: "<<t.volume<<endl;
}
void b(const box *t)
{
cout<<"pointer type \n";
  cout<<"the box's maker  is: "<<t->maker<<endl;
  cout<<"the box's height is: "<<t->height<<endl;
  cout<<"the box's width  is: "<<t->width<<endl;
  cout<<"the box's length is: "<<t->lenght<<endl;
  cout<<"the box's volume is: "<<(t->height)*(t->width)*(t->lenght)<<endl;
}
4.

#include "stdafx.h"
#include<iostream>
using namespace std;


long double properties(int choice,int n);
int _tmain(int argc, _TCHAR* argv[])
{
long double res=1;
int times=0,count =1;
cout<<"please enter the number : ";
cin>>times;

int num1,num2;
   long double re;
cout<<"please enter the total number of choices and \n"<<"the number of picks allowed :\n";
   while((cin>>num1>>num2)&&(num2<=num2)&&(count<times))
   {
      re =properties(num1,num2);
      cout<<"the properties is: "<<re<<endl; 
   res=res*re;
   count++;
cout<<"please enter the total number of choices and \n"<<"the number of picks allowed :\n";

}
   
cout<<"the total properties is : "<<res;
   

system("pause");
return 0;
}
 
long double properties(int choice,int n)
{   
long double sum=1;
int i,j;
for ( i=choice,j=n;j>0;i--,j--)
{
  sum=sum*choice/j;

}
long double result=1/sum;
return result;


}


5.

#include "stdafx.h"
#include<iostream>
using namespace std;


int s(unsigned int n);
int _tmain(int argc, _TCHAR* argv[])
{
int i=0;
unsigned int number;
cout<<"please enter the "<<i+1<<" number,q to quit :";

while(cin>>number)
{
 cout<<number<<"! is : "<<s(number)<<endl;
 cout<<"please enter the "<<i+1<<" number,q to quit :";
} 
system("pause");
return 0;
}
 
int s(unsigned int n)
{
int result=1;
    if (n==0)
 result=1;
if(n>0)
result=s(n-1)*n;

 return result;

}


6.

#include "stdafx.h"
#include<iostream>
using namespace std;


void Fill_array(double ar[],int n);
void Show_array(double ar[],int n);
void Reverse_array(double ar[],int n);
const int Max=5;
int _tmain(int argc, _TCHAR* argv[])
{   

double f_ar[Max];
Fill_array(f_ar,Max);
system("pause");
return 0;
}
 
void Fill_array(double ar[],int n)
{
unsigned int i=0;
double temp;

cout<<"Please enter the "<<i+1<<" number :";
while (cin>>temp)
{ 
   ar[i]=temp;
   i++;
if(i>=Max)
break;
cout<<"Please enter the "<<i+1<<" number :";
}
Show_array(ar,i-1);
Reverse_array(ar,i-1);
}
void Show_array(double ar[],int n)
{
cout<<"\n\nshow array\n\n";
for(int i=0;i<=n;i++)
{
 cout<<ar[i]<<endl;
}


}
void Reverse_array(double ar[],int n)
{
cout<<"reverse array\n\n";
for(int i=n;i>=0;i--)
{
 cout<<ar[i]<<endl;
}
cout<<"\n\n";
}



7.


#include "stdafx.h"
#include<iostream>
#include <string> 
#include<vector>
#include<fstream>
using namespace std;


const int Max=5;
int* fill_array(double *ar,int limit);


void show_array(const double *ar,int n);
void revalue(double r,double *ar,int n);


int _tmain(int argc, _TCHAR* argv[])
{


  double properties[Max];


  int *size = fill_array(properties,Max);
  show_array(properties,*size);
  if ((*size)>0)
  {
 cout<<"Enter revaluation facor: ";
 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";
cin.get();
cin.get();


    system("pause");  

return 0;
}
int* fill_array(double *ar,int limit)
{
  double temp;
  int i;
  for (i=0;i<limit;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;
 *ar= temp;


 ar++;
  }
  return &i;
}
void show_array(const double *ar,int n)
{
for (int i=0;i<n;i++)
{
cout<<"Properties #"<<(i+1)<<": $";
cout <<*ar<<endl;
ar++;

}
}


void revalue(double r,double *ar,int n)
{
   for(int i=0;i<n;i++)
(*ar)*=r;
   ar++;
}





8.a



#include "stdafx.h"
#include<iostream>
#include<array>
#include<string>
using namespace std;


const int Season=4;
const char* season[4]={"Spring","Summer","Autumn","Winter"};
double expense[4];
void fill(const char*s[],double ar[]);
void show(const char*s[],double ar[]);
int _tmain(int argc, _TCHAR* argv[])
{   

fill(season,expense);
show (season,expense);
system("pause");
return 0;
}
 void fill(const char*s[],double ar[])
 {
   for(int i=0;i<Season;i++)
   {
     cout<<"enter the expense of "<<*s<<" is : ";
cin>>ar[i];
     s++;
   }
   s=s-4;
 }
 
 void show(const char*s[],double ar[])
 {
cout<<"\n\n\n";
double total=0.0;
   for(int i=0;i<Season;i++)
   {
     cout<<"show the expense of "<<*s<<"is : "<<ar[i]<<endl;
total+=ar[i];
     s++;
   }
   cout<<"the total expense is : "<<total<<endl;
 }


8.b

#include "stdafx.h"
#include<iostream>
#include<array>
#include<string>
using namespace std;


const int Season=4;
const char* season[4]={"Spring","Summer","Autumn","Winter"};
struct expense
{
double ex;
};
void fill(const char*s[],expense *ep);
void show(const char*s[],expense *ep);
int _tmain(int argc, _TCHAR* argv[])
{   
expense exp;
fill(season,&exp);
show (season,&exp);
system("pause");
return 0;
}
 void fill(const char*s[],expense *ep)
 {
   for(int i=0;i<Season;i++)
   {
     cout<<"enter the expense of "<<*s<<" is : ";
cin>>ep->ex;
     s++;
ep++;
   }
   s=s-4;
   ep=ep-4;
 }
 
 void show(const char*s[],expense *ep)
 {
cout<<"\n\n\n";
double total=0.0;
   for(int i=0;i<Season;i++)
   {
     cout<<"show the expense of "<<*s<<" is : "<<ep->ex<<endl;
total+=ep->ex;
     s++;
ep++;
   }
   cout<<"the total expense is : "<<total<<endl;
 }




9.
// C7.cpp : 定义控制台应用程序的入口点。
//


#include "stdafx.h"
#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(student pa[],int n);


int _tmain(int argc, _TCHAR* argv[])
{   
	cout<<"Enter 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);
	/*cout<<"the number is :"<<entered;*/
	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";
	system("pause");
	return 0;
}


int getinfo(student pa[],int n)
{ 
	int i=0;
	cout<<"Please enter the "<<i+1 <<" student's name: ";
	       cin.get(pa->fullname,SLEN);
	while((strlen(pa->fullname)!=0)&&i<n)
		
	{		
	         cin.get();
			 cout<<"Please enter the "<<i+1 <<" student's hobby: ";
			 cin.get(pa->hobby,SLEN);
			
			 cout<<"Please enter the "<<i+1 <<" student's ooplevel: ";
			 cin>>pa->ooplevel;
			 cin.get();
			 i++;
			 pa++;
			 if(i>=n)
	         break;
	         cout<<"Please enter the "<<i+1 <<" student's name: ";
	         cin.get(pa->fullname,SLEN);
	}	   
		
	return i;
		


}
void display1(student st)
{
	cout<<"\nthe fullname is: "<<st.fullname<<endl;
	cout<<"the bobby is: "<<st.hobby<<endl;
	cout<<"the opplevel is: "<<st.ooplevel<<endl;
}
void display2(const student * ps)
{
	cout<<"\n\nthe fullname is: "<<ps->fullname<<endl;
	cout<<"the bobby is: "<<ps->hobby<<endl;
	cout<<"the opplevel is: "<<ps->ooplevel<<endl;
}
void display3(student pa[],int n)
{
	for(int i=0;i<n;i++)
	{
      cout<<"\n\n the fullname is: "<<pa->fullname<<endl;
	  cout<<" the bobby is: "<<pa->hobby<<endl;
	  cout<<" the opplevel is: "<<pa->ooplevel<<endl;
	  pa++;
   }
}



10.

#include "stdafx.h"
#include<iostream>
using namespace std;
double add(double x,double y);
double calculate(double m,double n,double(*pf)(double,double));

int _tmain(int argc, _TCHAR* argv[])
{   
	double number1,number2;
	cout<<"enter two numbers : ";
	while(cin>>number1>>number2)
	{
		cout<<"the result is: "<<calculate(number1,number2,add)<<endl;
	    cout<<"enter two numbers : ";
	}
	system("pause");
	return 0;
}

double add(double x,double y)
{
  return x+y;

}
double calculate(double m,double n,double(*pf)(double,double))
{
	return m+n+(*pf)(m,n);

}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值