C Primer Plus第六版第六章循环 源码

   声明:本站部分内容来自互联网,如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

28


源码


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