TXT

chapter1-4

By Denise Cruz,2014-11-09 21:33
8 views 0
chapter1-4

CÓïÑÔÔ??ÌÐò

    Àý1.1

    #include

    void main()

    {

    printf("Hello everyone\nStudy hard!\n"); }

    Àý1.2

    #include

    void main()

    {

    int x,y,sum;//define the variable printf("please input the first integer:\n");//display the prompt

    information

    scanf("%d",&x);//input one integer printf("please input the second integer:\n");//display the prompt

    information

    scanf("%d",&y);//input another integer sum=x+y;//caculate the sum

    printf("sum is %d\n",sum);//display the result

    }

    Àý1.3

    #include

    double max(double a,double y)

    {

    double m;

    if(x>y)

    m=x;

    else

    m=y;

    return m;

    }

    void main()

    {

    double a,b,c,t,maxdata;

    printf("please input three data:\n"); scanf("%lf %lf %lf",&a,&b,&c);

    t=max(a,b);/*µ?Óú?Êýmax??Çóa??bÖ??Ï?óÕß*/ maxdata=max(t,c);/*µ?Óú?Êýmax??Çót??cÖ??Ï?óÕß*/

    printf("the maximum data is :%lf\n",maxdata);/*Êä?ö×î?óÖµ*/

    }

    µÚ?þÕÂ

    //ASCIIÂëÓëÕûÊý?ìÓÃ

    #include

