C语言编写一个结构体变量与成员访问的小程序,输入10001 C语言 张明 35 2,输出编号:10001,书名:C语言,作者:张明,价格:35,数量:2
时间: 2024-04-30 17:19:59 浏览: 35
下面是一个示例程序,实现了一个书籍结构体 Book,以及输入和输出功能:
```c
#include <stdio.h>
// 定义书籍结构体
struct Book {
int id;
char name[20];
char author[20];
float price;
int quantity;
};
int main() {
// 定义一个 Book 结构体变量
struct Book book;
// 输入书籍信息
printf("请输入编号、书名、作者、价格和数量:");
scanf("%d%s%s%f%d", &book.id, book.name, book.author, &book.price, &book.quantity);
// 输出书籍信息
printf("编号:%d,书名:%s,作者:%s,价格:%.2f,数量:%d\n", book.id, book.name, book.author, book.price, book.quantity);
return 0;
}
```
运行程序,输入样例数据,输出结果如下:
```
请输入编号、书名、作者、价格和数量:10001 C语言 张明 35 2
编号:10001,书名:C语言,作者:张明,价格:35.00,数量:2
```
相关问题
c语言编写一个结构体变量与成员访问的小程序。 编程要求 根据提示,在右侧编辑器补充代码,从键盘输入图书数据并输出。 测试说明
下面是一个简单的 C 语言程序,用于定义一个名为 Book 的结构体,其中包含书名、作者和价格字段,并且从键盘输入并输出图书数据。
```c
#include <stdio.h>
#include <string.h>
struct Book {
char title[50];
char author[50];
float price;
};
int main() {
struct Book book;
printf("请输入书名:\n");
fgets(book.title, 50, stdin);
book.title[strcspn(book.title, "\n")] = 0;
printf("请输入作者名字:\n");
fgets(book.author, 50, stdin);
book.author[strcspn(book.author, "\n")] = 0;
printf("请输入价格:\n");
scanf("%f", &book.price);
printf("您输入的图书信息如下:\n");
printf("书名:%s\n", book.title);
printf("作者:%s\n", book.author);
printf("价格:%.2f\n", book.price);
return 0;
}
```
程序中首先定义了一个名为 Book 的结构体,其中包含了三个字段:title、author 和 price。接下来在 main 函数中创建了一个 Book 结构体变量 book,并从键盘输入了书名、作者和价格,并将其分别存储在结构体的对应字段中。最后,程序输出了输入的图书信息。
c语言利用结构体编写程序:输入水果的编号,输出水果对应的编号、名称、水果单价
好的,以下是利用结构体编写的C语言程序:
```c
#include <stdio.h>
#define FRUIT_NUM 3
struct Fruit {
int id;
char name[20];
float price;
};
int main() {
struct Fruit fruits[FRUIT_NUM] = {
{1, "apple", 3.0},
{2, "banana", 2.5},
{3, "orange", 4.0}
};
int id;
printf("请输入水果编号:");
scanf("%d", &id);
int i;
for (i = 0; i < FRUIT_NUM; i++) {
if (fruits[i].id == id) {
printf("编号:%d,名称:%s,单价:%.2f元\n", fruits[i].id, fruits[i].name, fruits[i].price);
break;
}
}
if (i == FRUIT_NUM) {
printf("没有找到编号为%d的水果\n", id);
}
return 0;
}
```
这个程序定义了一个结构体`Fruit`,包含水果的编号、名称和单价三个成员变量。在`main`函数中,首先定义了一个长度为`FRUIT_NUM`的`Fruit`数组,表示有几种水果可供选择。用户输入水果编号后,程序遍历数组,查找对应的水果,如果找到则输出该水果的编号、名称和单价,否则输出没有找到对应编号的水果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)