简单的“数据库”在 c++

发布于 2024-08-31 04:09:56 字数 2721 浏览 6 评论 0原文

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

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

我有一些代码,但仍然存在问题。 这是我所拥有的: 函数 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;
}

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.

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 ?

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.               
}

Am I thinking correct here ?

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;
   }    
}

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;
            }
        }
    }   
}

And main function to run it :

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心头的小情儿 2024-09-07 04:09:56

在您的设计中,必须有一个变量来维护数组的大小。该值将随着项目的添加或删除而调整。 C++ 语言没有获取数组变量大小的工具。

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

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

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

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

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.

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.

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.

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.

Lastly, don't use system("pause") which may not be portable. Instead, prefer cin.ignore which can be found in std::istream::ignore.

李不 2024-09-07 04:09:56

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

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

Unless you have some sort of terminator to the array, there is no easy way to get the size of an array pointed to by T.

You would have to do a loop through the array pointed to by T and count up elements until you find a terminator. (E.G. '\0' for char *)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文