C ++中的简单“数据库" [英] Simple 'database' in c++
问题描述
我的任务是用c ++创建伪数据库.给出了3个表,分别存储名称(char *),年龄(int)和性别(布尔).编写允许以下操作的程序:
-将新数据添加到表中
-显示所有记录
-使用条件对表格进行排序:
-名称增加/减少
-年龄增加/减少
-性爱
My task was to create pseudodatabase in c++. There are 3 tables given, that store name(char*), age(int), and sex (bool). Write a program allowing to :
- add new data to the tables
- show all records
- sort tables with criteria :
- name increasing/decreasing
- age increasing/decreasing
- sex
必须使用功能模板.数组的大小也必须是可变的,具体取决于记录的数量.
Using function templates is a must. Also size of arrays must be variable, depending on the amount of records.
我有一些代码,但是那里仍然有问题. 这是我所拥有的: 函数tabSize()用于返回数组的大小.但是目前它返回的是我猜想的指针大小:
I have some code but there are still problems there. Here's what I have: Function tabSize() for returning size of array. But currently it returns size of pointer I guess :
#include <iostream>
using namespace std;
template<typename TYPE> int tabSize(TYPE *T)
{
int size = 0;
size = sizeof(T) / sizeof(T[0]);
return size;
}
如何使其返回数组的大小,而不是其指针?
How to make it return size of array, not its pointer ?
接下来最重要的是:add()用于添加新元素.首先在里面,我得到数组的大小(但是因此它返回指针的值,而不是大小,它现在已经没有用了:/).然后,我认为我必须检查数据类型是否为char.还是我错了?
Next the most important : add() for adding new elements. Inside first I get the size of array (but hence it returns value of pointer, and not size it's of no use now :/). Then I think I must check if TYPE of data is char. Or am I wrong ?
// add(newElement, table)
template<typename TYPE> TYPE add(TYPE L, TYPE *T)
{
int s = tabSize(T);
//here check if TYPE = char. If yes, get the length of the new name
int len = 0;
while (L[len] != '\0') {
len++;
}
//current length of table
int tabLen = 0;
while (T[tabLen] != '\0') {
tabLen++;
}
//if TYPE is char
//if current length of table + length of new element exceeds table size create new table
if(len + tabLen > s)
{
int newLen = len + tabLen;
TYPE newTab = new [newLen];
for(int j=0; j < newLen; j++ ){
if(j == tabLen -1){
for(int k = 0; k < len; k++){
newTab[k] =
}
}
else {
newTab[j] = T[j];
}
}
}
//else check if tabLen + 1 is greater than s. If yes enlarge table by 1.
}
我想在这里正确吗?
我猜最后一个函数show()是正确的:
Last functions show() is correct I guess :
template<typename TYPE> TYPE show(TYPE *L)
{
int len = 0;
while (L[len] == '\0') {
len++;
}
for(int i=0; i<len; i++)
{
cout << L[i] << endl;
}
}
,sort()的问题如下:我可以影响排序是减少还是增加?我在这里使用气泡排序.
and problem with sort() is as follows : Ho can I influence if sorting is decreasing or increasing ? I'm using bubble sort here.
template<typename TYPE> TYPE sort(TYPE *L, int sort)
{
int s = tabSize(L);
int len = 0;
while (L[len] == '\0') {
len++;
}
//add control increasing/decreasing sort
int i,j;
for(i=0;i<len;i++)
{
for(j=0;j<i;j++)
{
if(L[i]>L[j])
{
int temp=L[i];
L[i]=L[j];
L[j]=temp;
}
}
}
}
运行它的主要功能:
int main()
{
int sort=0;
//0 increasing, 1 decreasing
char * name[100];
int age[10];
bool sex[10];
char c[] = "Tom";
name[0] = "John";
name[1] = "Mike";
cout << add(c, name) << endl;
system("pause");
return 0;
}
推荐答案
在您的设计中,您必须具有一个保持数组大小的变量.该值将在添加或删除项目时进行调整. C ++语言不具备获取数组变量大小的功能.
In your design, you must have a variable that maintains the size of the array. This value will be adjusted as items are added or removed. The C++ language has no facilities for obtaining the size of an array variable.
此外,更喜欢使用std::string
而不是char *
.如果您的讲师说使用char *
,则将其作为参数提供给您的函数,但在函数和类内部转换为std::string
.这将使您的生活更加轻松.
Also, prefer to use std::string
instead of char *
. If your instructor says to use char *
, then provide it as a parameter to your functions, but convert to std::string
inside functions and classes. This will make your life a lot easier.
不要实现自己的排序算法.优先使用std::sort
和其他比较功能. std::sort
算法已经过测试,可以节省您的时间和精力.
Don't implement your own sort algorithms. Prefer to use std::sort
and different compare functions. The std::sort
algorithm has been tested and will save you time and effort.
实施Visitor
设计模式.这将允许您以不同的方式访问表,而无需在表类中编写新方法.例如,使用Visitor
基类,您可以派生用于从文件读取,写入文件和显示内容的类,而无需更改表类.
Implement the Visitor
design pattern. This will allow you to access your tables in different ways without writing new methods in the table class. For example, with a Visitor
base class, you can derive classes for reading from files, writing to files and displaying content without changing the table class.
最后,请勿使用可能无法携带的system("pause")
.取而代之的是,可以在std::istream::ignore
中找到的cin.ignore
.
Lastly, don't use system("pause")
which may not be portable. Instead, prefer cin.ignore
which can be found in std::istream::ignore
.
这篇关于C ++中的简单“数据库"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!