C primer plus 6 编程练习答案

环境:vs2017

  1 /**编程练习2**/
  2 /*1*/
  3 #include<stdio.h>
  4 int main(void)
  5 {
  6     printf("张三\n");
  7     printf("张\n三\n");
  8     printf("");
  9     printf("三\n");
 10     getchar();
 11     return 0;
 12 }
 13 /*2*/
 14 #include<stdio.h>
 15 int main(void)
 16 {
 17     printf("姓名:张三\n地址:中国\n");
 18     getchar();
 19     return 0;
 20 }
 21 /*3*/
 22 #include<stdio.h>
 23 int main(void)
 24 {
 25     int    age = 0, days = 0;
 26     printf("请输入您的年龄:");
 27     scanf_s("%d", &age);
 28     days = age * 365;
 29     printf("您的年龄为:%d\n您的年龄转换成天数为:%d\n", age, days);
 30     getchar();
 31     getchar();
 32     return 0;
 33 }
 34 /*4*/
 35 #include<stdio.h>
 36 void jolly(void);
 37 void deny(void);
 38 int main(void)
 39 {
 40     jolly();
 41     jolly();
 42     jolly();
 43     deny();
 44     getchar();
 45     return 0;
 46 }
 47 void jolly(void)
 48 {
 49     printf("For he's a jolly good fellow!\n");
 50 }
 51 void deny(void)
 52 {
 53     printf("Which nobagy can deny!\n");
 54 }
 55 /*5*/
 56 #include<stdio.h>
 57 void br(void);
 58 void ic(void);
 59 int main(void)
 60 {
 61     br();
 62     ic();
 63     printf("India,China,\n");
 64     printf("Brazil,Russia\n");
 65     getchar();
 66     return 0;
 67 }
 68 void br(void)
 69 {
 70     printf("Brazil,Russia,");
 71 }
 72 void ic(void)
 73 {
 74     printf("India,China\n");
 75 }
 76 /*6*/
 77 #include<stdio.h>
 78 int main(void)
 79 {
 80     int toes = 10;
 81     printf("toes为:%d\ntoes的2倍为:%d\ntoes的平方为:%d\n", toes, 2 * toes, toes*toes);
 82     getchar();
 83     return 0;
 84 }
 85 /*7*/
 86 #include<stdio.h>
 87 void smile(void);
 88 int main(void)
 89 {
 90     smile();
 91     smile();
 92     smile();
 93     printf("\n");
 94     smile();
 95     smile();
 96     printf("\n");
 97     smile();
 98     printf("\n");
 99     getchar();
100     return 0;
101 }
102 void smile(void)
103 {
104     printf("Smile!");
105 }
106 /*8*/
107 #include<stdio.h>
108 void one_three(void);
109 void two(void);
110 int main(void)
111 {
112     printf("starting now:\n");
113     one_three();
114     printf("done!\n");
115     getchar();
116     return 0;
117 }
118 void one_three(void)
119 {
120     printf("one\n");
121     two();
122     printf("three\n");
123 }
124 void two(void)
125 {
126     printf("two\n");
127 }
  1 /**编程练习3**/
  2 /*1*/
  3 #include<stdio.h>
  4 int main(void)
  5 {
  6     int    a;
  7     float b,c;
  8     a = 10000000000;//整数上溢
  9     b = 3.4E38 + 10.0f;//浮点数上溢
 10     c = 0.1234E-10 / 10;//浮点数下溢
 11     printf("%d\n%f\n%f\n",a, b, c);
 12     getchar();
 13     return 0;
 14 }
 15 /*2*/
 16 #include<stdio.h>
 17 int main(void)
 18 {
 19     int a = 0;
 20     printf("请输入一个0-127中的一个整数:");
 21     scanf_s("%d", &a);
 22     printf("该数对应的ASCLL码为:%c\n", a);
 23     getchar();
 24     getchar();
 25     return 0;
 26 }
 27 /*3*/
 28 #include<stdio.h>
 29 int main(void)
 30 {
 31     printf("\a");//警报
 32     printf("Startled by the sudden sound,Sally shouted,\n");
 33     printf("\"By the Grate Pumpkin,what was that!\"");
 34     getchar();
 35     return 0;
 36 }
 37 /*4*/
 38 #include<stdio.h>
 39 int main(void)
 40 {
 41     float a;
 42     printf("输入一个小数:");
 43     scanf_s("%f", &a);
 44     printf("%f\n%e\n%.2a\n", a, a, a);
 45     getchar();
 46     getchar();
 47     return 0;
 48 }
 49 /*5*/
 50 #include<stdio.h>
 51 int main(void)
 52 {
 53     int age = 0;
 54     long min = 0;
 55     printf("请输入您的年龄:");
 56     scanf_s("%ld", &age);
 57     min = age * 3.156E7;
 58     printf("转换为秒为:%ld秒\n", min);
 59     getchar();
 60     getchar();
 61     return 0;
 62 }
 63 /*6*/
 64 #include<stdio.h>
 65 int    main(void)
 66 {
 67     double a,c;
 68     int b;
 69     printf("请输入水的夸脱数:");
 70     scanf_s("%d", &b);
 71     a = 950 * 3.0E-23;
 72     c = a * b;
 73     printf("水分子数量为:%e\n", c);
 74     getchar();
 75     getchar();
 76     return 0;
 77 }
 78 /*7*/
 79 #include<stdio.h>
 80 int main()
 81 {
 82     float tall_m, tall_cm;
 83     printf("请输入您的身高:");
 84     scanf_s("%f", &tall_m);
 85     tall_cm = 2.54*tall_m;
 86     printf("%.2f英寸转化为厘米为:%f厘米",tall_m, tall_cm);
 87     getchar();
 88     getchar();
 89     return 0;
 90 }
 91 /*8*/
 92 #include<stdio.h>
 93 int main()
 94 {
 95     float cup,p,a,t,c;
 96     printf("请输入杯数:");
 97     scanf_s("%f", &cup);
 98     p = cup/2;
 99     a = 8 * cup;
100     t = 2 * a;
101     c = 3 * t;
102     printf("%f杯等于%f品脱,%f杯等于%f盎司,%f盎司等于%f大汤勺,%f大汤勺等于%f茶勺\n", cup, p, cup, a, a, t, t, c);
103     getchar();
104     getchar();
105     return 0;
106 }
  1 /**编程练习4**/
  2 /*1*/
  3 #include<stdio.h>
  4 int main(void)
  5 {
  6     char surname[40] ;
  7     char  name[40];
  8     printf("请输入您的名:\n");
  9     scanf_s("%s",name,40);////////////////////////////////注意!!!scanf_s()在输入数组时需要指明字段大小/////////////////////////////////////////
 10     printf("请输入您的姓:\n");
 11     scanf_s("%s",surname,40);
 12     printf("您的名为:%s\n姓为:%s\n", name, surname);
 13     getchar();
 14     getchar();
 15     //getchar();
 16     return 0;
 17 }
 18 /*2*/
 19 #include<stdio.h>
 20 #include<string.h>
 21 int main(void)
 22 {
 23     char name[40];
 24     printf("请输入名字:\n");
 25     scanf_s("%s", name, 40);
 26     printf("\"%20s\"\n", name);
 27     printf("\"%-20s\"\n", name);
 28     printf("\"%*s\"\n", strlen(name) + 3, name);
 29     getchar();
 30     getchar();
 31     return 0;
 32 }
 33 /*3*/
 34 #include<stdio.h>
 35 int main(void)
 36 {
 37     float a;
 38     printf("输入一个小数:21.29\n");
 39     scanf_s("%f", &a);
 40     printf("输入的数是:%.1f或%.1e\n", a,a);
 41     printf("输入的数是:%2.2f或%.3E\n", a, a);
 42     getchar();
 43     getchar();
 44     return 0;
 45 }
 46 /*4*/
 47 #include<stdio.h>
 48 int main(void)
 49 {
 50     float heigh;
 51     char name[40];
 52     printf("请输入你的姓名:\n");
 53     scanf_s("%s", name, 40);
 54     printf("请输入你的身高(米):\n");
 55     scanf_s("%f", &heigh);
 56     printf("%s,你有%.3f米高", name,heigh);
 57     getchar();
 58     getchar();
 59     return 0;
 60 }
 61 /*5*/
 62 #include<stdio.h>
 63 int main(xoid)
 64 {
 65     float speed, size, time;
 66     printf("请输入以兆位每秒(Mb/s)为单位的下载速度和以兆(MB)为单位的文件大小\n");
 67     scanf_s("%f %f", &speed,&size);
 68     time = 8 * size / speed;
 69     printf("At %.2f megabits per second,a file of %.2f megabytes\n\
 70  downloads in %.2f seconds.\n",speed,size,time);
 71     getchar();
 72     getchar();
 73     return 0;
 74 }
 75 /*6*/
 76 #include<stdio.h>
 77 #include<string.h>
 78 int main(void)
 79 {
 80     char surname[40];
 81     char name[40];
 82     int length1,length2;
 83     printf("输入姓:\n");
 84     scanf_s("%s", surname, 40);
 85     printf("输入名:\n");
 86     scanf_s("%s", name, 40);
 87     length1 = strlen(surname);
 88     length2 = strlen(name);
 89     printf("%s %s\n", surname, name);
 90     printf("%*d %*d\n", length1, length1,length2,length2);
 91     printf("%s %s\n", surname, name);
 92     printf("%-*d %-*d\n", length1, length1, length2, length2);
 93     getchar();
 94     getchar();
 95     return 0;
 96 }
 97 /*7*/
 98 #include<stdio.h>
 99 #include<float.h>
