Day12课后作业

题1

head.h

#ifndef __HEAD_H__
#define __HEAD_H__

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

typedef unsigned int sizeof_t;
char *Create();
void Input(char *p);
sizeof_t my_strlen(const char *p);
char *my_strcat(char *dest,const char *src);
void Bubble(char *s);
char *free_fun(char *p);


#endif

tese.c

#include "head11.h"

//堆区申请空间
char *Create()
{
    char *p=(char *)malloc(sizeof(char)*20);
    if(p == NULL)
        return NULL;
    else
        return p;
}

//输入
void Input(char *p)
{
    printf("请输入一个字符串:");
    scanf("%s",p);
}

//计算字符串长度(非函数)
sizeof_t my_strlen(const char *p)
{
    sizeof_t i=0;
    while(*(p+i) != '\0')
        i++;
    return i;
}

//字符串连接
char *my_strcat(char *dest,const char *src)
{
    int i;
    for(i=0; *(dest+i)!='\0'; i++);
    for(int j=0; *(src+j)!='\0'; j++)
    {
        *(dest+i) = *(src+j);
        i++;
    }
    return dest;
}

//bubble
void Bubble(char *s)
{
    int len=0;
    while(*(s+len) != '\0')
        len++;
    for(int i=0; i<len-1; i++)
    {
        int count=0;
        for(int j=0; j<len-i-1; j++)
        {
            if(*(s+j) > *(s+j+1))
            {
                *(s+j) = *(s+j) ^ *(s+j+1);
                *(s+j+1) = *(s+j) ^ *(s+j+1);
                *(s+j) = *(s+j) ^ *(s+j+1);
                count++;
            }
        }
        if(count == 0)
            break;
    }

}

//释放空间
char *free_fun(char *p)
{
    if(p != NULL)
    {
        free(p);
        p=NULL;
    }
    return p;
}

main.c

#include "head11.h"
int main(int argc, const char *argv[])
{    
    char *s1 = Create();
    char *s2 = Create();

    Input(s1);
    Input(s2);

    sizeof_t len1 = my_strlen(s1);
    sizeof_t len2 = my_strlen(s2);

    printf("strlen(s1) = %d\n",len1);
    printf("strlen(s2) = %d\n",len2);
    
    char *s = my_strcat(s1,s2);
    printf("s = %s\n",s);
    
    printf("-------------------\n");

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

    s1 = free_fun(s1);
    s2 = free_fun(s2);

    return 0;
}

题2

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 64
struct car
{
    char name[N];
    char colour[N];
    float price;
}c[5];
void Input(struct car *p)
{
    for(int i=0; i<5; i++)
    {
        printf("品牌:");
        scanf("%s",(p+i)->name);
        printf("颜色:");
        scanf("%s",(p+i)->colour);
        printf("价格:");
        scanf("%f",&(p+i)->price);
    }
}
void Output(struct car *p)
{
    printf("品牌\t颜色\t价格\n");
    for(int i=0; i<5; i++)
    {
        printf("%s\t%s\t%.2fW\n",(p+i)->name,(p+i)->colour,(p+i)->price);
    }
}
int Max(struct car *p)
{
    int maxi;
    for(int i=0; i<5; i++)
    {
        if(i == 0)
            maxi = i;
        if((p+maxi)->price < (p+i)->price)
            maxi = i;
    }
    return maxi;
}
int main(int argc, const char *argv[])
{    
    //定义5辆车的信息,结构体成员包含【品牌、颜色、价格】
    Input(c);

    Output(c);
    
    int maxi = Max(c);

    printf("最贵的车辆信息为:\n%s\t%s\t%.2fW\n",c[maxi].name,c[maxi].colour,c[maxi].price);

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值