C primer plus(第六版)第八章源代码
程序员文章站
2022-06-21 10:46:35
...
第八章
/* 8.1 */
#include<stdio.h>
int main(void)
{
char ch;
while ((ch = getchar()) != '#')
putchar(ch);
return 0;
}
/* 8.2 */
#include<stdio.h>
int main(void)
{
int ch;
while ((ch = getchar()) != EOF)
putchar(ch);
return 0;
}
/* 8.3 */
#include<stdio.h>
#include<stdlib.h>
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);
}
while ((ch = getc(fp)) != EOF)
putchar(ch);
fclose(fp);
return 0;
}
/* 8.4 */
#include<stdio.h>
int main(void)
{
int guess = 1;
printf("Pick an integer from 1 to 100. I will try to guess ");
printf("it.\nRespond with a y if my guess is right and with");
printf("\nan n if it is wrong.\n");
printf("Uh...is your number %d?\n",guess);
while (getchar() != 'y')
printf("Well, then, is it %d?\n", ++guess);
printf("I know i could do it!\n");
return 0;
}
/* 8.5 */
#include<stdio.h>
void display(char cr, int lines, int width);
int main(void)
{
int ch;
int rows, cols;
printf("Enter a character and two integers:\n");
while ((ch = getchar()) != '\n')
{
scanf("%d %d", &rows, &cols);
display(ch, rows, cols);
printf("Enter another character and two integers:\n");
printf("Enter a newline to quit.\n");
}
printf("Bye.\n");
return 0;
}
void display(char cr, int lines, int width)
{
int row, col;
for (row = 1; row <= lines; row++)
{
for (col = 1; col <= width; col++)
putchar(cr);
putchar('\n');
}
}
/* 8.6 */
#include<stdio.h>
void display(char cr, int lines, int width);
int main(void)
{
int ch;
int rows, cols;
printf("Enter a character and two integers:\n");
while ((ch = getchar()) != '\n')
{
if (scanf("%d %d", &rows, &cols) != 2)
break;
display(ch, rows, cols);
while (getchar() != '\n')
continue;
printf("Enter another character and two integers:\n");
printf("Enter a newline to quit.\n");
}
printf("Bye.\n");
return 0;
}
void display(char cr, int lines, int width)
{
int row, col;
for (row = 1; row <= lines; row++)
{
for (col = 1; col <= width; col++)
putchar(cr);
putchar('\n');
}
}
/* 8.7 */
#include<stdio.h>
#include<stdbool.h>
//验证输入是一个整数
long get_long(void);
//验证范围的上下限是否有效
bool bad_limits(long begin, long end,
long low, long high);
//计算a~b之间的整数平方和
double sum_square(long a, long b);
int main(void)
{
const long MIN = -10000000L;
const long MAX = +10000000L;
long start;
long stop;
double answer;
printf("This program computes the sum of the squares of "
"integers in a range.\nThe lower bound should not "
"be less than -10000000 and\nthe upper bound "
"should not be more than +10000000.\nEnter the "
"limits (enter 0 for both limits to quit):\n"
"lower limit: ");
start = get_long();
printf("upper limit: ");
stop = get_long();
while (start != 0 || stop != 0)
{
if (bad_limits(start, stop, MIN, MAX))
printf("Please try again.\n");
else
{
answer = sum_square(start, stop);
printf("The sum of the squares of the integers ");
printf("from %ld to %ld is %g\n",
start, stop, answer);
}
printf("Enter the limits (enter 0 for both "
"limits to quit):\n");
printf("lower limit: ");
start = get_long();
printf("upper limit: ");
stop = get_long();
}
printf("Done.\n");
return 0;
}
long get_long(void)
{
long input;
char ch;
while (scanf("%ld",&input) != 1)
{
while ((ch = getchar()) != '\n')
putchar(ch);
printf(" is not an integer.\nPlease enter an ");
printf("integer value, such as 25, -178, or 3: ");
}
return input;
}
double sum_square(long a,long b)
{
double total = 0;
long i;
for (i = a; i <= b; i++)
total += (double) i * (double) i;
return total;
}
bool bad_limits(long begin, long end,
long low, long high)
{
bool not_good = false;
if (begin > end)
{
printf("%ld isn't smaller than %ld.\n",begin, end);
not_good = true;
}
if (begin < low || end <low)
{
printf("Values must be %ld or greater.\n",low);
not_good = true;
}
if (begin > high || end > high)
{
printf("Values must be %ld or less.\n",high);
not_good = true;
}
return not_good;
}
/* 8.8 */
#include<stdio.h>
char get_choice(void);
char get_first(void);
int get_int(void);
void count(void);
int main(void)
{
int choice;
void count(void);
while ((choice = get_choice()) != 'q')
{
switch (choice)
{
case 'a': printf("Buy low, sell high.\n");
break;
case 'b': putchar('\a');
break;
case 'c': count();
break;
default : printf("Program error!\n");
break;
}
}
printf("Bye.\n");
return 0;
}
void count(void)
{
int n, i;
printf("Count how far? Enter an integer:\n");
n = get_int();
for (i = 1;i <= n;i++)
printf("%d\n", i);
while (getchar() != '\n')
continue;
}
char get_choice(void)
{
int ch;
printf("Enter the letter of your choice:\n");
printf("a. advic b. bell\n");
printf("c. count q. quit\n");
ch = get_first();
while ((ch < 'a' || ch > 'c') && ch != 'q')
{
printf("Please respond with a, b, c, or q.\n");
ch = get_first();
}
return ch;
}
char get_first(void)
{
int ch;
ch = getchar();
while (getchar() != '\n')
continue;
return ch;
}
int get_int(void)
{
int input;
char ch;
while (scanf("%d", &input) != 1)
{
while ((ch = getchar()) != '\n')
putchar(ch);
printf(" is not an integer.\nPlease enter an");
printf("integer value, such as 25, -178, or 3: ");
}
return input;
}
上一篇: TreeMap源代码分析
推荐阅读
-
C++Primer Plus 习题_第四章
-
C++Primer(第10章课后程序题源代码)
-
c++ primer plus第五版读书笔记
-
C++Primer Plus 编程练习_第五章
-
C Primer Plus学习之C控制语句(分支和跳转)
-
C++ Primer Plus复习题及答案
-
[C++ Primer Plus]学习笔记--关于C++ string和c类型字符数组的对比
-
《C++ Primer Plus》学习笔记——第五章 循环和关系表达式(四)
-
《C++ Primer Plus》学习笔记——第四章 复合类型(三)
-
《C++ Primer Plus》学习笔记——第五章 循环和关系表达式(一)