C++ empty list oluşturma

RSK06

Hectopat
Katılım
29 Aralık 2020
Mesajlar
2.285
Çözümler
4
Daha fazla  
Cinsiyet
Erkek
[CODE lang="cpp" title="C++"]#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
char selection{};
vector<double> list { 1, 2, 3, 4, 5 };
double new_num{};
double average{};
double total {};
double max = list[0];
double min = list[0];

do {
cout << "Menu options: \n" << endl;
cout << "P - Print numbers\nA - Add a number\nM - Display mean of the numbers\nS - Display the smallest number\nL - Display the largest number\nQ - Quit\n" << endl;
cout << "Enter your choice: ";
cin >> selection;
if (selection == 'P' || selection == 'p') {
cout << "displaying all the elements in list: [ ";
for (unsigned i = 0 ; i < list.size(); i++)
cout << list << " ";

cout << " ]" << endl;
}
else if(selection == 'A' || selection == 'a') {
cout << "Add integer: ";
cin >> new_num;
cout << "Adding new number to the list: [ ";
list.push_back(new_num);
cout<< "Showing the updated list: ";
for (unsigned i = 0 ; i < list.size(); i++)
cout<<list << " ";
cout << " ]" << endl;
}
else if (selection == 'M' || selection == 'm') {
cout << "\nCalculating average of the list... " << endl;
for (auto ttl: list) {
total += ttl;
}
average = total / list.size();
cout << "\nThe Average is: " << average << "\n" << endl;
}
else if (selection == 'S' || selection == 's') {
for(int i = 0; i < list; i++){
if(list < min){
min = list;
}
}
cout << "\nThe smallest number in the list is: " << min << "\n" << endl;
}
else if (selection == 'L' || selection == 'l') {
for (int i = 0; i < list; i++) {
if (list > max) {
max = list;

}
}
cout << "\nThe largest number in the list is: " << max << "\n" << endl;
}

} while (selection != 'q' && selection != 'Q');
cout << endl;

return 0;
}[/CODE]

Kod bu şekilde yapmam gereken şey list boş ise "cout << "empty list" demem fakat nasıl boş liste oluşturacağım bilmiyorum. Mesela p seçince list boş ise "empty list" demesi lazım. Daha sonra push back ile sayı eklediğim zaman artık sadece sayıyı göstermeli.
 
Son düzenleyen: Moderatör:
Öncelikle list ile vector farklı şeyler.
tolower diye bir şey var.
Switch case diye bir şey var.
auto var.
Uniform initialization var.
endl diye bir şey de var.
Tek satır for kullansan da parantezini koy.
Değişkenleri double yapmışsın ama adama add integer diyorsun.
Her bir switch case için fonksiyon ayarlarsın. İçlerini ben düzeltiyorum. Gerekli başka fonksiyon da ekliyorum.

Sayı ekle deyip p girersen ne olduğuna bak istersen.

Kodu düzenliyorum. Sonra sevmediğin bir yer varsa söylersin.
 
Son düzenleme:
İ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;
}
 
İ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;
}

Kod çok güzel çalışıyor çok teşekkürler, tolower, Void gibi şeyleri daha bilmiyorum biraz araştırma ile öğrenmeye çalışcam tam anlamak için, Max vemin element kısmını 2-3 saat araştırmış bulamamıştım şimdi anladım ve Switch kullanmak gerçekten çok daha iyi olmuş aklıma gelmemişti teşekkürler.
 
İç 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;
}
 
İç 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;
}

Burada biraz bilmediğim kelime var, daha 2 haftadır çalışıyorum Udemy'den ders alarak burada bilmediğim şeylerin hepsine bakacağım çok teşekkürler.
 

Geri
Yukarı