1.
#includeusing namespace std; double average(double a, double b) { return (2.0 * a * b / (a + b)); } int main() { double x, y; while (cin >> x >> y) { if (x == 0 || y == 0) { cout << "end"; break; } else { cout << average(x, y); } } return 0; }
2.
#include#include using namespace std; int input(double a[]) { cout << "please input grades(most 10)(q to quit):"; int i; double c; for ( i = 0; i < 10; i++) { cin >> c; if (!cin) { cin.clear(); while (cin.get() != 'n')//读取无用输入 { continue; } break; } a[i] = c; } return i; } void show(double b[],int n) { for (int i = 0; i 3.
#include#include using namespace std; struct box { char maker[40]; float height; float width; float length; float volume; }; void show1(box a) { cout << a.maker< volume = b->height * b->width * b->length; } int main() { box c = { "zhanglincong", 1.2, 1.2, 1.2 }; shou2(&c); show1(c); return 0; } 4.
#include#include using namespace std; const int Num1 = 47; const int Num2 = 27; const int Bingo1 = 5; const int Bingo2 = 1; double cal(int num1, int num2, int bingo1, int bingo2) { double pro, pro1, pro2; pro1 = double(bingo1) / double(num1); pro2 = double(bingo2) / double(num2); pro = pro1 * pro2; return pro; } int main() { double a; a = cal(Num1, Num2, Bingo1, Bingo2); cout << a; return 0; } 5.
#includeusing namespace std; int a(int n) { if (n == 0) return 1; if (n == 1) return 1; return a(n - 1) * n; } int main() { int b; cin >> b; cout << a(b); return 0; } 6.
#include#include using namespace std; int fill_array(double a[], int n) { cout << "input double :"; int count = 0; for (int i = 0; i < n&&cin >> a[i]; i++) count++; return count; } void show_array(double a[], int n) { for (int i = 0; i < n; i++) { cout << a[i] << ' '; } cout << endl; } void reverse_array(double a[], int n) { double temp; for (int i = 0; i < n / 2; i++) { temp = a[i]; a[i] = a[n-1-i]; a[n - 1-i] = temp; } } int main() { const int size = 5; double a[size]; int n; n=fill_array(a, size); show_array(a, n); reverse_array(a, n); show_array(a, n); return 0; } 7.
#includeconst int MAX = 5; using namespace std; double* 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[i] = temp; } return (&ar[i]); } void show_array(const double ar[], double *n) { for (int i = 0; ar+i!=n; i++) { cout << "Property #" << (i + 1) << ": $"; cout << ar[i] << endl; } } void revalue(double r, double ar[], double* n) { for (int i = 0; ar + i != n; i++) { ar[i] *= r; } } int main() { double properties[MAX]; double *n = fill_array(properties, MAX); show_array(properties, n); if (properties[0] > 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, n); show_array(properties, n); } cout << "Done.n"; cin.get(); cin.get(); return 0; } 8.
#includeusing namespace std; const int seasons = 4; const char* sname[seasons] = { "spring","summer","fall","winter" }; void fill(double *pa) { for (int i = 0; i < seasons; i++) { cout << "Enter " << sname[i] << " expenses:"; cin >> pa[i]; } } void show(double* da) { double total = 0.0; cout << "nEXPENSESn"; for (int i = 0; i < seasons; i++) { cout << sname[i] << ": $" << da[i] << endl; total += da[i]; } cout << "Total Expenses: $" << total << endl; } int main() { double expenses[seasons]; fill(expenses); show(expenses); return 0; } 9.
#includeusing namespace std; const int SLEN = 30; struct student { char fullname[SLEN]; char hobby[SLEN]; int ooplevel; }; int getinfo(student pa[], int n) { int count = 0; for (int i = 0; i < n; i++) { cout << "Enter fullname: "; cin.getline(pa[i].fullname, SLEN); cout << "Enter hobby: "; cin >> pa[i].hobby; cout << "Enter ooplevel: "; cin >> pa[i].ooplevel; cin.get(); count++; } cout << "over!"; return count; } void display1(student st) { cout << "Fullname: " << st.fullname << "n"; cout << "Hobby: " << st.hobby << "n"; cout << "Ooplevel: " << st.ooplevel << "n"; } void display2(const student* ps) { cout << "Fullname: " << ps->fullname << "n"; cout << "Hobby: " << ps->hobby << "n"; cout << "Ooplevel: " << ps->ooplevel << "n"; } void display3(const student pa[], int n) { for (int i = 0; i < n; i++) { cout << "Fullname: " << pa[i].fullname << "n"; cout << "Hobby: " << pa[i].hobby << "n"; cout << "Ooplevel: " << pa[i].ooplevel << "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 << "Donen"; return 0; } 10.
#includeusing namespace std; double calculate(double a, double b, double (*c)(double x, double y)) { return (*c)(a, b); } double add(double x, double y) { return x + y; } int main() { double a, b; while (cin >> a && cin >> b) { cout<



