1、
#include <iostream>
using namespace std;
double Average(int x, int y);
int main()
{
double result, one, two;
cout << "Please enter two numbers:";
while (cin >> one >> two)
{
if(!one || !two)
break;
result = Average(one, two);
cout << "result = " << result << endl;
cout << "Please enter two numbers:";
}
return 0;
}
double Average(int x, int y)
{
return 2.0*x*y/(x+y);
}
2、
#include <iostream>
using namespace std;
int input(double *p, int size);
void show(double *p, int size);
double calculate(double *p, int size);
int main()
{
double data[10] = {0.0};
int num = 0;
double ave = 0;
num = input(data, 10);
show(data, num);
ave = calculate(data, num);
cout << "Average = " << ave << endl;
return 0;
}
int input(double *p, int size)
{
int count = 0;
cout << "Please enter max 10 numbers(q to quit):";
while(cin >> p[count++])
{
if(count == size)
return count;
}
return count-1;
}
void show(double *p, int size)
{
for(int i=0; i<size; i++)
cout << p[i] << " ";
}
double calculate(double *p, int size)
{
double sum = 0;
for(int i=0; i<size; i++)
{
sum += p[i];
}
return sum/size;
}
3、
#include <iostream>
using namespace std;
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
void show(struct box tmp);
void show_point(struct box *p);
int main()
{
struct box bb = {"BBB", 2.0, 3.0, 4.0, 10.0};
show(bb);
show_point(&bb);
show(bb);
return 0;
}
void show(struct box tmp)
{
cout << "maker=" << tmp.maker
<< ", height=" << tmp.height
<< ", width=" << tmp.width
<< ", length=" << tmp.length
<< ", volume=" << tmp.volume
<< endl;
}
void show_point(struct box *p)
{
p->volume = p->height*p->width*p->length;
}
4、
#include <iostream>
using namespace std;
long double probability(unsigned numbers, unsigned picks);
int main()
{
long double field = probability(47, 5);
long double special = probability(27, 1);
cout << "probability = " << field*special << endl;
return 0;
}
long double probability(unsigned numbers, unsigned picks)
{
long double result = 1.0;
long double n;
unsigned p;
for(n=numbers,p=picks; p>0; n--,p--)
{
result = result*n/p;
}
return result;
}
5、
#include <iostream>
using namespace std;
long long jiecheng(long n);
int main()
{
long data;
while(cin >> data)
{
cout << data << "! = " << jiecheng(data) << endl;
}
return 0;
}
long long jiecheng(long n)
{
if(n == 0)
return 1;
return n*jiecheng(n-1);
}
6、
#include <iostream>
using namespace std;
int Fill_array(double *arr, int size);
void Show_array(double *arr, int size);
void Reverse_array(double *arr, int size);
int main()
{
double arr[10] = {0.0};
int num = Fill_array(arr, 10);
Show_array(arr, num);
Reverse_array(arr, num);
Show_array(arr, num);
return 0;
}
int Fill_array(double *arr, int size)
{
int count = 0;
cout << "请输入double数(最多10个):";
while(cin >> arr[count])
{
count++;
if(count == size)
break;
}
return count;
}
void Show_array(double *arr, int size)
{
cout << "arr is: ";
for(int i=0; i<size; i++)
{
cout << arr[i] << " ";
if(i == size-1)
cout << endl;
}
}
void Reverse_array(double *arr, int size)
{
double * first = &arr[0];
double * last = &arr[size-1];
double tmp = 0;
while(first != last)
{
tmp = *first;
*first = *last;
*last = tmp;
first++;
last--;
}
}
7、
#include <iostream>
using namespace std;
const int Max = 5;
double * fill_array(double *begin, double *end);
void show_array(const double *begin, const double *end);
void revalue(double r, double *begin, double *end);
int main()
{
double properties[Max];
double *point = fill_array(properties, properties+Max);
show_array(properties, point);
if(point != properties)
{
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, point);
show_array(properties, point);
}
cout << "Done.\n";
cin.get();
cin.get();
return 0;
}
double *fill_array(double *begin, double *end)
{
double temp, *p;
int i=0;
p = begin;
while(p != end)
{
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;
*p = temp;
p++;
i++;
}
return p;
}
void show_array(const double *begin, const double *end)
{
const double *p = begin;
int i = 0;
while(p!=end)
{
cout << "Property #" << (i+1) << ": $";
cout << *p << endl;
p++;
i++;
}
}
void revalue(double r, double *begin, double *end)
{
double *p = begin;
while(p!=end)
{
*p *= r;
p++;
}
}
8、
#include <iostream>
using namespace std;
struct expense
{
double a[4];
};
const int Seasons = 4;
const char * Snames[4] = {"Spring", "Summer", "Fall", "Winter"};
void fill(double *arr);
void show(double *arr);
void fill_struct(struct expense *tmp);
void show_struct(struct expense *tmp);
int main()
{
double data[Seasons] = {0.0};
struct expense e;
fill(data);
show(data);
fill_struct(&e);
show_struct(&e);
return 0;
}
void fill(double *arr)
{
for(int i=0; i<Seasons; i++)
{
cout << "Enter " << Snames[i] << " expenses: ";
cin >> arr[i];
}
}
void show(double *arr)
{
double total = 0.0;
cout << "\nEXPENSES\n";
for(int i=0; i<Seasons; i++)
{
cout << Snames[i] << ": $" << arr[i] << endl;
total += arr[i];
}
cout << "Total Expenses: $" << total << endl;
}
void fill_struct(expense *tmp)
{
for(int i=0; i<Seasons; i++)
{
cout << "Enter " << Snames[i] << " expenses: ";
cin >> tmp->a[i];
}
}
void show_struct(expense *tmp)
{
double total = 0.0;
cout << "\nEXPENSES\n";
for(int i=0; i<Seasons; i++)
{
cout << Snames[i] << ": $" << tmp->a[i] << endl;
total += tmp->a[i];
}
cout << "Total Expenses: $" << total << endl;
}
9、
#include <iostream>
#include <cstring>
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()
{
student arr[3];
int size = getinfo(arr, 3);
display1(arr[1]);
display2(&arr[2]);
display3(arr, size);
return 0;
}
int getinfo(student pa[], int n)
{
int i=0;
while(i<n)
{
cout << "Enter #" << (i+1) << " fullname: ";
cin.getline(pa[i].fullname, SLEN);
if(pa[i].fullname[0] == '\0')
break;
cout << "Enter #" << (i+1) << " hobby: ";
cin.getline(pa[i].hobby, SLEN);
cout << "Enter #" << (i+1) << " ooplevel: ";
cin >> pa[i].ooplevel;
cin.get();
i++;
}
return i;
}
void display1(student st)
{
cout << "Student info: ("
<< st.fullname << ","
<< st.hobby << ","
<< st.ooplevel << ")" << endl;
}
void display2(const student *ps)
{
cout << "Student info: ("
<< ps->fullname << ","
<< ps->hobby << ","
<< ps->ooplevel << ")" << endl;
}
void display3(const student pa[], int n)
{
for(int i=0; i<n; i++)
{
cout << "student " << (i+1) << " fullname: " << pa[i].fullname << endl;
cout << "student " << (i+1) << " hobby: " << pa[i].hobby << endl;
cout << "student " << (i+1) << " ooplevel: " << pa[i].ooplevel << endl;
}
}
10、
#include <iostream>
using namespace std;
double add(double x, double y);
double minu(double x, double y);
double multi(double x, double y);
double divide(double x, double y);
double calculate(double x, double y, double (*ptr)(double, double));
int main()
{
double d1, d2;
double (*pf[3])(double, double) = {add, minu, divide};
cout << "Please enter two double numbers: " ;
while(cin >> d1 >> d2)
{
double q = calculate(d1, d2, add);
cout << "Add result is " << q << endl;
double q2 = calculate(d1, d2, minu);
cout << "Minus result is " << q2 << endl;
for(int i=0; i<3; i++)
{
double res = (*pf[i])(d1, d2);
cout << "Res is " << res << endl;
}
cout << "Please enter two double numbers: " ;
}
return 0;
}
double add(double x, double y)
{
return x+y;
}
double minu(double x, double y)
{
return x-y;
}
double multi(double x, double y)
{
return x*y;
}
double divide(double x, double y)
{
if(y==0)
{
cout << "y cannot be 0!" << endl;
return -1;
}
return x/y;
}
double calculate(double x, double y, double (*ptr)(double, double))
{
return (*ptr)(x, y);
}