Дан текст, который пользователь должен ввести в программу, найти наибольшее количество идущих подряд букв. Реализовать следующие возможности в программе:
- меню пользователя, состоящее как минимум из 4-х пунктов:
ввод данных (текст вводится пользователем );
обработка данных (поиск наибольшего количества, подряд идущих, букв);
вывод результата на стандартное устройство вывода (экран);
выход из программы. - организовать промежуточное хранение результата, это относится к пункту меню — вывод результата;
- разбить программу на функции.
Смотрим пример работы программы:
CppStudio.com
Menu: 1-To enter the string 2-To process the data 3-To see result 0-Exit 1 Enter the string: Think different. Please, enter your choice: 2 Processing the data.. Please, enter your choice: 3 Your result: 2 Please, enter your choice: 0
Исходный код предоставила Наталья Гришина, поблагодарим её. Код программы немного подправил, подключил недостающие библиотеки,
#include <cstdlib>
#include <iostream>
using namespace std;
char str[80];
int choice, res;
int max_count(char str[80]);
int main()
{
cout << "Menu:\n1-To enter the string\n2-To process the data\n3-To see result\n0-Exit\n";
cin >> choice;
while (choice) {
switch(choice) {
case 1:
cout << "Enter the string:\n";
cin.get ();
cin.getline (str, 80);
break;
case 2:
cout << "Processing the data..\n";
res = max_count (str);
break;
case 3:
cout << "\nYour result: " << res << "\n";
break;
case 0:
exit(1);
break;
default:
cout << "\nPlease, enter 0, 1 or 2\n";
break;
}
cout << "\nPlease, enter your choice: ";
cin >> choice;
}
return 0;
}
int max_count(char str[80])
{
char *p = str;
int n;
int maxi = 0;
while (*p != 0) {
n = 1;
while (*p == *(p+1)) {
n++;
p++;
}
if (maxi < n) {
maxi = n;
}
p++;
}
return maxi;
}
Результат работы программы смотрим ниже. Для примера, использовал слоган компании Apple — Think different.
CppStudio.com
Menu: 1-To enter the string 2-To process the data 3-To see result 0-Exit 1 Enter the string: Think different. Please, enter your choice: 2 Processing the data.. Please, enter your choice: 3 Your result: 2 Please, enter your choice: 0
Итак, на выходе мы получили число 2 — это наибольшее количество идущих подряд одинаковых букв.
Комментарии
Anufree
#include <iostream> using namespace std; int main(int argc, char** argv) { setlocale(LC_ALL, "Russian"); cout << "\t\tMenu:" << endl; cout << "1-To enter the string" << endl; cout << "2-To process the data" << endl; cout << "1-To see result" << endl; cout << "0-Exit" << endl; int s_m; bool b = true; char str[256]; int counter = 1; do { cout << "Please, enter your choice: "; cin >> s_m; switch(s_m) { case 1: cout << "Enter the string: "; cin >> str; break; case 2: for(int i = 0; i < strlen(str) - 1; ++i) { if(str[i] == str[i + 1]) counter++; } cout << "Processing the data.." << endl; break; case 3: cout << "Your result: " << counter << endl; break; case 0: b = false; break; default: cout << "Please, enter key 0 - 3." << endl; break; } }while(b); system("pause"); return 0; }Arthur
#include<iostream> #include<stdio.h.> #include <Windows.h> using namespace std; const int MAX = 1000; int main() { setlocale(LC_CTYPE,"rus"); SetConsoleCP(1251); SetConsoleOutputCP(1251); char str[MAX]; char ch; while(true) { cin.sync(); cout<<"Введите строку: "<<endl; gets(str); int i = 1, n = 1; char lastch = str[0]; char maxch; for(int x = 1; str[x] != 0; x++) if( lastch == str[x] && isalpha(str[x]) ) { i++; if(i>n) { n = i; maxch = str[x]; } } else { lastch = str[x]; i = 1; } if(n == 1) cout<<"Нет подряд идущих одинаковых букв"<<endl; else cout<<n<<" "<<maxch; cout<<"\nЕще раз? y/n >> "; cin>>ch; if(ch == 'n') break; } return 0; }