#include
double average ( double x, double y )
{
return 2 * x * y / ( x + y );
}
int main()
{
using namespace std;
double x, y;
while ( cin >> x >> y )
{
if ( x == 0 || y == 0 )
break;
else
cout << average ( x, y ) << endl;
cin.sync();
}
return 0;
}
/* note:输入时,显示第几个的语句要放对地方,不然输入完10个不是直接显示score,而是显示11 :
我之前就是把
std::cout << i + 1 << " : ";
放到了
i++;if(i==10) break;
前面,导致显示这样;
这题用while很简单,只要不是输入数字输入别的都会,退出。*/
#include
const int ArSize = 10;
static int k = 0;
void input ( double *a, int n )
{
int i = 0;
std::cout << "Please enter the number, enter any number other than the character to exit" << std::endl;
std::cout << i + 1 << " : ";
while ( std::cin >> a[i] && i < n )
{
i++;
if(i==10) break;
std::cout << i + 1 << " : ";
}
k = i;
}
void display ( double *a )
{
int i;
std::cout << "score : ";
for ( i = 0; i < k; i++ )
std::cout << a[i] << " ";
std::cout << std::endl;
}
double average ( double *a )
{
int i;
double sum = 0;
for ( i = 0; i < k; i++ )
sum += a[i];
return sum/k;
}
int main()
{
using namespace std;
double score[ArSize];
input ( score, ArSize );
display ( score );
cout << "Average : " << average ( score ) << endl;
return 0;
}
#include
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
void adisplay(box cube)
{
std::cin>>cube.maker>>cube.height>>cube.width>>cube.length;
cube.volume=cube.height*cube.length*cube.width;
std::cout<<"Value :"<>cube->maker>>cube->height>>cube->width>>cube->length;
cube->volume=cube->height*cube->length*cube->width;
std::cout<<"Pointer :"<maker<<" "<height<<" "<width<<" "<length<<" "<volume<
#include
long long factorial ( long long n )
{
if ( n == 1 || n == 0 )
return 1;
else
n *= factorial ( n - 1 );
return n;
}
int main()
{
int n, i = 0;
std::cout << i + 1 << " : " ;
while ( std::cin >> n )
{
std::cout << "n!=" << factorial ( n ) << std::endl;
std::cout << ++i + 1 << " : " ;
}
return 0;
}
反转数组中除第一个和最后一个元素之外的所有元素, 然后显示数组
#include
const int ArSize = 10;
void fill_array ( double *a, int& n )
{
int i = 0;
std::cout << "fill_array : " << std::endl;
std::cout << "Please enter double numbers : " << std::endl;
std::cout << "#" << i + 1 << " : " ;
while ( std::cin >> a[i] )
{
if ( i == 10 ) break;
i++;
std::cout << "#" << i + 1 << " : " ;
}
n = i;
std::cout << "return numbers : " << i << std::endl << std::endl;
}
void show_array ( double *a, int n )
{
int i = 0;
std::cout << "show_array :" << std:: endl;
for ( ; i < n; i++ )
std::cout << a[i] << " ";
std::cout << std::endl << std::endl;
}
void reverse_array ( double *a, int n )
{
int i,j;
double t;
j=n;
for ( i = 0; i < (n/2); i++, j-- )
{
t = a[i];
a[i] = a[j - 1];
a[j - 1] = t;
}
std::cout << "reverse_array :" << std::endl;
}
int main()
{
double ar[ArSize];
int len=ArSize;
fill_array ( ar, len );
show_array ( ar, len );
reverse_array ( ar, len );
show_array ( ar, len );
std::cin.clear();
std::cin.sync();
return 0;
}
该指针指向最后被填充的位置:其他的函数可以将该指针作为第二个参数,以标识数据结尾。
#include
using namespace std;
const int Max = 5;
// function prototypes
double *fill_array ( double *begin, double *end );
void show_array ( double *begin, double *end );
void revalue (double r, double *begin, double *end );
int main()
{
double properties[Max];
double *size = fill_array ( properties, properties + Max );
show_array ( properties, size );
cout << "Enter revaluation factor: ";
double factor;
cin >> factor;
revalue (factor, properties, size );
show_array ( properties, size );
cout << "Done.\n";
return 0;
}
double* fill_array ( double *begin, double *end )
{
double temp,*i = begin;
int j=0 ;
for ( ; i < end; i++ )
{
cout << "Enter value #" << ( j++ + 1 ) << ": ";
cin >> temp;
if ( !cin ) // bad input
{
cin.clear();
while ( cin.get() != '\n' )
continue;
cout << "Bad input; input process terminated.\n";
break;
}
else if ( temp < 0 ) // signal to terminate
break;
*i = temp;
}
return i;
}
// the following function can use, but not alter,
// the array whose address is ar
void show_array ( double *begin, double *end )
{
int j=0;
double* i = begin;
for ( ; i < end; i++ )
{
cout << "Property #" << ( j++ + 1 ) << ": $";
cout << *i << "\n";
}
}
// multiplies each element of ar[] by r
void revalue ( double r,double *begin, double *end )
{
double* i = begin;
for ( ; i < end; i++ )
*i *= r;
}
a.
#include
const int seasons = 4;
const char* snames[seasons] = {"spring", "summer", "fall", "winter"};
void fill ( double e[] );
void show ( double e[] );
int main()
{
double expenses[seasons];
fill ( expenses );
show ( expenses );
return 0;
}
void fill ( double e[] )
{
for ( int i = 0; i < seasons; i++ )
{
std::cout << "Entetr " << snames[i] << " expenses: ";
std::cin >> e[i];
}
}
void show ( double e[] )
{
std::cout << "EXPENSES" << std::endl;
int i = 0, sum = 0;
for ( ; i < seasons; i++ )
{
std::cout << snames[i] << " : $" << e[i] << std::endl;
sum += e[i];
}
std::cout << "Total : $" << sum << std::endl;
}
#include
const int seasons = 4;
const char* snames[seasons] = {"spring", "summer", "fall", "winter"};
struct pay
{
double expenses[seasons];
};
void fill ( pay *e );
void show ( pay *e );
int main()
{
pay e;
fill ( &e );
show ( &e );
return 0;
}
void fill ( pay *e )
{
for ( int i = 0; i < seasons; i++ )
{
std::cout << "Entetr " << snames[i] << " expenses: ";
std::cin >> e->expenses[i];
}
}
void show ( pay *e )
{
std::cout << "EXPENSES" << std::endl;
int i = 0, sum = 0;
for ( ; i < seasons; i++ )
{
std::cout << snames[i] << " : $" << e->expenses[i] << std::endl;
sum += e->expenses[i];
}
std::cout << "Total : $" << sum << std::endl;
}
10.设计一个名为calculate()的函数,它接受两个double值和一个指向函数的指针,而被指向的函数接受两个double参数,并返回一个double值、calculate()函数的类型也是double,并返回被指向的函数使用calculate()的两个double参数计算得到的值。例如,假如add()函数的定义如下:
double add(double x,double y)
{
return x + y;
}
可以采用数组初始化句法,并将函数名作为地址来初始化这样的数组。
#include
double calculate ( double x, double y, const double ( *pf ) ( double, double ) )
{
return ( *pf ) ( x, y );
}
const double add ( double a, double b )
{
return a + b;
}
const double mul ( double a, double b )
{
return a * b;
}
const double ( *pf[2] ) ( double x, double y ) = {add, mul};
int main()
{
double x, y;
int i = 0;
std::cout << "Please enter the two numbers" << std::endl;
while ( std::cin >> x >> y&&i<2 )
{
std::cout << "volume : " << calculate ( x, y, pf[i] ) << std::endl;
i++;
if(i==2)
i=0;
}
std:: cin.clear();
std::cin.sync();
return 0;
}