C primer plus 习题答案

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <string.h>
#include "ans.h"

void f2_1()
{
    printf("Li Jialong\n");
    printf("Li\nJialong\n");
    printf("Li");
    printf(" Jialong\n");
}

void f3_2()
{
    int num;
    printf("please input an ASCII num:");
    scanf("%d",&num);
    printf("%c\n",num);
}

void f3_3()
{
    printf("\aStartled by the sudden sound,Sally shouted,\n");
    printf("\"By the Great Pumpking, what was that!\n");
}

void f3_4()
{
    float num;
    printf("please input an float num:");
    scanf("%f",&num);
    printf("%f\n",num);
    printf("%e\n",num);
    printf("%p\n",&num);
}

void f3_5()
{
    long int num;
    printf("please input your name:");
    scanf("%ld",&num);
    printf("%e\n",num*3.156e7);
}

void f4_1()
{
    char firstname[10];
    char lastname[10];
    printf("Please input your firstname and lastname:");
    scanf("%s %s",firstname,lastname);
    printf("%s,%s",lastname,firstname);
}

void f4_2()
{
    char name[10];
    printf("Please input your name:");
    scanf("%s",name);
    printf("\"%s\"\n",name);
    printf("\"%18s\"\n",name);
    printf("\"%-18s\"\n",name);
}

void f4_6()
{
    char firstname[10];
    char lastname[10];
    printf("Please input your lastname:");
    scanf("%s",lastname);
    printf("Please input your firstname:");
    scanf("%s",firstname);
    printf("%s %s\n",lastname,firstname);
    printf("%*d %*d\n",strlen(lastname),strlen(lastname),strlen(firstname),strlen(firstname));
    printf("%s %s\n",lastname,firstname);
    printf("%-*d %-*d\n",strlen(lastname),strlen(lastname),strlen(firstname),strlen(firstname));
}

void f5_1()
{
    int times;
    while(scanf("%d",&times)!=EOF && times>0)
    {
        printf("%02d:%02d\n",times/60,times%60);
    }
}
void f5_2()
{
    int i;
    int num;
    printf("Please input a num:");
    scanf("%d",&num);
    for(i=0;i<10;i++)
    {
        printf("%d ",num+i);
    }
    printf("%d\n",num+10);
}

double f5_7_f1(double num)
{
    return num*num*num;
}

void f5_7()
{
    double num;
    printf("Please input a double number:");
    scanf("%lf",&num);
    printf("%f\n",f5_7_f1(num));
}

void f6_3()
{
    int i,j;
    for(i=0;i<6;i++)
    {
        for(j=0;j<=i;j++)
        {
            printf("%c",'F'-j);
        }
        printf("\n");
    }
}

void f6_4()
{
    int i,j;
    for(i=0;i<6;i++)
    {
        for(j=0;j<=i;j++)
        {
            printf("%c",'A'+(1+i)*i/2+j);
        }
        printf("\n");
    }
}

void f6_5()
{
    int i,j;
    char c;
    char arr[5][9]={0};
    printf("please inpit a capital letter(Ranged A-V):");
    scanf("%c",&c);
    for(i=0;i<5;i++)
    {
        for(j=0;j<9;j++)
        {
            if(j>=4-i && j<=4+i)
                printf("%c",c+i+(j-4<0?j-4:4-j));
            else
                printf(" ");
        }
        printf("\n");
    }
}

void f6_7()
{
    char word[20];
    int index;
    printf("please input a word:");
    scanf("%s",word);
    index=strlen(word)-1;
    while(index>=0)
    {
        printf("%c",word[index--]);
    }
    printf("\n");
}

void f7_1()
{
    char ch;
    int a=0,b=0,c=0;
    while((ch=getchar())!='#')
    {
        if(ch==' ')
            a++;
        else if(ch=='\n')
            b++;
        else
            c++;
    }
    printf("%d %d %d",a,b,c);
}

void f7_2()
{
    char ch;
    int count=0;
    while((ch=getchar())!='#')
    {
        ++count;
        if(ch !='\n')
            printf("%4c-%2d ",ch,ch);
        else
        {
            count=0;
            //printf("\n");
        }
        if(count%8==0)
            printf("\n");
    }
}

void copy_arr1(double *target,const double *sources,int n)
{
    int i;
    for(i=0;i<n;i++)
    {
        target[i]=sources[i];
    }
}

void copy_arr2(double *target,const double *sources,int n)
{
    int i;
    for(i=0;i<n;i++)
    {
        *target++=*sources++;
    }
}
void copy_arr3(double *target,const double *sources,double *end)
{
    while(sources<end)
    {
        *target++=*sources++;
    }
}

void f10_2()
{
    int i=0;
    double sources[5]={1.1,2.2,3.3,4.4,5.5};
    double target1[5];
    double target2[5];
    double target3[5];
    copy_arr1(target1,sources,5);
    copy_arr2(target2,sources,5);
    copy_arr3(target3,sources,sources+5);
    printf("%9s%9s%9s%9s\n","sources","target1","target2","target3");
    for(;i<5;i++)
    {
        printf("%9.2lf%9.2lf%9.2lf%9.2lf\n",sources[i],target1[i],target2[i],target3[i]);
    }
}

void f10_7()
{
    int i=0,j=0;
    double sources[3][5]={
        {1.1,1.2,1.3,1.4,1.5},
        {2.1,2.2,2.3,2.4,2.5},
        {3.1,3.2,3.3,3.4,3.5}
    };
    double target[3][5]={0};
    copy_arr3(target[0],sources[0],sources[0]+15);
    for(i=0;i<3;i++)
    {
        for(j=0;j<5;j++)
        {
            printf("%lf ",target[i][j]);
        }
        printf("\n");
    }
}

void f10_8()
{
    int i=0;
    double sources[7]={1.1,2.1,3.1,4.1,5.1,6.1,7.1};
    double target[3]={0};
    copy_arr3(target,sources+2,sources+5);
    for(;i<3;i++)
    {
        printf("%lf ",target[i]);
    }
}

void myprint(char * arr)
{
    int i=0;
    while(*arr!='\0')
        printf("%d ",*arr++);
    
    printf("\n");
}

void f11_1()
{
    int i=0;
    char arr[11];
    for(i=0;i<10;i++)
    {
        arr[i]=getchar();
    }
    arr[i]='\0';
    myprint(arr);
}

void f11_2()
{
    int i=0;
    char arr[11];
    for(i=0;i<10;i++)
    {
        arr[i]=getchar();
        if(arr[i]==' ' || arr[i]=='\t' || arr[i]=='\n')
        {
            arr[i]='\0';
            break;
        }
    }
    myprint(arr);
}
int f15_1_1(char * pbin)
{
    int r=0;
    int r1=0;
    int tmp=1;
    char *p=pbin+strlen(pbin);
    while(--p>=pbin)
    {
        tmp*=2;
        r+=tmp*(*p-'0');
    }
    return r/2;
    
}
void f15_1()
{
    char * pbin="01001001";
    printf("%d\n",f15_1_1(pbin));
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值