Függvényes feladat - menü

Feladat

Vegyük elő a tavalyi menüvel ellátott tömbös programot (a feladatleírás itt).

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <iostream>
using namespace std;

/*
Házi feladat

Vegyünk fel egy kezdetben üres tömböt.

Készítsünk menüt az alábbi opciókkal.
    1. elemek bevitele (adott n és az elemek)
    2. elem beszúrása a k. helyre (1. hely a [0] index, 2. hely az [1] stb.)
    3. elem törlése a k. helyrõl
    4. tömb kiírása
    (HF) 5. nulla elemek törlése
    (HF) 6. elemek számának kiírása
    (HF) 7. prímek számának kiírása
    (HF) 8. rendezés növekvő sorrendbe
    9. kilépés
*/

void beolvas(int &n, int t[])
{
    cout << "n = ";
    cin>>n;
    cout << "elemek: ";
    for(int i=0; i<n; i++)
        cin>>t[i];
}

void kiir(int n, int t[])
{
    for(int i=0; i<n; i++)
        cout << t[i] << " ";
    cout << endl;
}

void beszur(int &n, int t[])
{
    cout<<"k= ";
    int k;
    cin>>k;

    cout<<"elem= ";
    int elem;
    cin>>elem;

    for(int i=n; i>=k; i--){
        t[i]=t[i-1];
    }
    t[k-1]=elem;
    n++;
}


void torol(int &n, int t[])
{
    cout<<"k = ";
    int k;
    cin>>k;

    // töröljük a k-1 indexű elemet
    for(int i=k-1; i<=n-2; i++){
        t[i] = t[i+1];
    }
    n--;
}

void torol0(int &n, int t[])
{
    int i = 0;

    while (i < n) {
        if (t[i] == 0) {
            for (int j = i; j <= n-2; j++)
                t[j] = t[j+1];

            n--;
        }
        else
            i++;
    }
}

bool prim(int n)
{
    if (n < 2) return false;

    int db = 0;
    for (int i = 1; i <= n; i++)
        if (n % i == 0)
            db++;

    return db == 2;
}

int hany_prim(int n, int t[])
{
    int db = 0;

    for (int i = 0; i < n; i++)
        if (prim(t[i]))
            db++;

    return db;
}

void cserel(int &a, int &b)
{
    int temp = a;
    a = b;
    b = temp;
}

void rendez(int n, int t[])
{
    for (int i = 0; i < n; i++)
        for (int j = i+1; j < n; j++)
            if (t[i] > t[j])
                cserel(t[i], t[j]);
}

int main()
{
    int t[10000];
    int n;
    bool megy = true;

    while (megy) {
        cout << "Valasszon egyet az alabbiak kozul:" << endl;
        cout << "   1. elemek bevitele (adott n es az elemek)" << endl;
        cout << "   2. elem beszurasa a k. helyre" << endl;
        cout << "   3. elem torlese a k. helyrol" << endl;
        cout << "   4. tomb kiirasa" << endl;
        cout << "   5. nulla elemek torlese" << endl;
        cout << "   6. elemek szamanak kiirasa" << endl;
        cout << "   7. primek szamanak kiirasa" << endl;
        cout << "   8. rendezes novekvo sorrendbe" << endl;
        cout << "   9. kilepes" << endl;
        cout << "Opcio: ";

        int opcio;
        cin >> opcio;
        cout << endl;

        if (opcio == 1) {
            beolvas(n,t);
        }
        else if (opcio == 2) {
            beszur(n,t);
        }
        else if (opcio == 3) {
            torol(n,t);
        }
        else if (opcio == 4) {
            kiir(n,t);
        }
        else if (opcio == 5) {
            torol0(n,t);
        }
        else if (opcio == 6) {
            cout << n << endl;
        }
        else if (opcio == 7) {
            cout << hany_prim(n, t) << endl;
        }
        else if (opcio == 8) {
            rendez(n, t);
        }
        else if (opcio == 9) {
            megy = false;
        }
        else {
            cout << "Nincs ilyen opcio!" << endl;
        }

        cout << endl << endl;
    }

    return 0;
}