答疑解惑必采纳 2021-07-26 11:23 采纳率: 91.3%
浏览 54
已结题

c语言 我这个代码怎么补全,求解惑

在名称字段中存储第一个输入字符串
在类型字段中存储第二个输入字符串
将第三个输入字符串转换为int并将其存储在age字段中
将第四个输入字符串转换为整数并将其存储在weight字段中
img
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX_NAME_LENGTH 50
#define MAX_TYPE_LENGTH 50

struct pet {
char name[MAX_NAME_LENGTH];
char type[MAX_TYPE_LENGTH];
int age;
int weight;
};

// function declarations, do not change these
void setup_pet(
struct pet *my_pet,
char *name,
char *type,
char *age,
char *weight
);
void print_pet(struct pet *my_pet);

// do not change any code in the main function
int main(int argc, char *argv[]) {
if (argc < 5) {
printf("%s should receive four extra command line arguments.\n", argv[0]);
return 1;
}
struct pet new_pet;
setup_pet(&new_pet, argv[1], argv[2], argv[3], argv[4]);
print_pet(&new_pet);
return 0;
}

// A function that takes in four strings
// (given from the command line arguments in the main function)
// and stores their data in the pet struct.
//
// Note: you will need to convert the 'age' and 'weight' strings
// to an integer before storing them in the struct.
void setup_pet(
struct pet *my_pet,
char *name,
char *type,
char *age,
char *weight
) {
// YOUR CODE GOES HERE
}

// A function that prints out a human readable
// description of the pet:
// " is a who is years old and weighs kg\n"
void print_pet(struct pet *my_pet) {
// YOUR CODE GOES HERE
}

  • 写回答

2条回答 默认 最新

  • qfl_sdu 2021-07-26 11:32
    关注

    代码如下:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define MAX_NAME_LENGTH 50
    #define MAX_TYPE_LENGTH 50
    
    struct pet {
        char name[MAX_NAME_LENGTH];
        char type[MAX_TYPE_LENGTH];
        int age;
        int weight;
    };
    
    // function declarations, do not change these
    void setup_pet(
    struct pet *my_pet,
        char *name,
        char *type,
        char *age,
        char *weight
        );
    void print_pet(struct pet *my_pet);
    
    // do not change any code in the main function
    int main(int argc, char *argv[]) {
        if (argc < 5) {
            printf("%s should receive four extra command line arguments.\n", argv[0]);
            return 1;
        }
        struct pet new_pet;
        setup_pet(&new_pet, argv[1], argv[2], argv[3], argv[4]);
        print_pet(&new_pet);
        return 0;
    }
    
    // A function that takes in four strings
    // (given from the command line arguments in the main function)
    // and stores their data in the pet struct.
    //
    // Note: you will need to convert the 'age' and 'weight' strings
    // to an integer before storing them in the struct.
    void setup_pet(
    struct pet *my_pet,
        char *name,
        char *type,
        char *age,
        char *weight
        ) {
            // YOUR CODE GOES HERE
            strcpy(my_pet->name,name);
            strcpy(my_pet->type,type);
            my_pet->age = atoi(age);
            my_pet->weight = atoi(weight);
    }
    
    // A function that prints out a human readable
    // description of the pet:
    // " is a who is years old and weighs kg\n"
    void print_pet(struct pet *my_pet) {
        // YOUR CODE GOES HERE
        printf("%s is a %s who is %d years old and weights %dkg\n",my_pet->name,my_pet->type,my_pet->age,my_pet->weight);
    }
    
    

    运行结果:

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 8月3日
  • 已采纳回答 7月26日
  • 创建了问题 7月26日

悬赏问题

  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题