while()循環、基本運算符、平方、指數增加、

一、while(條件)ide

{代碼塊}函數

示例程序:blog

#include<stdio.h>
#define ADJUST 7.64
#define SCALE 0.325
int main()
{
double shoe,foot;
printf("shoe size (men's)     foot length\n");
shoe=3.0;
while(shoe<18.5)
{ foot=SCALE*shoe+ADJUST;
printf("%10.2f %15.2f inches\n",shoe,foot);
shoe=shoe+1.0;
}
printf("if the shoe fits,wear it.\n");
return 0;
}

運行結果:get

wKiom1l74G-xbc33AACV_ISSmjI782.png

二、基本運算符:=(賦值運算符)、+、-、*、/,c木有指定指數運算符,可是提供了pow()函數,例如pow(3.5,2.2),返回值爲3.5的2.2次冪。it

實例程序:緊接着如上:io

#include<stdio.h>
#define ADJUST 7.64
#define SCALE 0.325
int main()
{
double shoe,foot;
int jane,tarzan,cheeta;
int num=1;
printf("shoe size (men's)     foot length\n");
shoe=3.0;
while(shoe<18.5)
{ foot=SCALE*shoe+ADJUST;
printf("%10.2f %15.2f inches\n",shoe,foot);
shoe=shoe+1.0;
}
printf("if the shoe fits,wear it.\n");
/*三重賦值*/
cheeta=tarzan=jane=68;
printf("                   cheeta tarzan jean\n");
printf("first round score %4d %8d %8d\n",cheeta,tarzan,jane);//多種程序語言將在這裏的三重賦值處卡殼,可是c能夠順利接受它,賦值是從右向左依次進行的,先是jane獲得68,而後是Tarzan,cheeta.
/*加減法,他們被稱爲二元或者雙值運算符,由於他們的運算須要兩個操做數*/
printf("%d\n",4+20);
printf("%f\n",224.00-24.00);
//符號運算符:-和+,如rocky=-12;當這樣使用-號時,稱爲一元運算符,表示他只需一個操做數。
/*c沒有計算平方的函數,能夠經過如下程序實現*/
while (num<21)
{
printf("%4d %6d\n",num,num*num);
num=num+1;
}
return 0;
}

運行結果:
class

wKiom1l76QjzgkV1AABUvDvcIHc774.png

三、指數增加:程序

故事:有位強大的統治者想獎勵一個作出巨大貢獻的學者,學者指着棋盤說,在第一個方格里放一粒小麥,第二個放2粒,第三個放4粒,依次類推。im

實例程序:db

#include<stdio.h>
#define SQUARES 64
#define CROP 1E15//以粒記得美國的小麥產量
int main()
{
  double current,total;
  int count=1;
  printf("square grains total");
  printf("fraction of \n");
  printf("      added      grain     ");
  printf("US total\n");
  total=current=1.0;/*開始時是1粒*/
  printf("%4d  %13.2e  %12.2e  %12.2e\n",count,current,total,total/CROP);
  while(count<SQUARES)
  {
  count=count+1;
  current=2.0*current;
  total=total+current;
  printf("%4d  %13.2e %12.2e %12.2e\n",count,current,total,total/CROP);
  }
  printf("that's all.\n");
  return 0;
  }


運行程序:

wKioL1l7_jejJAqTAABsRxUQ6Ew838.png


wKiom1l7_r3QgMYbAABFxbG1LoM086.png


這個例子演示了指數增加的現象。世界人口的增加和咱們對能源的使用都遵循一樣的模式。

相關文章
相關標籤/搜索