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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
# include <iostream>
# include <vector>
# include <fstream>
# include <cmath>
using namespace std;
void creatingName(vector<string> *rows){
for(size_t x=0;x<rows->size();x++)
{
// cout << (*rows)[x] << endl;
for(size_t y=0;y<(*rows)[x].size();y++){
if((*rows)[x][y]==':'){
(*rows)[x].resize(y);
break;
}
}
}
}
void isSorted(bool *sorted, vector<int> *assembledNumber,int number){
for(size_t x=0;x<assembledNumber->size()-number;x++)
{
if((*assembledNumber)[x]<(*assembledNumber)[x+1])
*sorted = false;
else
*sorted = true;
}
}
void creatingNumber(vector<int> *number, vector<int> *assembledNumber)
{
int n = number->size(), sum = 0;
for(int x=0;x<n;x++){
if(n == 0)
sum += (*number)[x];
else
sum += (*number)[x]*pow(10, n-1);
n--;
}
cout << sum << endl;
assembledNumber->push_back(sum);
}
void checkIfInputIsCorrect(bool *input, vector<string> *rows)
{
string line;
ifstream file;
file.open("exam.txt");
while(getline(file, line)){
if (line == "")
break;
rows->push_back(line);
}
*input = true;
for(size_t x=0;x<rows->size();x++)
{
int signs1 = 0, signs2 = 0;
for(size_t y=0;y<(*rows)[x].size();y++)
{
if((*rows)[x][y]==':')
signs1++;
else if((*rows)[x][y]>='0' && (*rows)[x][y]<= '9')
signs2++;
}
if(signs1 == 0 || signs2 == 0)
*input = false;
}
file.close();
}
int main()
{
bool inputCorrect, sorted = false;
vector<string> rows;
vector<int> number, assembledNumber;
/*fstream file("exam.txt");
file.open("exam.txt");*/// No need to declare those here
int replacingNumber,sadNumbuer = 1;
string replacingString;
checkIfInputIsCorrect(&inputCorrect, &rows);
if(rows.empty())
{
cout << "No data has been input!" << endl;
}else
{
if(inputCorrect)
{
for(size_t x=0;x<rows.size();x++)
{
int n = rows[x].size();
for(int y=0;y<n;y++)
{
char c = rows[x][y];
if(isdigit(c))
{
int a = c-48;
number.push_back(a);
}
}
creatingNumber(&number, &assembledNumber);
number.clear();
}
while(!sorted)
{
isSorted(&sorted,&assembledNumber,sadNumbuer);
for(size_t x=0;x<assembledNumber.size()-1;x++)
{
if(assembledNumber[x]<assembledNumber[x+1])
{
replacingNumber = assembledNumber[x+1];
assembledNumber[x+1] = assembledNumber[x];
assembledNumber[x] = replacingNumber;
replacingString = rows[x+1];
rows[x+1] = rows[x];
rows[x] = replacingString;
}
}if(sadNumbuer<4)sadNumbuer++;
}
creatingName(&rows);
cout << "\n" << rows[0] << " had the highest score with " << assembledNumber[0] << "!" << endl;
}else
cout << "Your input was incorrect!\nTry again!" << endl;
}
}
|