Нужна помощь в решении ошибки в коде программы - вопрос №4126422

Проблема в том, что я подаю на IndexArr2 пустой массив типа List, который в функции заполняю данными из списка. Если запросить вывод внутри этой функции, все прекрасно отображается. Однако если запросить вывод после выполнения функции, в int main(), то программа выведет все значения, кроме первого — arr1[i].data.a. Будет выведен data.b, data.c, data.d, но вот a почему-то где-то теряется. Очень нужна ваша помощь.

Файл с базой данных по ссылке на облако. База данных

#include <cmath>
#include <conio.h>
#include <cstdlib>
#include <string.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <time.h>

using namespace std;
const unsigned short int Page = 20;

struct record2 {
char a[30];
unsigned short int b;
char c[10];
char d[22];
};

struct List {
List *next;
union {
record2 data;
unsigned char Digit[sizeof(data)];
int Weight;
};
List *head;
List *tail;
};

struct List_A1 {
List_A1 *left;
List_A1 *right;
List_A1 *next;
union {
record2 data;
//unsigned char Digit[sizeof(data)];
int Weight;
};
};

void PrintElems(record2 x) {
cout << x.a << " ";
cout.width(5);
cout << x.b << " " << x.c << " " << x.d << endl;
}

void ReadList(List **p, unsigned int &size) {
fstream *file = new fstream;
file->open(«testBase3.dat», ios::in | ios::binary);
record2 *data = new record2;
List *temp = *p = new List;
file->read((char *)data, sizeof(*data)).eof();
temp->data = *data;
size++;
while (!file->read((char *)data, sizeof(*data)).eof()) {
temp->next = new List;
temp = temp->next;
temp->data = *data;
size++;
}
temp->next = 0;
file->close();
}

void PrintList(List *head) {
List *p = head;
int j = 0;
int key;
int i = Page;
do {
system(«CLS»);
cout << endl;
for (; (j < i) && (p != 0); j++, p = p->next) {
cout.width(30);
cout << (j + 1) << ")"
<< " ";
PrintElems(p->data);
}
cout << endl;
cout.width(75);
cout << " <-- 1 || Exit 0 || 2 --> " << endl;
cin >> key;
if (key == 1) {
if (j != Page) {
i = i — Page;
j = i — Page;
p = head;
for (int f = 0; f < j; f++)
p = p->next;
}
} else if (key == 2) {
if (p != 0) {
i = i + Page;
}
} else if (key == 0) {
break;
}
} while (key != 0);
}

void DigitalSort(List **S) {
List **unSort = S;
int KDI[6];
for (int i = 0; i < 3; i++)
KDI[i] = i;
for (int i = 3; i < 6; i++)
KDI[i] = i + 28;
int L = 6;
List q[256];
List *p;
unsigned char d;
int k;
for (int j = L — 1; j >= 0; j--) {
for (int i = 0; i <= 255; i++) {
q[i].tail = (List *)&(q[i].head);
}
k = KDI[j];
while ((*unSort) != 0) {
d = (*unSort)->Digit[k];
q[d].tail->next = *S;
q[d].tail = *S;
(*unSort) = (*unSort)->next;
}
p = (List *)S;
for (int i = 0; i <= 255; i++) {
if (q[i].tail != (List *)&(q[i].head)) {
p->next = q[i].head;
p = q[i].tail;
}
}
p->next = 0;
}
}

void IndexArr(record2* &arr, List *p, unsigned int size) {
arr = new record2[size];
for (int i = 0; p != 0; p = p->next, i++)
arr[i] = p->data;
}

void IndexArr2(List* &arr, List *p, unsigned int size) {
arr = new List[size];
for (int i = 0; p != 0; p = p->next, i++) //{
arr[i].data = p->data;
//cout<<(*arr[i]).data.a<<endl; }
}

void Search(record2 * &arr, char * key, List * head, int n, int &size2){
List *p = head;
List *pred = new List;
int i=0;
int l=1;
int r=n;
int m;
char k[4];

while(l<r){
i=(l+r)/2;
k[0]=arr[i].a[0];
k[1]=arr[i].a[1];
k[2]=arr[i].a[2];
m=strcmp(k,key);

if(m<0){
l=i+1;
} else{
r=i;
}
}
if(strcmp(k,key)==-1) {
i++;
k[0]=arr[i].a[0];
k[1]=arr[i].a[1];
k[2]=arr[i].a[2];
}
size2 = 1;
if(strcmp(k,key)==0){
while(1){
if(strcmp(k,key)!=0){
p->next=NULL;
break;
}
p->data = arr[i];
pred=p;
p =new List;
pred->next=p;
i++;
k[0]=arr[i].a[0];
k[1]=arr[i].a[1];
k[2]=arr[i].a[2];
size2++;
}
}else{
cout<<«Not finded»<<endl;
head=NULL;
}
}

int main() {
srand(time(0));
List *List = 0;
unsigned int size = 0;
char key[4];
struct List *fSearch;
fSearch= new struct List;
struct List_A1 *NewList;
NewList = new struct List_A1;
NewList = NULL;
ReadList(&List, size);
record2 *arr;
struct List *arr1;
arr = new record2[size];
DigitalSort(&List);
IndexArr(arr, List, size);
cout<<" Enter a three letters from depositor's Full name: ";
cin>>key;
int size2=0;
Search(arr, key, fSearch, size, size2);
arr1 = new struct List[size2-1];
IndexArr2(arr1, fSearch, size2-1);
for (int i=0; i<size2-1; i++){
arr1[i].Weight=rand()%100+1;
cout<<arr1[i].data.a<<endl;
}
return 0;
}

26.12.20
0 ответов
Ответов пока нет
Посмотреть всех экспертов из раздела Технологии > C/C++
Пользуйтесь нашим приложением Доступно на Google Play Загрузите в App Store