【问题标题】:Simple 'database' in c++C ++中的简单“数据库”
【发布时间】:2010-05-03 20:03:16
【问题描述】:

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

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

我有一些代码,但那里仍然存在问题。 这是我所拥有的: 用于返回数组大小的函数 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++ arrays sorting sizeof


    【解决方案1】:

    在您的设计中,您必须有一个变量来保持数组的大小。此值将在添加或删除项目时进行调整。 C++ 语言没有获取数组变量大小的工具。

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

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

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

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

    【讨论】:

    • 如果作业要求使用char*,我不建议在内部存储为std::string,除非您先询问您的讲师(或助教)。虽然在任何实际场景中,您都希望使用std::string,但任务的一部分可能是学习手动内存管理,如果他们阅读您的代码并找到std::string,您可能会因此而失去信用.
    【解决方案2】:

    除非您对数组有某种终结符,否则没有简单的方法可以获取T 指向的数组的大小。

    您必须对T 指向的数组进行循环并计数元素,直到找到终止符。 (例如,'\0' 代表 char *

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多