100 int main(void)
101 {
102     double a=1.0 / 3.0;
103     float b=1.0 / 3.0;
104     printf("a=%.6f\tb=%.6f\n", a, b);
105     printf("a=%.12f\tb=%.12f\n", a, b);
106     printf("a=%.16f\tb=%.16f\n", a, b);
107     printf("float类型的最小有效数字为:%d\tdouble类型的最小有效数字为:%d\n", FLT_DIG, DBL_DIG);
108     getchar();
109     return 0;
110 }
111 /*8*/
112 #include<stdio.h>
113 #define GALLON 3.785 //1加仑等于3.785升
114 #define MILE 1.609   //1英里等于1.609千米
115 int main(void)
116 {
117     float gallon, mile;
118     printf("输入旅行里程:\n");
119     scanf_s("%f", &mile);
120     printf("输入消耗汽油量:\n");
121     scanf_s("%f", &gallon);
122     printf("油耗为:%.1f英里/加仑\n", mile / gallon);
123     printf("油耗转换为:%.1f升/100公里\n", gallon*GALLON / (mile*MILE) * 100);
124     getchar();
125     getchar();
126     return 0;
127 }
  1 /****编程练习5****/
  2 /*1*/
  3 #include<stdio.h>
  4 int main(void)
  5 {
  6     const int m_to_h = 60;
  7     int m,h,left;
  8     printf("输入一个时间(分钟):\n");
  9     scanf_s("%d", &m);
 10     while (m>0)
 11     {
 12         h = m/m_to_h;
 13         left=m%m_to_h;
 14         printf("%d分钟转化为小时为:%d小时%d分钟\n", m, h,left);
 15         scanf_s("%d", &m);
 16     }
 17     getchar();
 18     //getchar();
 19     return 0;
 20 }
 21 /*2*/
 22 #include<stdio.h>
 23 int main(void)
 24 {
 25     int a,b;
 26     printf("请输入一个整数:\n");
 27     scanf_s("%d", &a);
 28     b = a + 10;
 29     while (a<=b)
 30     {
 31         printf("打印比该数大10的整数为:%d\n", a);
 32         a++;
 33     }
 34     getchar();
 35     getchar();
 36     return 0;
 37 }
 38 /*3*/
 39 #include<stdio.h>
 40 int main(void)
 41 {
 42     const int day_to_week = 7;
 43     int days,week,left;
 44     printf("请输入天数:\n");
 45     scanf_s("%d", &days);
 46     while (days>0)
 47     {
 48         week = days/day_to_week;
 49         left = days%day_to_week;
 50         printf("%d天转化为%d周%d天\n", days, week, left);
 51         scanf_s("%d", &days);
 52     }
 53     getchar();
 54     getchar();
 55     return 0;
 56 }
 57 /*4*/
 58 #include<stdio.h>
 59 #define FEET_TO_CM 30.48
 60 #define INCHE_TO_CM 2.54
 61 int main(void)
 62 {
 63     float heigh,inches;//inches英寸
 64     int foot;//英尺
 65     printf("输入身高(cm):\n");
 66     scanf_s("%f", &heigh);
 67     while (heigh>0)
 68     {
 69         foot = heigh / FEET_TO_CM;
 70         inches = (heigh - foot * FEET_TO_CM) / INCHE_TO_CM;
 71         printf("%.2fcm=%d英尺%.2f英寸", heigh, foot, inches);
 72         scanf_s("%f", &heigh);
 73     }
 74     getchar();
 75     //getchar();
 76     return 0;
 77 }
 78 /*5*/
 79 #include<stdio.h>
 80 int main(void)
 81 {
 82     int sum=0, count=0,day=0;
 83     printf("输入天数:\n");
 84     scanf_s("%d", &day);
 85     while (count++<day)
 86     {
 87         sum = sum + count;
 88     }
 89     printf("%d天挣了%d元钱\n", day, sum);
 90     getchar();
 91     getchar();
 92     return 0;
 93 }
 94 /*6*/
 95 #include<stdio.h>
 96 int main(void)
 97 {
 98     int sum = 0, count = 0, day = 0;
 99     printf("输入天数:\n");
100     scanf_s("%d", &day);
101     while (count++ < day)
102     {
103         sum = sum + count*count;
104     }
105     printf("%d天挣了%d元钱\n", day, sum);
106     getchar();
107     getchar();
108     return 0;
109 }
110 /*7*/
111 #include<stdio.h>
112 void value3(double a);
113 int main(void)
114 {
115     double a;
116     printf("输入一个小数:\n");
117     scanf_s("%lf", &a);
118     value3(a);
119     getchar();
120     getchar();
121     return 0;
122 }
123 void value3(double a)
124 {
125     double b;
126     b = a * a*a;
127     printf("%lf立方为:%lf\n", a, b);
128 }
129 /*8*/
130 #include<stdio.h>
131 int main(void)
132 {
133     int a, b;
134     printf("\t*****求模运算*****\t\n");
135     printf("输入一个数作为第2个运算对象:\n");
136     scanf_s("%d", &a);
137     printf("输入第二个数作为第1个运算对象:\n");
138     scanf_s("%d", &b);
139     printf("求模运算%d%%%d=%d\n", b, a, b%a);
140     printf("输入下一个数作为第1个运算对象:\n");
141     scanf_s("%d", &b);
142     while (b>0)
143     {
144         printf("求模运算%d%%%d=%d\n", b, a, b%a);
145         printf("输入下一个数作为第1个运算对象:\n");
146         scanf_s("%d", &b);
147     }
148     printf("结束\n");
149     getchar();
150     getchar();
151     return 0;
152 }
153 /*9*/
154 #include<stdio.h>
155 void Temperatures(double h_tem);
156 int main(void)
157 {
158     double h_tem;//华氏度
159     printf("请输入华氏温度(输入非数字以退出):\n");
160     while (scanf_s("%lf", &h_tem)==1)
161     {
162         Temperatures(h_tem);
163         printf("请输入华氏温度(输入非数字以退出):\n");
164     }
165     printf("程序退出\n");
166     getchar();
167     getchar();
168     return 0; 
169 }
170 void Temperatures(double h_tem)
171 {
172     double s_tem;//摄氏度
173     double k_tem;//开氏度
174     const double h_to_s = 32.0;//华氏度转摄氏度要用到的常量值
175     const double s_to_k = 273.16;//摄氏度转开氏度要用到的常量值
176     s_tem = 5.0 / 9.0 * (h_tem - h_to_s);
177     k_tem = s_tem + s_to_k;
178     printf("%.2lf华氏度等于%.2lf摄氏度和%.2lf开氏度\n", h_tem, s_tem, k_tem);
179 }
  1 /****编程练习6****/
  2 /*1*/
  3 #include<stdio.h>
  4 #define size 26
  5 int main(void)
  6 {
  7     char ch[size];
  8     int a;
  9     for (a = 0; a<size; a++)
 10         ch[a] = 'a' + a;
 11     for (a = 0;a < size;a++)
 12         printf("%c", ch[a]);
 13     getchar();
 14     return 0;
 15 }
 16 /*2*/
 17 #include<stdio.h>
 18 int main(void)
 19 {
 20     int row, clo;
 21     char ch = '$';
 22     for (row=0;  row< 5; row++)
 23     {
 24         for (clo = 0; clo < row + 1; clo++)
 25             printf("%c",ch);
 26         printf("\n");
 27     }
 28     getchar();
 29     return 0;
 30 }
 31 /*3*/
 32 #include<stdio.h>
 33 int main(void)
 34 {
 35     char ch = 'F';
 36     char start, end;
 37     for (end=ch;end>='A'; end--)
 38     {
 39         for (start=ch;start>=end;start--)
 40             printf("%c", start);
 41         printf("\n");
 42     }
 43     getchar();
 44     return 0;
 45 }
 46 /*4*/
 47 #include<stdio.h>
 48 int main(void)
 49 {
 50     const char end = 'U';
 51     char start;
 52     int row, clo;
 53     for (row=0,start = 'A'; start<=end;row++ )
 54     {
 55         for (clo = 0;clo <= row;clo++)
 56         {
 57             printf("%c", start);
 58             start++;
 59         }
 60         printf("\n");
 61     }
 62     getchar();
 63     return 0;
 64 }
 65 /*5*/
 66 #include<stdio.h>
 67 int main(void)
 68 {
 69     char start = 'A';
 70     char end;
 71     char ch = start;
 72     int row, clo;
 73     printf("输入一个大写字母:\n");
 74     scanf_s("%c", &end);
 75     printf("  字母金字塔\n");
 76     for ( row=0; row <= end-start; row++,ch = start)
 77     {
 78         for (clo = 0;clo < end - start - row;clo++)
 79             printf(" ");
 80         for (int k = 0;k <= row;k++)
 81         {
 82             printf("%c", ch);
 83             ch++;
 84         }
 85         ch -= 2;
 86         for (int c = 0;c < row;c++)
 87         {
 88                 printf("%c", ch);
 89             ch--;
 90         }
 91         printf("\n");
 92     }
 93     getchar();
 94     getchar();
 95     return 0;
 96 }
 97 /*6*/
 98 #include <stdio.h> 
 99 int main( void ) 
