Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <set>
- #include <string>
- #include <algorithm>
- using namespace std;
- const set <char> setOfDigitsAndOperations { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '-', '*', '/', ':', '^'};
- char takeSource() {
- char choice;
- cin >> choice;
- while ((choice != '1') && (choice != '2')) {
- while (cin.get() != '\n') {
- cin.clear();
- }
- cout << "Incorrect input!!!\nSelect the source:\n1:Console\n2:File\nEnter 1 or 2: ";
- cin >> choice;
- }
- return choice;
- }
- string takeInPath() {
- string path;
- bool isIncorrect;
- cout << "Enter file path: ";
- do {
- isIncorrect = true;
- cin >> path;
- ifstream fin(path);
- if (fin.is_open()) {
- isIncorrect = false;
- fin.close();
- }
- else {
- cout << "File is not found\nEnter file path: ";
- }
- if (!isIncorrect && ((path.length() < 4) || (!(path.substr(path.length() - 4) == ".txt")))) {
- cout << "File is found, but it is not \".txt\" type file\nEnter file path : ";
- isIncorrect = true;
- }
- } while (isIncorrect);
- return path;
- }
- string takeOutPath() {
- string path;
- bool isIncorrect;
- cout << "Enter file path: ";
- do {
- isIncorrect = false;
- cin >> path;
- if ((path.length() < 4) || (!(path.substr(path.length() - 4) == ".txt"))) {
- cout << "It should be a \".txt\" file\nEnter file path: ";
- isIncorrect = true;
- }
- } while (isIncorrect);
- return path;
- }
- int takeSize() {
- const int MIN_SIZE = 2;
- const int MAX_SIZE = 30;
- int number = 0;
- bool isIncorrect;
- do {
- isIncorrect = false;
- cin >> number;
- if (!cin.good()) {
- isIncorrect = true;
- cout << "Incorrect input!!!\n";
- while (cin.get() != '\n') {
- cin.clear();
- }
- }
- if ((!isIncorrect) && (number < MIN_SIZE || number > MAX_SIZE)) {
- isIncorrect = true;
- cout << "Incorrect input!!!\n";
- }
- } while (isIncorrect);
- return number;
- }
- char takeChar() {
- char inputChar;
- bool isIncorrect;
- string line;
- do {
- cin >> line;
- isIncorrect = false;
- if ((line.length() != 1)) {
- cout << "Incorrect input!!! Must be char:";
- isIncorrect = true;
- }
- else {
- inputChar = line[0];
- }
- } while (isIncorrect);
- return inputChar;
- }
- set <char> takeSetFromConsole() {
- cout << "Enter size: ";
- int size = takeSize();
- set <char> inputSet;
- for (int i = 0; i < size; i++) {
- cout << "Enter element " << (i + 1) << ": ";
- inputSet.insert(takeChar());
- }
- return inputSet;
- }
- set <char> takeSetFromFile(const string path) {
- ifstream fin(path);
- set <char> inputSet;
- string sign;
- bool isCorrect = true;
- while (isCorrect && !fin.eof()) {
- fin >> sign;
- if (sign.size() == 1) {
- inputSet.insert(sign[0]);
- }
- else {
- cout << "Incorrect file data\n";
- isCorrect = false;
- }
- }
- fin.close();
- if (!isCorrect) {
- cout << "Enter set from console:\n";
- inputSet = takeSetFromConsole();
- }
- return inputSet;
- }
- set <char> takeSet(const char source) {
- string inPath;
- set <char> inputSet;
- if (source == '1') {
- inputSet = takeSetFromConsole();
- }
- else {
- inPath = takeInPath();
- inputSet = takeSetFromFile(inPath);
- }
- return inputSet;
- }
- set <char> findAnswer(set <char> inputSet) {
- set <char> answer;
- set_intersection(inputSet.begin(), inputSet.end(), setOfDigitsAndOperations.begin(), setOfDigitsAndOperations.end(), inserter(answer, answer.end()));
- return answer;
- }
- void outputToConsole(set <char> setOfAnswers) {
- cout << "Answer is:\n(";
- set<char>::iterator number;
- for (number = setOfAnswers.begin(); number != setOfAnswers.end(); number++) {
- cout << " " << *number;
- }
- cout << ")";
- }
- void outputToFile(const string path, set <char> setOfAnswers) {
- ofstream fout;
- fout.open(path);
- fout << "Answer is:\n(";
- set<char>::iterator number;
- for (number = setOfAnswers.begin(); number != setOfAnswers.end(); number++) {
- fout << " " << *number;
- }
- fout << ")";
- fout.close();
- cout << "Done";
- }
- void output(const char source, set <char> setOfAnswers) {
- string outPath;
- if (source == '1') {
- outputToConsole(setOfAnswers);
- }
- else {
- outPath = takeOutPath();
- outputToFile(outPath, setOfAnswers);
- }
- }
- int main() {
- cout << "Welcome to the program that will print a set, whose elements are signs of arithmetic operations and digits from a given set\nSelect the source for entering the sentence:\n1:Console\n2:File\nEnter 1 or 2: ";
- char inputSource = takeSource();
- set <char> inputSet = takeSet(inputSource);;
- set <char> answer = findAnswer(inputSet);
- cout << "Select the source for output:\n1:Console\n2:File\nEnter 1 or 2: ";
- char outputSource = takeSource();
- output(outputSource, answer);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement