chore: Lab06

This commit is contained in:
Fándly Gergő 2020-04-12 12:02:41 +03:00
parent b379440f66
commit 7adb051d16
3 changed files with 153 additions and 0 deletions

51
lab05/tema.cpp Normal file
View File

@ -0,0 +1,51 @@
#include <iostream>
#include <string>
#include <list>
#include <sstream>
#include <algorithm>
using namespace std;
void insertToPlace(list<string>* v, const string* word){
if(!v->size() || *word < v->front()){
v->push_front(*word);
return;
}
if(*word == v->front()){
return;
}
for(list<string>::iterator it = next(v->begin()); it != v->end(); it++){
if(*word == *it){
return;
}
if(*word > *prev(it) && *word < *it){
v->insert(it, *word);
return;
}
}
v->push_back(*word);
}
int main(){
ios::sync_with_stdio(false);
cout<<"Introduce a line: ";
string line;
getline(cin, line);
list<string> v;
istringstream sstream(line);
string word;
while(getline(sstream, word, ' ')){
insertToPlace(&v, &word);
}
cout<<"Distinct words ordered alphabetically:\n";
for(list<string>::iterator it = v.begin(); it != v.end(); it++){
cout<<*it<<"\n";
}
cout.flush();
return 0;
}

36
lab06/01.cpp Normal file
View File

@ -0,0 +1,36 @@
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
int main(){
ios::sync_with_stdio(false);
fstream f;
f.open("examene.txt", ios_base::in);
if(!f.is_open()){
cout<<"Error: file does not exist";
return 1;
}
int sum=0;
for(int i=0; i<6; i++){
int buff;
f>>buff;
sum+=buff;
}
f.close();
f.open("media.txt", ios_base::out);
double avg = (double)sum / 6;
f<<avg;
f.close();
return 0;
}

66
lab06/02.cpp Normal file
View File

@ -0,0 +1,66 @@
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <algorithm>
using namespace std;
class Student{
public:
string nume, prenume, grupa;
string stringify(){
return nume+" "+prenume+" "+grupa;
}
};
bool studentCompare(const Student a, const Student b){
if(a.nume == b.nume){
if(a.prenume <= b.prenume){
return true;
}
else{
return false;
}
}
else if(a.nume <= b.nume){
return true;
}
else{
return false;
}
}
int main(){
ios::sync_with_stdio(false);
fstream f;
f.open("an.txt", ios_base::in);
if(!f.is_open()){
cout<<"Error: file does not exist";
return 1;
}
vector<Student> v;
string buff;
while(f>>buff){
Student tmp;
tmp.nume = buff;
f>>tmp.prenume;
f>>tmp.grupa;
v.push_back(tmp);
}
f.close();
sort(v.begin(), v.end(), studentCompare);
f.open("an_sort.txt", ios_base::out);
for(vector<Student>::iterator it = v.begin(); it < v.end(); it++){
f<<it->stringify();
f<<"\n";
}
f.close();
return 0;
}