代码应该从文件读取输入并将该信息放入散列表中,如果出现任何冲突,它也应该处理这些。我不知道如何修复我使用int count = 0
行的错误,这是错误警告:
non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]
..
#include <iostream>
#include<stdlib.h>
#include<string.h>
#include<map>
#include <ctime>
#include<bits/stdc++.h>
using namespace std ;
struct record{
char last_name[15] ;
char first_name[15] ;
unsigned int account_num ;
unsigned int month ;
unsigned int day ;
unsigned int year ;
float annual_salary ;
char dept_code ;
char phone_num[12] ;
};
struct Node{
record p ;
struct Node * next ;
};
struct Hash{
struct Node * head ;
int count = 0 ;
};
Hash H[37] ;
/* Given a reference (pointer to pointer) to the head
of a list and an int, appends a new node at the end */
void append(struct Node** head_ref,record new_data)
{
/* 1. allocate node */
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
struct Node *last = *head_ref; /* used in step 5*/
/* 2. put in the data */
new_node->p = new_data;
/* 3. This new node is going to be the last node, so make next of
it as NULL*/
new_node->next = NULL;
/* 4. If the Linked List is empty, then make the new node as head */
if (*head_ref == NULL)
{
*head_ref = new_node;
return;
}
/* 5. Else traverse till the last node */
while (last->next != NULL)
last = last->next;
/* 6. Change the next of last node */
last->next = new_node;
return;
}
void insert(record r)
{
int x = r.account_num %37 ;
append(&(H[x].head),r) ;
}
map<int,string> m ;
void printdata()
{
time_t now = time(0);
char* dt = ctime(&now) ;
int z = strlen(dt) ;
int yea = 0 , temp = 1;
z--;
while(dt[z]!=' ')
{
yea += temp*(dt[z]-'0');
temp = temp*10 ;
z--;
}
int mont ;
char ch[3] ;
ch[0] = dt[4] ;
ch[1] = dt[5] ;
ch[2] = dt[6] ;
for(int i=1;i<=12;i++)
{
if(m[i]==ch)
{
mont = i ;
break ;
}
}
m[1] = "Jan" ;
m[2] = "Feb" ;
m[3] = "Mar" ;
m[4] = "Apr" ;
m[5] = "May" ;
m[6] = "Jun" ;
m[7] = "Jul" ;
m[8] = "Aug" ;
m[9] = "Sep" ;
m[10] = "Oct" ;
m[11] = "Nov" ;
m[12] = "Dec" ;
struct Node * q ;
cout << "LAST Name Acct Number date of birth annual salary department code age\n";
for(int i=0;i<=36;i++)
{
q = (H[i].head);
int cnt = 0 ;
while(q!=NULL)
{
cnt++;
q = q->next ;
}
temp = q->p.year - yea + (q->p.month - mont>=6 ? 1:0) ;
q = H[i].head ;
cout << q->p.last_name <<" "<<q->p.account_num ;
if(cnt>1)cout<<"*";
cout <<" "<<m[q->p.month]<<". "<<q->p.day<<","<<q->p.year<<" "<<q->p.annual_salary<<" "<<q->p.dept_code<<" "<<temp<<"\n" ;;
if(q==NULL)continue ;
q = q->next ;
while(q!=NULL)
{
temp = q->p.year - yea + (q->p.month - mont>=6 ? 1:0) ;
cout << q->p.last_name <<" "<<q->p.account_num ;
cout <<" "<<m[q->p.month]<<". "<<q->p.day<<","<<q->p.year<<" "<<q->p.annual_salary<<" "<<q->p.dept_code<<" "<<temp<<"\n" ;;
q = q->next ;
}
}
return ;
}
int main()
{
record r ;
freopen("data.txt","r",stdin);
while(cin >> r.last_name)
{
cin >> r.first_name >> r.account_num >> r.month >> r.day >> r.year >> r.annual_salary >> r.dept_code ;
if(cin >> r.phone_num) ;
insert(r) ;
}
return 0 ;
}
错误消息是不言自明的。您不能像在代码的第27行中那样初始化struct声明中的非静态数据成员。相反,您可以,例如:
Hash
结构的构造函数并初始化c-tor中的count
成员。Hash
结构的对象后初始化count成员。我不推荐这种解决方案,特别是如果你坚持在全球范围内声明Hash H[37]
阵列(我也不建议这样做,但这完全是另一个问题。)。在上面第1点的意义上,Hash
类声明可能如下所示:
struct Hash{
Hash() :
head(NULL),
count(0)
{}
struct Node * head ;
int count;
};