#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
int SIZE;
struct WordCount
{
string name;
int count;
};
//сортировка слов
void bubbleSort(string arr[], int size)
{
for (int stopMarker = 1; stopMarker < size; stopMarker++)
{
for (int j = size - 1; j >= stopMarker; j--)
{
if (arr[j] < arr[j - 1])
{
swap(arr[j], arr[j - 1]);
}
}
}
}
//подсчет количества слов в текстовом файле
int lenCount(int nCount){
//int fSpace = 1, nCount = 0;
int fSpace = 1;
nCount = 0;
char ch[2] = {0};
FILE *fh = fopen("./words.txt", "r");
while (! feof(fh))
{
fgets(ch, 2, fh);
if (ch[0] > 32)
fSpace = 0;
else if (0 == fSpace)
{
nCount++;
fSpace = 1;
}
ch[0] = 0;
}
fclose(fh);
//printf("%d\n", nCount);
//cout << nCount;
return nCount;
}
int main()
{
int nCount = 0;
int x = lenCount(nCount);
//cout << x << endl;
SIZE = x;
cout << SIZE << endl;
string wordArray[SIZE];
string temp;
WordCount arr[SIZE];
WordCount item;
ifstream input;
input.open("./words.txt");
for (int i = 0; i < SIZE; i++)
{
input >> wordArray[i];
temp.erase(std::remove_if(temp.begin(), temp.end(), [](char c)
{
return c == ',' || c == '.' || c == '!';
}),
temp.end());
}
input.close();
bubbleSort(wordArray, SIZE);
int j = 0;
for (int i = 0; j < SIZE; i++)
{
if (item.name != wordArray[j] && j < SIZE)
{
item.name = wordArray[j];
item.count = 1;
arr[i] = item;
j = j + 1;
}
while (j < SIZE && item.name == wordArray[j])
{
item.count++;
arr[i] = item;
j = j + 1;
}
}
for (int i = 0; i < 135; i++)
{
//cout << setw(13) << left << arr[i].name << setw(10) << "Count = " << arr[i].count << endl;
cout << setw(13) << left << arr[i].name << setw(10) << endl;
//cout << arr[i].name;
}
return 0;
}
Labor idea face Bush administration insistence training wage White House unrestricted six-month training wage time worker age job
string wordArray[SIZE];
нужно использовать std::vectorint j = 0;
for (int i = 0; j < SIZE; i++)
for (int i = 0; i < 135; i++)
{
//cout << setw(13) << left << arr[i].name << setw(10) << "Count = " << arr[i].count << endl;
cout << setw(13) << left << arr[i].name << setw(10) << endl;
//cout << arr[i].name;
}