gpt4 book ai didi

c++ - 鸟类调查项目在代码中遇到问题。如何像数据库系统一样存储数据

转载 作者:行者123 更新时间:2023-11-30 05:18:10 25 4
gpt4 key购买 nike

如何将我的输入存储在以下类别中,即水、家禽等。当我提供有关水鸟的信息时,我会提供有关家禽的信息。但是第二个信息会覆盖第一个信息。有没有办法存储数据分类明智?

/* declaring header files */
#include<iostream>
#include<conio.h>
#include<stdio.h>
#include <fstream>
#include <bits/stdc++.h>
#include <string>

using namespace std;
class Bird{

private:
char name[50],colour[50],nature[50],location[50];
float living_duration;

public:
int code;
int set_info(){
char name='\0';
char colour='\0';
char nature='\0';
char location='\0';
float living_duration=0.0;
}
int get_info(){
cout<<"\nEnter bird's name: ";
cin>>name;
cout<<"Colour: ";
cin>>colour;
cout<<"Nature: ";
cin>>nature;
cout<<"Location: ";
cin>>location;
cout<<"Living Duration: ";
cin>>living_duration;
cout<<"Bird's code: ";
cin>>code;
}
int display_info(){
cout<<"\nBird's name: "<<name;
cout<<"\nColour : "<<colour;
cout<<"\nNature : "<<nature;
cout<<"\nlocation : "<<location;
cout<<"\nLiving Duration : "<<living_duration<<" year";
cout<<"\nCode : "<<code;
}
}obj[100];

int main(){
int i,j,k,n,m;
do{
cout<<"\n\nWhat do you want to do\n1.Input bird's information"
<<"\n2.Display\n3.Search\n4.Exit."
<<"\n\nChoose appropriate number: ";
cin>>n;

switch(n){
case 1://bird information
cout<<"Please Select Birds Category"<<endl;
cout<<"------------------"<<endl;
cout<<"1)Water\n2)Domestic\n3)prey\n4)treebased\n5)flightless\n6)migratory\n"<<endl;
cin>>m;
switch(m){
case 1:
cout<<"Enter the number of bird how many to input: ";
cin>>j;
for(i=1;i<=j;i++){
cout<<"\nInformation of Bird "<<i<<".\n";
obj[i].get_info();
}
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
default:
cout<<"Wrong choice!!\nPlease enter correct number.";
break;
}


case 2://display
for(i=1;i<=j;i++)
{
cout<<"\nBird no "<<i<<".\n";
obj[i].display_info();
cout<<"\n";
}
break;

case 3://search
cout<<"\nEnter the bird code: ";
cin>>k;

for(i=1;i<=j;i++)
{
if(k==obj[i].code)
{
cout<<"\nBird no "<<i<<".\n";
obj[i].display_info();
break;
}
}
if(k!=obj[i].code)
cout<<"Wrong code input...\n";
break;

case 4://exit
break;

default:
cout<<"Wrong choice!!\nPlease enter correct number.";
break;
}

}while(n!=4);
}

最佳答案

您需要将您的概念分为鸟类数据 和数据的容器

关系数据库中,您会有。让表的列由结构的数据成员表示。表的一条记录(行)将是记录结构的一个实例:

class Bird
{
public:
std::string name;
std::string colour;
std::string nature;
std::string location;
float living_duration;
};

对于容器或表,您可以使用 std::vector :
std::vector<Bird> bird_table;

许多关系数据库还包含索引表以加快搜索速度。索引表将包含对、键(或列值)和 std::vector 中的索引。 . C++ 语言有一个方便的容器,称为 std::map :

std::map<string, unsigned int> name_index;

string参数表示键或列类型。
unsigned int参数表示数据库中的索引(也称为外键)。

要按名称检索 Bird 记录,您首先访问索引表,然后访问 vector :

   unsigned int database_index = name_index["crow"];
Bird crow = database[index];

关于c++ - 鸟类调查项目在代码中遇到问题。如何像数据库系统一样存储数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41855333/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com