学生查询


#include<string.h>
#include<stdio.h>
#include<malloc.h>
#include <iostream>
using namespace std;
void FindStable()
{
int n; //学生人数
scanf("%d",&n);
char c[10];
gets(c);
char **H=(char**)malloc(n*sizeof(char*));
for(int i=0;i<n;i++)
{
H[i]=(char *)malloc(100 *sizeof(char));
gets(H[i]);
}
char *p;
char *str;
int strnum; //用来存储从学员表中截取的学员编号
int num; //需要查询的学员学号。
cin >>num;
for(int j=0;j<n;j++)
{
p=H[j];
str=p;
while (p&&*p!='\0'&&*p!=' ')
p++;
if(*p==' ')
*p++='\0';
char *q; //建立这个指针的目的是为了可以把字符串转化成int型数据。
q=str; //让q一开始指向str
int sum=0; //定义一个新的整形用来存储成功转换后的数据
while(*q!='\0')
{
sum=sum*10+(*q-'0');
q++;
}
strnum=sum;
//strnum=atoi(str); 这个语句可以直接实现字符串转化成int类型。
if(strnum==num)
{
printf("%s %s\n",H[j],p);
}
}
}
int main()
{
int i,m; //m是样例数
cin >>m;
for(i=0;i<m;i++)
{
FindStable();
}
return 0;
}
C语言中将字符串转换为数字的方法
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;
int main()
{
char *str="100";
int num=atoi(str);
printf("The string 'str' is %s and the number 'num' is %d. \n",str, num);
}


方法二:
个人采用的方法显然是过于麻烦了,实际上这道题应该采用结构体的方法显然是会更好一些。
#include<string.h>
#include <iostream>
using namespace std;
struct student
{
int no;
string name;
string gender;
int age;
}student[20];
int main()
{
int m,n;
cin >>m;
while(m>0)
{
cin >> n;
int findnum;
for(int i=0;i<n;i++)
{
cin>>student[i].no>>student[i].name>>student[i].gender>>student[i].age;
}
cin>>findnum;
cout<<student[findnum-1].no<<' '<<student[findnum-1].name<<' '<<student[findnum-1].gender<<' '<<student[findnum-1].age;
}
return 0;
}

本文介绍了一个使用C语言和C++实现的学生信息查询系统,通过读取输入的学生数据,包括学号、姓名、性别和年龄,然后根据用户输入的学号查询并显示学生的详细信息。系统采用了动态内存分配和字符串处理技术。
1395

被折叠的 条评论
为什么被折叠?