void main()

    {

    char b='a';

    int c;

    printf("a=%c\n",a);

    printf("a=%d\n",a);

    c=10*b;

    printf("c=%d\n",c);

    }

    //?ûºÅ??Á?

    #define PRICE 30

    #include

    void main()

    {

     int num,total;

     num=10;

     total=num*PRICE;

     printf("total=%d\n",total);

    }

    //Êý?Ý???È

    #include

    void main()

    {

     int

    len_char,len_short,len_int,len_long,len_unsigned_short,len_unsigne

    d_int,len_unsigned_long,len_float,len_double;

     len_char=sizeof(char);

     printf("char?äÁ?µÄÊý?Ý???ÈΪ%d?ö×Ö?Ú\n",len_char);

     len_short=sizeof(short);

     printf("short?äÁ?µÄÊý?Ý???ÈΪ%d?ö×Ö?Ú\n",len_short);

     len_int=sizeof(int);

     printf("int?äÁ?µÄÊý?Ý???ÈΪ%d?ö×Ö?Ú\n",len_int);

     len_long=sizeof(long);

     printf("long?äÁ?µÄÊý?Ý???ÈΪ%d?ö×Ö?Ú\n",len_long);

     len_unsigned_short=sizeof(unsigned short);

     printf("unsigned

    short?äÁ?µÄÊý?Ý???ÈΪ%d?ö×Ö?Ú\n",len_unsigned_short);

     len_unsigned_int=sizeof(unsigned int);

     printf("unsigned

    int?äÁ?µÄÊý?Ý???ÈΪ%d?ö×Ö?Ú\n",len_unsigned_int);

     len_unsigned_long=sizeof(unsigned long);

     printf("unsigned

    long?äÁ?µÄÊý?Ý???ÈΪ%d?ö×Ö?Ú\n",len_unsigned_long);

     len_float=sizeof(float);

     printf("float?äÁ?µÄÊý?Ý???ÈΪ%d?ö×Ö?Ú\n",len_float);

     len_double=sizeof(double);

     printf("double?äÁ?µÄÊý?Ý???ÈΪ%d?ö×Ö?Ú\n",len_double);

}

    //×Ö?û????Á?

    #include void main()

    {

    char string[20]="I am a student of CJLU.";

    printf("%s\n",string);

    }

//Àý2.3

    #include void main()

    {

    char c='A';

    putchar(c);

    putchar('A');

    putchar('\n'); putchar('\101'); putchar(65);

    putchar('\n'); putchar('A'+1); putchar('\n'); }

    //Àý2.4

    #include void main()

    {

    char c1,c2,c3; c1=getchar();

    c2=getchar();

    c3=getchar();

    putchar(c1);

    putchar(c2);

    putchar(c3);

    printf("\n");

    }

    //Àý2.5

    #include void main()

    {

    double d,h,area; printf("input d h:\n");

    scanf("%lf,%lf",&d,&h);

    area=d*h/2;

    printf("d=%.3f h=%.3f area=%.3f\n",d,h,area);

    }

    //sqrt

    #include #include void main()

    {

    double x,y;

    scanf("%lf",&x); //x=fabs(x);

    x=x>0?x:-x;

    y=sqrt(x);

    printf("%.4f\n",y); }

    µÚÈýÕÂ

    //++;--

    #include void main()

    {

    int i=3,j=3;

    i++;

    j--;

    printf("i=%d,j=%d\n",i,j);

    }

    //a++;++a

    #include void main()

    {

    int i=3,m,n;

    m=i++;

    printf("m=%d,i=%d\n",m,i);

    i=3;

    n=++i;

    printf("n=%d,i=%d\n",n,i);

    }

    1.

    #include void main()

    {

    int a=5, b=3;

printf("%d\n", a

    }

    //Àý3.4

    #include

    void main()

    {

     float x,y=0.3;

     y=y*11;

     x=3+0.3;

     if(x==y) printf("ok,x==y");

     else printf("no,x!=y"); }

    //

    #include

    void main()

    {

     int k=3,y=6,c;

     c=k-2==y-4<5;

     printf("c=%d\n",c);

}

    //Àý3.5

    #include

    void main()

    { int a,b,c;

     scanf("%d%d%d",&a,&b,&c); //ÊäÈë?º2 3 4

     a=b!=c;

     printf("a=%d,b=%d,c=%d\n",a,b,c);

     a==(b=c++*3);

     printf("a=%d,b=%d,c=%d\n",a,b,c);

     a=b>c>2;

     printf("a=%d,b=%d,c=%d\n",a,b,c);

    }

    //ÒÔÏÂÈýÀýΪÂß??ÔËËã?ûʵÀý #include

    void main()

    {

     int x,y,z;

    z=(x=1)||(y=0);

    printf("x=%d,y=%d,z=%d\n",x,y,z);

    }

    //

    #include

void main()

    {

    int x=5,y=5,a=5,z; z=(x=0)||(y=3)||(a=3); printf("%d,%d,%d,%d\n",x,y,a,z);

}

    //

    #include void main()

    { int x,y,z,m;

     x=y=z=0 ;

     m=++x&&++y||++z;

     printf("m=%d x=%d y=%d z=%d\n", m,x,y,z);

    }

    //Ñ?Éú?ðÒÉ?ºP54?ØÓÚÔËËãÓÅÏÈ??µÄÎÊÌâ

    #include void main()

    {

    int a,b,c;

    a=(b=1)&&(c=1)||1; printf("a=%d,b=%d,c=%d\n",a,b,c);

}

    //µÚËÄÕÂ

    4.1.2

    #include void main()

    {

    int i;

    float sum=0,data;

     for(i=1;i<=10;i++)

     {

     scanf("%f",&data);

     if(data>0)

     sum=sum+data;

     }

     printf("sum=%f\n",sum); }

    //Êä?öÎÞ?ûºÅÊý16?øÖÆ #include void main()

    {

    int a=-32767;

    printf("a=%x\n",a); }

    //Àý4.2

    #include #include void main()

    {

    double a,b,c,s,area; scanf("%lf %lf %lf",&a,&b,&c);

    if(a+b>c&&a+c>b&&b+c>a)

     {s=1.0/2*(a+b+c);

     area=sqrt(s*(s-a)*(s-b)*(s-c));

     printf("the area is %f\n",area);

     }

    else

    printf("it is not a triangle\n");

}

    //Àý4.3

    #include #include void main()

    {

    float score;;

    scanf("%f",&score); if(score>=60)

    printf("pass\n"); else

    printf("failed\n"); }

    //Àý4.6

    #include #include void main()

    {

    float score;

    scanf("%f",&score); if(score<=59)

    printf("fail\n"); else if(score<=79) printf("pass\n"); else

    printf("good\n"); }

//Àý4.7

    #include

    #include

    void main()

    {

    double a,b,c,dalt;

    double x1,x2,real,imag;

    scanf("%lf %lf %lf",&a,&b,&c);

    if(a==0)

    printf("it is not a quadratic equation\n"); else

     {

     dalt=b*b-4*a*c;

     if(dalt>=0)

     {

     x1=(-b+sqrt(dalt))/(2*a);

     x2=(-b-sqrt(dalt))/(2*a);

     printf("x1=%f\n",x1);

     printf("x2=%f\n",x2);

     }

     else

     {

     real=-b/(2*a);

     imag=sqrt(-dalt)/(2*a);

     printf("x1=%.2f+%.2fi\n",real,imag);

     printf("x2=%.2f-%.2fi\n",real,imag);

     }

     }

    }

    //Àý4.9

    #include

    void main()

    { int choice; // ?æ?ÅÑ?Ôñ?àºÅ

     printf(" 1---crisps\n"); // ÏÔÊ?ÊíÆ??˵?Ïî

     printf(" 2----gum\n");

     printf(" 3---chocolate\n");

     printf(" 4---cola\n");

     printf("Please input choice[1-4]: ");

     scanf("%d",&choice); // ÊäÈëÑ?ÏîºÅ?

     switch (choice){

     case 1: printf("%.2f\n", 2.50);

     break; // breakÌø?öswitch

     case 2: printf(" price=%.2f\n",1.25);

     break;

     case 3: printf(" price=%.2f\n",4.0 );

     break;

     case 4: printf(" price=%.2f\n",3.30);

     break;

     default: printf(" Sorry,your choice is error\n");

     }

     printf(" Thank you!\n"); }

    //Àý4.11

    #include

    void main()

    {

     double f,c; // ?Ö?ð?íÊ??ªÊÏ??ÉãÊÏÎÂ?È

     f=30; // ?õÊ????ªÊÏÎÂ?ÈfÖµ

     printf("fahr celsius\n");

     while(f<=40){ // Èç?ûf<=40,ÖØ???ÆËã

     c=(5.0/9)*(f-32); // ?ÆËãf?ÔÓ?µÄÉãÊÏÎÂ?È

     printf("%.1f%8.1f\n",f,c); // Êä?ö

     f+=1; // ?ªÊÏÎÂ?ȵÝÔö1

     }

    }

    //?ðÒÉ1?ºÊý?ÝÊäÈëÊ?ÖÐ?äÓÐ×Ö?û?ñÊ?ÊäÈë #include

    #include

    void main()

    {

    int i;

    float f;

    double d;

    char c1;

    scanf("%d$%c%f%lf",&i,&c1,&f,&d);

    printf("i=%d,c1=%c,f=%f,d=%f\n",i,c1,f,d); printf("c1=%d\n",c1);

}

    //Àý4.16Èç?û??ÓÃbreakÓï?ä?øÓÃdo-whileʵÏÖ #include

    void main()

    {

    int i=1,sum=0;

    do

{

    sum=sum+i;

    i++;

    }while(sum<=500);

printf("i=%d,sum=%d\n",i,sum);

    }

    //4.16 ÓÃforÓï?äʵÏÖ #include void main()

    {

     int n,s=0;

     for(n=1;;n++)

     {

     s=s+n;

     if(s>=500)break;

     }

    printf("s=%d,n=%d\n",s,n);

    }

    //Àý4.17ËØÊý×Ô?à?ÌÐò #include void main()

    {

    int data,i,flag=0; printf("please input a positive integer:\n");

    scanf("%d",&data); for(i=2;i<=data-1;i++)

     {

     if(data%i==0)

     {

     flag=1;

     break;

     }

     }

    if(flag==1)

    printf("no\n"); else

    printf("yes\n"); }

    //ËØÊý

    #include void main()

    {

    int a,i,flag;

Report this document

For any questions or suggestions please email
cust-service@docsford.com