1
#include <stdio.h> void critic(int *n); int main() { int units; printf("多少磅重的黄油?\n"); scanf_s("%d", &units); while (units != 56) critic(&units); printf("你一定看过它!\n"); system("pause"); return 0; } void critic(int *n) { printf("没有运气, 我的朋友. 再试一次.\n"); scanf_s("%d", n); }
2
/* pe12-2a.h */ #include <stdio.h> void set_mode(int n); void get_info(void); void show_info(void);
/* pe12-2a.c */ #include "pe12-2a.h" static int s_mode = 0; static double distance = 0; static double petrol = 0; void set_mode(int n) { char s1_mode[2][5] = { "公制","US" }; switch (n) { case 0: s_mode = 0; break; case 1: s_mode = 1; break; default: printf("指定的模式无效, 使用上次输入模式 %d(%s).\n", s_mode, s1_mode[s_mode]); break; } } void get_info(void) { if (s_mode == 0) { printf("输入行驶的距离[公里]: "); scanf_s("%lf", &distance); while (distance != 1 && distance <= 0) { while (getchar() != '\n') continue; printf("输入错误, 输入行驶的距离[公里]: "); scanf_s("%lf", &distance); } printf("输入耗油量[升]: "); scanf_s("%lf", &petrol); while (petrol != 1 && petrol <= 0) { while (getchar() != '\n') continue; printf("输入错误, 输入耗油量[升]: "); scanf_s("%lf", &petrol); } } else { printf("输入行驶的距离[英里]: "); scanf_s("%lf", &distance); while (distance != 1 && distance <= 0) { while (getchar() != '\n') continue; printf("输入错误, 输入行驶的距离[英里]: "); scanf_s("%lf", &distance); } printf("输入耗油量[加仑]: "); scanf_s("%lf", &petrol); while (petrol != 1 && petrol <= 0) { while (getchar() != '\n') continue; printf("输入错误, 输入耗油量[加仑]: "); scanf_s("%lf", &petrol); } } } void show_info(void) { double total; if (s_mode == 0) { total = petrol / distance * 100; printf("燃油消耗量为每百公里 %.2f 升.\n", total); } else { total = distance / petrol; printf("燃油消耗是每加仑 %.2f 英里.\n", total); } }
/* pe12-2b.c */ #include <stdio.h> #include "pe12-2a.h" int main() { int mode; printf("对于公制模式输入0, 对于US模式输入1: "); scanf_s("%d", &mode); while (mode >= 0) { set_mode(mode); get_info(); show_info(); printf("对于公制模式输入0, 对于US模式输入1 [-1退出]: "); scanf_s("%d", &mode); } printf("完成.\n"); system("pause"); return 0; }
3
/* pe12-2a.h */ #include <stdio.h> void set_mode(int n, int *s_mode); void get_info(int s_mode, double *distance, double *petrol); void show_info(int s_mode, double distance, double petrol);
/* pe12-2a.c */ #include "pe12-2a.h" void set_mode(int n, int *s_mode) { char s1_mode[2][5] = { "公制","US" }; switch (n) { case 0: *s_mode = 0; break; case 1: *s_mode = 1; break; default: printf("指定的模式无效, 使用上次输入模式 %d(%s).\n", *s_mode, s1_mode[*s_mode]); break; } } void get_info(int s_mode, double *distance, double *petrol) { if (s_mode == 0) { printf("输入行驶的距离[公里]: "); scanf_s("%lf", distance); while (*distance != 1 && *distance <= 0) { while (getchar() != '\n') continue; printf("输入错误, 输入行驶的距离[公里]: "); scanf_s("%lf", distance); } printf("输入耗油量[升]: "); scanf_s("%lf", petrol); while (*petrol != 1 && *petrol <= 0) { while (getchar() != '\n') continue; printf("输入错误, 输入耗油量[升]: "); scanf_s("%lf", petrol); } } else { printf("输入行驶的距离[英里]: "); scanf_s("%lf", distance); while (*distance != 1 && *distance <= 0) { while (getchar() != '\n') continue; printf("输入错误, 输入行驶的距离[英里]: "); scanf_s("%lf", distance); } printf("输入耗油量[加仑]: "); scanf_s("%lf", petrol); while (*petrol != 1 && *petrol <= 0) { while (getchar() != '\n') continue; printf("输入错误, 输入耗油量[加仑]: "); scanf_s("%lf", petrol); } } } void show_info(int s_mode, double distance, double petrol) { double total; if (s_mode == 0) { total = petrol / distance * 100; printf("燃油消耗量为每百公里 %.2f 升.\n", total); } else { total = distance / petrol; printf("燃油消耗是每加仑 %.2f 英里.\n", total); } }
/* pe12-2b.c */ #include <stdio.h> #include "pe12-2a.h" int main() { int mode; int s_mode = 0; double distance = 0; double petrol = 0; printf("对于公制模式输入0, 对于US模式输入1: "); scanf_s("%d", &mode); while (mode >= 0) { set_mode(mode, &s_mode); get_info(s_mode, &distance, &petrol); show_info(s_mode, distance, petrol); printf("对于公制模式输入0, 对于US模式输入1 [-1退出]: "); scanf_s("%d", &mode); } printf("完成.\n"); system("pause"); return 0; }
4
#include <stdio.h> #include <time.h> #include <stdlib.h> int roll(int n); void show(int n); int total1 = 0; int total2 = 0; int main() { int num; srand((unsigned int)time(0)); //获取时间 随机种子 puts("请输入你要掷硬币的次数: "); while (scanf_s("%d", &num) == 1) { if (num <= 0) break; show(num); puts("请输入你要掷硬币的次数[0退出]: "); } printf("roll() 函数被调用了 %d 次.\n", total1); printf("show() 函数被调用了 %d 次.\n", total2); puts("完成"); system("pause"); return 0; } int roll(int n) { int tot; tot = rand() % n; total1++; return tot; } void show(int n) { int i, m; int j; char coin[][9] = { "正面朝上","反面朝上" }; for (i = 1, j = 1; i <= n; i++) { m = roll(2); printf("第 %d 次, 你把硬币掷出结果: %s\n", j, coin[m]); j++; } total2++; }
5
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> #define SIZE 100 //生成随机数 int roll(int n); //统计100次随机数 void count(int *str, int n); //排序 void sorting(int *str, int n); int main() { int tot[SIZE]; int *ptr; ptr = tot; srand((unsigned int)time(0)); //读取时间 随机种子 count(tot, SIZE); sorting(ptr, SIZE); system("pause"); return 0; } int roll(int n) { int num; num = rand() % n + 1; return num; } void count(int *str, int n) { int i; for (i = 0; i < n; i++) { str[i] = roll(10); } puts("原始随机数: "); for (i = 0; i < n; i++) { if (i % 10 == 0 && i != 0) putchar('\n'); printf("%-2d ", str[i]); } putchar('\n'); } void sorting(int *str, int n) { int i, j; int ptr; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (str[j] < str[j + 1]) { ptr = str[j]; str[j] = str[j + 1]; str[j + 1] = ptr; } } } puts("排序随机数[倒序]: "); for (i = 0; i < n; i++) { if (i % 10 == 0 && i != 0) putchar('\n'); printf("%-2d ", str[i]); } putchar('\n'); }
6
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> #define SIZE 1000 //生成随机数 int roll(int n); //统计随机数 void count(int n); int main() { int n; //srand((unsigned int)time(0)); //读取时间 随机种子 printf("输入随机种子: "); while (scanf_s("%d", &n) == 1) { if (n <= 0) break; srand(n); count(SIZE); printf("输入随机种子[0退出]: "); } puts("完成"); system("pause"); return 0; } int roll(int n) { int num; num = rand() % n + 1; return num; } void count(int n) { int i; int tot[10] = { 0,0,0,0,0,0,0,0,0,0 }; int num; for (i = 0; i < n; i++) { num = roll(10); switch (num) { case 1: tot[0] += 1; break; case 2: tot[1] += 1; break; case 3: tot[2] += 1; break; case 4: tot[3] += 1; break; case 5: tot[4] += 1; break; case 6: tot[5] += 1; break; case 7: tot[6] += 1; break; case 8: tot[7] += 1; break; case 9: tot[8] += 1; break; case 10: tot[9] += 1; break; default: break; } } for (i = 0; i < 10; i++) { printf("%d 出现的次数: %d 次.\n", i + 1, tot[i]); } }
7
/* diceroll.h */ extern int roll_count; void roll_n_dice(int tot, int dice, int sides);
/* diceroll.c 掷筛子模拟程序 */ #include <stdio.h> #include <stdlib.h> #include "diceroll.h" static int rollem(int sides); static int rollem(int sides) //该函数属于该文件私有 { int roll; roll = rand() % sides + 1; return roll; } void roll_n_dice(int tot, int dice, int sides) { int i, d; int total; extern int *ptr; for (i = 0; i < tot; i++) { for (d = 0, total = 0; d < dice; d++) total += rollem(sides); ptr[i] = total; } printf("您掷了 %d 次 %d 个 %d 面筛子.\n", tot, dice, sides); for (i = 0; i < tot; i++) { if (i % 15 == 0 && i != 0) putchar('\n'); printf("%2d ", ptr[i]); } putchar('\n'); }
/* manydice.c //多次掷筛子的模拟程序 */ #include <stdio.h> #include <stdlib.h> //提供 srand() #include <time.h> //提供 time() #include "diceroll.h" int *ptr; int main() { int dice, tot; //dice 筛子个数 int sides; //筛子面数 srand((unsigned int)time(0)); //随机种子 配合 rand() 使用, 用来获取系统随机时间, 因为系统时间时刻在变化 printf("输入次数, 输入q退出: "); while (scanf_s("%d", &tot) == 1) { if (tot <= 0) { printf("输入错误, 请输入大于0的整数, 输入次数, 输入q退出: "); continue; } ptr = (int *)malloc(tot * sizeof(int)); if (ptr == NULL) { puts("内存分配失败, 结束."); //exit(EXIT_FAILURE); break; } printf("多少面和多少骰子: "); bk:while (scanf_s("%d%d", &sides, &dice) != 2) { printf("输入错误, 请输入大于0的整数, 多少面和多少骰子: "); } if (sides <= 0 || dice <= 0) { printf("输入错误, 请输入大于0的整数, 多少面和多少骰子: "); goto bk; } roll_n_dice(tot, dice, sides); free(ptr); printf("输入次数, 输入q退出: "); } puts("祝你好运!"); system("pause"); return 0; }
8
// pe12-8.c #include <stdio.h> #include <stdlib.h> int *make_array(int elem, int val); void show_array(const int ar[], int n); int main() { int *pa; int size; int value; printf("输入元素的数量: "); while (scanf_s("%d", &size) == 1 && size > 0) { printf("输入初始值: "); scanf_s("%d", &value); pa = make_array(size, value); if (pa) { show_array(pa, size); free(pa); } printf("输入元素的数量[<1 退出]: "); } puts("完成."); system("pause"); return 0; } int *make_array(int elem, int val) { int *ptr; int i; ptr = (int *)malloc(elem * sizeof(int)); for (i = 0; i < elem; i++) { ptr[i] = val; } return ptr; } void show_array(const int ar[], int n) { int i; for (i = 0; i < n; i++) { if (i % 8 == 0 && i != 0) putchar('\n'); printf("%d ", ar[i]); } putchar('\n'); }
9
#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <stdbool.h> #include <string.h> int main() { int size; char **ptr1; int i; printf("请输入你要输入的单词数量: "); bk:while (scanf_s("%d", &size) != 1) { printf("输入错误, 请输入大于0的整数: "); } if (size <= 0) { printf("输入错误, 请输入大于0的整数: "); goto bk; } ptr1 = (char **)malloc(size * sizeof(char *)); //每个元素都指向char指针 这里malloc函数返回一个指向指针的指针 printf("请输入 %d 个单词: \n", size); i = 0; while (i < size) { char ch; bool judge = false; while ((ch = getchar()) != EOF) //不能用'\n'因为用了最后的'\n'会被拦截, 下面无法判断结束 { static j = 0; char word[50]; if (isalpha(ch)) { word[j] = ch; j++; judge = true; } else if (judge) { word[j] = '\0'; j = 0; judge = false; ptr1[i] = (char *)malloc(strlen(word) + 1); //字符指针指向对象大小 strcpy_s(ptr1[i], strlen(word) + 1, word); i++; if (i >= size) //判断单词数量从而跳出循环 break; } } } puts("你输入的单词: "); for (i = 0; i < size; i++) { printf("%s\n", ptr1[i]); } free(ptr1); system("pause"); return 0; }