1
#include <iostream> double num(double x, double y); int main() { using namespace std; double n, m; cout << "请输入2个数[0退出]: "; while ((cin >> n >> m) && (n != 0 && m != 0)) { cout << n << " " << m << " 调和平均数: " << num(n, m) << endl; cout << "请输入2个数[0退出]: "; } system("pause"); return 0; } double num(double x, double y) { return (2.0*x*y / (x + y)); }
2
#include <iostream> using namespace std; int innum(double ar[], int n); void show(double ar[], int n); void average(double ar[], int n); int main() { const int size = 10; double num[size]; int n = innum(num, size); show(num, n); average(num, n); system("pause"); return 0; } int innum(double ar[], int n) { int i; for (i = 0; i < n; i++) { cout << "请输入第 " << i + 1 << " 个高尔夫成绩[q退出]: "; double num; if (!(cin >> num)) break; ar[i] = num; } return i; } void show(double ar[], int n) { cout << "****高尔夫成绩****\n"; for (int i = 0; i < n; i++) cout << i + 1 << " : " << ar[i] << endl; } void average(double ar[], int n) { double tot = 0; for (int i = 0; i < n; i++) tot += ar[i]; cout << "平均成绩: " << tot / n << endl; }
3
#include <iostream> using namespace std; struct box { char maker[40]; float height; float width; float length; float volume; }; void show(box pst); void compute(box *pst); int main() { box Box = { "索尼 Ps4",39,288,265 }; cout.precision(20); compute(&Box); show(Box); system("pause"); return 0; } void compute(box *pst) { pst->volume = pst->height*pst->width*pst->length; } void show(box pst) { cout << pst.maker << " " << pst.length << " " << pst.width << " " << pst.height << " " << pst.volume << endl; }
4
#include <iostream> long double odds(int n, int m); int main() { using namespace std; long double A_chance, B_chance; A_chance = odds(47, 5); B_chance = odds(27, 1); cout << "中奖几率: " << A_chance * B_chance << endl; cout << "完成.\n"; system("pause"); return 0; } long double odds(int n, int m) { long double chance = 1.0; double i, j; for (i = n, j = m; j > 0; i--, j--) chance *= i / j; return chance; }
5
#include <iostream> using namespace std; long long factorial(long long n); int main() { long long n; while ((cin >> n) && (n > 0)) cout << n << "! = " << factorial(n) << endl; system("pause"); return 0; } long long factorial(long long n) { if (n == 0) return 1; return (n * factorial(n - 1)); }
6
#include <iostream> #include <array> #define SIZE 10 using namespace std; int Fill_array(double ar[], int n); void Show_array(const double ar[], int n); void Reverse_array(double ar[], int n); int main() { double num[SIZE]; int count; count = Fill_array(num, SIZE); Show_array(num, count); Reverse_array(num, count); Show_array(num, count); cout.put('\n'); system("pause"); return 0; } int Fill_array(double ar[], int n) { double temp; int count = 0; for (int i = 0; i < n; i++) { cout << "请输入第 " << i + 1 << " 个数: "; if (!(cin >> temp)) break; ar[i] = temp; count++; } return count; } void Show_array(const double ar[], int n) { cout << "\n*****显示内容*****\n"; for (int i = 0; i < n; i++) { cout << ar[i] << " "; } } void Reverse_array(double ar[], int n) { double temp; for (int i = 1; i < (n / 2); i++) { temp = ar[i]; ar[i] = ar[n - 1 - i]; ar[n - 1 - i] = temp; } }
7
#include <iostream> const int Max = 5; double *fill_array(double *ar, double *end); void show_array(const double *ar, double *end); void revalue(double r, double *ar, double *end); int main() { using namespace std; double properties[Max]; double *End = fill_array(properties, properties + Max); show_array(properties, End); if (End != properties) { cout << "输入重估系数: "; double factor; while (!(cin >> factor)) { cin.clear(); while (cin.get() != '\n') continue; cout << "输入错误, 请输入一个数字: "; } revalue(factor, properties, End); show_array(properties, End); } cout << "完成.\n"; system("pause"); return 0; } double *fill_array(double *ar, double *end) { using namespace std; double temp; double *fp = ar; int i = 1; while (ar != end) { cout << "输入值 #" << i++ << ": "; cin >> temp; if (!cin) { cin.clear(); while (cin.get() != '\n') continue; cout << "输入错误, 请输入一个数字: \n"; break; } else if (temp < 0) break; *ar++ = temp; fp = ar; } return fp; } void show_array(const double *ar, double *end) { using namespace std; int i = 1; while (ar!=end) { cout << "财产 #" << i++ << ": $"; cout << *ar++ << endl; } } void revalue(double r, double *ar, double *end) { while (ar != end) *ar++ *= r; }
8
a
#include <iostream> #include <string> const int Seasons = 4; const char *Snames[] = { "春季", "夏季", "秋季", "冬季" }; void fill(double *ar, int n); void show(const double *ar, int n); int main() { using namespace std; double expenses[Seasons]; fill(expenses, Seasons); show(expenses, Seasons); system("pause"); return 0; } void fill(double *ar, int n) { for (int i = 0; i < n; i++) { std::cout << "输入 " << Snames[i] << " 花费: "; std::cin >> ar[i]; } } void show(const double *ar, int n) { double total = 0.0; std::cout << "\n花费\n"; for (int i = 0; i < n; i++) { std::cout << Snames[i] << ": $" << ar[i] << '\n'; total += ar[i]; } std::cout << "总计: $" << total << '\n'; }
b
#include <iostream> #include <string> const int Seasons = 4; const char *Snames[] = { "春季", "夏季", "秋季", "冬季" }; struct expenses { double Expenses; }; void Fill(expenses *pst, int n); void show(expenses *pst, int n); int main() { using namespace std; expenses list[Seasons]; Fill(list, Seasons); show(list, Seasons); system("pause"); return 0; } void Fill(expenses *pst, int n) { for (int i = 0; i < n; i++) { std::cout << "输入 " << Snames[i] << " 花费: "; std::cin >> pst[i].Expenses; } } void show(expenses *pst, int n) { double total = 0.0; std::cout << "\n花费\n"; for (int i = 0; i < n; i++) { std::cout << Snames[i] << ": $" << pst[i].Expenses << '\n'; total += pst[i].Expenses; } std::cout << "总计: $" << total << '\n'; }