下面的是我上C语言的的笔记:

C语言学起来,感觉记不得的,而且每次都有新知识,真的学得有点蛋疼了...

不过我相信努力还是会成功的!

第一个例子:测试身体健康程序;

输入的格式如:h  —— 1.60     w ——  50

 

#include <stdio.h>

main()

{

    float h,w,t;

    printf("Please enter h.w:");

    scanf("%f%f",&h,&w);

    t = w/(h*h);

    if(t<18)   //如果t<18的话就输出下面那句

        printf("t=%7.2f\tLower weight!\n",t);

    else if (t<25)   //再如果t<25就输出下面那句

        printf("t=%7.2f\tStandard weight!\n",t);

    else if (t<27)    //再如果t<27就输出下面那句

       printf("t=%7.2f\tHigher weight!\n",t);

    else     //否则就输出最后这句

        printf("t=%7.2f\tToo fat!\n",t);

        getch();   //这个语句只是在win-tc软件下测试用的,

}

第二个例子:也是测试身体健康程序;

输入的格式如:h  —— 1.60     w ——  50

这次用到switch循环语句

#include <stdio.h>
 
main()
{
    float h,w,t; int k;
    printf("Please enter h.w:");
    scanf("%f%f",&h,&w);
    t = w/(h*h);
    k=1*(t<18)+2*(t>=18&&t<25)+3*(t>=25&&t<27)+4*(t>=27);
    switch (k)  
//switch语句是先设定好条件,然后逐步判断,若条件都不符合case里面的条件的话,就输出default的值!
    {
    case 1:printf("t=%7.2f\tLower weight!\n",t);
    break;
    case 2:printf("t=%7.2f\tStandard weight!\n",t);
    break;
    case 3:printf("t=%7.2f\tHigher weight!\n",t);
    break;
    case 4:printf("t=%7.2f\tToo fat!\n",t);
    break;
    default: ;
 
 
}
}
 
第三个例子:
排序....
 
#include <stdio.h>
 
main()
{
    int a=0,b=0,c=0,t=0;
    printf("Please enter a,b,c:");
    scanf("%d%d%d",&a,&b,&c);
   printf("%d%d%d\n",a,b,c);
   if(a>b)
   {t=a;a=b;b=t;}
   if(b>c)
   {t=b;b=c;c=t;}
   if(a>b)
   {t=a;a=b;b=t;}
   printf("%d%d%d\n",a,b,c);
//这题的实现步骤是先两个值互相判断大小,然后互换位置.. 
    getch();
 
}
 
 
第四个例子:求直角三角形的体积和周长:并前提是判断三边能否构成三角形
#include<stdio.h>
#include<math.h>
main() 
{
float a,b,c,p ,l;
double  s;
printf("please input the a,b,c:\n"); 
scanf("%f%f%f",&a,&b,&c);
if (a*a+b*b==c*c||b*b+c*c==a*a||c*c+a*a==b*b)
//这题数字基础是不可缺少的,至少要知道直角三角形的构入条件:就是边*边+边*边=边*边 , ||是或者的意思 , &&的话就是与的意思
{
l = a+b+c;   //求周长的公式
p=(a+b+c)/2.0;    
s =sqrt(p*(p-a)*(p-b)*(p-c)); //求体积的公式。 
printf("a=%7.2f, b=%7.2f, c=%7.2f, l=%7.2f\n",a,b,c,l);      
printf("a=%7.2f, b=%7.2f, c=%7.2f, s=%7.2f\n",a,b,c,s);      
}
else
  printf("a、b、c不能构成三角形");
  getch();
}
 
 
第五个例子:随机生成扑克牌。
#include "stdio.h"
#include "stdlib.h"
main()
{
 int a,i,k,c,b,d;
 srand(time()) ;
 a=rand()%4;  
           if (a==0)  printf("%c",6) ;
           if (a==1)  printf("%c",3) ;
           if (a==2)  printf("%c",5) ;
           if (a==3)  printf("%c",4) ;
  i=rand()%13+1;
  switch(i)
  { case 1:
           printf("%c",'A');
           break;
    case 10: printf("%c%c",'1','0') ;
               break;
    case 11:
            printf("%c",'J');
           break;
    case 12:
            printf("%c",'Q'); ;
           break;
    case 13:
             printf("%c",'K');;
               break;
    default:
            k='0'+i;
             printf("%c\n",k);
  }
  c=rand()%4;
           if (c==0)  printf("%c",6) ;
           if (c==1)  printf("%c",3) ;
           if (c==2)  printf("%c",5) ;
           if (c==3)  printf("%c",4) ;
 
   b=rand()%13+1;
  switch(b)
  { case 1:
           printf("%c",'A');
           break;
    case 10: printf("%c%c",'1','0') ;
               break;
    case 11:
            printf("%c",'J');
           break;
    case 12:
            printf("%c",'Q'); ;
           break;
    case 13:
             printf("%c",'K');;
               break;
    default:
            d='0'+b;
             printf("%c",d);
  }
    getch();
}  
 
 
第六个例子: 石头剪刀布游戏 
#include<stdio.h>
void main()
{
     char  ch1,ch2;  
     printf("请选择石头(0)、剪刀(1) 、布(2) \n");
     printf("玩家1:\n");
     ch1=getchar();    
     getchar();        
     printf("玩家2:\n");
     ch2=getchar();  
     if (ch1==ch2)   printf ("平局!\n");
     if (ch1-ch2==-1 ||  ch1-ch2==2)
             printf ("玩家1胜!玩家2负!\n");
     if (ch1-ch2==1 || ch1-ch2==-2) 
             printf ("玩家1输!玩家2赢!\n");
}
 
 
第七例子: 判断输入的一个小于999的数字,判断它是一位数还是二位数还是三位数
 
#include <stdio.h>
 
main()
{
    int n;
    printf("Please input a num:");
    scanf("%d",&n);
    if(n<10)printf("%d:Is a one number",n);
    if(n<100&&n>=10)printf("%d1:Is a two number",n);
    if(n<1000&&n>=100)printf("%d:Is a three number",n);
 
    getch();
 
}