-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex8.11.c
307 lines (287 loc) · 5.13 KB
/
ex8.11.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*Programming Exercise8-1*/
/*
#include<stdio.h>
main()
{
int num=0;
int c;
while((c=getchar())!=EOF)
{
if(c=='\n')
;
else
num++;
}
printf("%d characters read\n",num);
return 0;
}
*/
/*Programming Exercise8-2*/
/*
#include<stdio.h>
main()
{
char c;
printf("The program will receive the char and print the char and its ASCII\n");
while((c=getchar())!=EOF)
{
if(c=='\n')
printf("\\n\n");
else if(c=='\t')
printf("\\t\n");
else if(c>0&&c<27)
printf("^%c\n",c+64);
else
printf("%c\t\t%d\n",c,c);
}
return 0;
}
/*并未按要求每行打印10对值,不美观*/
/*Programming Exercise8-3*/
/*
#include<stdio.h>
#include<ctype.h>
main()
{
int c;
int up=0,low=0;
printf("count the uppercase and lowercase\n");
while((c=getchar())!=EOF)
{
if(isupper(c))
up++;
else if(islower(c))
low++;
else
;
}
printf("The number of uppercase:%d\n",up);
printf("The number of lowercase:%d\n",low);
return 0;
}
*/
/*Programming Exercise8-4*/
/*
#include<stdio.h>
#include<ctype.h>
int main()
{
float num_c=0;
float num_words=1;
int c;
while((c=getchar())!=EOF)
{
if(c==' ')
num_words++;
else if(ispunct(c))
;
else
num_c++;
}
printf("there are %.0f words and %.0f charactors\n",num_words,num_c);
printf("the average char in word is %.3f\n",num_c/num_words);
return 0;
}
*/
/*Programming Exercise8-5*/
/*
#include<stdio.h>
int main()
{
int x;
int c;
int start=0,end=100;
int y=(start+end)/2;
printf("---------------------------------------\n");
printf("please set a number between 0 and 100: ");
scanf("%d",&x);
printf("---------------------------------------\n");
printf("h:high\tl:low\tc:correct\n");
printf("---------------------------------------\n");
printf("computer:\"Is %d high,low or correct?\"\n",y);
while((c=getchar())!='c')
{
if((c=getchar())=='h')
{
end=y;
y=(start+end)/2;
}
else if(c=='l')
{
start=y;
y=(start+end)/2;
}
else if(c=='c')
{
printf("oh!!!yeah!!!\n");
break;
}
else
printf("invalid input\n");
printf("computer:\"Is %d high,low or correct?\"\n",y);
}
return 0;
}
*/
/*Programming Exercise8-6*/
/*
#include<stdio.h>
#include<ctype.h>
char get_first(void);
int main()
{
int c;
c=get_first();
printf("%c is the first char\n",c);
return 0;
}
char get_first(void)
{
int c;
while((c=getchar())!=EOF)
{
if(c==' '||c=='\t')
;
else
return c;
}
}
*/
/*Programming Exercise8-7*/
/*
#include<stdio.h>
int main()
{
int x;
int start=0,end=100;
int y=(start+end)/2;
printf("*****************************************************************\n");
printf("Enter the number corresponding to the desired pay rate or action:\n");
printf("a) $8.75/hr\t\tb) $9.33/hr\n");
printf("c) $10.00/hr\t\td) $11.20/hr\n");
printf("q) quit\n");
printf("*****************************************************************\n");
x=getchar();
switch(x)
{
case 'a':
break;
case 'b':
break;
case 'c':
break;
case 'd':
break;
case 'q':
break;
}
}
*/
/*Programming Exercise8-8*/
#include<stdio.h>
#include<ctype.h>/*包含isdigit()*/
#include<stdlib.h>/*包含atof()*/
#define N 20
void Add(void);
void Subtract(void);
void Mutliply(void);
void Divide(void);
float get_num(void);
int main()
{
int x;
int start=0,end=100;
int y=(start+end)/2;
printf("*****************************************************************\n");
printf("Enter the number corresponding to the desired pay rate or action:\n");
printf("a) add\t\t\ts) subtract\n");
printf("m) multiply\t\td) divide\n");
printf("q) quit\n");
printf("*****************************************************************\n");
x=getchar();
switch(x)
{
case 'a':
Add();
break;
case 's':
Subtract();
break;
case 'm':
Mutliply();
break;
case 'd':
Divide();
break;
case 'q':
printf("Bye~~~~~~\n");
break;
}
}
void Add(void)
{
float num1,num2;
printf("The first numbers:\n");
num1=get_num();
printf("The second numbers:\n");
num2=get_num();
printf("%.3f +%c%.3f%c= %.3f\n",num1,num2<0?'(':' ',num2,num2<0?')':' ',num1+num2);
}
void Subtract(void)
{
float num1,num2;
printf("The first numbers:\n");
num1=get_num();
printf("The second numbers:\n");
num2=get_num();
printf("%.3f -%c%.3f%c= %.3f\n",num1,num2<0?'(':' ',num2,num2<0?')':' ',num1-num2);
}
void Mutliply(void)
{
float num1,num2;
printf("The first numbers:\n");
num1=get_num();
printf("The second numbers:\n");
num2=get_num();
printf("%.3f *%c%.3f%c= %.3f\n",num1,num2<0?'(':' ',num2,num2<0?')':' ',num1*num2);
}
void Divide(void)
{
float num1,num2;
printf("The first numbers:\n");
num1=get_num();
printf("The second numbers:\n");
num2=get_num();
while(!num2)
{
printf("Enter a number other than 0\n");
num2=get_num();
}
printf("%.3f /%c%.3f%c= %.3f\n",num1,num2<0?'(':' ',num2,num2<0?')':' ',num1/num2);
}
float get_num(void)
{
char a[N];
float num;
do
{
scanf("%s",a);
num=atof(a);
if(num)
break;
else
{
if(isdigit(a))
{
break;
}
else
{
printf("%s is not a number.\n",a);
printf("Please input a number,such as 2.5,-1.78E8,or 3\n");
}
}
}while(!atof(a));
return num;
}
/*此程序不能正确处理0的情况,待完善*/