C primer plus(第六版) 第七章 分支和跳轉 源碼


//7.1
#include<stdio.h>
int main(void)
{
	const int FREEZING = 0;
	float temperature;
	int cold_days = 0;
	int all_days = 0;
	
	printf("Enter the list of daily low temperature.\n");
	printf("Use Celsius, and enter q to quit.\n");
	while (scanf("%f",&temperature) == 1)
	{
		all_days++;
		if (temperature < FREEZING)
			cold_days++;
	} 
	if (all_days != 0)
		printf("%d days total: %.1f%% were below freezing.\n",
			all_days, 100.0 * (float) cold_days / all_days);
	if (all_days == 0)
		printf("No data entered!\n");
		
	return 0;  
} 
/*   7.2   */
#include<stdio.h>
#define SPACE ' '
int main(void)
{
	char ch;
	
	ch = getchar();          //讀取一個字符 
	while (ch != '\n')       //當一行未結束 
	{
		if (ch == SPACE)    //留下空格 
			putchar(ch);    //該字符不變 
		else
			putchar(ch + 1);//改變其他字符 
		ch = getchar();     //獲取下一個字符 
	}
	putchar(ch);           //打印換行符 
	
	return 0;
}
/*      7.3      */
#include<stdio.h>
int main(void)
{
	char ch;
	
	while ((ch = getchar()) != '\n')
	{
		if (isalpha(ch))    //如果是一個字符 
			putchar(ch + 1); 
		else
			putchar(ch);
	}
	putchar(ch);
	
	return 0;
}
/*    7.4     */
#include<stdio.h>
#define RATE1 0.13230
#define RATE2 0.15040
#define RATE3 0.30025
#define RATE4 0.34025
#define BREAK1 360.0
#define BREAK2 468.0
#define BREAK3 720.0
#define BASE1 (RATE1 * BREAK1)
#define BASE2 (BASE1 + (RATE2 * (BREAK2 - BREAK1)))
#define BASE3 (BASE1 + BASE2 + (RATE3 * (BREAK3 - BREAK2)))
int main(void)
{
	double kwh;
	double bill;
	
	printf("Please enter the kwh used.\n");
	scanf("%lf",&kwh);
	if (kwh <= BREAK1)
		bill = RATE1 * kwh;
	else if (kwh <= BREAK2)
		bill = BASE1 + (RATE2 * (kwh - BREAK1));
	else if (kwh <= BREAK3)
		bill = BASE2 + (RATE3 * (kwh - BREAK2));
	else
		bill = BASE3 + (RATE4 * (kwh - BREAK3));
	printf("The charge for %.1f kwh is $%1.2f.\n",kwh, bill);
	
	return 0; 
} 
/*     7.5      */
#include<stdio.h>
#include<stdbool.h>
int main(void)
{
	unsigned long num;
	unsigned long div;
	bool isPrime;
	
	printf("Please enter an integer for analysis: ");
	printf("Enter q to quit.\n");
	while (scanf("%lu",&num) == 1)
	{
		for (div = 2,isPrime = true; (div * div) <= num;div++)
		{
			if (num % div == 0)
			{
				if(div * div != num)
				printf("%lu is divisible by %lu and %lu.\n",
						num, div, num / div);
				else
					printf("%lu is divisible by %lu.\n",
						num, div);
				isPrime = false;
			}
		}
	if (isPrime)
		printf("%lu is prime.\n",num);
	printf("Please enter another integer for analysis: ");
	printf("Enter q to quit.\n");
	}
	printf("Bye.\n");
	
	return 0;
}
/*   7.6     */
#include<stdio.h>
#define PERIOD '.'
int main(void)
{
	char ch;
	int charcount = 0;
	
	while ((ch = getchar()) != PERIOD)
	{
		if (ch != '"' && ch != '\'')
			charcount++;
	}
	printf("There are %d non-quote characters.\n",charcount);
	
	return 0;
} 
/*    7.7     */
#include<stdio.h>
#include<ctype.h>
#include<stdbool.h>
#define STOP '|'
int main(void)
{
	char c;
	char prev;
	long n_chars = 0L;
	int n_lines = 0;
	int n_words = 0;
	int p_lines = 0;
	bool inword = false;
	
	printf("Enter text to be analyzed (| to derminate):\n");
	prev = '\n';
	while ((c = getchar()) != STOP)
	{
		n_chars++;
		if (c == '\n')
			n_lines++;
		if (!isspace(c) && !inword)
		{
			inword = true;
			n_words++;
		}
		if (isspace(c) && inword)
			inword = false;
		prev = c;
	}
	if (prev != '\n')
		p_lines = 1;
	printf("characters = %ld, words = %d, lines = %d, ",
			n_chars, n_words, n_lines);
	printf("partial lines = %d\n",p_lines);
	
	return 0;
} 
/*   7.8   */
#include<stdio.h>
#define COVERAGE 350
int main(void)
{
	int sq_feet;
	int cans;
	
	printf("Enter number of square feet to be painted:\n");
	while (scanf("%d",&sq_feet) == 1)
	{
		cans = sq_feet / COVERAGE;
		cans += ((sq_feet % COVERAGE == 0)) ? 0: 1;
		printf("You need %d %s of paint.\n",cans,
				cans == 1 ? "can" : "cans");
		printf("Enter next value (q to quit):\n");
	}
	
	return 0;
}
/*   7.9    */
#include<stdio.h>
int main(void)
{
	const float MIN = 0.0f;
	const float MAX = 100.0f;
	
	float score;
	float total = 0.0f;
	int n = 0;
	float min = MAX;
	float max = MIN;
	
	printf("Enter the first score (q to quit):");
	while (scanf("%f",&score) == 1)
	{
		if (score < MIN || score > MAX)
		{
			printf("%0.1f is an invalid value. Try again: ",score);
			continue;
		}
		printf("Accepting %0.1f:\n",score);
		min = (score < min) ? score : min;
		max = (score > max) ? score : max;
		total += score;
		n++;
		printf("Enter next score (q to quit): ");
	}
	if (n > 0)
	{
		printf("Average of %d scores is %0.1f.\n", n, total/n);
		printf("Low = %0.1f, high = %0.1f\n", min, max);
	}
	else
		printf("No valid scores were entered.\n");
		
	return 0;
} 
/*   7.10    */
#include<stdio.h>
int main(void)
{
	float length, width;
	
	printf("Enter the length of the rectangle:\n");
	while (scanf("%f",&length) == 1)
	{
		printf("Length = %0.2f:\n",length);
		printf("Enter its width:\n");
		if (scanf("%f",&width) != 1)
			break;
		printf("Width = %0.2f:\n",width);
		printf("Area = %0.2f:\n",length * width);
		printf("Enter the length of the rectangle:\n");
	}
	printf("Done.\n");
	
	return 0;
}


 

