//打开一个文件并显示该文件
#include <stdio.h>
#include <stdlib.h> // 为了使用exit()
int main()
{
int ch;
FILE * fp;
char fname[50]; // 储存文件名
printf("Enter the name of the file: ");
scanf("%s", fname);
fp = fopen(fname, "r"); // 打开待读取文件
if (fp == NULL) // 如果失败
{
printf("Failed to open file. Bye\n");
exit(1); // 退出程序
}
// getc(fp)从打开的文件中获取一个字符
while ((ch = getc(fp)) != EOF)
putchar(ch);
fclose(fp); // 关闭文件
return 0;
}
EOF
//EOF在windows系统往往通过输入 ctrl+z来表达
//但是仅仅这个这个是不够的,在此之前输入的前一个字符必须是换行符
//如果没有换行符,输入ctrl+z后并不会识别为EOF
编程练习
下面的一些程序要求输入以EOF终止。
如果你的操作系统很难或根本无法使用重定向,请使用一些其他的测试来终止输入,如读到&字符时停止。
后面的题遇到了很多堆栈错误 暂未解决
编程练习答案参考
1、
设计一个程序,统计在读到文件结尾之前读取的字符数。
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int ch;
int n = 0;
while ((ch = getchar()) != EOF)
{
if (isalnum(ch))
n++;
}
printf("%d\n", n);
return 0;
}
2、
编写一个程序,在遇到 EOF 之前,把输入作为字符流读取。
程序要打印每个输入的字符及其相应的ASCII十进制值。
注意,在ASCII序列中,空格字符前面的字符都是非打印字符,要特殊处理这些字符。
如果非打印字符是换行符或制表符,则分别打印\n或\t。
否则,使用控制字符表示法。
例如,ASCII的1是Ctrl+A,可显示为^A。
注意,A的ASCII值是Ctrl+A的值加上 64。
其他非打印字符也有类似的关系。
除每次遇到换行符打印新的一行之外,每行打印10对值。
(注意:不同的操作系统其控制字符可能不同。)
# include <stdio.h>
int main(void)
{
int ch;
int i = 0;
while ((ch = getchar()) != EOF)
{
if (ch < ' ')
switch (ch)
{
case '\n':
printf("\\n");
break;
case '\t':
printf("\\t");
break;
default:
printf("^%c:%d", ch + 64, ch);
break;
}
else
printf("%c:%d ", ch, ch);
i++;
if (i % 10 == 0)
putchar('\n');
}
return 0;
}
3、
编写一个程序,在遇到 EOF 之前,把输入作为字符流读取。该程序 要报告输入中的大写字母和小写字母的个数。假设大小写字母数值是连续 的。或者使用ctype.h库中合适的分类函数更方便。
# include <stdio.h>
# include <ctype.h>
int main(void)
{
int ch;
int i = 0,j = 0;
while ((ch = getchar()) != EOF)
{
if (isupper(ch))
i++;
else if (islower(ch))
j++;
}
printf("%d %d\n", i, j);
return 0;
}
/*
abcd123AB
^Z
2 4
*/
4、
编写一个程序,在遇到EOF之前,把输入作为字符流读取。
该程序要报告平均每个单词的字母数。
不要把空白统计为单词的字母。
实际上,标点符号也不应该统计,但是现在暂时不同考虑这么多
(如果你比较在意这点, 考虑使用ctype.h系列中的ispunct()函数)。
# include <stdio.h>
# include <ctype.h>
int main(void)
{
int ch;
float i = 0;
float total = 0;
float num = 0;
while ((ch = getchar()) != EOF)
{
if(isalpha(ch))
{
i = i + 1;
}
else
{
num = num + 1;
printf("%.2f\n", i);
total = total + i;
i = 0;
}
}
printf("words:%.2f total:%.2f average:%.2f\n",num,total,total / num);
return 0;
}
/*
nothing compares to you
7.00
8.00
2.00
3.00
^Z
words:4.00 total:20.00 average:5.00
*/
5、
修改程序清单8.4的猜数字程序,使用更智能的猜测策略。
例如,程序最初猜50,询问用户是猜大了、猜小了还是猜对了。
如果猜小了,那么下一 次猜测的值应是50和100中值,也就是75。
如果这次猜大了,那么下一次猜测的值应是50和75的中值,等等。
使用二分查找(binary search)策略,如果用户没有欺骗程序,那么程序很快就会猜到正确的答案。
# include <stdio.h>
# include <ctype.h>
int main(void)
{
int guess = 50;
int upper = 100;
int lower = 0;
char ch;
printf("Pick an integer from 1 to 100. I will try to guess it.\n");
printf("Let's begin!\n");
printf("Uh...is your number %d?(y/n)\n", guess);
while ((ch = getchar()))
{
if (!isalpha(ch))
continue;
if (ch == 'y')
break;
printf("bigger or smaller?(b/s): ");
while ((ch = getchar()))
{
if (!isalpha(ch))
continue;
switch (ch)
{
case 'b':
lower = guess;
guess = guess + ((upper - lower) / 2);
break;
case 's':
upper = guess;
guess = guess - ((upper - lower) / 2);
break;
default:
printf("bigger or smaller?(b/s): ");
continue;
}
break;
}
printf("is it %d?(y/n)\n", guess);
}
printf("OK!\n");
return 0;
}
6、
修改程序清单8.8中的get_first()函数,
让该函数返回读取的第1个非空白字符,
并在一个简单的程序中测试。
# include <stdio.h>
# include <ctype.h>
char get_first(void);
int main(void)
{
char ch;
while ((ch = get_first()) != EOF)
putchar(ch);
return 0;
}
char get_first(void)
{
int ch;
while (isblank(ch = getchar()));
while (getchar() != '\n');
return ch;
}
7、
修改第7章的编程练习8,用字符代替数字标记菜单的选项。
用q代替5作为结束输入的标记。
//堆栈问题 ‘q’ 为解决
#include <stdio.h>
#define BT 40
#define OT 1.5
#define T1 0.15
#define T2 0.2
#define T3 0.25
#define A1 300
#define A2 150
#define BW1 8.75
#define BW2 9.33
#define BW3 10.00
#define BW4 11.20
void menu();
int main(void)
{
char ch;
float t;
float BW;
float bw, tax, net;
menu();
printf("Your choice:");
scanf("%s",&ch);
while (ch != 'q') //输入q发生ch堆栈错误 暂未解决
{
if (ch > 96 && ch < 101)
{
switch (ch)
{
case 97:
BW = BW1;
break;
case 98:
BW = BW2;
break;
case 99:
BW = BW3;
break;
case 100:
BW = BW4;
break;
}
printf("please enter your hours:");
scanf("%f", &t);
if (t > 0)
{
if (t <= BT)
bw = t * BW;
else
bw = BT * BW + (t - BT) * OT * BW;
if (bw < A1)
tax = bw * T1;
else if (bw > A1 && bw <= (A1 + A2))
tax = A1 * T1 + (bw - A1) * T2;
else
tax = A1 * T1 + A2 * T2 + (bw - A1 - A2) * T3;
net = bw - tax;
printf("Wage is %.2f Taxex is %.2f Net is %.2f\n", bw, tax, net);
}
else
{
printf("Your hours too short\n");
printf("Please re-enter!\n");
/*menu();
printf("Your choice:");
scanf("%s", &ch);*/
}
}
else
{
printf("Please re-enter!\n");
}
menu();
printf("Your choice:");
scanf("%s", &ch);
}
return 0;
}
void menu()
{
printf("Enter the number corresponding to the desired pay rate or action:\n");
printf("a) $8.75 / hr\n");
printf("b) $9.33 / hr\n");
printf("c) $10.00 / hr\n");
printf("d) $11.20 / hr\n");
printf("q) quit\n");
}
8、
编写一个程序,显示一个提供加法、减法、乘法、除法的菜单。
获得用户选择的选项后,程序提示用户输入两个数字,然后执行用户刚才选择的操作。
该程序只接受菜单提供的选项。
程序使用float类型的变量储存用户输入的数字,如果用户输入失败,则允许再次输入。
进行除法运算时,如果用户输入0作为第2个数(除数),程序应提示用户重新输入一个新值。
该程序 的一个运行示例如下:
Enter the operation of your choice:
a. add s. subtract
m. multiply d. divide
q. quit
a
Enter first number: 22 .4
Enter second number: one
one is not an number.
Please enter a number, such as 2.5, -1.78E8, or 3: 1
22.4 + 1 = 23.4
Enter the operation of your choice:
a. add s. subtract
m. multiply d. divide
q. quit
d
Enter first number: 18.4
Enter second number: 0
Enter a number other than 0: 0.2
18.4 / 0.2 = 92
Enter the operation of your choice:
a. add s. subtract
m. multiply d. divide
q. quit
q
Bye.
//堆栈问题 ‘q’ 为解决
#include <stdio.h>
#include <ctype.h>
void menu();
int main(void)
{
char ch;
float a, b;
float results;
menu();
printf("your choice:");
scanf("%s", &ch);
do
{
printf("Enter first number: ");
scanf("%f", &a);
printf("Enter second number: ");
scanf("%f", &b);
if (b == 0)
{
printf("Please re-enter!\n");
printf("Enter second number: ");
scanf("%f", &b);
}
switch(ch)
{
case 'a':
results = a + b;
break;
case 'b':
results = a - b;
break;
case 'c':
results = a * b;
break;
case 'd':
results = a / b;
break;
}
printf("results:%.2f\n", results);
menu();
printf("your choice:");
scanf("%s", &ch);
} while (ch != 'q');
printf("Bye!\n");
return 0;
}
void menu()
{
printf("Enter the operation of your choice:\n");
printf("a) add\n");
printf("b) subtract\n");
printf("c) multiply\n");
printf("d) divide\n");
printf("q) quit\n");
}