【问题标题】:Probelm in facing about Structure Types in c面对 c 中的结构类型的问题
【发布时间】:2022-01-04 04:10:00
【问题描述】:

我目前正在研究我目前正在研究的示例中的结构类型

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

typedef struct {
int day, month, year;
} Date;

typedef struct {
char customer_name[1000]; //Name of the customer placing the order
int order_number; //The order number
Date order_date; //The date on which the order was placed
double total_price;
} Order;

/* print_order(the_order)
Given an Order object, print the order's details in the same format
described in the section above.
*/

void print_order(Order the_order){
printf("Order %d: Placed by %s on %d/%d/%d (price: $%f)\n",the_order.order_number, 
the_order.customer_name, the_order.order_date.month, the_order.order_date.day, 
the_order.order_date.year, the_order.total_price);
}

int main(){

//Create an order with order number 111, placed on November 25, 2021
Order orderA = {111, 11, 25, 2021, "Rebecca Raspberry"};
strcpy(orderA.customer_name, "Rebecca Raspberry");
orderA.order_number = 111;
orderA.order_date.year = 2021;
orderA.order_date.month = 11;
orderA.order_date.day = 25;
orderA.total_price = 6.10;

//Make a second order, this time with order number 116, placed on November 29, 2021
//This order is created using the { } initializer syntax. Notice the nested initializer
//for the date.
Order orderB = { "Fiona Framboise", 116, { 29, 11, 2021 }, 17.0 };

print_order(orderA);
print_order(orderB);

printf("Order %d: Placed by %s on %d/%d/%d (price: $%f)\n", orderA.order_number, 
orderA.customer_name, orderA.order_date.month, orderA.order_date.day, 
orderA.order_date.year, orderA.total_price); 

return 0;
}

我想知道 print_order(the_order) 是什么意思,它要求类型定义为日、月和年吗?对于 OrderB,我是否以与 orderA 相同的方式格式化它们,或者这是错误的吗?我需要的输出是:

Order 111: Placed by Rebecca Raspberry on 11/25/2021 (price: $6.10)
Order 116: Placed by Fiona Framboise on 11/29/2021 (price: $17.00)

需要回复/帮助:)

【问题讨论】:

  • 如果您从main 中删除printf,并将print_order 中的格式从%f 更改为%.2f,它似乎给出了您正在寻找的输出。我不确定你实际上在问什么。 print_order 接收到 Order 结构的副本并将其打印出来。
  • 可以给我看看吗?

标签: c function types structure


【解决方案1】:
  1. void print_order(Order the_order) 表示print_order 需要Order 类型的副本(值),它是具有4 个成员(指针、整数、具有3 个整数和双精度的结构)的结构。通常,您按地址传递结构以避免复制大型结构。

  2. 您既初始化又为orderA 赋值,这令人困惑。初始化顺序(orderA)必须与结构体顺序一致:

Order orderA = { "Rebecca Raspberry", 111, { 11, 25, 2021 }};

或使用指示符列表,在这种情况下您可以选择顺序:

Order orderA = { .customer_name = "Rebecca Raspberry", .order_number = 111, .order_date = { 11, 25, 2021 }};

注意:total_price 未初始化,但您是否为它分配了值6.10orderB 是正确的。 @RetiredNinja 已经告诉你如何修复格式字符串:

void print_order(Order the_order){
    printf("Order %d: Placed by %s on %d/%d/%d (price: $%.2f)\n",the_order.order_number, 
        the_order.customer_name, the_order.order_date.month, the_order.order_date.day, 
        the_order.order_date.year, the_order.total_price);
}

我不知道你为什么在main() 中同时使用print_order()printf 语句打印orderA 两次。

您的代码格式非常规(即很差),难以阅读:

【讨论】:

    猜你喜欢
    • 2019-10-31
    • 1970-01-01
    • 1970-01-01
    • 2019-08-11
    • 1970-01-01
    • 2020-05-09
    • 1970-01-01
    • 1970-01-01
    • 2011-03-24
    相关资源
    最近更新 更多