/*    7.11    */
#include<stdio.h>
#include<ctype.h>
int main(void)
{
	char ch;
	
	printf("Give me a letter of the alphabet, and i will give ");
	printf("an animal name\nbeginning with that letter.\n");
	printf("Please type in a letter; type # to end my act.\n");
	while ((ch = getchar()) != '#')
	{
		if ('\n' == ch)
			continue;
		if (islower(ch))
			switch (ch)
		{
			case 'a':
				printf("argali, a wild sheep of Asia\n");
				break;
			case 'b':
				printf("babirusa, a wild pig of Malay\n");
			case 'c':
				printf("coati, racoonlike mammal\n");
				break;
			case 'd':
				printf("desman, aquatic, molelike critter\n");
				break;
			case 'e':
				printf("echidna, the spiny anteater\n");
				break;
			case 'f':
				printf("fisher, brownish marten\n");
				break;
			default:
				printf("That's a stumper!\n");
		}
		else
			printf("I recognize only lowercase letters.\n");
		while (getchar() != '\n')  
			continue;            //跳過輸入行的剩餘部分 
		printf("Please type another letter or a #.\n");
	}
	printf("Bye!\n");
	
	return 0;
}
/*    7.12    */
#include<stdio.h>
int main(void)
{
	char ch;
	int a_ct, e_ct, i_ct, o_ct, u_ct;
	
	a_ct = e_ct = i_ct = o_ct = u_ct = 0;
	
	printf("Enter some text; enter # to quit.\n");
	while ((ch = getchar()) != '#')
	{
		switch (ch)
		{
			case 'a':
			case 'A': a_ct++;
					  break;
			case 'e':
			case 'E': e_ct++;
					  break;
			case 'i':
			case 'I': i_ct++;
					  break;
			case 'o':
			case 'O': o_ct++;
					  break;
			case 'u':
			case 'U': u_ct++;
					  break;
			default : break; 
		}
	}
	printf("number of vowels: A    E    I    O   U\n");
	printf("               %4d %4d %4d %4d %4d\n",
			a_ct, e_ct, i_ct, o_ct, u_ct);
			
	return 0;
}

 

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