【发布时间】: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