Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <cstring>
- #include <ctime>
- #include <cstdlib>
- using namespace std;
- struct elev
- {
- char id[6];
- char nume[40];
- char scoala[50];
- }t[100];
- int n;
- void Separa(char s[200])
- {
- char x[50];
- int k = 0, i;
- for(i = 0; s[i] != '-'; i++)
- x[k++] = s[i];
- x[k] = 0;
- while(x[k-1] == ' ')
- {
- x[k-1] = 0;
- k--;
- }
- strcpy(t[n].id, x);
- i++;
- for(; s[i] != '-'; i++)
- x[k++] = s[i];
- x[k] = 0;
- while(x[k-1] == ' ')
- {
- x[k-1] = 0;
- k--;
- }
- strcpy(t[n].nume, x);
- i++;
- while(s[i] == ' ') i++;
- strcpy(x, s+i);
- k = strlen(x);
- while(x[k-1] == ' ')
- {
- x[k-1] = 0;
- k--;
- }
- strcpy(t[n].scoala, x);
- }
- void Citire()
- {
- char s[200];
- n = 0;
- ifstream fin("elevi.txt");
- fin.getline(s, 199);
- while(strcmp(s, "***") != 0)
- {
- Separa(s);
- n++;
- fin.getline(s, 199);
- }
- for(int i = 0; i < n; i++)
- cout << t[i].id << "*" << t[i].nume << "*" << t[i].scoala << "\n";
- fin.close();
- }
- void Amesteca()
- {
- int i, j, k;
- elev x;
- srand(time(0));
- for(k = 1; k <= 100000; k++)
- {
- i = rand() % n;
- j = rand() % n;
- swap(t[i], t[j]);
- }
- for(i = 1; i < n;)
- {
- if((t[i].id == t[i+1].id)||(t[i].id == t[i-1].id))
- {
- x = t[i];
- for(j = i+1; j < n-1; j++)
- t[j] = t[j+1];
- t[n-1] = x;
- }
- else i++;
- }
- for(i = 0; i < n; i++)
- cout << t[i].id;
- }
- void Afisare()
- {
- ofstream fout("elevi.out");
- for(int i = 0; i < n; i++)
- fout << t[i].id << " " << t[i].nume << " " << t[i].scoala << "\n";
- fout << "\n";
- fout.close();
- }
- int main()
- {
- Citire();
- Amesteca();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement