第1题
#include <iostream>
using namespace std;
double harmean_result(double x, double y);
int main()
{
cout << "Input two numbers (0 to quit): ";
double x, y;
while (cin >> x >> y && x != 0 && y != 0)
{
cout << "Harmean is " << harmean_result(x, y) << endl;
cout << "Input two number: ";
}
cout << "Done!\n";
}
double harmean_result(double x,double y)
{
//double harmean;
//harmean = 2.0 * x * y / (x + y);
//return harmean;
return 2.0 * x * y / (x + y);
}
第2题
#include <iostream>
const int Max = 10;
using namespace std;
int fill_array(double ar[], int limit);
void compute_average(const double ar[], int n);
void show_array(const double ar[], int n);
int main()
{
double scores[Max];
cout << "Input golf scores (q to quit): \n";
int size = fill_array(scores, Max);
if (size > 0)
{
show_array(scores, size);
compute_average(scores, size);
}
else
{
cout << "There is no golf score.\n";
}
}
int fill_array(double ar[], int limit)
{
double temp;
int i = 0;
while (i < limit)
{
cout << "Enter value #" << i + 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)
{
cout << "Bad input. input process terminated.\n";
break;
}
ar[i] = temp;
i++;
}
return i;
}
void compute_average(const double ar[], int n)
{
double sum = 0, average;
for (int i = 0; i < n; i++)
{
sum += ar[i];
}
average = sum / n;
cout << "Average is : " << average << endl;
}
void show_array(const double ar[], int n)
{
for (int i = 0; i < n; i++)
{
cout << "score #" << i + 1 << ": " << ar[i] << " ";
}
cout << endl;
}
第3题
#include <iostream>
using namespace std;
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
void show_PassByValue(box box1);
void set_volume(box* box2);
int main()
{
box box0 = { "andy",2,3,4 };
show_PassByValue(box0);
set_volume(&box0);
cout << "\nAfter set volume,value change:\n\n";
show_PassByValue(box0);
}
void show_PassByValue(box box1)
{
cout << "Maker: " << box1.maker << endl;
cout << "Height: " << box1.height << endl;
cout << "Width: " << box1.width << endl;
cout << "Length: " << box1.length << endl;
cout << "Volume: " << box1.volume << endl;
}
void set_volume(box* box2)
{
box2->volume = box2->height * box2->width * box2->length;
}
第4题
#include <iostream>
using namespace std;
long double probability(unsigned numbers, unsigned picks);
int main()
{
double total1, total2, choice1, choice2;
long double probability_total, probability1, probability2;
cout << "Enter #1 total number of choices on the game card and the number of picks allowed:\n";
cin >> total1 >> choice1;
cout << "Enter #2 total number of choices on the game card and the number of picks allowed:\n";
cin >> total2 >> choice2;
probability1 = probability(total1, choice1);
probability2 = probability(total2, choice2);
probability_total = probability1 * probability2;
cout << fixed;
cout << "You have one chance in " << probability_total << " of winning.\n";
}
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;
int Factorial(int n);
int main()
{
int x;
cout << "Input a number: ";
while (cin >> x)
{
if (x < 0)
{
cout << "Negative number. \n";
}
else
{
cout << "Factorial is: " << Factorial(x) << endl;
}
cout << "Please input a new number(q to quit): ";
}
cout << "Done.\n";
}
int Factorial(int n)
{
int factorial;
if (n == 0)
{
factorial = 1;
}
else
{
factorial = n * Factorial(n - 1);
}
return factorial;
}
第6题
#include <iostream>
using namespace std;
const int Max = 6;
int Fill_array(double ar[], int l);
void Reverse_array1(double ar[], int l);
void Reverse_array2(double ar[], int l); //Reverse except fist and last number
void Show_array(const double ar[], int l);
int main()
{
double arr[Max];
int size = Fill_array(arr, Max);
Show_array(arr, size);
cout << "Reverse all:\n";
Reverse_array1(arr, size);
Show_array(arr, size);
cout << "Reverse except fist and last number:\n";
Reverse_array2(arr, size);
Show_array(arr, size);
}
int Fill_array(double ar[], int l)
{
int i = 0, count = 0;
cout << "Input value #1: ";
while (i < l && cin >> ar[i])
{
count++;
i++;
if (i < Max)
{
cout << "Input value #" << i + 1 << ": ";
}
}
cout << "Done.\n";
return count;
}
//Reverse all
void Reverse_array1(double ar[], int l)
{
for (int Min = 0, Max = l-1; Min < Max; Min++, Max--)
{
double temp;
temp = ar[Min];
ar[Min] = ar[Max];
ar[Max] = temp;
}
}
//Reverse except fist and last number
void Reverse_array2(double ar[], int l)
{
for (int Min = 1, Max = l - 2; Min < Max; Min++, Max--)
{
double temp;
temp = ar[Min];
ar[Min] = ar[Max];
ar[Max] = temp;
}
}
void Show_array(const double ar[], int l)
{
for (int i = 0; i < l; i++)
{
cout << "#" << i + 1 << ": " << ar[i] << endl;
}
}
第7题
#include <iostream>
using namespace std;
const int Max = 5;
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* pt = fill_array(properties, properties + Max);
show_array(properties, pt);
if (properties != pt)
{
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, pt);
show_array(properties, pt);
}
cout << "Done.\n";
}
double* fill_array(double* begin, double* end)
{
double* pt;
int i = 1;
for (pt = begin; pt != end; pt++, i++)
{
cout << "Enter value #" << i << ": ";
cin >> *pt;
if (!cin)
{
cin.clear();
while (cin.get() != '\n')
{
continue;
}
cout << "Bad input; input process terminated.\n";
break;
}
else if (*pt < 0)
{
break;
}
}
return pt;
}
void show_array(double* begin, double* end)
{
double* pt;
int i = 1;
for (pt = begin; pt != end; pt++, i++)
{
cout << "Property #" << i << ": $" << *pt << endl;
}
}
void revalue(double r, double* begin, double* end)
{
double* pt;
for (pt = begin; pt != end; pt++)
{
*pt *= r;
}
}
第8题
a
#include <iostream>
using namespace std;
const int Seasons = 4;
const char* Snames[Seasons] = { "Spring","Summer","Fall","Winter" };
void fill(double expenses[]);
void show(double expenses[]);
int main()
{
double expenses[Seasons];
fill(expenses);
show(expenses);
}
void fill(double expenses[])
{
for (int i = 0; i < Seasons; i++)
{
cout << "Enter " << Snames[i] << " expenses: ";
cin >> expenses[i];
}
}
void show(double expenses[])
{
double total = 0.0;
cout << "\nEXPENSES\n";
for (int i = 0; i < Seasons; i++)
{
cout << Snames[i] << ": $" << expenses[i] << endl;
total += expenses[i];
}
cout << "Total Expenses: $" << total << endl;
}
b
#include <iostream>
using namespace std;
const int Seasons = 4;
const char* Snames[Seasons] = { "Spring","Summer","Fall","Winter" };
struct expense
{
double value[Seasons];
};
void fill(expense* e);
void show(const expense* s);
int main()
{
expense expenses;
fill(&expenses);
show(&expenses);
}
void fill(expense* e)
{
for (int i = 0; i < Seasons; i++)
{
cout << "Enter " << Snames[i] << " expenses: ";
cin >> e->value[i];
}
}
void show(const expense *s)
{
double total = 0.0;
cout << "\nEXPENSES\n";
for (int i = 0; i < Seasons; i++)
{
cout << Snames[i] << ": $" << s->value[i] << endl;
total += s->value[i];
}
cout << "Total Expenses: $" << total << endl;
}
第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 ps[], int n);
int main()
{
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);
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";
}
int getinfo(student pa[], int n)
{
int i;
for (i = 0; i < n; i++)
{
cout << "Student #" << i + 1 << ":\n";
cout << "Fullname: ";
cin.getline(pa[i].fullname, SLEN);
if (pa[i].fullname[0] == '\0')
{
break;
}
cout << "Hobby: ";
cin.getline(pa[i].hobby, SLEN);
cout << "Ooplevel: ";
cin >> pa[i].ooplevel;
cin.get();
}
return i;
}
void display1(student st)
{
cout << "#Display1 Fullname: " << st.fullname << "\t Hobby: " << st.hobby << "\t Ooplevel: " << st.ooplevel << endl;
}
void display2(const student* ps)
{
cout << "#Display2 Fullname: " << ps->fullname << "\t Hobby: " << ps->hobby << "\t Ooplevel: " << ps->ooplevel << endl;
}
void display3(const student ps[], int n)
{
cout << "***** Display3 Begin *****\n";
for (int i = 0; i < n; i++)
{
cout << "Student #" << i + 1 << ":\n";
cout << "Fullname: " << ps[i].fullname << endl;
cout << "Hobby: " << ps[i].hobby << endl;
cout << "Ooplevel: " << ps[i].ooplevel << endl;
}
cout << "***** Display3 End *****\n";
}
第10题
普通:
#include <iostream>
using namespace std;
double add(double x, double y);
double subtract(double x, double y);
double calculate(double a, double b, double (*pf)(double, double));
int main()
{
double x, y;
cout << "Enter two number(x y): ";
while (cin >> x >> y)
{
cout << "Add: " << x << " + " << y << " = " << calculate(x, y, add) << endl;
cout << "Subtract:" << x << " - " << y << " = " << calculate(x, y, subtract) << endl;
cout << "Enter two number(x y): ";
}
}
double add(double x, double y)
{
return x + y;
}
double subtract(double x, double y)
{
return x - y;
}
double calculate(double a, double b, double (*pf)(double, double))
{
return (*pf)(a, b);
}
指针数组:
#include <iostream>
using namespace std;
double add(double x, double y);
double subtract(double x, double y);
double multiply(double x, double y);
double divide(double x, double y);
double calculate(double a, double b, double (*pf)(double, double));
int main()
{
double x, y;
double (*pf[4])(double, double) = { add,subtract,multiply,divide };
cout << "Enter two number(x y): ";
while (cin >> x >> y)
{
for (int i = 0; i < 4; i++)
{
cout << "Answer is " << calculate(x, y, *pf[i]) << endl;
}
cout << "Enter two number(x y): ";
}
}
double add(double x, double y)
{
return x + y;
}
double subtract(double x, double y)
{
return x - y;
}
double multiply(double x, double y)
{
return x * y;
}
double divide(double x, double y)
{
return x / y;
}
double calculate(double a, double b, double (*pf)(double, double))
{
return (*pf)(a, b);
}