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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fstream>
#include <algorithm>
#include <iostream>
using namespace std;
int getLineCount();
struct students{
int Nr;
string vards;
string uzvards;
string apl_nr;
string vid;
};
bool sortByvid(students lhs, students rhs) { return lhs.vid > rhs.vid; }
int main()
{
int lineCount = 13, i, Nr;
students visiStudenti[lineCount];
ifstream file;
file.open ("text.txt");
for(int i=0; i< lineCount; i++){
string line;
getline(file,line);
int k1 = line.find(":"); // start of uzvards
int k2 = line.find(","); // end of uzvards
int k3 = k2-k1-1; // uzvards length
int k4 = line.length();
string vards = line.substr(0,k1);
string uzvards = line.substr(k1+1,k3);
string apl_nr = line.substr(k2+1,9);
string vid = line.substr(k4-3,3);
struct students eachstudent;
eachstudent.Nr = i+1;
eachstudent.uzvards = uzvards;
eachstudent.vards = vards;
eachstudent.apl_nr = apl_nr;
eachstudent.vid = vid;
visiStudenti[i] = eachstudent;
}
printf("Studentu saraksts\n");
for(i=0; i< lineCount; i++){
cout << visiStudenti[i].Nr << " : " ;
cout << visiStudenti[i].vards << " ";
cout << visiStudenti[i].uzvards << " ";
cout << visiStudenti[i].apl_nr<< " ";
cout << visiStudenti[i].vid << " ";
cout << endl;
}
system("pause");
system("cls");
std::sort(visiStudenti, visiStudenti+lineCount, sortByvid);
printf("Studentu saraksts dilstosa seciba pec videjas atzimes\n");
for(int i=0; i< lineCount; i++){
cout << visiStudenti[i].vards << " " << visiStudenti[i].vid << endl;
}
file.close();
printf("\nProgrammas beigas!\n");
system("pause");
return 0;
}
int getLineCount(){
int lineCount = 0;
char ch, file_name[25];
FILE *fp;
printf("Ievadiet failu, kuraa tiek saglabaati studentu dati: ");
gets(file_name);
fp = fopen(file_name,"r");
if( fp == NULL )
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
while( ( ch = fgetc(fp) ) != EOF ){
if(ch == ','){ // tāpēc ka tikai 1 komats linijā
lineCount++;
}
}
fclose(fp);
return lineCount;
}
|