#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void printList(vector<double> list) {
cout << "[ ";
for (auto i : list) {
cout << i << " ";
}
cout << "]" << endl;
}
int main() {
vector<double> list;
auto selection{ '0' };
auto new_num{ 0.0 };
auto sum{ 0.0 };
do {
cout << "Menu options:" << endl;
cout << "P - Print numbers" << endl
<< "A - Add a number" << endl
<< "M - Display mean of the numbers" << endl
<< "S - Display the smallest number" << endl
<< "L - Display the largest number" << endl
<< "Q - Quit" << endl << endl;
cout << "Enter your choice: ";
cin >> selection;
switch (tolower(selection)) {
case 'p':
cout << "Displaying all the elements in list: ";
printList(list);
break;
case 'a':
cout << "Add integer: ";
cin >> new_num;
cout << "Adding new number to the list." << endl;
list.push_back(new_num);
cout << "Showing the updated list: ";
printList(list);
break;
case 'm':
cout << "Calculating average of the list." << endl;
sum = 0;
for (auto i : list) {
sum += i;
}
cout << "The average is: " << sum / list.size() << endl;
break;
case 's':
cout << "The smallest number in the list is: "
<< *min_element(list.begin(), list.end()) << endl;
break;
case 'l':
cout << "The largest number in the list is: "
<< *max_element(list.begin(), list.end()) << endl;
break;
default:
break;
}
cout << endl;
} while (selection != 'q' && selection != 'Q');
return 0;
}
İftar gelmiş. Sen yaz, ben sonra bakarım.
Sayı eklerken girilenin sayı olup olmadığını kontrol etme ekle.
Ortalamayı hesaplarken toplam değerini sıfırlamayı unutmuşsun.
Derleyince derliyor. Test etmedim.
Kod:#include <iostream> #include <vector> #include <algorithm> using namespace std; void printList(vector<double> list) { cout << "[ "; for (auto i : list) { cout << i << " "; } cout << "]" << endl; } int main() { vector<double> list; auto selection{ '0' }; auto new_num{ 0.0 }; auto sum{ 0.0 }; do { cout << "Menu options:" << endl; cout << "P - Print numbers" << endl. << "A - Add a number" << endl. << "M - Display mean of the numbers" << endl. << "S - Display the smallest number" << endl. << "L - Display the largest number" << endl. << "Q - Quit" << endl << endl; cout << "Enter your choice: "; cin >> selection; switch (tolower(selection)) { case 'p': cout << "Displaying all the elements in list: "; printList(list); break; case 'a': cout << "Add integer: "; cin >> new_num; cout << "Adding new number to the list." << endl; list.push_back(new_num); cout << "Showing the updated list: "; printList(list); break; case 'm': cout << "Calculating average of the list." << endl; sum = 0; for (auto i : list) { sum += i; } cout << "The average is: " << sum / list.size() << endl; break; case 's': cout << "The smallest number in the list is: " << *min_element(list.begin(), list.end()) << endl; break; case 'l': cout << "The largest number in the list is: " << *max_element(list.begin(), list.end()) << endl; break; default: break; } cout << endl; } while (selection != 'q' && selection != 'Q'); return 0; }
#include <iostream>
#include <algorithm>
#include <sstream>
#include <vector>
#include <numeric>
using namespace std;
void printMenu() {
cout << "Menu options:" << endl
<< "P - Print numbers" << endl
<< "A - Add a number" << endl
<< "M - Display mean of the numbers" << endl
<< "S - Display the smallest number" << endl
<< "L - Display the largest number" << endl
<< "Q - Quit" << endl << endl;
}
template <class T>
void printList(vector<T>& list) {
cout << "[ ";
for (auto i : list) {
cout << i << " ";
}
cout << "]" << endl;
}
template <class T>
void p(vector<T>& list) {
cout << "Displaying all the elements in list: ";
printList(list);
}
template <class T>
void a(vector<T>& list) {
string input{ "" };
cout << "Add integer: ";
cin >> input;
auto num{ double() };
auto str{ stringstream(input) };
str >> num;
if (str.fail() || !str.eof()) {
cout << "invalid input" << endl;
return;
}
cout << "Adding new number to the list." << endl;
list.push_back(num);
cout << "Showing the updated list: ";
printList(list);
}
template <class T>
void m(vector<T>& list) {
if (list.empty()) {
cout << "List is empty" << endl;
return;
}
cout << "Calculating average of the list." << endl
<< "The average is: " << accumulate(list.begin(), list.end(), 0.0) / list.size() << endl;
}
template <class T>
void s(vector<T>& list) {
if (list.empty()) {
cout << "List is empty" << endl;
return;
}
cout << "The smallest number in the list is: "
<< *min_element(list.begin(), list.end()) << endl;
}
template <class T>
void l(vector<T>& list) {
if (list.empty()) {
cout << "List is empty" << endl;
return;
}
cout << "The largest number in the list is: "
<< *max_element(list.begin(), list.end()) << endl;
}
template <class T>
void select(vector<T>& list, char selection) {
switch (tolower(selection)) {
case 'p': p(list); break;
case 'a': a(list); break;
case 'm': m(list); break;
case 's': s(list); break;
case 'l': l(list); break;
default: break;
}
}
int main() {
vector<double> list;
auto selection{ '0' };
do {
printMenu();
cout << "Enter your choice: ";
cin >> selection;
select(list, selection);
cout << endl;
} while (tolower(selection) != 'q');
return 0;
}
İç kısımlarını da fonksiyon yap. Bu sayede programına yeni bir şey eklemen gerektiğinde bir fonksiyon yazıp Switch'e 1-2 satır eklersin o kadar.
Max ve Min'ı ben de bilmiyordum. Ama C++'ta bunun için bir şeyler vardır dedim. Ortalama için de var ama numeric kütüphanesindeymiş. Kütüphane eklemiş olmayayım dedim.
For each döngüsü yaparken bir yerde list kalmış. Onun yüzünden bazı yerleri değiştirmem yarıda kalmıştı. Biraz daha düzenleyip paylaşıyorum.
Switch Case'i fonksiyona koymak while içinde çok fazla kere çağrılabileceği için muhtemelen çok da mantıklı değil.
Bir girdinin double olup olmamasına bakmayı da yaptığım gibi buldum. Daha güzeli var mıdır bilmiyorum.
Kod:#include <iostream> #include <algorithm> #include <sstream> #include <vector> #include <numeric> using namespace std; void printMenu() { cout << "Menu options:" << endl. << "P - Print numbers" << endl. << "A - Add a number" << endl. << "M - Display mean of the numbers" << endl. << "S - Display the smallest number" << endl. << "L - Display the largest number" << endl. << "Q - Quit" << endl << endl; } template <class T> void printList(vector<T>& list) { cout << "[ "; for (auto i : list) { cout << i << " "; } cout << "]" << endl; } template <class T> void p(vector<T>& list) { cout << "Displaying all the elements in list: "; printList(list); } template <class T> void a(vector<T>& list) { string input{ "" }; cout << "Add integer: "; cin >> input; auto num{ double() }; auto str{ stringstream(input) }; str >> num; if (str.fail() || !str.eof()) { cout << "invalid input" << endl; return; } cout << "Adding new number to the list." << endl; list.push_back(num); cout << "Showing the updated list: "; printList(list); } template <class T> void m(vector<T>& list) { if (list.empty()) { cout << "List is empty" << endl; return; } cout << "Calculating average of the list." << endl. << "The average is: " << accumulate(list.begin(), list.end(), 0.0) / list.size() << endl; } template <class T> void s(vector<T>& list) { if (list.empty()) { cout << "List is empty" << endl; return; } cout << "The smallest number in the list is: " << *min_element(list.begin(), list.end()) << endl; } template <class T> void l(vector<T>& list) { if (list.empty()) { cout << "List is empty" << endl; return; } cout << "The largest number in the list is: " << *max_element(list.begin(), list.end()) << endl; } template <class T> void select(vector<T>& list, char selection) { switch (tolower(selection)) { case 'p': p(list); break; case 'a': a(list); break; case 'm': m(list); break; case 's': s(list); break; case 'l': l(list); break; default: break; } } int main() { vector<double> list; auto selection{ '0' }; do { printMenu(); cout << "Enter your choice: "; cin >> selection; select(list, selection); cout << endl; } while (tolower(selection) != 'q'); return 0; }