假设学生信息放在一个文件中,详细代码如下(如有帮助,请采纳一下,谢谢):
#include <stdio.h>
#include <map>
#include <string>
std::map<std::string,std::string> g_mapStuInfo; //保存学生信息
//假设学生信息保存在文件中,格式为:
//学号1,姓名1
//学号1,姓名1
bool ReadFile(const char* filename)
{
FILE* fp = 0;
fp = fopen(filename,"r");
if (fp == 0)
{
return false;
}else
{
char buf[128] = {0};
while (!feof(fp))
{
fgets(buf,128,fp);
//去掉行位的换行符
for (int i = 0; i < 128; i++)
{
if (buf[i] == 0x0a)
{
buf[i] = 0;
break;
}
}
std::string s = buf;
int index = s.find(',');
if (index > 0 && index < s.length())
{
std::string xh = s.substr(0,index);
std::string xm = s.substr(index +1,s.length() - index);
g_mapStuInfo.insert(std::pair<std::string,std::string>(xh,xm));
}
memset(buf,0,128);
}
fclose(fp);
}
return true;
}
int main()
{
if (ReadFile("E:\\test.txt"))
{
while (1)
{
printf("请输入学号或者姓名\n");
char buff[20] = {0};
scanf("%s",buff);
std::map<std::string,std::string>::iterator it = g_mapStuInfo.begin();
bool bfind = false;
for (; it != g_mapStuInfo.end(); it++)
{
std::string xh = it->first;
std::string xm = it->second;
if (xh.compare(buff) == 0)
{
printf("姓名:%s\n",xm.c_str());
bfind = true;
}else if (xm.compare(buff) == 0)
{
printf("学号:%s\n",xh.c_str());
bfind = true;
}
}
if (!bfind)
{
printf("查无此人\n");
}
}
}else
printf("学生信息读取错误\n");
return 0;
}
test.txt中内容如下:
11111,abc
22222,efg
33333,xxxx
什么语言实现?
挺简单的呀,前端整两个input,一个姓名框一个学号框。写两个sql:select 学号 from 表名 where 姓名 = “xxx” ; select 姓名 from 表名 where 学号 = “xxx”。后端写个接口调用sql就行了呀。
#include<string>
#include<iostream>
using namespace std;
int main() {
string NUM[] = { "2737773","2343332" };
string NAME[] = { "lihua","zhangsan" };
int n = sizeof(NUM) / sizeof(NUM[0]);
char in[10];
scanf("%s", in);
int i;
for (i = 0; i < n; i++)
if (!(NUM[i].compare(in)))
printf("%s", NAME[i].c_str());
if (i == n)
for (i = 0; i < n; i++)
if (!(NAME[i].compare(in)))
printf("%s", NUM[i].c_str());
return 0;
}