指向字符数组中的特定元素

时间:2017-01-31 13:27:58

标签: c++ arrays pointers

我一直试图绕过这个问题一个星期,但我似乎无法在网上找到任何东西,我已经放弃了试图自己解决它。

我的任务是编写一个程序,该程序将从文件中读取名称并接受用户的新条目,然后对entires进行排序并将其写入文件。关于这一点的唯一关键是我必须在函数中对它们进行排序并使用指针来完成它。这段代码应该用C ++编写,也可以使用字符数组。

我现在的代码看起来像这样。这是一个工作版本,唯一的问题是我既不使用指针也不使用函数来对名称进行排序。

#include<iostream>
#include<cstdlib>
#include<fstream>
#include<cstring>

bool sorted;

using namespace std;

int main()
{
    int i = 0;
    int numNames = 0;
    ifstream ifs;
    ifs.open("namn.txt");

    char c[20][20];

    if(ifs.is_open())
    {
        while(!ifs.eof())
        {
            ifs >> c[i];
            i++;
        }
    }

    cout<<"How many names do you want to enter?"<<endl;
    cin>>numNames;

    for(int l = i-1; l<numNames+i-1; l++)
    {
        system("cls");
        cout<<"Enter a name: ";
        cin>>c[l];
    }
    while(sorted == false)
    {
        for(int j = 0; j<numNames+i-1; j++)
        {
            for(int k = j; k<numNames+i-1; k++)
            {
                if(c[j][0] > c[k][0])
                {
                    char snorre[20];
                    strcpy(snorre,c[j]);
                    strcpy(c[j],c[k]);
                    strcpy(c[k],snorre);
                }
                else if(c[j][0] == c[k][0])
                {
                    if(c[j][1] > c[k][1])
                    {
                        char snorre[20];
                        strcpy(snorre,c[j]);
                        strcpy(c[j],c[k]);
                        strcpy(c[k],snorre);
                    }
                }
            }
        }
        cout<<endl<<endl<<endl;
        ofstream ofs;
        ofs.open("namn.txt");
        for(int o = 0; o<numNames+i-1; o++)
        {
            cout<<c[o]<<" ";
            ofs<<c[o]<<endl;
        }
        ofs.close();
        system("pause");
        sorted = true;
    }
}

所以希望有人可以帮我解决这个问题,提前谢谢! :)

2 个答案:

答案 0 :(得分:0)

要使代码使用指针和函数,您可以执行此操作 - 您应该更改代码并使其使用以下内容:

首先,使用std::string将文件中的每个名称改为getline(ifstream_object, std::string_object),以供参考,参见here

使用const char *将每个转换为.c_str()(也在该示例中显示)。

对输入的每个新名称执行以下操作。

存储在此数组指针中输入的所有名称:char *names[20];,如下所示:names[i] = name;

接下来,创建一个function,如下所示:

int location_of_bigger_string(const char* s1, const char* s2)
{
    // Returns 1 if s1 should be before s2 and 2 otherwise
    // This way, you use functions and pointers together.
    // Use strcmp(s1,s2) here to determine returning value
}

strcmp(char*, char*) - 阅读here

最后,要对所有字符串进行排序,请使用qsort或此example.

答案 1 :(得分:0)

这是完整的代码,

请注意,compare函数获取指向元素的指针,这里的元素是指针本身,因此传递给“compare”函数的是“char **”类型

{

#include "stdafx.h"
#include<iostream>

//retruns +1 if str1 > str2 alphabetically
int compare(const void * a, const void * b )
{
    const char * str1 = *((const char **)a);
    const char * str2 = *((const char **)b);

    int i;
    for ( i = 0 ; str1[i] && str2[i] ; i++ )
    {
        if ( str1[i] > str2[i] )
        {
            return +1;
        }
        else if ( str1[i] < str2[i] )
        {
            return -1;
        }
    }

    //one or both strings have ended


    if (str1[i]) //str1 is longer
        return +1;
    else if (str2[i]) //str2 is longer
        return -1;
    else
        return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
    char * names[]={"Zebra","Kousha","Koosha","Kou","Koush","Test"};

    qsort( names, 6, sizeof(char *), compare );

    return 0;
}

}