2 / 2 / 0
Регистрация: 25.10.2013
Сообщений: 108
|
|
1
|
Нужен совет по чтению из файла и записи в структуру
19.05.2014, 21:02. Показов 580. Ответов 1
Суть следующая
я могу просто считать из файла с выводом на экран
нужен совет или направление в котором Думать , для того чтобы то что я считываю втолкнуть в структуру
вот мой код
C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
| #include <stdio.h>
#include <Windows.h>
#include <conio.h>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
SetConsoleCP(1251);// установка кодовой страницы win-cp 1251 в поток ввода
SetConsoleOutputCP(1251); // установка кодовой страницы win-cp 1251 в поток вывода
struct WORKER
{
char family[100], name[100], dolzhnost[100] ;
int year ;
WORKER* next;
};
WORKER *head = 0 ;
WORKER *current = 0 ;
char c;
int schetchik = 0;
int schetchik_goda = 0;
int sred_god = 0;
cout<<"Ввести данные или считать из файла? в/с"<<endl;
c=_getche();
if( c == 'в' )
while( c == 'в' )
{
schetchik++;
if ( head != 0 )
{
current -> next = new WORKER ;
current = current -> next ;
}
else
{
head = new WORKER ;
current = head ;
}
cout<<"\nВведите имя" << endl ;
cin >> current -> name ;
cout << "Введите фамилию" << endl ;
cin >> current -> family ;
cout << "Введите должность" << endl ;
cin >> current -> dolzhnost ;
cout << "Введите год поступления на работу" << endl ;
cin >> current -> year ;
schetchik_goda += current -> year ;
current -> next = 0 ;
cout << "Ввести данные?(в/н)" << endl ;
c = _getche();
}
if( c == 'с' )
{
char file[100];
cout << "\nВведите имя файла" << endl;
cin >> file;
ifstream in(file,ios_base::in);
if (!in.is_open()) // если файл небыл открыт
{
cout << "Файл не может быть открыт или создан\n"; // напечатать соответствующее сообщение
return 1; // выполнить выход из программы
}
char str[100];
while( in.getline( str , 100) )
{
cout << str << endl ;
};
in.close();
system("pause");
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool stop = true; // продолжение сортировки
WORKER *prev = 0; // адрес пердыдущего
current = head;
while( stop )
{
stop = false ;
current = head ;
while( current != 0 )
{
if( current -> next !=0 && ( current -> year > current -> next -> year ) )
{
if( current == head ) // если первый - меняем первый элемент на последующий
{
head = head -> next ;
current -> next = head -> next ;
head -> next = current ;
prev = head ; // станет предыдущим для следующей итерации...
}
else // иначе ЕСЛИ за ним идет последний - меняем элемент с последним
if( current -> next -> next == 0 )
{
current -> next -> next = current ;
prev -> next = current -> next ;
current -> next = 0 ; // фактически выйдем из цикла
}
else // инача промежуточный элемент
{
prev -> next = current -> next ;
current -> next = current -> next -> next ;
prev -> next -> next = current;
prev = prev -> next ;
}
stop = true ; // т.к. заменаа прошла, то возможно будут еще... разрешаем цикл дальше
}
else // если критерий замены не прошел - сохнаняем предыдущий и переходим к следующему
{
prev = current ;
current = current -> next ;
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if( head != 0 )
{
printf("\n _______________________________________________________________\n");
printf("\n|Имя |Фамилия |Должность |Год поступления|\n");
printf(" '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''\n");
current = head;
while( current != 0 )
{
printf("\n|%15s|%15s|%15s|%15i|\n", current -> name , current -> family , current -> dolzhnost , current -> year );
printf(" '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''\n");
current = current -> next;
};
}
else
printf("Ничего не введено\n");
/////////////////////////фамилии тех, чей стаж меньше среднего//////////////////////////////////////////////////////////////////////////
sred_god = schetchik_goda / schetchik;
current = head;
printf("\n\n Стаж работы ниже среднего ");
printf("\n _______________________________________________________________\n");
printf("\n|Имя |Фамилия |Должность |Год поступления|\n");
printf(" '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''\n");
while(current != 0)
{
if(current -> year > sred_god)
{
printf("|%15s|%15s|%15s|%15i|\n", current -> name , current -> family , current -> dolzhnost , current -> year );
current = current -> next;
}
else
current = current -> next;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
ofstream out("lab.txt");
current = head;
while( current != 0 )
{
out << current -> name << endl;
out << current -> family << endl;
out << current -> dolzhnost << endl;
out << current -> year << endl;
current = current -> next;
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
system( "pause" ) ;
return 0 ;
} |
|
0
|