100 {    
101     int start, end;
102     printf("输入开始的数字:\n");
103     scanf_s("%d", &start);
104     printf("输入结束数字:\n");
105     scanf_s("%d", &end);
106     printf("原数\t平方\t立方\n");
107     for (start;start <= end;start++)
108     {
109         printf("%d\t%d\t%d\n", start, start*start, start*start*start);
110     }
111     getchar();
112     getchar();
113     return 0;
114 }
115 /*7*/
116 #include<stdio.h>
117 #include<string.h>
118 #define SIZE 40
119 int main(void)
120 {
121     char word[SIZE];
122     int i;
123     printf("请输入一个单词:\n");
124     scanf_s("%s", word, 40);
125     printf("该单词有%d个字母,单词是%s\n", strlen(word), word);
126     printf("倒序为:");
127     for (i = strlen(word - 1);i >= 0;i--)//strlen(word - 1)跳过字符串后面的空格
128         printf("%c", word[i]);
129     printf("\n");
130     getchar();
131     getchar();
132     return 0;
133 }
134 /*8*/
135 #include<stdio.h>
136 int main(void)
137 {
138     double a, b;
139     int status;//输入状态
140     printf("输入2个浮点数(输入q退出):\n");
141     while (scanf_s("%lf%lf", &a, &b) == 2)//验证是否输入了2个值
142     {
143         printf("结果是%.3lf", (a - b) / (a*b));
144         printf("继续输入2个浮点数(输入q退出):\n");
145     }
146     getchar();
147     return 0;
148 }
149 /*9*/
150 #include<stdio.h>
151 float result(float a, float b);
152 int main(void)
153 {
154     double a, b;
155     int status;//输入状态
156     printf("输入2个浮点数(输入q退出):\n");
157     while (scanf_s("%lf%lf", &a, &b) == 2)//验证是否输入了2个值
158     {
159         printf("结果是%.3lf", result(a,b));
160         printf("继续输入2个浮点数(输入q退出):\n");
161     }
162     getchar();
163     return 0;
164 }
165 float result(float a, float b)
166 {
167     float result;//result名称不唯一
168     result = (a - b) / (a*b);
169     return result;
170 }
171 /*10*/
172 #include<stdio.h>
173 int main(void)
174 {
175     int up, down;//上限和下限
176     printf("输入上限整数和下限整数:\n");
177     scanf_s("%d %d", &up, &down);
178     while (up>down)
179     {
180         int sum = 0;//初始化平方和sum
181         for (int i = sum;i <= up;i++)
182             sum = sum + i * i;
183         printf("平方和从%d到%d是%d\n", up*up, down*down, sum);
184         printf("继续输入上限整数和下限整数:\n");
185         scanf_s("%d %d", &up, &down);
186     }
187     printf("结束!\n");
188     getchar();
189     getchar();
190     return 0;
191 }
192 /*11*/
193 #include<stdio.h>
194 #define SIZE 8
195 int main(void)
196 {
197     int a[SIZE];
198     int i;
199     for ( i = 0; i < SIZE; i++)
200         scanf_s("%d", &a[i]);
201         for (i = SIZE - 1;i >= 0;i--)
202             printf("%d", a[i]);
203     getchar();
204     getchar();
205     return 0;
206 }
207 /*12*/
208 #include<stdio.h>
209 int main(void)
210 {
211     int times;
212     float count1 = 1.0f;//分母
213     float count2 = 1.0f;//分母
214     float sum1 = 0.0;
215     float sum2 = 0.0;
216     printf("输入计算次数(输入0或负数退出):\n");
217     scanf_s("%d", &times);
218     for (int i = 1;i <= times;i++, count1++)
219         sum1 = sum1 + 1.0 / count1;
220     for (int j = 1;j <= times;j++, count2++)
221     {
222         if (j%2==0)
223         {
224             sum2 = sum2 - 1.0 / count2;
225         }
226         else
227         {
228             sum2 = sum2 + 1.0 / count2;
229         }
230     }
231     printf("第一个公式的计算结果为:%f\n", sum1);
232     printf("第二个公式的计算结果为:%f\n", sum2);
233     getchar();
234     getchar();
235     return 0;
236 }
237 /*13*/
238 #include<stdio.h>
239 #define SIZE 8
240 int main(void)
241 {
242     int arry[SIZE];
243     int i, n;
244     for (i = 0, n = 2;i < SIZE;i++, n *= 2)
245         arry[i] = n;
246     i = 0;
247     do
248     {
249         printf("%d\n", arry[i]);
250         i++;
251     } while (i<SIZE);
252     getchar();
253     return 0;
254 }
255 /*14*/
256 #include<stdio.h>
257 #define SIZE 8
258 int main(void)
259 {
260     double arry1[SIZE];
261     double arry2[SIZE];
262     int i;
263 
264     printf("输入值:\n");
265     for (i = 0;i < SIZE;i++)
266         scanf_s("%lf", &arry1[i]);//读入第一个数组
267     arry2[0] = arry1[0];//初始化第二个数组
268     for (i = 1;i < SIZE;i++)
269         arry2[i] = arry2[i - 1] + arry1[i];//计算第二个元组各数之和
270     for (i = 0;i < SIZE;i++)
271         printf("%8.2lf", arry1[i]);
272     printf("\n");
273     for (i = 0;i < SIZE;i++)
274         printf("%8.2lf", arry2[i]);
275     
276     getchar();
277     getchar();
278     return 0;
279 }
280 /*15*/
281 #include<stdio.h>
282 #define SIZE 255
283 int main(void)
284 {
285     char arry[SIZE];
286     int i = -1;
287     printf("输入一个单词:\n");
288     do
289     {
290         i++;//由arry[0]开始循环读入
291         scanf_s("%c", &arry[i]);
292     } while (arry[i]!='\n');
293     for (;i >= 0;i--)
294         printf("%c", arry[i]);
295     getchar();
296     return 0;
297 }
298 /*16*/
299 #include<stdio.h>
300 #define Daphne_RATE 0.10    //Daphne利率
301 #define Deiedre_RATE 0.05   //Deiedre利率
302 #define MONEY 100.0        //投资100元
303 int main(void)
304 {
305     int year=0;
306     double Daphne_money=MONEY;   //Daphne的投资额
307     double Deiedre_money=MONEY;  //Deiedre的投资额
308     while (Deiedre_money <= Daphne_money)   //当Deiedre投资额超过Daphne的投资额退出循环
309     {
310         year++;
311         Daphne_money = Daphne_money + MONEY * Daphne_RATE;
312         Deiedre_money = Deiedre_money + Deiedre_money * Deiedre_RATE;
313     }
314     printf("用了%d年Deiedre的投资额超过Daphne的投资额\n", year);
315     printf("Daphne的投资额为:%f\tDeiedre的投资额为%f\n", Daphne_money, Deiedre_money);
316     getchar();
317     return 0;
318 }
319 /*17*/
320 #include<stdio.h>
321 #define RATE 0.08
322 int main(void)
323 {
324     double money = 100;
325     int year = 0;
326     while (money>0)
327     {
328         year++;
329         money = money + money * RATE;
330         money = money - 10;
331     }
332     printf("%d年后Chuckie会取完账户里的钱\n", year);
333     getchar();
334     return 0;
335 }
336 /*18*/
337 #include<stdio.h>
338 #define DB_NUMBER 150    //邓巴数
339 int main(void)
340 {
341     int weeks = 1;
342     int friends = 5;
343     while (friends<DB_NUMBER)
344     {
345         friends = (friends - weeks) * 2;
346         printf("%d周后,Rabnud博士有%d个朋友\n", weeks, friends);
347         weeks++;
348     }
349     getchar();
350     return 0;
351 }
  1 /*****编程练习7*****/
  2 /*1*/
  3 #include<stdio.h>
  4 int main(void)
  5 {
  6     char  ch;
  7     int spa_count=0, lin_count=0, oth_count=0;   //空格数,换行符数,其他字符数
  8     printf("输入字符进行统计(输入字符‘#’退出程序):\n");
  9     while ((ch=getchar())!='#')
 10     {
 11         if (ch == ' ')
 12             spa_count++;
 13         else if (ch == '\n')
 14             lin_count++;
 15         else
 16             oth_count++;
 17     }
 18     printf("你输入的字符中空格数有%d个,换行符有%d个,其他字符数有%d个\n", spa_count, lin_count, oth_count);
 19     getchar();
 20     getchar();
 21     return 0;
 22 }
 23 /*2*/
 24 #include<stdio.h>
 25 int main(void)
 26 {
 27     char ch;
 28     const int line = 8;
 29     int chsum = 0;//统计字符数
 30     printf("输入字符进行统计(输入‘#’字符结束):\n");
 31     while ((ch=getchar())!='#')
 32     {
 33         printf("%c:%d\t", ch, ch);
 34         chsum++;
 35         if (chsum%line == 0)
 36             printf("\n");
 37     }
 38     printf("\n");
 39     getchar();
 40     getchar();
 41     return 0;
 42 }
 43 /*3*/
 44 #include<stdio.h>
 45 int main(void)
 46 {
 47     int a;
 48     int ou_count = 0;//偶数个数
 49     int ji_count = 0;//奇数个数
 50     int ou_sum = 0;//偶数的和
 51     int ji_sum = 0;//奇数和
 52     float ou_average;//偶数平均值
 53     float ji_average;//奇数平均值
 54     printf("输入整数进行计算(输入‘0’结束):");
 55     while (scanf_s("%d", &a) == 1 && a != 0)
 56     {
 57         if (a % 2 == 0)
 58         {
 59             ou_count++;
 60             ou_sum += a;
 61         }
 62         else
 63         {
 64             ji_count++;
 65             ji_sum += a;
 66         }
 67     }
 68         printf("偶数个数:%d\n", ou_count);
 69         if (ou_count>0)
 70         {
 71             ou_average = ou_sum / ou_count;
 72             printf("偶数平均值为:%.2f\n", ou_average);
 73         }
 74         printf("奇数个数:%d\n", ji_count);
 75         if (ji_count>0)
 76         {
 77             ji_average = ji_sum / ji_count;
 78             printf("奇数平均值为:%.2f\n", ji_average);
 79         }
 80     printf("结束\n");
 81     getchar();
 82     getchar();
 83     return 0;
 84 }
 85 /*4*/
 86 #include<stdio.h>
 87 int main(void)
 88 {
 89     char ch;
 90     int count1=0;//替换句号的次数
 91     int count2=0;//替换感叹号的次数
 92     printf("输入字符进行统计(输入‘#’结束):\n");
 93     while ((ch=getchar())!='#')
 94     {
 95         if (ch == '.')
 96         {
 97             putchar('!');
 98             count1++;
 99         }
100         else if (ch == '!')
101         {
102             putchar('!');
103             putchar('!');
104             count2++;
105         }
106         else
107             putchar(ch);
108     }
109     printf("\n'!'替代'.'进行了%d次\n", count1);
110     printf("'!!'替代'!'进行了%d次\n", count2);
111     getchar();
112     getchar();
113     return 0;
114 }
115 /*5*/
116 #include<stdio.h>
117 int main(void)
118 {
119     char ch;
120     int count1 = 0;//替换句号的次数
121     int count2 = 0;//替换感叹号的次数
122     printf("输入字符进行统计(输入‘#’结束):\n");
123     while ((ch = getchar()) != '#')
124     {
125         switch (ch)
126         {
127         case'.':
128             putchar('!');
129             count1++;
130             break;
131         case'!':
132             putchar('!');
133             putchar('!');
134             count2++;
135             break;
136         default:
137             putchar(ch);
138         }
139     }
140     printf("\n'!'替代'.'进行了%d次\n", count1);
141     printf("'!!'替代'!'进行了%d次\n", count2);
142     getchar();
143     getchar();
144     return 0;
145 }
146 /*6*/
147 #include<stdio.h>
148 int main(void)
149 {
150     char ch;
151     char fir, sec;//第一个接收到的字符和第二个字符
152     int count = 0;//ei出现的次数
153     printf("输入进行统计(输入‘#’结束):\n");
154     while ((ch=getchar())!='#')
155     {
156         if (ch=='e')
157         {
158             fir = ch;
159             continue;
160         }
161         if (ch=='i')
162         {
163             sec = ch;
164 
165             if (fir == 'e'&&sec == 'i')
166                 count++;                //判断前一个字符是‘e’,第二个是‘i’,则次数加1
167             //初始化fir和sec
168             fir = 'a';
169             sec = 'a';
170             continue;
171         }
172         continue;
173     }
174     printf("'ei'出现了%d次\n",count);
175     getchar();
176     getchar();
177     return 0;
178 }
179 /*7*/
180 #include<stdio.h>
181 #define BASE_MONEY 10.00//基本工资
182 #define BASE_TIME 40//基本加班时间
183 #define OVER_TIME 1.5//超过基本时间
184 #define MONY1 300//税率分界线1
185 #define MONY2 150//税率分界线2
186 #define RATE1 0.15//MONY1前的税率
187 #define RATE2 0.20//MONY2的税率
188 #define RATE3 0.25//剩下的税率
189 int main(void)
190 {
191     double hours;//一周工作时间
192     double sum;//工资总数
193     double rate_mony;//税金
194     double income;//净收入
195     printf("输入一周工作小时数:\n");
196     scanf_s("%lf", &hours);
197     if (hours <= BASE_TIME)
198         sum = hours * BASE_MONEY;
199     else
200         sum = BASE_TIME * BASE_MONEY + (hours - BASE_TIME)*BASE_MONEY*OVER_TIME;
201     if (sum <= MONY1)
202         rate_mony = sum * RATE1;
203     else if (sum <= MONY1 + MONY2)
204         rate_mony = MONY1 * RATE1 + (sum - MONY1)*RATE2;
205     else
206         rate_mony = MONY1 * RATE1 + MONY2 * RATE2 + (sum - MONY1 - MONY2)*RATE3;
207     income = sum - rate_mony;
208     printf("工资总额是:%.2lf\t税金是:%.2lf\t净收入是:%.2lf\n", sum, rate_mony, income);
209     getchar();
210     getchar();
211     return 0;
212 }
213 /*8*/
214 #include<stdio.h>
215 #define BASE_TIME 40     //基本加班时间
216 #define OVER_TIME 1.5    //超过基本时间
217 #define MONY1 300       //税率分界线1
218 #define MONY2 150          //税率分界线2
219 #define BASE_LEVEL1 8.75
220 #define BASE_LEVEL2 9.33
221 #define BASE_LEVEL3 10.00
222 #define BASE_LEVEL4 11.20
223 #define RATE1 0.15             //MONY1前的税率
224 #define RATE2 0.20            //MONY2的税率
225 #define RATE3 0.25           //剩下的税率
226 int main(void)
227 {
228     int choice;
229     double basy_money;
230     double hours;            //一周工作时间
231     double sum;             //工资总数
232     double rate_money;      //税金
233     double income;        //净收入
234     printf("*******************************************************\n");
235     printf("输入与薪资或操作对应的编号进行操作:\n");
236     printf("1) $8.75/hours      \t2) $9.33/hours\n");
237     printf("3) $10.00/hours     \t4) $11.20/hours\n");
238     printf("5) 退出\n");
239     printf("*******************************************************\n");
240     while (scanf_s("%d",&choice) == 1 && choice != 5)
241     {
242         switch (choice)
243         {
244         case 1:
245             basy_money = BASE_LEVEL1;
246             break;
247         case 2:
248             basy_money = BASE_LEVEL2;
249             break;
250         case 3:
251             basy_money = BASE_LEVEL3;
252             break;
253         case 4:
254             basy_money = BASE_LEVEL4;
255             break;
256         default:
257             printf("请输入1-4数字进行选择(输入数字5退出)。\n请输入正确的数字\n");
258             continue;
259         }
260         printf("输入一周工作小时数:\n");
261         scanf_s("%lf", &hours);
262         if (hours <= BASE_TIME)
263             sum = basy_money * hours;
264         else
265             sum = BASE_TIME * basy_money + (hours - BASE_TIME)*basy_money*OVER_TIME;
266         if (sum <= MONY1)
267             rate_money = sum * RATE1;
268         else if (sum <= (MONY1 + MONY2))
269             rate_money = MONY1 * RATE1 + (sum - MONY1) * RATE2;
270         else
271             rate_money = MONY1 * RATE1 + MONY2 * RATE2 + (sum - MONY1 - MONY2)*RATE3;
272         income = sum - rate_money;
273         printf("工资总额是:%.2lf\t税金是:%.2lf\t净收入是:%.2lf\n", sum, rate_money, income);
274         printf("输入下一个数字继续\n");
275     }
276     getchar();
277     return 0;
278 }
279 /*9*/
280 #include<stdio.h>
281 #include<stdbool.h>
282 int main(void)
283 {
284     int limit;//输入的数
285     int num;
286     int div;
287     bool numIsPrime;   //判断是否为素数
288     printf("输入正整数:");
289     while (scanf_s("%d",&limit) == 1 && limit > 0)
290     {
291         if (limit > 1)
292             printf("以下为素数:\n", limit);
293         else
294             printf("没有素数\n");
295         for (num=2 ; num <= limit; num++)
296         {
297             for (div = 2, numIsPrime = true;(div*div) <= num;div++)
298                 if (num%div == 0)
299                     numIsPrime = false;
300             if (numIsPrime)
301                 printf("%d是素数。\n", num);
302         }
303         printf("输入正整数(输入q退出):");
304     }
305     printf("结束\n");
306     getchar();
307     return 0;
308 }
309 /*10*/
310 #include<stdio.h>
311 #define LEVEL1 17850
312 #define LEVEL2 23900
313 #define LEVEL3 29750
314 #define LEVEL4 14875
315 #define RATE1 0.15
316 #define RATE2 0.28
317 int main(void)
318 {
319     int choice;             //选择号码
320     long level;            //税金等级    
321     double income;         //收入
322     double taxmoney;       //税金
323     printf("****************************************************************************\n");
324     printf("输入类别编号:\n");
325     printf("1) 单身   \t2) 户主   \t3) 已婚,共有   \t4) 已婚,离异\n");
326     printf("5) 退出\n");
327     printf("****************************************************************************\n");
328     while (scanf_s("%d",&choice) == 1 && choice != 5)
329     {
330         switch (choice)
331         {
332         case 1:
333             level = LEVEL1;
334             break;
335         case 2:
336             level = LEVEL2;
337             break;
338         case 3:
339             level = LEVEL3;
340             break;
341         case 4:
342             level = LEVEL4;
343             break;
344         default:
345             printf("请输入1-4数字进行选择(输入数字5退出)。\n请输入正确的数字\n");
346             break;
347         }
348         printf("请输入你的收入:");
349         scanf_s("%lf", &income);
350         if (income <= level)
351             taxmoney = income * RATE1;
352         else
353             taxmoney = level * RATE1 + (income - level)*RATE2;
354         printf("你的税金为%.2lf元\n", taxmoney);
355         printf("继续输入选择(输入5退出):\n");
356     }
357     getchar();
358     return 0;
359 }
360 /*11*/
361 #include<stdio.h>
362 #define Y_PRICE 2.05        //洋蓟 单价
363 #define T_PRICE 1.15        //甜菜
364 #define H_PRICE 1.09       //胡萝卜
365 #define DISCOUNT 0.05      //折扣优惠
366 
367 #define WEIGHT_LEVEL1 5      //重量层次
368 #define WEIGHT_LEVEL2 20
369 
370 #define TRANS_LEVEL1 6.5      //运费和包装费层次
371 #define TRANS_LEVEL2 14
372 
373 #define TRANS_LEVEL3 0.5       //额外增费
374 int main(void)
375 {
376     char ch;       //选择字符
377     float input;     //输入磅数
378     float y_weight = 0;   //洋蓟磅数
379     float t_weight = 0;    //甜菜磅数
380     float h_weight = 0;     //胡萝卜磅数
381     float total_weight;   //总重
382     float cost;            //花费
383     float total_cost;         //总花费:买菜+运费
384     float discount;           //折扣
385     float tran_cost;         //运费
386     float total_all;
387     printf("************************************************\n");
388     printf("输入编号进行选择:\n");
389     printf("a:洋蓟\tb:甜菜\tc:胡萝卜\nq:退出并打印账单\n");
390     printf("************************************************\n");
391     while ((ch = getchar()) != 'q')
392     {
393         if (ch == '\n')
394             continue;
395         while (getchar() != '\n')
396             continue;
397         switch (ch)
398         {
399         case 'a':
400             printf("输入洋蓟的磅数:");
401             scanf_s("%f", &input);
402             y_weight = y_weight + input;
403             break;
404         case 'b':
405             printf("输入甜菜的磅数:");
406             scanf_s("%f", &input);
407             t_weight = t_weight + input;
408             break;
409         case 'c':
410             printf("输入胡萝卜的磅数:");
411             scanf_s("%f", &input);
412             h_weight = h_weight + input;
413             break;
414         default:
415             printf("输入错误,请输入a,b,c选择蔬菜,q退出\n");
416             break;
417         }
418         printf("继续输入进行购物\n");
419     }
420     total_weight = y_weight + t_weight + h_weight;
421     cost = y_weight * Y_PRICE + t_weight * T_PRICE + h_weight * H_PRICE;
422     if (cost >= 100)
423         discount = cost * DISCOUNT;
424     else
425         discount = 0;
426     if (total_weight <= 0)
427         tran_cost = 0;
428     else if (total_weight <= WEIGHT_LEVEL1)
429         tran_cost = TRANS_LEVEL1;
430     else if (total_weight <= WEIGHT_LEVEL2)
431         tran_cost = TRANS_LEVEL2;
432     else
433         tran_cost = TRANS_LEVEL2 + TRANS_LEVEL3 * total_weight;
434     total_all = cost + tran_cost - discount;
435     printf("你的订单如下:\n");
436     printf("2.05美元/磅的洋蓟订购了%.2f磅\n", y_weight);
437     printf("1.15美元/磅的甜菜订购了%.2f磅\n", t_weight);
438     printf("1.09美元/磅的胡萝卜订购了%.2f磅\n", h_weight);
439     printf("买蔬菜的费用为:%.2f\n", cost);
440     if(cost>100)
441         printf("折扣为:%.2f\n", discount);
442     printf("运费和包装费为:%.2f\n", tran_cost);
443     printf("所有费用为:%.2f\n", total_all);
444     getchar();
445     getchar();
446     return 0;
447 }

 

  1 /****编程练习8****/
  2 /*1*/
  3 #include<stdio.h>
  4 int main(void)
  5 {
  6     char ch;
  7     int a = 0;
  8     while ((ch=getchar())!=EOF)
  9         a++;
 10     printf("读取到%d个字符。\n", a);
 11     getchar();
 12     return 0;
 13 }
 14 /*2*/
 15 #include<stdio.h>
 16 int main(void)
 17 {
 18     char ch;
 19     int i=0;            //统计每行的值
 20     while ((ch=getchar())!=EOF)
 21     {
 22         if (ch == '\n')
 23         {
 24             putchar('\\');
 25             putchar('n');
 26             printf(" %d\n", ch);
 27             i = 0;
 28         }
 29         else if (ch == '\t')
 30         {
 31             putchar('\\');
 32             putchar('t');
 33             printf("%d\n", ch);
 34         }
 35         else if (ch < ' ')
 36         {
 37             putchar('^');
 38             putchar(ch + 64);
 39             printf("%d\t", ch);
 40         }
 41         else
 42             printf("%c %d\t", ch, ch);
 43         if (++i == 10)
 44         {
 45             printf("\n");
 46             i = 0;
 47         }
 48     }
 49     getchar();
 50     return 0;
 51 }
 52 /*3*/
 53 #include<stdio.h>
 54 #include<ctype.h>
 55 int main(void)
 56 {
 57     int ch;
 58                                  //统计个数,用不到负数
 59     unsigned A_count = 0;        //大写
 60     unsigned a_count = 0;        //小写
 61     unsigned oth_count = 0;       //其他字符个数
 62     while ((ch=getchar())!=EOF)
 63     {
 64         //if (isupper(ch))         //用isupper()函数判断是否为大写
 65         if(ch>='A'&&ch<='Z')       //可以用该行替换上一行,这种方法不用再调用ctype.h
 66             A_count++;
 67         else if (islower(ch))      //用islower()函数判断是否为小写    
 68             a_count++;
 69         else
 70             oth_count++;
 71     }
 72     printf("输入中大写字母有%d个\n", A_count);
 73     printf("小写字母有%d个\n", a_count);
 74     printf("其他字符有%d个\n", oth_count);
 75     getchar();
 76     return 0;
 77 }
 78 /*4*/
 79 #include<stdio.h>
 80 #include<ctype.h>
 81 #include<stdbool.h>
 82 int main(void)
 83 {
 84     char ch;
 85     int words = 0, count = 0;      //单词   字母总数
 86     bool inword = false;           //判断是否是一个单词
 87     float average;
 88     while ((ch=getchar())!=EOF)
 89     {
 90         if (isalpha(ch))
 91             count++;               //如果是个字符,字母+1
 92         if (!isspace(ch) && !inword)
 93         {
 94             inword = true;
 95             words++;               //如果不是空格且inword为true,单词+1
 96         }
 97         if (isspace(ch) && inword)
 98             inword = false;           //如果是个空格,且inword为true,此时并不是个完整单词,需要初始化inword
 99         if (ispunct(ch))
100             continue;               //如果是字符,中断本次循环
101     }
102     average = count / words;
103     printf("平均每个单词有%.2f个字母", average);
104     getchar();
105     return 0;
106 }
107 /*5*/
108 #include<stdio.h>
109 int main(void)
110 {
111     int high = 100;
112     int low = 1;
113     int    guess=(low+high)/2;
114     char response;
115     printf("你在1-100中选一个数字,我能猜出那个数字!");
116     printf("\n如果我回答正确,就输入'y'");
117     printf("\n如果我猜的数太高,就输入'h'");
118     printf("\n如果我猜的数太小,就输入'l'\n");
119     printf("你选择的数字是 %d?\n", guess);
120     while ((response=getchar())!='y')
121     {
122         if (response == '\n')
123             continue;
124         if (response != 'h'&&response != 'l')
125         {
126             printf("对不起,输入错误!请重新输入!\n");
127             continue;
128         }
129         if (response == 'h')
130             high = guess - 1;
131         else if (response == 'l')
132             low = guess + 1;
133         guess = (high + low) / 2;
134         printf("你选择的数字是 %d?\n", guess);
135     }
136     printf("哈哈!我猜对了!\n");
137     getchar();
138     getchar();
139     return 0;
140 }
141 /*6*/
142 #include<stdio.h>
143 #include<ctype.h>
144 char get_first(void);
145 int main(void)
146 {
147     char ch;
148     ch = get_first();
149     putchar(ch);
150     getchar();
151     return 0;
152 }
153 char get_first(void)
154 {
155     char ch;
156     ch = getchar();
157     while (isspace(ch))
158         ch = getchar();
159     while (getchar() != '\n')
160         continue;
161     return ch;
162 }
163 /*7*/
164 #include<stdio.h>
165 #include<ctype.h>
166 #include<math.h>
167 #define BASY_MONEY1 8.75       //基本工资
168 #define BASY_MONEY2 9.33
169 #define BASY_MONEY3 10.00
170 #define BASY_MONEY4 11.20
171 
172 #define BASY_TIME 40         //基本工作时间
173 #define EXTR_TIME 1.5         //加班
174 
175 #define LIN_MONEY1 300       //税率分界线
176 #define LIN_MONEY2 150       
177 
178 #define RATE1 0.15            //税率
179 #define RATE2 0.20
180 #define RATE3 0.25
181 int get_first(void);          //创建函数,如果输入为空,则丢弃
182 int main(void)
183 {
184     char ch;
185     double basy_money;
186     double hours;
187     double sum;           //工资总数
188     double rate_money;    //税金
189     double income;        //净收入
190     printf("*******************************************************\n");
191     printf("输入与薪资或操作对应的编号进行操作:\n");
192     printf("a) $8.75/hours      \tb) $9.33/hours\n");
193     printf("c) $10.00/hours     \td) $11.20/hours\n");
194     printf("q) 退出\n");
195     printf("*******************************************************\n");
196     while ((ch=get_first())!='q')   //调用get_first()函数
197     {
198         if (ch =='\n')
199             continue;
200         ch = tolower(ch);       //将大写字母转为小写
201         switch (ch)
202         {
203         case 'a':
204             basy_money = BASY_MONEY1; break;
205         case 'b':
206             basy_money = BASY_MONEY2; break;
207             case 'c':
208                 basy_money = BASY_MONEY3; break;
209             case 'd':
210                 basy_money = BASY_MONEY4; break;
211         default:
212             printf("请输入a-d进行选择(输入q退出)。\n请输入正确的数字\n"); break;
213         }
214         printf("输入一周工作小时数:\n");
215         scanf_s("%lf", &hours);
216         if (hours <= BASY_TIME)
217             sum = basy_money * hours;
218         else
219             sum = basy_money * BASY_TIME + (hours - BASY_TIME)*basy_money*EXTR_TIME;
220         if (sum <= LIN_MONEY1)
221             rate_money = sum * RATE1;
222         else if (sum <= (LIN_MONEY1 + LIN_MONEY2))
223             rate_money = LIN_MONEY1*RATE1 + (sum - LIN_MONEY1)*RATE2;
224         else
225             rate_money = LIN_MONEY1 * RATE1 + LIN_MONEY2 * RATE2 + BASY_MONEY2 + (sum - LIN_MONEY1 - LIN_MONEY2)*RATE3;
226         income = sum - rate_money;
227         printf("工资总额是:%.2lf\t税金是:%.2lf\t净收入是:%.2lf\n", sum, rate_money, income);
228         printf("输入下一个数字继续\n");
229     }
230     getchar();
231     return 0;
232 }
233 int get_first(void)
234 {
235     int ch;
236     ch = getchar();
237     while (isspace(ch))
238         ch = getchar();
239     while (getchar() != '\n')
240         continue;
241     return ch;
242 }
243 /*8*/
244 #include<stdio.h>
245 #include<ctype.h>
246 int get_first(void);
247 float get_num(void);
248 int main(void)
249 {
250     char ch;
251     float first, second;
252     printf("输入你想进行运算的选项:\n");
253     printf("a.加法\ts.减法\n");
254     printf("m.乘法\td.除法\n");
255     printf("q.退出\n");
256     while ((ch=get_first())!='q')
257     {
258         ch = tolower(ch);
259         if (ch != 'a'&& ch != 's'&& ch != 'm'&& ch != 'd')
260         {
261             printf("你只能输入a,s,m,d,q中的一个:");
262             continue;
263         }
264         printf("输入第一个数:");
265         first = get_num();
266         printf("输入第二个数:");
267         second = get_num();
268         if (ch == 'd')
269         {
270             while (second == 0)
271             {
272                 printf("输入一个大于0的数:");
273                 second = get_num();
274             }
275         }
276         switch (ch)
277         {
278         case 'a':
279             printf("%.1f+%.1f=%.1f\n", first, second, first + second); break;
280         case 's':
281             printf("%.1f-%.1f=%.1f\n", first, second, first - second); break;
282         case 'm':
283             printf("%.1f*%.1f=%.1f\n", first, second, first*second); break;
284         case 'd':
285             printf("%.1f/%.1f=%.1f\n", first, second, first / second); break;
286         default:
287             ;
288             break;
289         }
290         printf("输入你想进行运算的选项:\n");
291         printf("a.加法\ts.减法\n");
292         printf("m.乘法\td.除法\n");
293         printf("q.退出\n");
294     }
295     getchar();
296     return 0;
297 }
298 int get_first(void)
299 {
300     int ch;
301     while ((ch = getchar()) == ' ' || ch == '\t' || ch == '\n')
302         continue;
303     while (getchar() != '\n')
304         continue;
305     return ch;
306 }
307 float get_num(void)
308 {
309     float input;
310     char ch;
311     while (scanf_s("%f",&input)!=1)
312     {
313         while ((ch = getchar()) != '\n')
314             putchar(ch);
315         printf("%c不是数字\n请输入例如2.5,-1.78E8,3之类的数字:",ch);
316     }
317     return input;
318 }
  1 /****编程练习8****/
  2 /*1*/
  3 #include<stdio.h>
  4 int main(void)
  5 {
  6     char ch;
  7     int a = 0;
  8     while ((ch=getchar())!=EOF)
  9         a++;
 10     printf("读取到%d个字符。\n", a);
 11     getchar();
 12     return 0;
 13 }
 14 /*2*/
 15 #include<stdio.h>
 16 int main(void)
 17 {
 18     char ch;
 19     int i=0;            //统计每行的值
 20     while ((ch=getchar())!=EOF)
 21     {
 22         if (ch == '\n')
 23         {
 24             putchar('\\');
 25             putchar('n');
 26             printf(" %d\n", ch);
 27             i = 0;
 28         }
 29         else if (ch == '\t')
 30         {
 31             putchar('\\');
 32             putchar('t');
 33             printf("%d\n", ch);
 34         }
 35         else if (ch < ' ')
 36         {
 37             putchar('^');
 38             putchar(ch + 64);
 39             printf("%d\t", ch);
 40         }
 41         else
 42             printf("%c %d\t", ch, ch);
 43         if (++i == 10)
 44         {
 45             printf("\n");
 46             i = 0;
 47         }
 48     }
 49     getchar();
 50     return 0;
 51 }
 52 /*3*/
 53 #include<stdio.h>
 54 #include<ctype.h>
 55 int main(void)
 56 {
 57     int ch;
 58                                  //统计个数,用不到负数
 59     unsigned A_count = 0;        //大写
 60     unsigned a_count = 0;        //小写
 61     unsigned oth_count = 0;       //其他字符个数
 62     while ((ch=getchar())!=EOF)
 63     {
 64         //if (isupper(ch))         //用isupper()函数判断是否为大写
 65         if(ch>='A'&&ch<='Z')       //可以用该行替换上一行,这种方法不用再调用ctype.h
 66             A_count++;
 67         else if (islower(ch))      //用islower()函数判断是否为小写    
 68             a_count++;
 69         else
 70             oth_count++;
 71     }
 72     printf("输入中大写字母有%d个\n", A_count);
 73     printf("小写字母有%d个\n", a_count);
 74     printf("其他字符有%d个\n", oth_count);
 75     getchar();
 76     return 0;
 77 }
 78 /*4*/
 79 #include<stdio.h>
 80 #include<ctype.h>
 81 #include<stdbool.h>
 82 int main(void)
 83 {
 84     char ch;
 85     int words = 0, count = 0;      //单词   字母总数
 86     bool inword = false;           //判断是否是一个单词
 87     float average;
 88     while ((ch=getchar())!=EOF)
 89     {
 90         if (isalpha(ch))
 91             count++;               //如果是个字符,字母+1
 92         if (!isspace(ch) && !inword)
 93         {
 94             inword = true;
 95             words++;               //如果不是空格且inword为true,单词+1
 96         }
 97         if (isspace(ch) && inword)
 98             inword = false;           //如果是个空格,且inword为true,此时并不是个完整单词,需要初始化inword
 99         if (ispunct(ch))
100             continue;               //如果是字符,中断本次循环
101     }
102     average = count / words;
103     printf("平均每个单词有%.2f个字母", average);
104     getchar();
105     return 0;
106 }
107 /*5*/
108 #include<stdio.h>
109 int main(void)
110 {
111     int high = 100;
112     int low = 1;
113     int    guess=(low+high)/2;
114     char response;
115     printf("你在1-100中选一个数字,我能猜出那个数字!");
116     printf("\n如果我回答正确,就输入'y'");
117     printf("\n如果我猜的数太高,就输入'h'");
118     printf("\n如果我猜的数太小,就输入'l'\n");
119     printf("你选择的数字是 %d?\n", guess);
120     while ((response=getchar())!='y')
121     {
122         if (response == '\n')
123             continue;
124         if (response != 'h'&&response != 'l')
125         {
126             printf("对不起,输入错误!请重新输入!\n");
127             continue;
128         }
129         if (response == 'h')
130             high = guess - 1;
131         else if (response == 'l')
132             low = guess + 1;
133         guess = (high + low) / 2;
134         printf("你选择的数字是 %d?\n", guess);
135     }
136     printf("哈哈!我猜对了!\n");
137     getchar();
138     getchar();
139     return 0;
140 }
141 /*6*/
142 #include<stdio.h>
143 #include<ctype.h>
144 char get_first(void);
145 int main(void)
146 {
147     char ch;
148     ch = get_first();
149     putchar(ch);
150     getchar();
151     return 0;
152 }
153 char get_first(void)
154 {
155     char ch;
156     ch = getchar();
157     while (isspace(ch))
158         ch = getchar();
159     while (getchar() != '\n')
160         continue;
161     return ch;
162 }
163 /*7*/
164 #include<stdio.h>
165 #include<ctype.h>
166 #include<math.h>
167 #define BASY_MONEY1 8.75       //基本工资
168 #define BASY_MONEY2 9.33
169 #define BASY_MONEY3 10.00
170 #define BASY_MONEY4 11.20
171 
172 #define BASY_TIME 40         //基本工作时间
173 #define EXTR_TIME 1.5         //加班
174 
175 #define LIN_MONEY1 300       //税率分界线
176 #define LIN_MONEY2 150       
177 
178 #define RATE1 0.15            //税率
179 #define RATE2 0.20
180 #define RATE3 0.25
181 int get_first(void);          //创建函数,如果输入为空,则丢弃
182 int main(void)
183 {
184     char ch;
185     double basy_money;
186     double hours;
187     double sum;           //工资总数
188     double rate_money;    //税金
189     double income;        //净收入
190     printf("*******************************************************\n");
191     printf("输入与薪资或操作对应的编号进行操作:\n");
192     printf("a) $8.75/hours      \tb) $9.33/hours\n");
193     printf("c) $10.00/hours     \td) $11.20/hours\n");
194     printf("q) 退出\n");
195     printf("*******************************************************\n");
196     while ((ch=get_first())!='q')   //调用get_first()函数
197     {
198         if (ch =='\n')
199             continue;
200         ch = tolower(ch);       //将大写字母转为小写
201         switch (ch)
202         {
203         case 'a':
204             basy_money = BASY_MONEY1; break;
205         case 'b':
206             basy_money = BASY_MONEY2; break;
207             case 'c':
208                 basy_money = BASY_MONEY3; break;
209             case 'd':
210                 basy_money = BASY_MONEY4; break;
211         default:
212             printf("请输入a-d进行选择(输入q退出)。\n请输入正确的数字\n"); break;
213         }
214         printf("输入一周工作小时数:\n");
215         scanf_s("%lf", &hours);
216         if (hours <= BASY_TIME)
217             sum = basy_money * hours;
218         else
219             sum = basy_money * BASY_TIME + (hours - BASY_TIME)*basy_money*EXTR_TIME;
220         if (sum <= LIN_MONEY1)
221             rate_money = sum * RATE1;
222         else if (sum <= (LIN_MONEY1 + LIN_MONEY2))
223             rate_money = LIN_MONEY1*RATE1 + (sum - LIN_MONEY1)*RATE2;
224         else
225             rate_money = LIN_MONEY1 * RATE1 + LIN_MONEY2 * RATE2 + BASY_MONEY2 + (sum - LIN_MONEY1 - LIN_MONEY2)*RATE3;
226         income = sum - rate_money;
227         printf("工资总额是:%.2lf\t税金是:%.2lf\t净收入是:%.2lf\n", sum, rate_money, income);
228         printf("输入下一个数字继续\n");
229     }
230     getchar();
231     return 0;
232 }
233 int get_first(void)
234 {
235     int ch;
236     ch = getchar();
237     while (isspace(ch))
238         ch = getchar();
239     while (getchar() != '\n')
240         continue;
241     return ch;
242 }
243 /*8*/
244 #include<stdio.h>
245 #include<ctype.h>
246 int get_first(void);
247 float get_num(void);
248 int main(void)
249 {
250     char ch;
251     float first, second;
252     printf("输入你想进行运算的选项:\n");
253     printf("a.加法\ts.减法\n");
254     printf("m.乘法\td.除法\n");
255     printf("q.退出\n");
256     while ((ch=get_first())!='q')
257     {
258         ch = tolower(ch);
259         if (ch != 'a'&& ch != 's'&& ch != 'm'&& ch != 'd')
260         {
261             printf("你只能输入a,s,m,d,q中的一个:");
262             continue;
263         }
264         printf("输入第一个数:");
265         first = get_num();
266         printf("输入第二个数:");
267         second = get_num();
268         if (ch == 'd')
269         {
270             while (second == 0)
271             {
272                 printf("输入一个大于0的数:");
273                 second = get_num();
274             }
275         }
276         switch (ch)
277         {
278         case 'a':
279             printf("%.1f+%.1f=%.1f\n", first, second, first + second); break;
280         case 's':
281             printf("%.1f-%.1f=%.1f\n", first, second, first - second); break;
282         case 'm':
283             printf("%.1f*%.1f=%.1f\n", first, second, first*second); break;
284         case 'd':
285             printf("%.1f/%.1f=%.1f\n", first, second, first / second); break;
286         default:
287             ;
288             break;
289         }
290         printf("输入你想进行运算的选项:\n");
291         printf("a.加法\ts.减法\n");
292         printf("m.乘法\td.除法\n");
293         printf("q.退出\n");
294     }
295     getchar();
296     return 0;
297 }
298 int get_first(void)
299 {
300     int ch;
301     while ((ch = getchar()) == ' ' || ch == '\t' || ch == '\n')
302         continue;
303     while (getchar() != '\n')
304         continue;
305     return ch;
306 }
307 float get_num(void)
308 {
309     float input;
310     char ch;
311     while (scanf_s("%f",&input)!=1)
312     {
313         while ((ch = getchar()) != '\n')
314             putchar(ch);
315         printf("%c不是数字\n请输入例如2.5,-1.78E8,3之类的数字:",ch);
316     }
317     return input;
318 }
  1 /*********编程练习9***********/
  2 /*复习题9*/
  3 #include<stdio.h>
  4 void mennu(void);        //菜单函数
  5 int limt(int up, int down);    //上下限函数
  6 int main(void)
  7 {
  8     int ch;
  9     mennu();
 10     while ((ch=limt(4,1))!=4)
 11     {
 12         printf("%d一个正确的选择!\n",ch);
 13         mennu();
 14     }
 15     getchar();
 16     getchar();
 17     getchar();
 18     return 0;
 19 }
 20 void mennu(void)
 21 {
 22     printf("请选择列表中对应的数字:\n");
 23     printf("1) 复制文件 \t2) 移动文件\n");
 24     printf("3) 删除文件 \t4) 退出\n");
 25     printf("输入你选择的数字:\n");
 26 }
 27 int limt(int up, int down)
 28 {
 29     int input;
 30     int a=scanf_s("%d", &input);
 31     while (a==1&&(input > up || input < down))
 32     {
 33         printf("输入数值超过限制!\n");
 34         mennu();
 35         scanf_s("%d", &input);
 36     }
 37     if (a!=1)
 38     {
 39         printf("没有数字输入!\n");
 40         input = 4;
 41     }
 42     return input;
 43 }
 44 /*编程练习1*/
 45 #include<stdio.h>
 46 double min(double x, double y);       
 47 int main(void)
 48 {
 49     double a, b;
 50     printf("输入两个小数,将输出较小的值(输入q退出):\n");
 51     while (scanf_s("%lf %lf", &a, &b)==2)
 52     {
 53         printf("%lf和%lf中较小的数是:%lf", a, b, min(a, b));
 54         printf("继续输入进行比较(输入q退出):\n");
 55     }
 56     getchar();
 57     return 0;
 58 }
 59 double min(double x, double y)
 60 {
 61     return x < y ? x : y;
 62 }
 63 /*2*/
 64 #include<stdio.h>
 65 void chline(char ch, int i, int j);
 66 int main(void)
 67 {
 68     char c;
 69     int row;
 70     int clum;
 71     printf("该程序要求输入一个字符并按输入的行列数打印\n");
 72     printf("输入你想打印的字符:");
 73     scanf_s("%c", &c);
 74     printf("输入你想打印的行和列(用空格分开):");
 75     scanf_s("%d %d", &row, &clum);
 76     printf("\n");
 77     chline(c, row, clum);
 78     getchar();
 79     getchar();
 80     return 0;
 81 
 82 }
 83 void chline(char ch, int i, int j)
 84 {
 85     for (int x = 0; x < i; x++)
 86     {
 87         for (int y = 0; y < j; y++)
 88             putchar(ch);
 89         putchar('\n');
 90     }
 91 }
 92 /*3*/
 93 #include<stdio.h>
 94 void chline(char ch, int i, int j);
 95 int main(void)
 96 {
 97     char ch;
 98     int row, clo;
 99     printf("输入想要打印的字符:");
100     while ((ch=getchar())!='#')
101     {
102         if (ch == '\n')
103             continue;
104         printf("输入想要打印的次数和行数:");
105         if (scanf_s("%d %d", &clo, &row) != 2)
106             break;
107         chline(ch, clo, row);
108         printf("继续输入下一个想打印的字符(输入#退出):");
109     }
110     getchar();
111     return 0;
112 }
113 void chline(char ch, int i, int j)
114 {
115     for (int x = 0; x < i; x++)
116             {
117                 for (int y = 0; y < j; y++)
118                     putchar(ch);
119                 putchar('\n');
120             }
121 }
122 /*4*/
123 #include<stdio.h>
124 double Har_average(double num1, double num2);        //调和平均数
125 int main(void)
126 {
127     double a = 3.14, b = 3.15, c;
128     c=Har_average(a, b);
129     printf("%lf", c);
130     getchar();
131     return 0;
132 }
133 double Har_average(double num1, double num2)
134 {
135     double down_num1, down_num2, down_avweage;
136     down_num1 = 1 / num1;
137     down_num2 = 1 / num2;
138     down_avweage = 1 / ((down_num1 + down_num2) / 2);
139     return down_avweage;
140 }
141 /*5*/
142 #include<stdio.h>
143 void large_of(double *p1, double *p2);
144 int main(void)
145 {
146     double a, b;
147     printf("输入两个值:");
148     while (scanf_s("%lf %lf",&a,&b)==2)
149     {
150         large_of(&a, &b);
151         printf("交换后的值为%lf和%lf\n", a, b);
152         printf("继续输入(输入q退出):");
153     }
154     getchar();
155     return 0;
156 }
157 void large_of(double *p1, double *p2)
158 {
159     if (*p1 > *p2)
160         *p2 = *p1;
161     else
162         *p1 = *p2;
163 }
164 /*6*/
165 #include<stdio.h>
166 void change(double *p1, double *p2, double*p3);
167 int main(void)
168 {
169     double a, b, c;
170     while (scanf_s("%lf %lf %lf", &a, &b,&c) == 3)
171             {
172                 change(&a, &b, &c);
173                 printf("交换后的值为%lf和%lf和%f\n", a, b,c);
174                 printf("继续输入(输入q退出):");
175             }
176             getchar();
177             return 0;
178 }
179 void change(double *p1, double *p2, double *p3)
180 {
181     double max, min, mid;
182     max = *p1;
183     if (max < *p2)
184         max = *p2;
185     if (max < *p3)
186         max = *p3;
187     min = *p1;
188     if (min > *p2)
189         min = *p2;
190     if (min > *p3)
191         min = *p3;
192     mid = *p1 + *p2 + *p3 - max - min;
193     *p1 = min;
194     *p2 = mid;
195     *p3 = max;
196 }
197 /*7*/
198 #include<stdio.h>
199 #include<ctype.h>
200 int report(char ch);
201 int main(void)
202 {
203     char ch;
204     int pos;            //数值位置
205     printf("请输入字符:");
206     while ((ch=getchar())!=EOF)
207     {
208         if (ch == '\n')
209         {
210             continue;
211         }
212         pos = report(ch);
213         if (pos == -1)
214         {
215             printf("%c不是字母!\n", ch);
216         }
217         else
218         {
219             printf("%c在字母表中的位置是%d!\n", ch, pos);
220         }
221         printf("继续输入:");
222     }
223     getchar();
224     return 0;
225 }
226 int report(char ch)
227 {
228     if (isalpha(ch))                 //判断是否为字母
229     {
230         return toupper(ch) - 'A' + 1;       //将小写转换为大写;也可以将大写转换为小写,‘A’要改为‘a’
231     }
232     else
233     {
234         return -1;
235     }
236 }
237 /*8*/
238 #include<stdio.h>
239 double power(double n, int p);
240 int main(void)
241 {
242     double x, xpow;
243     int exp;
244     printf("输入数字和指数,计算整数幂结果(q退出):");
245     while (scanf_s("%lf %d",&x,&exp)==2)
246     {
247         xpow = power(x, exp);
248         if (xpow == 1)
249             continue;
250         printf("%.3g的%d次幂为%.5g\n", x, exp, xpow);
251         printf("继续输入(q退出):");
252     }
253     getchar();
254     return 0;
255 }
256 double power(double n, int p)
257 {
258     double pow = 1;
259     if (p == 0)
260     {
261         if (n==0)
262         {
263             printf("0的0次幂未定义,请输入其它值:\n");
264         }
265         pow = 1;
266     }
267     else if (p==0)
268     {
269         pow = 0;
270     }
271     else if (p>0)
272     {
273         for (int i = 1; i <= p; i++)
274         {
275             pow *= n;
276         }
277     }
278     else
279     {
280         pow = 1 / power(n, -p);     //运用了递归   
281         ///////不用递归的写法:for(int i=1; i <= -p; i++)
282          //////////////////       {
283 ////////////////////////////            pow*=n;
284 ////////////////////////////         }          //此种方法返回的是 1/pow
285     }
286     return pow;
287 }
288 /*9*/
289 ////上题所示/////////
290 /*10*/
291 #include<stdio.h>
292 void to_base_n(int x, int system);
293 int main(void)
294 {
295     int num;
296     int system_num;
297     int status;
298     printf("输入一个整数:");
299     while (scanf_s("%d",&num)==1)
300     {
301         printf("输入进制(2-10):");
302         while ((status=scanf_s("%d",&system_num))==1&&(system_num<2||system_num>10))
303         {
304             printf("输入进制超过限定值,请输入2-10之间的数字:");
305         }
306         if (status != 1)
307             break;
308         to_base_n(num, system_num);
309         printf("\n");
310         printf("继续输入一个整数(q退出):");
311     }
312     getchar();
313     return 0;
314 }
315 void to_base_n(int x, int system)
316 {
317     int r;
318     r = x % system;
319     if (x >= system)
320     {
321         to_base_n(x / system, system);           //递归
322     }
323     putchar('0' + r);           //因为putchar()函数只能输出字符,我们要输出的是字符,所以运用ASCALL码值加上r
324 }
325 /*11*/
326 #include<stdio.h>
327 void Fibonacci(int n);
328 int main(void)
329 {
330     int n;
331     printf("输入整数:");
332     while ((scanf_s("%d",&n)==1))
333     {
334         Fibonacci(n);
335         printf("继续输入(q退出):");
336     }
337     getchar();
338     return 0;
339 }
340 void Fibonacci(int n)
341 {
342     int f1, f2, f3;
343     printf("斐波那契是:");
344     for (int i = 1; i <= n; i++)
345     {
346         if (i == 1)
347         {
348             f1 = 1;
349             printf("%d", f1);
350         }
351         else if (i==2)
352         {
353             f2 = 1;
354             printf("%d", f2);
355                  }
356         else
357         {
358             f3 = f2 + f1;
359             f1 = f2;
360             f2 = f3;
361             printf("%d", f3);
362         }
363         putchar(' ');
364     }
365     putchar('\n');
366 }

 

  1 /********编程练习10**********/
  2 /*1*/
  3 #include<stdio.h>
  4 #define MONTHS 12
  5 #define YEARS 5
  6 int main(void)
  7 {
  8     const float rain[YEARS][MONTHS] =        //2010-2014年降水量
  9     {
 10         {4.3,4.3,4.3,3.0,2.0,1.2,0.2,0.2,0.4,2.4,3.5,6.6},
 11         {8.5,8.2,1.2,1.6,2.4,0.0,5.2,0.9,0.3,0.9,1.4,7.3},
 12         {9.1,8.5,6.7,4.3,2.1,0.8,0.2,0.2,1.1,2.3,6.1,8.4},
 13         {7.2,9.9,8.4,3.3,1.2,0.8,0.4,0.0,0.6,1.7,4.3,6.2},
 14         {7.6,5.6,3.8,2.8,3.8,0.2,0.0,0.0,0.0,1.3,2.6,5.2}
 15     };
 16     int year, month;
 17     float subtot, total;                     //初始化要放在循环中
 18     printf("年份\t降水量\n");
 19     for (year = 0,total=0; year < YEARS; year++)
 20     {
 21         for (month = 0,subtot = 0; month < MONTHS; month++)
 22             subtot += *(*(rain + year) + month);     //每年每月降水量
 23         printf("%d\t%.1f\n", 2010 + year, subtot);
 24         total += subtot;                            //5年总降水量
 25     }
 26     printf("\n每年的平均降水量为:%.1f\n", total / YEARS);
 27     printf("每月5年中平均降水量:\n");
 28     printf(" 1   2   3   4   5   6   7   8   9   10  11  12\n");
 29     for (month = 0; month < MONTHS; month++)
 30     {
 31         for (year = 0, subtot = 0; year < YEARS; year++)
 32         {
 33             subtot += *(*(rain + year) + month);
 34         }
 35         printf("%4.1f", subtot / YEARS);
 36     }
 37     getchar();
 38     return 0;
 39 }
 40 /*2*/
 41 #include<stdio.h>
 42 void copy_arr(double target[], double source[], int n);
 43 void copy_ptr(double *target, double *source, int n);
 44 void copy_ptrs(double *target, double *source_begin, int *source_end );
 45 int main(void)
 46 {
 47     int i;
 48     double source[5] = { 1.1, 2.2, 3.3, 4.4, 5.5 };
 49     double target1[5];
 50     double target2[5];
 51     double target3[5];
 52     printf("source: ");
 53     for ( i = 0; i < 5; i++)
 54     {
 55         printf("%.1lf", source[i]);
 56         printf(" ");
 57     }
 58     putchar('\n');
 59     copy_arr(target1, source, 5);
 60     printf("target1:");
 61     for (i = 0; i < 5; i++)
 62     {
 63         printf("%.1lf", target1[i]);
 64         putchar(' ');
 65     }
 66     putchar('\n');
 67     copy_ptr(target2, source, 5);
 68     printf("target2:");
 69     for ( i = 0; i < 5; i++)
 70     {
 71         printf("%.1lf", *(target2 + i));
 72         putchar(' ');
 73     }
 74     putchar('\n');
 75     copy_ptrs(target3, source, source+5);
 76     printf("target3:");
 77     for (i = 0; i < 5; i++)
 78     {
 79         printf("%.1lf", target3[i]);
 80         putchar(' ');
 81     }
 82     putchar('\n');
 83     getchar();
 84     return 0;
 85 }
 86 void copy_arr(double target[], double source[], int n)
 87 {
 88     for (int i = 0; i < n; i++)
 89     {
 90         target[i] = source[i];
 91     }
 92 }
 93 void copy_ptr(double *target, double *source, int n)
 94 {
 95     for (int i = 0; i < n; i++)
 96     {
 97         *(target + i) = *(source + i);
 98     }
 99 }
