C primer plus 6 編程練習答案

環境:vs2017c++

 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 }
相關文章
相關標籤/搜索