录入与查询程序

本蒟蒻刚刚对结构体有所认知,现在来看一个应用…
录入与查询程序_第1张图片
步骤1:定义一个结构体Student
步骤2:各字段的表示:int id,char name[16],int score[3];
步骤3:数据保存,数组Student[100];
步骤4:记录总个数

定义数据:

#include
using namespace std;
struct Student
{
	int id;
	char name[16];
	int score[3];
};
Student data[100];//存储所有数据
int count = 0;//记录个数

输入信息:

//定义一个用于信息输入的函数
//将输入的信息保存到输出参数s
//成功录入返回0,录入失败返回-1
int input(Student* s)
{
	printf("请输入id:");
	scanf("%d", &s->id);
	printf("请输入name:");
	scanf("%s", s->name);
	printf("请输入score:");
	int a, b, c;
	scanf("%d%d%d", &a, &b, &c);
	s->score[0] = a;
	s->score[1] = b;
	s->score[2] = c;
	//*scanf("%d%d%d", &s->score[0], &s->score[1], &s->score[2]);
	return 0;
}

添加一个对象:

//将新录入的学生信息保存到数据源
//返回值 0:成功 -1:失败
int add(const Student* s)
{
	data[count] = *s;
	count++;
	return 0;//总是成功
}

打印所有信息:

void list_all()
{
	printf("id \tname \tscore,%d,%d \n");
	printf("-------------------------------\n");
	for (int i = 0; i < count; i++)
	{
		Student* s = &data[i];
		printf("%d \t%s \t%d,%d,%d \n",
			s->id,
			s->name,
			s->score[0],
			s->score[1],
			s->score[2]);
	}
	printf("-------------------------------\n");
}

菜单:
录入与查询程序_第2张图片
完整程序:

#define _CRT_SECURE_NO_WARNINGS
#include
struct Student
{
	int id;
	char name[16];
	int score[3];
};
Student data[100];//存储所有数据
int count = 0;//记录个数
//定义一个用于信息输入的函数
//将输入的信息保存到输出参数s
//成功录入返回0,录入失败返回-1
int input(Student* s)
{
	printf("请输入id:");
	scanf("%d", &s->id);
	printf("请输入name:");
	scanf("%s", s->name);
	printf("请输入score:");
	int a, b, c;
	scanf("%d%d%d", &a, &b, &c);
	s->score[0] = a;
	s->score[1] = b;
	s->score[2] = c;
	//*scanf("%d%d%d", &s->score[0], &s->score[1], &s->score[2]);
	return 0;
}
//将新录入的学生信息保存到数据源
//返回值 0:成功 -1:失败
int add(const Student* s)
{
	data[count] = *s;
	count++;
	return 0;//总是成功
}
void list_all()
{
	printf("id \tname \tscore \n");
	printf("-------------------------------\n");
	for (int i = 0; i < count; i++)
	{
		Student* s = &data[i];
		printf("%d \t%s \t%d,%d,%d \n",
			s->id,
			s->name,
			s->score[0],
			s->score[1],
			s->score[2]);
	}
	printf("-------------------------------\n");
}
//按照名称查看
Student* find(const char* name)
{
	for (int i = 0; i < count; i++)
	{
		Student* s = &data[i];	
		if (strcmp(name, s->name) == 0)
		{
			return s;
		}
	}
	return NULL;
}
int main()
{
	char cmdline[128];
	while (1)
	{
		printf("需要执行的命名:");
		scanf("%s", cmdline);
		//printf("cmd:%s \n", cmdline);
		//输入exit退出
		if (strcmp(cmdline, "exit") == 0)//返回值为0表示相同,即输入的为exit
		{
			printf("now exit....\n");
			break;
		}
		if (strcmp(cmdline, "add") == 0)
		{
			Student s;
			input(&s);
			add(&s);
			continue;
		}
		if (strcmp(cmdline, "list") == 0)
		{
			list_all();
			continue;
		}
		if (strcmp(cmdline, "count") == 0)
		{
			printf("total:%d \n", count);
			continue;
		}
		if (strcmp(cmdline, "find") == 0)
		{
			printf("enter name:");
			char name[16]; 
			scanf("%s", name);
			Student* s = find(name);
				if(s)
				{
					printf("ta的 id:%d,name:%s,score:%d,%d,%d\n",
					s->id,
					s->name,
					s->score[0], s->score[1], s->score[2]);
				}
				else
				{
					printf("没有查询到\n");
				}
			continue;
		}
	}
}
	

结果:
录入与查询程序_第3张图片
尼玛的,累死我了,蒟蒻写的可能也有问题,仅供参考,毕竟初学…

你可能感兴趣的:(c++,c语言)