100 void copy_ptrs(double *target, double *source_begin, int *source_end)
101 {
102     while (source_begin < source_end)
103     {
104         *target = *source_begin;
105         target++;
106         source_begin++;
107     }
108 }
109 /*3*/
110 #include<stdio.h>
111 #define SIZE 10
112 void show_arr(const arr[], int n);
113 int max_arr(const arr[], int n);
114 int main(void)
115 {
116     int arr[SIZE] = { 1,2,3,4,5,6,7,8,9,10 };
117     int max;
118     show_arr(arr, SIZE);
119     max = max_arr(arr, SIZE);
120     printf("%d是数组中的最大值。\n", max);
121     getchar();
122     return 0;
123 }
124 int max_arr(const arr[], int n)
125 {
126     int max = arr[0];
127     for (int i = 0; i < n; i++)
128     {
129         if (max < arr[i])
130             max = arr[i];
131     }
132     return max;
133 }
134 void show_arr(const arr[], int n)
135 {
136     for (int i = 0; i < n; i++)
137     {
138         printf("%d", arr[i]);
139         putchar(' ');
140     }
141     putchar('\n');
142 }
143 /*4*/
144 #include<stdio.h>
145 #define SIZE 10
146 double max(const arr[], int n);
147 void show_arr(const arr[], int n);
148 int main(void)
149 {
150     int arr[SIZE] = { 1,2,3,4,5,6,7,8,9,10 };
151     int max_sub;
152     show_arr(arr, SIZE);
153     max_sub = max(arr, SIZE);
154     printf("最大值的下标是%d\n", max_sub);
155     getchar();
156     return 0;
157 }
158 double max(const arr[], int n)
159 {
160     int max = arr[0];
161     int sub;                 //最大值下标
162     for (int i = 0; i < n; i++)
163     {
164         if (max < arr[i])
165             max = arr[i];
166         sub = i;
167     }
168     return sub;
169 }
170 void show_arr(const arr[], int n)
171 {
172     for (int i = 0; i < n; i++)
173     {
174         printf("%d", arr[i]);
175         putchar(' ');
176     }
177     putchar('\n');
178 }
179 /*5*/
180 #include<stdio.h>
181 #define SIZE 10
182 double diff_val(const double arr[], int n);
183 int main(void)
184 {
185     double arr[SIZE] = { 1,2,3,4,5,6,7,8,9,10 };
186     double value;
187     value = diff_val(arr, SIZE);
188     printf("最大值与最小值的差是%lf\n", value);
189     getchar();
190     return 0;
191 }
192 double diff_val(const double arr[], int n)
193 {
194     double max = arr[0];
195     double min = arr[0];
196     for (int i = 0; i < n; i++)
197     {
198         if (max < arr[i])
199         {
200             max = arr[i];
201         }
202         else if(min>arr[i])
203         {
204             min = arr[i];
205         }
206     }
207     return max - min;
208 }
209 /*6*/
210 #include<stdio.h>
211 #define SIZE 10
212 void reverse(double arr[], int n);
213 int main(void)
214 {
215     double arr[SIZE] = { 11,33, 22,44,66,55,77,88,99,10 };
216     printf("排序前的结果是:");
217     for (int i = 0; i < SIZE; i++)
218     {
219         printf("%.2lf", arr[i]);
220         putchar(' ');
221     }
222     putchar('\n');
223     reverse(arr, SIZE);
224     printf("排序后的结果是:");
225     for (int i = 0; i < SIZE; i++)
226     {
227         printf("%.2lf", arr[i]);
228         putchar(' ');
229     }
230     putchar('\n');
231     getchar();
232     return 0;
233 }
234 void reverse(double arr[], int n)
235 {
236     for (int i = 0; i < n / 2; i++)
237     {
238         double tmp = arr[i];
239         arr[i] = arr[n - 1 - i];
240         arr[n - 1 - i] = tmp;
241     }
242 }
243 /*7*/
244 #include<stdio.h>
245 #define ROWS 3    //
246 #define COLS 4    //
247 void copy_arr(double target_arr[][COLS], double source_arr[][COLS], int row);
248 int main(void)
249 {
250     double source_arr[ROWS][COLS] = 
251     {
252         {1.1,2.2,3.3,4.4},
253         {2.2,2.3,2.4,2.5},
254         {3.3,4.4,5.5,6.6}
255     };
256     double target_arr[ROWS][COLS];
257     copy_arr(target_arr, source_arr, ROWS);
258     printf("结果为:\n");
259     for (int i = 0; i < ROWS; i++)
260     {
261         for (int j = 0; j < COLS; j++)
262         {
263             printf("%.2lf", target_arr[i][j]);
264             putchar(' ');
265         }
266         putchar('\n');
267     }
268     getchar();
269     return 0;
270 }
271 void copy_arr(double target_arr[][COLS], double source_arr[][COLS], int row)
272 {
273     for (int i = 0; i < row; i++)
274     {
275         for (int j = 0; j < COLS; j++)
276         {
277             target_arr[i][j] = source_arr[i][j];
278         }
279     }
280 }
281 /*8*/
282 #include<stdio.h>
283 #define SIZE1 7
284 #define SIZE2 3
285 void copy_arr(int target_arr[],const int source_arr[], int n);
286 int main(void)
287 {
288     int source_arr[SIZE1] = { 1,2,3,4,5,6,7 };
289     int target_arr[SIZE2];
290     int values;
291     copy_arr(target_arr, source_arr + 2, SIZE2);
292     for (int i = 0; i < SIZE2; i++)
293     {
294         printf("%d", target_arr[i]);
295         putchar(' ');
296     }
297     printf("\n");
298     getchar();
299     return 0;
300 }
301 void copy_arr(int target_arr[], const int source_arr[], int n)
302 {
303     for (int i = 0; i < n; i++)
304     {
305         target_arr[i] = source_arr[i];
306     }
307 }
308 /*9*/
309 #include<stdio.h>
310 void copy_arr(int rows, double target_arr[][5],double source_arr[][5]);
311 void show_arr(int rows, double target_arr[][5], double source_arr[][5]);
312 int main(void)
313 {
314     double source_arr[3][5] = 
315     {
316         {1.1,2.2,3.3,4.4,5.5},
317         {2.2,3.3,4.4,5.5,6.6},
318         {3.3,4.4,5.5,6.6,7.7}
319     };
320     double target_arr[3][5];
321     copy_arr(3, target_arr, source_arr);
322     show_arr(3, target_arr, source_arr);
323     getchar();
324     return 0;
325 }
326 void copy_arr(int rows, double target_arr[][5], double source_arr[][5])
327 {
328     for (int i = 0; i < rows; i++)
329     {
330         for (int j = 0; j < 5; j++)
331         {
332             target_arr[i][j] = source_arr[i][j];
333         }
334     }
335 }
336 void show_arr(int rows, double target_arr[][5], double source_arr[][5])
337 {
338     for (int i = 0; i < rows; i++)
339     {
340         for (int j = 0; j < 5; j++)
341         {
342             printf("%.2lf", source_arr[i][j]);
343             putchar(' ');
344         }
345     }
346     putchar('\n');
347     for (int i = 0; i < rows; i++)
348     {
349         for (int j = 0; j < 5; j++)
350         {
351             printf("%.2lf", target_arr[i][j]);
352             putchar(' ');
353         }
354     }
355     putchar('\n');
356 }
357 /*10*/
358 #include<stdio.h>
359 void add_arr(int source_arr1[], int source_arr2[], int target_arr[], int n);
360 int main(void)
361 {
362     int    source_arr1[4] = { 2,4,5,8 };
363     int source_arr2[4] = { 1,0,4,6 };
364     int target_arr[4];
365     add_arr(source_arr1, source_arr2, target_arr, 4);
366     for (int i = 0; i < 4; i++)
367     {
368         printf("%d", target_arr[i]);
369         putchar(' ');
370     }
371     putchar('\n');
372     getchar();
373     return 0;
374 }
375 void add_arr(int source_arr1[], int source_arr2[], int target_arr[], int n)
376 {
377     for (int i = 0; i < n; i++)
378     {
379         target_arr[i] = source_arr1[i] + source_arr2[i];
380     }
381 }
382 /*11*/
383 #include<stdio.h>
384 void times2_arr(int source_arr[][5], int target_arr[][5], int rows);
385 void show_arr(int source_arr[][5], int target_arr[][5], int rows);
386 int main(void)
387 {
388     int source_arr[3][5] = 
389                             {
390                                 {1,2,3,4,5},
391                                 {2,3,4,5,6},
392                                 {3,4,5,6,7}
393                             };
394     int target_arr[3][5];
395     times2_arr(source_arr, target_arr, 3);
396     show_arr(source_arr, target_arr, 3);
397     putchar('\n');
398     getchar();
399     return 0;
400 }
401 void times2_arr(int source_arr[][5], int target_arr[][5], int rows)
402 {
403     for (int i = 0; i < rows; i++)
404     {
405         for (int j = 0; j < 5; j++)
406         {
407             target_arr[i][j] = 2*source_arr[i][j];
408         }
409     }
410 }
411 void show_arr(int source_arr[][5], int target_arr[][5], int rows)
412 {
413     for (int i = 0; i < rows; i++)
414     {
415         for (int j = 0; j < 5; j++)
416         {
417             printf("%4d", source_arr[i][j]);
418             //putchar(' ');
419             //putchar(' ');
420         }
421     }
422     putchar('\n');
423     for (int i = 0; i < rows; i++)
424     {
425         for (int j = 0; j < 5; j++)
426         {
427             printf("%4d", target_arr[i][j]);
428             //putchar(' ');
429         }
430     }
431     putchar('\n');
432 }
433 /*12*/
434 #include<stdio.h>
435 #define YEARS 5
436 #define MONTHS 12
437 void total_rain(const float rain[][MONTHS], int years);
438 void average(const float rain[][MONTHS], int years);
439 int main(void)
440 {
441     const float rain[YEARS][MONTHS] =        //2010-2014年降水量
442     {
443         {4.3,4.3,4.3,3.0,2.0,1.2,0.2,0.2,0.4,2.4,3.5,6.6},
444         {8.5,8.2,1.2,1.6,2.4,0.0,5.2,0.9,0.3,0.9,1.4,7.3},
445         {9.1,8.5,6.7,4.3,2.1,0.8,0.2,0.2,1.1,2.3,6.1,8.4},
446         {7.2,9.9,8.4,3.3,1.2,0.8,0.4,0.0,0.6,1.7,4.3,6.2},
447         {7.6,5.6,3.8,2.8,3.8,0.2,0.0,0.0,0.0,1.3,2.6,5.2}
448     };
449     total_rain(rain, YEARS);
450     printf("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NEV DEC\n");
451     average(rain, YEARS);
452     putchar('\n');
453     getchar();
454     return 0;
455 }
456 void total_rain(const float rain[][MONTHS], int years)
457 {
458     int i, j;
459     float subtotal,total = 0;
460     for ( i = 0; i < years; i++)          //每次循环需要将total归零。因为计算的是每年每月降水量
461     {
462         for ( j = 0, subtotal = 0; j < MONTHS; j++)
463         {
464             subtotal += rain[i][j];
465         }
466         printf("%d\t%.1f\n", 2010 + i, subtotal);
467         total += subtotal;
468     }
469     printf("5年总降水量为%.1f\n", total / years);
470 }
471 void average(const float rain[][MONTHS], int years)
472 {
473     int i, j;
474     float subtotal;
475     for ( i = 0; i < MONTHS; i++)
476     {
477         for ( j = 0, subtotal = 0; j < years; j++)
478         {
479             subtotal += rain[j][i];
480         }
481         printf("%-4.1f", subtotal / years);
482     }
483 }
484 /*13*/
485 #include<stdio.h>
486 void input_arr(double arr[][5], int rows);
487 double average_cols(double arr[], int cols);
488 double average_all(double arr[][5], int rows);
489 double max_arr(double arr[][5], int rows);
490 void show_arr(double arr[][5], int rows);
491 int main(void)
492 {
493     double arr[3][5];
494     printf("输入15个浮点值:\n");
495     input_arr(arr, 3);
496     putchar('\n');
497     for (int i = 0; i < 3; i++)
498     {
499         printf("%d组数据的平均值是%lf\n", i+1, average_cols(arr[i], 5));
500     }
501     printf("所有数据的平均值是%lf\n", average_all(arr, 3));
502     printf("15个数据中最大值为:%lf\n", max_arr(arr, 3));
503     show_arr(arr, 3);
504     getchar();
505     return 0;
506 }
507 void input_arr(double arr[][5], int rows)
508 {
509     for (int i = 0; i < rows; i++)
510     {
511         printf("请输入第%d组数据:\n", i + 1);
512         for (int j = 0; j < 5; j++)
513         {
514             scanf_s("%lf", &arr[i][j]);
515             getchar();
516         }
517     }
518 }
519 double average_cols(double arr[], int cols)
520 {
521     double sum = 0.0;
522     for (int i = 0; i < cols; i++)
523     {
524         sum += arr[i];
525     }
526     return (sum / cols);
527 }
528 double average_all(double arr[][5], int rows)
529 {
530     double sum = 0.0;
531     for (int i = 0; i < rows; i++)
532     {
533         for (int j = 0; j < 5; j++)
534         {
535             sum += arr[i][j];
536         }
537     }
538     return (sum / (5 * rows));
539 }
540 double max_arr(double arr[][5], int rows)
541 {
542     double max = 0.0;
543     for (int i = 0; i < rows; i++)
544     {
545         for (int j = 0; j < 5; j++)
546         {
547             if (max<arr[i][j])
548             {
549                 max = arr[i][j];
550             }
551         }
552     }
553     return max;
554 }
555 void show_arr(double arr[][5], int rows)
556 {
557     for (int i = 0; i < rows; i++)
558     {
559         for (int j = 0; j < 5; j++)
560         {
561             printf("%lf", arr[i][j]);
562             putchar(' ');
563         }
564         putchar('\n');
565     }
566 }
567 /*14*/
568 //////////////我的vs2017似乎不支持变长数组,但大致定义如下
569 /*#include<stdio.h>
570 #define ROWS 3
571 #define COLS 5
572 void input_arr(int rows,int cols, double arr[rows][cols]);
573 double average_cols(int cols,double arr[cols]);
574 double average_all(int rows, int cols, double arr[rows][cols]);
575 double max_arr(int rows, int cols, double arr[rows][cols]);
576 void show_arr(int rows, int cols, double arr[rows][cols]);
577 int main(void)
578 {
579 
580     getchar();
581     return 0;
582 }*/

 

 

 

posted @ 2019-07-14 21:31  HaroldC  阅读(991)  评论(0编辑  收藏  举报