#include <iostream>
#include <cstring>
#include <fstream>
#include <cstdlib>
using namespace std;
class SurnameInfo
{
private:
char *name;
int counter;
float pctrace[6];
public:
char getname(){ return *name; };
int getcounter(){ return counter; };
float getpctrace(int i){ return pctrace[i]; };
void setname(char*, int);
void setcounter(int);
void setpctrace(float, int);
void printname() {cout << name; };
};
void SurnameInfo::setname(char * nm, int len)
{
name = new char[len + 1];
name = nm;
}
void SurnameInfo::setcounter(int ct)
{
counter = ct;
}
void SurnameInfo::setpctrace(float prace, int i)
{
pctrace[i] = prace;
}
int numberOfNames = 0;
const int MAXARRAY = 151671;
SurnameInfo * surnames[MAXARRAY];
const int MAXLINE = MAXARRAY;
void processLine(char *, int);
SurnameInfo * binarySeach(SurnameInfo **, int, char *);
void mostPopular(int);
void searchName();
int main()
{
ifstream inputfile;
inputfile.open("names.csv");
char line[MAXLINE];
if (!inputfile) return 0;
inputfile.getline(line, MAXLINE);
inputfile.getline(line, MAXLINE);
while (!inputfile.eof())
{
processLine(line, numberOfNames++);
inputfile.getline(line, MAXLINE);
}
cout << "Here is a list of names most exclusive to each race: " << endl;
cout << "Press enter to continue.";
cin.clear(); cin.sync(); cin.get();
system("CLS");
cout << "Highest Percentage: \n\nWHITE: \n";
mostPopular(0);
cout << "\n\nBLACK:\n";
mostPopular(1);
cout << "\n\nASIAN AND PACIFIC ISLANDER: \n";
mostPopular(2);
cout << "\n\nAMERICAN INDIAN OR ALASKAN NATIVE: \n";
mostPopular(3);
cout << "\n\n2 OR MORE RACES: \n";
mostPopular(4);
cout << "\n\nHISPANIC: \n";
mostPopular(5);
inputfile.close();
cin.clear(); cin.sync(); cin.get();
return 0;
}
void processLine(char *line, int n)
{
surnames[n] = new SurnameInfo;
char * pch = strtok(line, ",");
int len = strlen(pch);
surnames[n]->setname(pch,len);
surnames[n]->setcounter(atoi(strtok(NULL, ",")));
for (int i = 0; i < 6; i++)
{
pch = strtok(NULL, ",");
surnames[n]->setpctrace(pch[0] == '(' ? -1 : atof(pch), i);
}
}
void mostPopular(int race)
{
const int TOPS = 20;
int tops[TOPS + 1];
int kept = 0;
for (int i = 0; i < numberOfNames; i++)
{
if (surnames[i]->getcounter() < 10000) continue;
int j = kept - 1;
for (; j >= 0; j--)
{
if (surnames[i]->getpctrace(race) > surnames[tops[j]]->getpctrace(race))
tops[j + 1] = tops[j];
else break;
}
if (j + 1 < TOPS) tops[j + 1] = i;
if (kept < TOPS) kept++;
}
for (int i = 0; i < TOPS; i++)
{
surnames[tops[i]]->printname();
cout << " " << surnames[tops[i]]->getpctrace(race) << endl;
}
}
在上面的行surnames[tops[i]]->printname();
中,printname函数实际上并不打印名称,即使该函数放在程序的其他位置也能正常工作。我不能为我的生活找出原因。我知道我可能会有一些函数,而且程序顶部不会被调用。我还没有完成,但我需要弄清楚这里发生了什么。
答案 0 :(得分:0)
我认为问题是:
在使用int tops[TOPS + 1];
之前,您尚未初始化此阵列。如果数据不够,则无法通过for
函数初始化数组。例如如果surnames[i]->getcounter() < 10000
都为真,则tops数组将不会出现数据。然后你使用surnames[tops[i]]
,可能会收到错误。
int tops[TOPS + 1] = {0}; // you may need to consider this problem.
在void SurnameInfo::setname(char * nm, int len)
中,您将指针复制为nm,但指针nm
指向您getline
数据。在下一个getline
之后,您name
将指向下一行,因此您什么也得不到,因为它指向文件结束。
void SurnameInfo::setname(char * nm, int len)
{
name = new char[len + 1];
memset(name, len+1, 0);
memcpy(name, nm, len);
}
SurnameInfo::~SurnameInfo()
{
delete [] name;
}