写一个程序,要求输入学号能输出对应学号的姓名,输入姓名输出对应姓名的学号,上述两个输入为并列关系

输入:16300430,输出:huhai 输入:huhai,输出:16300430 大一的课堂练习,我写了两个还是有错误,想来看看大神的操作

#include "stdio.h"
#include<string.h>
#define N 2
typedef struct student{
 int stuno;
 char stuname[10];
}student;
int main(int argc, char* argv[])
{
	int num;
	int i=0;
	int stuno;
	char stuname[10];
	student stu[N]={{1001,"zhangsan"},{1002,"lisi"}};
	printf("请输入您想查询的方式:1、根据学号查询 2、根据姓名查询");
	scanf("%d",&num);
	if(num==1){
		 printf("请输入学号:");
		 scanf("%d",&stuno);
		 for(i=0;i<N;i++){
			if(stu[i].stuno==stuno){
				printf("%s\n",stu[i].stuname);
				break;
			}
		}
	}else if(num==2){
		printf("请输入姓名:");
		scanf("%s",&stuname);
		for(i=0;i<N;i++){
			if(strcmp(stu[i].stuname, stuname) == 0){
				printf("%d\n",stu[i].stuno);
				break;
			}
		}
		if(i==N){
			printf("姓名为%s的学生不存在",stuname);
		}
	}else{
		printf("只能输入1或者2");
	} 
	return 0;
}

代码如上,万望采纳。

要先定义一个结构体数组,不需要录入学生成绩的功能吗?把代码发出来看看。

感觉不难啊,你的代码报什么错了啊,发出来看看?

#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

typedef struct studdd{ 
char name[64]; 
char num[32]; 
} STUD_INFO; 

int main()
{
	STUD_INFO data;
	char str[64]={0};
	
	strcpy(data.name,"huhai");
	strcpy(data.num,"16300430");
	
	printf("请输入检索条件:\n");
	scanf("%s", str);
	if(strcmp(data.name,str)==0)
	{
		printf("依据名称[%s],查询到学号:[%s]\n",data.name,data.num);
	}
	else if(strcmp(data.num,str)==0)
	{
		printf("依据学号[%s],查询到名称:[%s]\n",data.num,data.name);
	}
	else
	{
		printf("查询失败\n");
	}
	return 0;
}

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632

#include <iostream>
#include<stdio.h>
#include <stdlib.h>
#include<string.h>
using namespace std;


typedef struct student
{
	unsigned long nID;
	char name[20];
}stu;

char* GetStudentName(stu *pStu, int n,unsigned long id)
{
	for (int i = 0; i < n; i++)
		if (pStu->nID == id)
			return pStu->name;
	return NULL;
}

unsigned long GetStudentID(stu *pStu, int n, char *pName)
{
	for (int i = 0; i < n; i++)
		if (strcmp(pStu->name, pName) == 0)
			return pStu->nID;
	return 0;
}

int main()
{
	stu s[2];
	s[0].nID = 320219;
	strcpy_s(s[0].name, "小唐");
	s[1].nID = 320218;
	strcpy_s(s[1].name ,"小高");
	//
	printf("请输入要查询学生姓名的学号:");
	unsigned long id;
	scanf_s("%ld", &id);
	char *pName = GetStudentName(s,2,id);
	printf("学号%d的学生名字是:%s\n", id, pName);
	//
	printf("请输入要查询学号的学生姓名:");
	char name[20];
	scanf_s("%s", name,20);
	unsigned long d = GetStudentID(s, 2, name);
	printf("学生%s的学号是:%d\n", name, d);
	
	return 0;
}