C语言中怎么实现按要求查询相关的信息
导读:在C语言中,可以通过编写函数来实现按要求查询相关的信息。下面是一个简单的示例: #include <stdio.h> #include <string.h> struct Student { char na...
在C语言中,可以通过编写函数来实现按要求查询相关的信息。下面是一个简单的示例:
#include <
stdio.h>
#include <
string.h>
struct Student {
char name[50];
int age;
float score;
}
;
void searchByName(struct Student students[], int numStudents, char searchName[]) {
for (int i = 0;
i <
numStudents;
i++) {
if (strcmp(students[i].name, searchName) == 0) {
printf("Name: %s\n", students[i].name);
printf("Age: %d\n", students[i].age);
printf("Score: %.2f\n", students[i].score);
}
}
}
int main() {
struct Student students[3] = {
{
"Alice", 20, 85.5}
,
{
"Bob", 22, 78.3}
,
{
"Charlie", 21, 92.0}
}
;
char searchName[50];
printf("Enter the name you want to search: ");
scanf("%s", searchName);
searchByName(students, 3, searchName);
return 0;
}
在这个示例中,我们定义了一个结构体Student
来存储学生的信息。然后我们编写了一个函数searchByName
来按姓名查询学生的信息。在main
函数中,我们初始化了一个包含3个学生信息的数组,并通过用户输入来进行查询。
当用户输入要查询的姓名后,程序会调用searchByName
函数来查找并输出该学生的信息。这是一个简单的示例,你可以根据具体需求来扩展和修改。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: C语言中怎么实现按要求查询相关的信息
本文地址: https://pptw.com/jishu/679548.html