编程环境Visual Studio 2017
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
// chapter9.cpp #include <iostream> #include <string> #include <cstring> #include <cctype> #include <new> #include "sales.h" using namespace std; // practice 1 const int Len = 40; struct golf { char fullname[Len]; int handicap; }; void setgolf(golf & g, const char * name, int hc) { strcpy(g.fullname, name); g.handicap = hc; } int setgolf(golf & g) { cout << "Please enter the full name of golf player: "; cin.getline(g.fullname, Len); if (strcmp(g.fullname, "") == 0) { return 0; } cout << "Please enter the hanicap of golf player: "; cin >> g.handicap; cin.get(); return 1; } void handicap(golf & g, int hc) { g.handicap = hc; } void showgolf(const golf & g) { cout << "full name of golf player: " << g.fullname << endl; cout << "handicap of golf player: " << g.handicap << endl; } void p9_1(void) { golf g[10]; int n = 0; cout << "Enter the information of golf player: " << endl; while ((n < 10) && (setgolf(g[n]))) { n++; cout << "Next golf player: " << endl; } cout << "Show all golf player information: " << endl; for (int i = 0; i < n; i++) { showgolf(g[i]); } } // practice 2 const int ArSize = 10; void strcount(const string str) { static int total = 0; int count = 0; cout << "\"" << str.c_str() << "\" contains "; while (str[count]) { count++; } total += count; cout << count << " characters\n"; cout << total << " characters total\n"; } void p9_2(void) { string input; cout << "Enter a line:\n"; getline(cin, input); while (cin) { strcount(input); cout << "Enter next line (empty line to quit): \n"; getline(cin, input); if (input == "") { break; } } cout << "Bye\n"; return; } // practice 3 struct chaff { char dross[20]; int slag; }; char buffer[1024]; void p9_3(void) { chaff *pcha = new (buffer) chaff[2]; char *pc = new char[1024]; chaff *pcha2 = new (pc) chaff[2]; char dross[20] = { 0 }; int slag = 0; for (int i = 0; i < 2; i++) { cout << "Enter dross of #" << i << " chaff: " << endl; cin.getline(dross, 20); cout << "Enter slag of #" << i << " chaff: " << endl; cin >> slag; cin.get(); strcpy(pcha[i].dross, dross); strcpy(pcha2[i].dross, dross); pcha[i].slag = pcha2[i].slag = slag; } for (int i = 0; i < 2; i++) { cout << "staff #" << (i + 1) << ":" << endl; cout << "pcha.dross: " << pcha[i].dross << ". pcha.slag: " << pcha[i].slag << endl; cout << "pcha2.dross: " << pcha2[i].dross << ". pcha2.slag: " << pcha2[i].slag << endl; } cout << "address of buffer: " << (void *)buffer << endl; cout << "address of pcha: " << pcha << ". address of pcha[0]: " << &pcha[0] << ". address of pcha[1]: " << &pcha[1] << endl; cout << "address of pc: " << (void *)pc << endl; cout << "address of pcha2:" << pcha2 << ". address of pcha2[0]: " << &pcha2[0] << ". address of pcha2[1]: " << &pcha2[1] << endl;; delete[] pc; } // practice 4 void p9_4(void) { SALES::Sales sales1; SALES::Sales sales2; double ar[3] = { 32.1, 23.2, 65.3 }; SALES::setSales(sales1, ar, 3); SALES::setSales(sales2); SALES::showSales(sales1); SALES::showSales(sales2); } int main(int argc, char **argv) { p9_4(); while (cin.get()); return 0; } // sales.h #pragma once #include <iostream> namespace SALES { const int QUARTERS = 4; struct Sales { double sales[QUARTERS]; double average; double max; double min; }; void setSales(Sales & s, const double ar[], int n); void setSales(Sales & s); void showSales(const Sales & s); } // sales.cpp #include "sales.h" namespace SALES { using namespace std; void setSales(Sales & s, const double ar[], int n) { double total = 0.0; double max = ar[0]; double min = ar[0]; for (int i = 0; i < n; i++) { total += ar[i]; s.sales[i] = ar[i]; if (max < ar[i]) { max = ar[i]; } if (min > ar[i]) { min = ar[i]; } } for (int i = n; i < QUARTERS; i++) { s.sales[i] = 0.0; } s.average = total / n; s.max = max; s.min = min; } void setSales(Sales & s) { double total = 0.0; double max = 0.0; double min = 0.0; double input = 0.0; cout << "Enter 4 double number; " << endl; for (size_t i = 0; i < QUARTERS; i++) { cin >> input; if (i == 0) { max = input; min = input; } if (min > input) { min = input; } if (max < input) { max = input; } total += input; s.sales[i] = input; } s.average = total / QUARTERS; s.max = max; s.min = min; } void showSales(const Sales & s) { cout << "sales: "; for (size_t i = 0; i < QUARTERS; i++) { cout << s.sales[i] << " "; } cout << endl; cout << "average: " << s.average << endl; cout << "max: " << s.max << endl; cout << "min: " << s.min << endl; } } |
此文为博主原创文章,转载请注明出处