C Primer Plus 第六章程序清单——2015.4.22

来源:互联网 发布:淘宝小号淘气值查看 编辑:程序博客网 时间:2024/05/17 22:17

C Primer Plus 
                        ——普拉塔(第五版)

今天在编写以金字塔形式输出字符时被困好长时间,不知怎么的换行符一直没起到预期的作用。

第六章 C控制语句:循环

程序清单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\n");
status=scanf("%ld",&num);
while(status==1)
{
sum=sum+num;
printf("Pleaase enter next integer (q to quit):\n ");
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("n = %d\n",n);
}
printf("The loop has finished.\n");
return 0;
}


程序清单6.3
#include<stdio.h>
int main(void)
{
int n=0;
while(n<3)
{
printf("n is %d\n",n);
   n++;
}
printf("That's all this program does\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<stdio.h>
#include<math.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("Pleaase 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("Thoes 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:");
input_is_good =(scanf("%ld",&sum)==1);
}
printf("Thoes integer 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 valent!\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 myValent!\n");
return 0;
}
程序清单6.12
#include<stdio.h>
int main(void)
{
int num;
printf("   n   ncubed\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=37;
const int NEXT_OZ=23;
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.13
#include<stdio.h>
int main(void)
{
int t_ct;
double time,x;
int limit;
printf("Enter the number of terms you want: ");
scanf("%d",&limit);
for(time=0,x=1,t_ct=1;t_ct<=limit;t_ct++,x*=2.0)
{
time+=1.0/x;
printf("time = %f when terms =%d.\n",time,t_ct);
}
return 0;
}
程序清单6.14
#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.15
#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 triskaidekaphbia therapy club,\n");
printf("please enter he secret code number:");
scanf("%d",&code_entered);
}
printf("Congratulations! You are cured!\n");
return 0;
}
程序清单6.16
#include<stdio.h>
#define ROWS 6
#define CHARS 80
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.17
#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.18
#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 score:\n",SIZE);
for(index=0;index<SIZE;index++)
   scanf("%d",&score[index]);
printf("The scores 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 postive 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("quit\n");
}
printf("Hope you enjoyed y=this poer 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;
}
0 0
原创粉丝点击