明解C语言入门篇 练习题第12章

12-1

#include <stdio.h>

#define NAME_LEN 64

struct student
{   char name[NAME_LEN];
    int height;
    float weight;
    long schols;
};
int main(void)
{
    struct student takao= {"Takao",173,86.2};
    printf("姓名地址:%p\n",&(takao.name));
    printf("身高地址:%p\n",&(takao.height));
    printf("体重地址:%p\n",&(takao.weight));
    printf("奖学金地址:%p\n",&(takao.schols));

}

12-2

#include <stdio.h>

#define NAME_LEN 64

typedef struct student
{
    char name[NAME_LEN];
    int height;
    float weight;
    long schols;
} Student;

void hiroko(Student *std)
{
    if(std->height<180)
        std->height=180;
    if(std->weight>80)
        std->weight=80;
}
int main(void)

{
    Student sanaka;
    printf("请输入学生姓名:");
    scanf("%s",sanaka.name);
    printf("\n");
    printf("请输入学生身高:");
    scanf("%d",&(sanaka.height));
    printf("\n");
    printf("请输入学生体重:");
    scanf("%f",&(sanaka.weight));
    printf("\n");
    printf("请输入学生奖学金:");
    scanf("%ld",&(sanaka.schols));
    printf("\n");
    hiroko(&sanaka);
    printf("姓名=%s\n",sanaka.name);
    printf("身高=%d\n",sanaka.height);
    printf("体重=%.lf\n",sanaka.weight);
    printf("奖学金=%ld\n",sanaka.schols);

}

12-3

#include <stdio.h>


struct xyz
{
	int x;
	long y;
	double z;
};

struct xyz scan_xyz(int x, long y,double z)
{
	struct xyz t;
	t.x = x;
	t.y = y;
	t.z = z;
	return t;
}

int main(void)
{
	int x;
	long y;
	double z;
	struct xyz p;
	printf("请输入x:");
	scanf("%d",&x);
	printf("请输入y:");
	scanf("%ld", &y);
	printf("请输入z:");
	scanf("%lf", &z);
	p = scan_xyz(x, y, z);
	printf("x = %d\n",p.x);
	printf("y = %ld\n", p.y);
	printf("z = %lf\n",p.z);

	return 0;
}

12-4

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

#define NUMBER 5
#define NAME_LEN 64


typedef struct
{
    char name[NAME_LEN];
    int height;
    float weight;
    long schols;

} Student;

void swap_Student(Student *x,Student *y)
{
    Student t=*x;
    *x=*y;
    *y=t;
}
void sort_by_height(Student a[],int n)
{
    int i,j;
    for(i=0; i<n-1; i++)
    {
        for(j=n-1; j>i; j--)
        {
            if(a[j].height<a[j-1].height)
            {
                swap_Student(&a[j],&a[j-1]);
            }
        }
    }

}

void sort_by_name(Student a[],int n)
{
    int i,j;
    for(i=0; i<n-1; i++)
    {
        for(j=n-1; j>i; j--)
        {
            if(strcmp(a[j].name,a[j-1].name)<0)
            {
                swap_Student(&a[j],&a[j-1]);
            }
        }
    }

}

int main(void)
{
    int i=0;
    int j=NUMBER;
    Student std[NUMBER];

    while(j>0)
    {
        printf("请录入第%d个学生的信息:\n",i+1);
        printf("name:");
        scanf("%s",std[i].name);
        printf("height:");
        scanf("%d",&std[i].height);
        printf("weight:");
        scanf("%f",&std[i].weight);
        printf("schols:");
        scanf("%ld",&std[i].schols);
        printf("\n");
        i++;
        j--;
    }
    sort_by_height(std,NUMBER);
    puts("按照身高排序:");
    for(i=0; i<NUMBER; i++)
    {
        printf("%-8s %6d%6.1f%7ld\n",std[i].name,std[i].height,std[i].weight,std[i].schols);
    }
    printf("\n");

    sort_by_name(std,NUMBER);
    puts("按照姓名排序:");
    for(i=0; i<NUMBER; i++)
    {
        printf("%-8s %6d%6.1f%7ld\n",std[i].name,std[i].height,std[i].weight,std[i].schols);
    }
    printf("\n");
    return 0;

}




12-5

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

#define sqr(n) ((n)*(n))

typedef struct
{
    double x;
    double y;
} Point;

typedef struct
{
    Point pt;
    double flue;
} Car;

double distance_of(Point pa,Point pb)
{
    return sqrt(sqr(pa.x-pb.x)+sqr(pa.y-pb.y));
}
void put_info(Car c)
{
    printf("当前位置:(%.2f,%.2f)\n",c.pt.x,c.pt.y);
    printf("剩余燃料:%.2f升\n",c.flue);

}

int move_1(Car *c,Point dest)
{
    double d=distance_of(c->pt,dest);
    if(d>c->flue)
    {
        return 0;
    }
    c->pt=dest;
    c->flue-=d;
    return 1;
}

int move_2(Car *c,double px,double py)
{
    double d=sqrt(sqr(px)+sqr(py));
    if(d>c->flue)
    {
        return 0;
    }
    c->pt.x+=px;
    c->pt.y+=py;
    c->flue-=d;
    return 1;
}

int main(void)
{
    int cho;
    Car mycar= {{0.0,0.0},90.0};
    printf("请选择一种方法:\n1.输入目的地坐标--1\n2.输入行驶距离--2\n");
    scanf("%d",&cho);
    if(cho==1)
    {
        while(1)
        {
            int select;
            Point dest;
            put_info(mycar);
            printf("开动汽车吗?[yes--1/no--0]:");
            scanf("%d",&select);
            if(select==0)
                break;
            printf("目的地的X坐标:");
            scanf("%lf",&dest.x);
            printf("目的地的y坐标:");
            scanf("%lf",&dest.y);
            if(!move_1(&mycar,dest))
                puts("燃料不足无法行驶");
        }
    }
    if(cho==2)
    {
        while(1)
        {
            int select2;
            double px,py;
            put_info(mycar);
            printf("开动汽车吗?[yes--1/no--0]:");
            scanf("%d",&select2);
            if(select2==0)
                break;
            printf("在x轴方向上行驶:");
            scanf("%lf",&px);
            printf("在y轴方向上行驶:");
            scanf("%lf",&py);
            if(!move_2(&mycar,px,py))
                puts("燃料不足无法行驶");
        }
    }

    return 0;

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值