C Primer Plus第六版第六章循環 源碼


源碼


/*  6.1   */
#include<stdio.h>
int main(void)
{
	long num;
	long sum = 0L;
	int status;
	
	printf("Please enter an integer to be summed ");
	printf("(q to quit): ");
	status = scanf("%ld",&num);         
	while (status == 1)
	{
		sum = sum + num;
		printf("Please enter next integer (q to quit): ");
		status = scanf("%ld",&num);
	}
	printf("Those integers sum to %ld.\n",sum);
	
	return 0;
}
/*   6.2   */
#include<stdio.h>
int main(void)
{
	int n = 5;
	
	while (n < 7)
	{
		printf("n = %d\n",n);
		n++;
		printf("Now n = %d\n",n);
	}
	printf("The loop has finished.\n");
	
	return 0;
}
/* 6.4*/
#include<stdio.h>
int main(void)
{
	int n = 0;	
	while (n++ < 3);
		printf("n is %d\n",n);
	printf("That's all this program does.\n");
	
	return 0;
}
/*      6.5    */
#include<math.h>
#include<stdio.h>
int main(void)
{
	const double ANSWER = 3.14159;
	double response;
	
	printf("What is the value of pi?\n");
	scanf("%lf",&response);
	while(fabs(response - ANSWER) > 0.0001)
	{
		printf("Try again!\n");
		scanf("%lf",&response);
	}
	printf("Close enough!\n");
	
	return 0;
}
/*   6.6   */
#include<stdio.h>
int main(void)
{
	int true_val, false_val;
	
	true_val = (10 > 2);
	false_val = (10 == 2);
	printf("true = %d; false = %d \n",true_val, false_val);
	
	return 0;
}
/*    6.7     */
#include<stdio.h>
int main(void)
{
	int n = 3;
	
	while (n)
		printf("%2d is true\n",n--);
	printf("%2d is false\n",n);
	
	n = -3;
	while (n)
		printf("%2d is true\n",n++);
	printf("%2d is false\n",n);
	
	return 0;
}
/*      6.8     */
#include<stdio.h>
int main(void)
{
	long num;
	long sum = 0L;
	int status;
	
	printf("Please enter an integer to be summed ");
	printf("(q to quit): ");
	status = scanf("%ld",&num);
	while (status = 1)
	{
		sum = sum + num;
		printf("Please enter next integer (q to quit): ");
		status = scanf("%ld",&num);
	}
	printf("Those integers sum to %ld.\n",sum);
	
	return 0;
} 
/*     6.9   */
#include<stdio.h>
int main(void)
{
	long num;
	long sum = 0L;
	_Bool input_is_good;
	
	printf("Please enter an integer to be summed ");
	printf("(q to quit): ");
	input_is_good = (scanf("%ld",&num) == 1);
	while (input_is_good)
	{
		sum = sum + num;
		printf("Please enter next integer (q to quit): ");
		input_is_good = (scanf("%ld",&num) == 1);
	}
	printf("Those integers sum to %ld.\n",sum);
	
	return 0;
}
/*     6.10     */
#include<stdio.h>
int main(void)
{
	const int NUMBER = 22;
	int count = 1;
	
	while (count <= NUMBER)
	{
		printf("Be my valentine!\n");
		count++;
	}
	
	return 0;
}
/*   6.11    */
#include<stdio.h>
int main(void)
{
	const int NUMBER = 22;
	int count;
	
	for (count = 1; count <=NUMBER; count++)
		printf("Be my Valentine!\n");
	
	return 0;
}
/*     6.12    */
#include<stdio.h>
int main(void)
{
	int num;
	
	printf("    n   n cubed\n");
	for (num = 1; num <= 6; num++)
		printf("%5d %5d\n",num,num*num*num);
		
		return 0;
}
/*   6.13   */
#include<stdio.h>
int main(void)
{
	const int FIRST_OZ = 46;
	const int NEXT_OZ = 20;
	int ounces, cost;
	
	printf(" ounces cost\n");
	for (ounces = 1, cost = FIRST_OZ; ounces <= 16; ounces++,cost += NEXT_OZ)
		printf("%5d  $%4.2f\n",ounces, cost/100.0);
		
	return 0;
		
}
/*     6.14    */
#include<stdio.h>
int main(void)
{
	int t_ct;
	double time, power_of_2;
	int limit;
	
	printf("Enter the number of terms your want: ");
	scanf("%d",&limit);
	for (time = 0, power_of_2 = 1, t_ct = 1; t_ct <= limit;
								t_ct++,power_of_2 *= 2.0)
	{
		time += 1.0 / power_of_2;
		printf("time = %f when terms = %d.\n",time, t_ct);
	}
	
	return 0;
} 
/*  6.15   */
#include<stdio.h>
int main(void)
{
	const int secret_code = 13;
	int code_entered;
	
	do
	{
		printf("To enter the triskaidekaphobia therapy club,\n");
		printf("please enter the secret code number: ");
		scanf("%d",&code_entered);
	} while(code_entered != secret_code);
	printf("Congratulations! You are cured!\n");
	
	return 0;
}
/*    6.16    */
#include<stdio.h>
int main(void)
{
	const int secret_code = 13;
	int code_entered;
	
	printf("To enter the triskaidekaphobia therapy club,\n");
	printf("please enter the secret code number: ");
	scanf("%d",&code_entered);
	while (code_entered != secret_code)
	{
		printf("To enter the triskaidekaphobia therapy club,\n");
		printf("please enter the secret code number: ");
		scanf("%d",&code_entered);
	}
	printf("Congratulations! You are cured!\n");
	
	return 0;
}
/*   6.17   */
#include<stdio.h>
#define ROWS 6
#define CHARS 10
int main(void)
{
	int row;
	char ch;
	
	for (row = 0; row < ROWS; row++)
	{
		for (ch = 'A'; ch <('A'+CHARS); ch++)
			printf("%c", ch);
		printf("\n");
	}
	
	return 0;
}
/*   6.18   */
#include<stdio.h>
int main(void)
{
	const int ROWS = 6;
	const int CHARS = 6;
	int row;
	char ch;
	
	for (row = 0; row < ROWS; row++)
	{
		for (ch = ('A' + row); ch <('A' + CHARS); ch++)
			printf("%c", ch);
		printf("\n");
	}
	
	return 0;
}
/*   6.19  */
#include<stdio.h>
#define SIZE 10
#define PAR 72
int main(void)
{
	int index, score[SIZE];
	int sum = 0;
	float average;
	
	printf("Enter %d golf scores:\n",SIZE);
	for (index = 0; index < SIZE; index++)
		scanf("%d",&score[index]);
	printf("The score read in are as follows:\n");
	for (index < 0;index < SIZE; index++)
		printf("%5d",score[index]);
	printf("\n");
	for (index = 0; index < SIZE; index++)
		sum += score[index];
	average = (float)sum / SIZE;
	printf("Sum of scores = %d, average = %.2f\n", sum, average);
	printf("That's a handicap of %.0f.\n",average - PAR);
	
	return 0;
}
/*    6.20    */
#include<stdio.h>
double power(double n,int p);
int main(void)
{
	double x, xpow;
	int exp;
	
	printf("Enter a number and the positive integer power");
	printf(" to which\nthe number will be raised. Enter q");
	printf(" to quit.\n");
	while (scanf("%lf%d",&x, &exp) == 2)
	{
		xpow = power(x, exp);
		printf("%.3g to the power %d is %.5g\n",x, exp, xpow);
		printf("Enter next pair of numbers or q to quit.\n");
	}
	printf("Hope you enjoyed this power trip -- bye!\n");
	
	return 0;
}        
double power(double n,int p)
{
	double pow = 1;
	int i;
	
	for (i = 1;i <= p; i++)
		pow *= n;
		
	return pow;
}

 

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