3.3 , 3.5 i 2.8 - primerna kontrolna

closed account (4Gv5Djzh)
3.3 /* Даден е текстов файл с думи - на всеки ред по една дума. Да се напише програма,която създава втори файл, в който думите да се напишат във въз. ред. */

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

const string inputFile = "C:/Users/Yavor Georgiev/Desktop/NTD1.txt";
const string outputFile = "C:/Users/Yavor Georgiev/Desktop/NTD2.txt";

int _tmain()
{
fstream fi, fo;
fi.open(inputFile, ios::in);
if(!fi.is_open())
{
cout<<"Cannot open input file"<<endl;
return -1;
}
fo.open(outputFile, ios::app);
if(!fo.is_open())
{
cout<<"Cannot open output file"<<endl;
return -1;
}
string word;
vector<string> v;
while(!fi.eof())
{
getline(fi, word);
int i;
for(i=0;i<v.size();i++)
{
if(v[i]>word)
{
v.insert(v.begin()+i, word);
break;
}
}
if(i==v.size()) v.push_back(word);
}
for(int i=0; i<v.size(); i++)
{
fo<<v[i]<<endl;
}
return 0;
}
///////////////////////////////////////////////////////////
3.5 Daden e tekstov fail: Dyma\tSreshtaniq\n
Spisuka s dymite se sortira v nizhodqsht red po chestota na sreshtane na dymite, po metoda na prqkata selekciq i da se izvede na ekran!

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <vector>
#include <utility>
using namespace std;

const string inputFile = "C:/Users/Yavor Georgiev/Desktop/eee.txt";

int _tmain()
{
fstream fi;
fi.open(inputFile.c_str(), ios::in);
if(!fi.is_open())
{
cout<<"Cant open input file\n";
return -1;
}
string word;
int count;
vector< pair<string, int> > v;

while(!fi.eof())
{
getline(fi, word, '\t');
fi>>count;
v.push_back(make_pair(word, count));
}
for(int i=0; i<v.size(); i++)
{
int max = i;
for(int j=i; j<v.size(); j++)
if(v[max].second<v[j].second)
max = j;

swap(v[max],v[i]);
}
for(int i=0; i<v.size();i++)
{
cout<<v[i].first<<'\t'<<v[i].second<<'\n';
}

return 0;
}
////////////////////////////////////////////////////////////////////

2.8 Ot avtomati4na meteorologi4na stanciq pristiga fail sus struktura "vreme->temperata".
/*programa koqto izvejda:
a) kolko pyti e bila izmerena vsqka ot stoinostite za temperatura "temperatura -> broi sreshtaniq"
b) da se izvedat izmerenite temperaturi v nizhodqsht red
*/
#include "stdafx.h"
#include <string>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;

const string inputFile = "tempfile.txt";

int _tmain()
{
fstream fi;
fi.open(inputFile.c_str(), ios::in);
if(!fi.is_open())
{
cout<<"Cannot open file"<<endl;
return -1;
}
string time;
double temp;
int i;
vector< pair<double, int> > v2;
while(!fi.eof())
{
getline(fi, time,'\t');
fi>>temp;
for(i=0; i<v2.size();i++)
{
if(temp == v2[i].first)
{
v2[i].second++;
break;
}
}
if(i>=v2.size())
{
v2.push_back(make_pair(temp, 1));
}
}
for(i=0;i<v2.size();i++)
{
cout<<v2[i].first<<'\t'<<v2[i].second<<endl;
}

cout<<endl<<endl<<endl;

for(int i=0; i<v2.size(); i++)
{
int max = i;
for(int j=i; j<v2.size(); j++)
if(v2[max].second<v2[j].second)
max = j;
swap(v2[max],v2[i]);
}
for(i=0;i<v2.size();i++)
{
cout<<v2[i].first<<'\t'<<v2[i].second<<endl;
}
return 0;
}
Last edited on
And what is the question?!
Pretty sure this guy is posting this to cheat on a test or something...all his posts are like this.
I would rewrite the first example the following way (without testing):

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
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <string>
#include <fstream>

const char *inputFile  = "C:/Users/Yavor Georgiev/Desktop/NTD1.txt";
const char *outputFile = "C:/Users/Yavor Georgiev/Desktop/NTD2.txt";

int _tmain()
{
   std::ifstream fi( inputFile );

   if ( !fi )
   {
      std::cerr << "Cannot open file " << inputFile << std::endl;
      return -1;
   }

   std::ofstream fo( outputFile );

   if ( !fo )
   {
      std::cerr << "Cannot open output file " << outputFile << std::endl;
      return -1; 
   }

   std::vector<std::string> v;
   v.reserve( 100 );

   while( fi )
   {
         std::string word;

         std::getline( fi, word );

         std::vector<std::string>::const_iterator it;
         
         it = std::find_if( v.cbegin(), v.cend(), std::bind2nd( std::greater<std::string>(), word ) );

         v.insert( it, word );
   }

   std::copy( v.cbegin(), v.cend(), std::ostream_iterator<std::string>( fo, "\n" ) );

   return 0;
}
Last edited on
Topic archived. No new replies allowed.