Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <set>
- using namespace std;
- ifstream fin;
- ofstream fout;
- bool choose() {
- int inputNumber;
- bool isIncorrect;
- const int MIN_NUM = 0;
- const int MAX_NUM = 1;
- do {
- cin >> inputNumber;
- isIncorrect = false;
- if (cin.fail()) {
- isIncorrect = true;
- cout << "Please, enter a number." << endl;
- cin.clear();
- while (cin.get() != '\n');
- }
- if (!isIncorrect && (inputNumber < MIN_NUM || inputNumber > MAX_NUM)) {
- cout << "You are out of input range!" << endl;
- isIncorrect = true;
- }
- } while (isIncorrect);
- if (inputNumber == 1)
- return true;
- else
- return false;
- }
- string inputFilePath() {
- const int EXTENSION_SIZE = 4;
- string path;
- string ext;
- bool isIncorrect;
- do {
- isIncorrect = false;
- cout << "Input path to file: " << endl;
- cin >> path;
- fin.open(path);
- if (path.size() > EXTENSION_SIZE) {
- ext = path.substr(path.size() - EXTENSION_SIZE, EXTENSION_SIZE);
- }
- else {
- cout << "Incorrect file name." << endl;
- isIncorrect = true;
- } if (!isIncorrect && ext != ".txt") {
- cout << "Must have .txt!" << endl;
- isIncorrect = true;
- }
- else if (!isIncorrect && !fin.is_open()) {
- cout << "Wrong way to file." << endl;
- isIncorrect = true;
- }
- } while (isIncorrect);
- fin.close();
- return path;
- }
- string inputSequence(){
- string sequence;
- cout << "Input Sequence" << endl;
- cin >> sequence;
- return sequence;
- }
- string inputSequenceFromFile(string path) {
- string sequence;
- fin.open(path);
- fin >> sequence;
- return sequence;
- }
- void fileOutput(set <char> setOfSymbols) {
- set <char>::iterator output;
- string path = inputFilePath();
- fout.open(path);
- if (setOfSymbols.empty())
- fout << "You have not entered any signs of arithmetic operations or digits.";
- else{
- fout << "The resulting set" << endl;
- for (output = setOfSymbols.begin(); output != setOfSymbols.end(); ++output)
- fout << *output << " ";
- }
- cout << "Successful output in file." << endl;
- fout.close();
- }
- void consoleOutput(set <char> setOfSymbols) {
- set <char>::iterator output;
- if (setOfSymbols.empty())
- cout << "You have not entered any signs of arithmetic operations or digits." << endl;
- else{
- cout << "The resulting set" << endl;
- for (output = setOfSymbols.begin(); output != setOfSymbols.end(); ++output)
- cout << *output << " ";
- }
- }
- set <char> makeSet(string sequence){
- set <char> setOfSymbols{};
- set <char> symbols{ '/', '*', '-', '+', '%', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
- for (int i = 0; i < size(sequence); i++){
- if (symbols.count(sequence[i]) == 1)
- setOfSymbols.insert(sequence[i]);
- }
- return setOfSymbols;
- }
- int main() {
- cout << "Given a non-empty sequence of characters, it is required to construct and print a set whose elements are signs of arithmetic operations and digits that occur in the sequence." << endl;
- string sequence;
- set <char> setOfSymbols{};
- cout << "Type 1 - console input, type 0 - file input." << endl;
- bool choice = choose();
- if (choice)
- sequence = inputSequence();
- else
- {
- string path = inputFilePath();
- sequence = inputSequenceFromFile(path);
- }
- setOfSymbols = makeSet(sequence);
- cout << "Type 1 - console output, type 0 - file output." << endl;
- choice = choose();
- if (choice)
- consoleOutput(setOfSymbols);
- else
- fileOutput(setOfSymbols);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement