c++ - C++ 中的简单 'database'

标签 c++ arrays sorting sizeof

我的任务是用 C++ 创建伪数据库。给出了 3 个表,存储名称(char*)、年龄(int)和性别(bool)。编写一个程序允许:
- 向表中添加新数据
- 显示所有记录
- 按标准对表格进行排序:
- 名称递增/递减
- 年龄增加/减少
- 性别

使用函数模板是必须的。数组的大小也必须可变,具体取决于记录的数量。

我有一些代码,但那里仍然存在问题。 这是我所拥有的: 返回数组大小的函数 tabSize()。但目前它返回我猜的指针大小:

#include <iostream>
using namespace std;

template<typename TYPE> int tabSize(TYPE *T)
{
   int size = 0;  
   size = sizeof(T) / sizeof(T[0]);
   return size;
}

如何让它返回数组的大小,而不是它的指针?

接下来是最重要的:add() 用于添加新元素。首先我得到数组的大小(但因此它返回指针的值,而不是它现在没有用的大小:/)。然后我想我必须检查数据的类型是否为字符。还是我错了?

// 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() 是正确的:

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() 的问题如下:我可以影响排序是减少还是增加?我在这里使用冒泡排序。

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++ 语言没有获取数组变量大小的工具。

此外,更喜欢使用 std::string 而不是 char *。如果您的讲师说要使用 char *,则将其作为参数提供给您的函数,但在函数和类中转换为 std::string。这将使您的生活更轻松。

不要实现自己的排序算法。更喜欢使用 std::sort 和不同的比较函数std::sort 算法已经过测试,将为您节省时间和精力。

实现Visitor 设计模式。这将允许您以不同的方式访问您的表,而无需在表类中编写新方法。例如,使用 Visitor 基类,您可以派生用于读取文件、写入文件和显示内容的类,而无需更改表类。

最后,不要使用可能不可移植的 system("pause")。相反,更喜欢可以在 std::istream::ignore 中找到的 cin.ignore

关于c++ - C++ 中的简单 'database',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2760840/

相关文章:

c++ - 实现抽象工厂模式的最佳方法

c++ - const char* 在分配给 char* 后被修改

c++ - boost 灵气 : Compile error on slight rule change

c - 如何将二维数组乘以c中的数字

java - 在带有 JDBC 驱动程序的 Java 中使用准备好的语句和变量绑定(bind) Order By

c++ - 为什么当我输入 12 位数字时,以下代码会崩溃?

c++ - 类型转换长指针问题

c# - 将对象转换为多维字符串数组

c - 初始化结构数组 - {NULL} 与 {}

regex - Vim 正则表达式重复行分组