编程环境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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
#include <iostream> #include <string> #include <cctype> #include <cstring> using namespace std; // practice 1 void fun_of_p8_1(char *str, int print_times = 0) { // 这里也将print_times也打印出来,方便演示 cout << "String: " << str << " , print_times: " << print_times << endl; if (print_times > 1) { print_times--; fun_of_p8_1(str, print_times); } return; } void p8_1(void) { char str[128]; int print_times = 0; cout << "Enter the string you want to print: "; cin.getline(str, 128); cout << "Enter the times you want to print: "; cin >> print_times; // 测试传入两个参数 cout << "Test fun which has two prematers: " << endl; fun_of_p8_1(str, print_times); // 测试传入一个参数 cout << "Test fun which has one premater: " << endl; fun_of_p8_1(str); } // practice 2 struct CandyBar { char company[128]; double weight; int heat; }; void print_CandyBar(const struct CandyBar &candybar) { cout << "CandyBar company: " << candybar.company << endl; cout << "CandyBar weight: " << candybar.weight << endl; cout << "CandyBar heat: " << candybar.heat << endl; } void fill_CandyBar(struct CandyBar &candybar, char *company, double weight, int heat) { /* 如果使用strcpy遇到如下错误: error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. 1>c:\program files (x86)\windows kits\10\include\10.0.15063.0\ucrt\string.h(136): note: 参见“strcpy”的声明 可以这样子解决:右键工程名-->属性-->C/C++-->预处理器-->预处理器定义,编辑右边输入框加入:_CRT_SECURE_NO_WARNINGS */ strcpy(candybar.company, company); candybar.weight = weight; candybar.heat = heat; } void p8_2(void) { struct CandyBar candybar = { "" }; char company[128]; double weight = 0.0; int heat = 0; cout << "Enter the company of candybar: "; cin.getline(company, 128); cout << "Enter the weight of candybar: "; cin >> weight; cout << "Enter the heat of candybar: "; cin >> heat; fill_CandyBar(candybar, company, weight, heat); print_CandyBar(candybar); return; } // practice 3 void p8_3(void) { string input; cout << "Enter a string (q to quit): "; getline(cin, input); while (input[0] != 'q') { for (size_t i = 0; i < input.length(); i++) { input[i] = toupper(input[i]); } cout << input << endl; cout << "Next string (q to quit): "; getline(cin, input); } } // practice 4 struct stringy { char *str; int ct; }; void set(struct stringy &in_stringy, char * in_string) { int string_length = strlen(in_string); in_stringy.str = new char(string_length + 1); strcpy(in_stringy.str, in_string); in_stringy.ct = string_length; } void show(const struct stringy &in_stringy, int print_times = 1) { for (int i = 0; i < print_times; i++) { cout << "member string of struct stringy: " << in_stringy.str << endl; } } void show(const char * str, int print_times = 1) { for (int i = 0; i < print_times; i++) { cout << "Print char string: " << str << endl; } } int p8_4(void) { stringy beany; char testing[] = "Reality isn't what it used to be."; set(beany, testing); show(beany); show(beany, 2); testing[0] = 'D'; testing[1] = 'u'; show(testing); show(testing, 3); show("Done!"); return 0; } // practice 5 template <typename T> T max5(T in_array[]) { T max = in_array[0]; for (size_t i = 0; i < 5; i++) { if (max < in_array[i]) { max = in_array[i]; } } return max; } void p8_5(void) { int int_array[5] = {32, 43, 66, 23, 54}; double double_array[5] = { 32.4, 33.3, 61.3, 646.3, 23.5 }; int int_array_max = max5(int_array); double double_array_max = max5(double_array); cout << "max of int array: " << int_array_max << endl; cout << "max of double array: " << double_array_max << endl; } // practice 6 template <typename T> T maxn(T in_array[], int array_size) { T max = in_array[0]; for (int i = 0; i < array_size; i++) { if (max < in_array[i]) { max = in_array[i]; } } return max; } // 显示具体化 template <> const char * maxn(const char *in_str[], int n) { const char * str = in_str[0]; for (int i = 0; i < n; i++) { if (strlen(str) < strlen(in_str[i])) { str = in_str[i]; } } return str; } void p8_6(void) { int int_array[6] = { 43, 235, 54, 232, 123, 65 }; double double_array[4] = { 32.1, 453.2, 53.3, 67.4 }; const char * str_array[5] = { "Hello Jimmy!", "Hello World!", "ABCDEFG,HIJKLMN", "Today is a goood day!", "C++ Primer Plus!" }; int int_max = maxn(int_array, 6); double double_max = maxn(double_array, 4); const char * length_max_str = maxn(str_array, 5); cout << "max of int array: " << int_max << endl; cout << "max of double array: " << double_max << endl; cout << "max length string of string array: " << length_max_str << endl; } // practice 7 template <typename T> T SumArray(T arr[], int n); template <typename T> T SumArray(T * arr[], int n); struct debts { char name[50]; double amount; }; int p8_7(void) { int thing[6] = { 13, 31, 103, 301, 310, 130 }; int int_sum = 0; struct debts mr_E[3] = { {"Ima Wolfe", 2400.0}, {"Ura Foxe", 1300.0}, {"Iby Stout", 1800.0} }; double *pd[3]; double double_sum = 0.0; for (size_t i = 0; i < 3; i++) { pd[i] = &mr_E[i].amount; } int_sum = SumArray(thing, 6); double_sum = SumArray(pd, 3); cout << "Sum of int array: " << int_sum << endl; cout << "Sum of double* array: " << double_sum << endl; return 0; } template <typename T> T SumArray(T arr[], int n) { // 下面的sum初始化,在便宜的时候会有warning提示,有个简单的方法可以解决,那就是将sum初始化为arr的第一个元素,然后for循环从i=1开始 T sum = 0.0; for (int i = 0; i < n; i++) { sum += arr[i]; } return sum; } template <typename T> T SumArray(T * arr[], int n) { T sum = 0.0; for (int i = 0; i < n; i++) { sum += *(arr[i]); } return sum; } // main int main(int argc, char **argv) { p8_7(); while (cin.get()); } |
此文为博主原创文章,转载请注明出处
第六题有问题,char* 编译器不通过 const char*可以
谢谢告知,已修改
第一题有问题,应为print_times > 0,因为题目要求第二个参数不为0时,打印字符串的次数要为函数被调用的次数,而非第二个参数的值
书不在身边,所以没办法确认。不过按照你这样说的话,print_times>1没有错啊,print_times=1的时候就调用fun_of_p8_1一次,print_times=2就调用两次......以此类推
不是啊,你这样是利用迭代实现了打印指定次数,而不是打印函数调用次数。
附上题目:
1.编写通常接受一个参数(字符串的地址),并打印该字符串的函数。然而,如果提供了第二个参数(int类型),且该参数不为0,则该函数打印字符串的次数将为该函数被调用的次数(注意,字符串的打印次数不等于第二个参数的值,而等于函数被调用的次数)。是的,这是一个非常可笑的函数,但它让您能够使用本章介绍的一些技术。在一个简单的程序中使用该函数,以演示该函数是如何工作的。
第三题,while (input[0] != 'q'),如果我输入的单词第一个字母为q的话不是不可以转化为小写嘛。
emmm……你是对的,我没考虑到这种情况