• Предмет: Информатика
  • Автор: kamovi2000
  • Вопрос задан 4 месяца назад

Написати програму на С++ використовуючи рядки string і методи класу
string із приміренням як
вбудованих функцій і так і без вбудованих функцій
1. Обробку заданого рядка оформити у вигляді функції (обмін даними
має бути організовано лише через параметри).
2. Основна програма (main), реалізація використовуваних функцій і їх
інтерфейс мають знаходитися в різних файлах.
3. Кожна функція і вся програма в цілому повинні мати специфікацію у
вигляді коментарів
4. Дані (вихідний рядок) увести з файлу, результати вивести у файл.
5. Реалізувати перевірку коректності запису й зчитування файлу
(формат уведення-виведення вибрати самостійно).
6. Реалізувати перевірку коректності зчитування й запису файлу.
Саме завдання:
1. З клавіатури вводиться текстовий рядок. Скласти програму, яка
підраховує кількість слів, які мають однакову кількість приголосних і
голосних літер; виводить на екран найдовше слово.
2. Вводиться з клавіатури маса в кілограмах/грамах наприклад 52,12..
Записати дану масу текстом П‘ятдесят два кілограми дванадцять грам.
Перед виведенням результат помістити в об‘єкт типу string.

Ответы

Ответ дал: mylittleswallow152
0

Можу надати вам структуру коду для завдань, які ви згадали.

Для першого завдання:

// main.cpp

#include <iostream>

#include <fstream>

#include <string>

#include "string_functions.h" // header file for the string functions

using namespace std;

int main() {

   string input_str, longest_word;

   int num_words;

   ifstream in_file("input.txt"); // file containing input string

   ofstream out_file("output.txt"); // file to output results

   

   if (!in_file) {

       cout << "Error reading input file." << endl;

       return 1;

   }

   if (!out_file) {

       cout << "Error writing to output file." << endl;

       return 1;

   }

   

   // read input string from file

   getline(in_file, input_str);

   

   // call function to count number of words and find longest word

   num_words = count_words(input_str, longest_word);

   

   // output results to file

   out_file << "Number of words with equal vowels and consonants: " << num_words << endl;

   out_file << "Longest word: " << longest_word << endl;

   

   // close files

   in_file.close();

   out_file.close();

   

   return 0;

}

// string_functions.h

#ifndef STRING_FUNCTIONS_H

#define STRING_FUNCTIONS_H

#include <string>

using namespace std;

int count_words(string input_str, string& longest_word);

#endif

// string_functions.cpp

#include "string_functions.h"

int count_words(string input_str, string& longest_word) {

   // implementation of function to count number of words and find longest word

}

Для другого завдання:

// main.cpp

#include <iostream>

#include <fstream>

#include <string>

#include "string_functions.h" // header file for the string functions

using namespace std;

int main() {

   double mass;

   string mass_str;

   ifstream in_file("input.txt"); // file containing input mass

   ofstream out_file("output.txt"); // file to output result

   

   if (!in_file) {

       cout << "Error reading input file." << endl;

       return 1;

   }

   if (!out_file) {

       cout << "Error writing to output file." << endl;

       return 1;

   }

   

   // read mass from file

   in_file >> mass;

   

   // call function to convert mass to string

   mass_str = mass_to_string(mass);

   

   // output result to file

   out_file << mass_str << endl;

   

   // close files

   in_file.close();

   out_file.close();

   

   return 0;

}

// string_functions.h

#ifndef STRING_FUNCTIONS_H

#define STRING_FUNCTIONS_H

#include <string>

using namespace std;

string mass_to_string(double mass);

#endif

// string_functions.cpp

#include "string_functions.h"

string mass_to_string(double mass) {

   // implementation of function to convert mass to string

}


kamovi2000: Так, було б добре якби ви надали структуру коду для завдань
Вас заинтересует