C Primer Plus第六版第八章字符,輸入輸出驗證 源碼

//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.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.4
#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.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); 
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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章