作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何将我的输入存储在以下类别中,即水、家禽等。当我提供有关水鸟的信息时,我会提供有关家禽的信息。但是第二个信息会覆盖第一个信息。有没有办法存储数据分类明智?
/* 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/
NoSQL 是 RDBMS 的替代品,可以很好地处理包含大量数据的简单数据模型。 MongoDB、Google 的 BigTable、Dojo 的 Persevere、Amazon 的 Dynamo、
我是一名优秀的程序员,十分优秀!