编程环境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 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 |
#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <math.h> // practice 1 double min_rewrite(double x, double y) { return (x < y ? x : y); } void p9_1(void) { double number1 = 0.0; double number2 = 0.0; printf("Enter the first number:"); scanf_s("%lf", &number1); getchar(); printf("Enter the second number:"); scanf_s("%lf", &number2); getchar(); printf("The min number of %lf and %lf is %lf", number1, number2, min_rewrite(number1, number2)); return; } // practice 2 void chline(char *ch, int i, int j) { int m = 0; int n = 0; int tmp = 0; while ((*(ch + tmp)) != '\0') { if (m == i - 1) { break; } if ('\n' == (*(ch + tmp))) { m++; } tmp++; } for (n = 0; n < j; n++) { if (n == j - 1) { putchar(ch[tmp]); } tmp++; } } void p9_2(void) { int i = 0; int j = 0; char ch = 0; char *words = "hello world!\nThis is the bulteaful world!\nLearn to program in c language!\nc language is a nice language!"; printf("Please enter the row:"); scanf_s("%d", &i); getchar(); printf("Please enter the column:"); scanf_s("%d", &j); getchar(); printf("The character of %d row %d column is ", i, j); chline(words, i, j); printf("\n"); return; } // practice 3 void print_n_char(char ch, int repeat_time, int repeat_row) { for (int i = 0; i < repeat_row; i++) { for (int j = 0; j < repeat_time; j++) { putchar(ch); } putchar('\n'); } return; } void p9_3() { int i = 0; int j = 0; char ch = 0; printf("Enter the character you want to print:"); ch = getchar(); getchar(); printf("Enter the number of times to print:"); scanf_s("%d", &i); getchar(); printf("Enter the number of lines to print:"); scanf_s("%d", &j); getchar(); print_n_char(ch, i, j); return; } // practice 4 double harmonic_avg(double number1, double number2) { double reciprocal1 = 1 / number1; double reciprocal2 = 1 / number2; double sum = reciprocal1 + reciprocal2; double sum_avg = sum / 2; return (1 / sum_avg); } void p9_4(void) { double number1 = 0.0; double number2 = 0.0; double result = 0.0; printf("Ener the first double number:"); scanf_s("%lf", &number1); getchar(); printf("Enter the second double number:"); scanf_s("%lf", &number2); getchar(); result = harmonic_avg(number1, number2); printf("The harmonic average of %lf and %lf is %lf\n", number1, number2, result); return; } // practice 5 void larger_of(double *x, double *y) { if (*x > *y) { *y = *x; } else { *x = *y; } } void p9_5(void) { double number1 = 0.0; double number2 = 0.0; printf("Enter the first double number:"); scanf_s("%lf", &number1); getchar(); printf("Enter the second double number:"); scanf_s("%lf", &number2); getchar(); printf("Before larger_of, the two numbers are %lf and %lf\n", number1, number2); larger_of(&number1, &number2); printf("After larger_of, the two number are %lf and %lf\n", number1, number2); return; } // practice 6 void rank_three_double(double *number1, double *number2, double *number3) { double tmp = 0.0; if (*number1 > *number2) { tmp = *number1; *number1 = *number2; *number2 = tmp; } if (*number1 > *number3) { tmp = *number1; *number1 = *number3; *number3 = tmp; } if (*number2 > *number3) { tmp = *number2; *number2 = *number3; *number3 = tmp; } return; } void p9_6(void) { double number1 = 0.0; double number2 = 0.0; double number3 = 0.0; printf("Enter the first double number:"); scanf_s("%lf", &number1); getchar(); printf("Enter the second double number:"); scanf_s("%lf", &number2); getchar(); printf("Enter the third double number:"); scanf_s("%lf", &number3); getchar(); printf("Before rank, the three double number are: %lf, %lf and %lf\n", number1, number2, number3); rank_three_double(&number1, &number2, &number3); printf("After rank, the three double number are: %lf, %lf and %lf\n", number1, number2, number3); return; } // practice 7 // 不好模拟文件末尾,所以用#代替文件末尾 int judge_alpha(char ch) { if (isalpha(ch)) { return ch - 'a' + 1; } else { return -1; } } void p9_7(void) { char ch = 0; int result = 0; printf("Enter the character:(# to quit):"); while ((ch = getchar()) != '#') { if ('\n' == ch) { continue; } int jud = judge_alpha(ch); if (-1 == jud) { printf("%c is not a alpha.\n", ch); } else { printf("%c:%d\n", ch, jud); } printf("Enter the next character:(# to quit):"); } return; } // practice 8 double pow_rewrite(double base, double index) { if (index > 0) { return pow(base, index); } if (0 == index) { if (0 == base) { printf("The base number is 0 is not defined!\n"); } return 1; } if (index < 0) { double tmp = pow(base, -index); return (1 / tmp); } } void p9_8(void) { double base = 0.0; double index = 0.0; double result = 0.0; printf("Enter the base number:"); scanf_s("%lf", &base); getchar(); printf("Enter the index number:"); scanf_s("%lf", &index); getchar(); result = pow_rewrite(base, index); printf("base number: %lf, index number: %lf, the result: %lf\n", base, index, result); return; } // practice 9 double pow_recursive(double base, double index) { if (index > 0) { index--; return (base * pow_recursive(base, index)); } if (0 == index) { if (base == 0) { printf("The base number is 0 is not defined!\n"); } return 1; } if (index < 0) { index++; double tmp = 1 / base; return (tmp * pow_recursive(base, index)); } } void p9_9(void) { double base = 0.0; double index = 0.0; double result = 0.0; printf("Enter the base number:"); scanf_s("%lf", &base); getchar(); printf("Enter the index number:"); scanf_s("%lf", &index); getchar(); result = pow_recursive(base, index); printf("base number: %lf, index number: %lf, the result: %lf\n", base, index, result); return; } // practice 10 void to_base_n(unsigned long n, int base) { int r = 0; r = n % base; if (n >= base) { to_base_n(n / base, base); } putchar(('0' + r)); return; } void p9_10(void) { unsigned long number = 0; int base = 0; printf("Enter the value that needs to be converted:"); scanf_s("%ld", &number); getchar(); printf("Enter the value of the base number:"); scanf_s("%d", &base); getchar(); printf("The result is: "); to_base_n(number, base); putchar('\n'); return; } // practice 11 void Fibonacci(unsigned int n) { int fib1 = 1; int fib2 = 1; printf("%d, %d", fib1, fib2); for (size_t i = 0; (i = (fib1 + fib2)) < n;) { printf(", %d", i); fib1 = fib2; fib2 = i; } putchar('\n'); return; } void p9_11(void) { unsigned long num = 0; printf("Enter the larger number of fabonicci:"); scanf_s("%ld", &num); getchar(); Fibonacci(num); return; } int main(int argc, char **argv) { p9_11(); while (getchar()) { } return; } |
此文为博主原创文章,转载请注明出处
第八题
你看下是不是有问题?
我看不懂,我编译器结果输出不正确。
递归这玩意该怎么理解啊?
第八题我这边看这没有问题啊;递归理解可以简单理解为在一个函数里面自己调用自己,然后在合适的时候通过return来借宿递归
:exclaim:博主大大= =,能不能和我解释一下11题的代码最后int main(int argc, char **argv)这个函数的意义。感觉摸不到头脑,删掉这段结果也无误。如果能抽空解惑的话,万分感谢
呃兄弟,这是c程序的main函数啊
= =之前没弄懂 char**是什么hh。现在已经明白了hh
博主 请问一下为什么第一题要用getchar()
新手求教 万分感谢!!!
博主 请问第一题为什么要用getchar()??
新手求教 万分感谢
用来读取缓冲里面的回车键
第八题复制你的代码编译有警告,warning: control reaches end of non-void function。 位置是子函数的最后一行,是编译器的问题吗?我用的codeblock
这是因为编译器检查到pow_rewrite子函数里面并不是每个执行路径里面都有返回值,所以才报的warning,你可以将if(index > 0)这个判断语句去掉,然后将return pow(base, index)作为默认的return语句
第二题 第三题 我怎么感觉是同一个意思,楼主能解释下区别么 这两题
楼主,你这个函数有问题,运行起来不能完全显示出来,如果输入n等于6,显示前6个Fibonacci。
结果只能显示:1 1 2 3 5,因为条件fib1 + fib2 等于8 ,判别式fib1 + fib2 < n 此时n等于6已经不成立。
// practice 11
void Fibonacci(unsigned int n)
{
int fib1 = 1;
int fib2 = 1;
printf("%d, %d", fib1, fib2);
for (size_t i = 0; (i = (fib1 + fib2)) < n;)
{
printf(", %d", i);
fib1 = fib2;
fib2 = i;
}
putchar('\n');
return;
}
应改为:
void Fibonacci(unsigned int n)
{
int fib1 = 1;
int fib2 = 1;
int sum =0;
printf("%d %d", fib1, fib2);
for(int i = 2; i < n; i++)
{
sum = fib1 + fib2;
printf(" %d", sum);
fib1 = fib2;
fib2 = sum;
}
return ;
}
这样的话就能完全达到题目要求。
博主 你第九题用递归处理幂函数的递归部分,在Xcode中提示这个bug"Control may reach end of non-void function" 请问如何解决呢
博主,第七题中,如果是大写的话,题目要求返回的是跟大写一样的值,是不是应该多加一个大小写的判断?
直接return tolower(ch)-'a'+1;就可以了。
答主递归写错了
请问第11题主函数为什么要写while语句呢?