代码之家  ›  专栏  ›  技术社区  ›  Arjay

c++将数组元素向右推

  •  1
  • Arjay  · 技术社区  · 7 年前

    块引用

        // automatac++.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <iostream>
    #include <list>
    #include <iterator>
    #include <conio.h>
    #include <cstring>
    
    //using namespace std;
    using std::cout;
    using std::cin;
    using std::endl;
    void menu_sel();
    void push_element();
    //void rem_element();
    
    int in;
    char array[100];
    
    int p_elements;
    
    int main()
    {
    
        menu_sel();
    
        return 0;
    }
    
    void push_element() {
        int p_elements;
    
        for (int e = 0; e < 10; e++) {
    
    
        }
    
        cout << "Enter number of elements:";
        cin >> p_elements;
        cout << "Enter only  " << p_elements << " elements"<<endl;
    
        for (int e = 0; e < p_elements; e++  ) {
            cin >> array[e];
        }
        cout << "Pushed elements are :";
        for (int e = 0; e < p_elements; ++e) {
            cout << array[e]<<" ";
    
        }
    
        //getch();
        menu_sel();
    
    }
    /*
    void rem_element() {
        char remove;
        int arr_position;
    
        cout << "You have selected Pop.\nRemove elements from arrays \n";
        cout << "Enter data to remove";
        cin >>remove;
    
        for (int ie -) {
    
        }
    
    
        system("pause");
    
    
    
    }
    */
    
    
    void menu_sel() {
    
    
        int input;
        cout << "\n****Menu Selection Here****";
        cout << "\n1. Push \n2. Pop \n3. Exit \n";
        cout << "select options here :";
        cin >> input;
    
        switch (input) {
        case 1:
            cout << "You have selected Push Stack\n";
            push_element();
            break;
    
        case 2:
            //cout << "You have selected Pop Stack\n";
            //rem_element();
            break;
    
        default:
            cout << "You have selected Invalid Options";    
            break;
            system("cls");
            return;
    
        }
    }
    

    这是输出

    the output shows pushing elements to the left

    我想展示这样的输出“4 r e 3”

    1 回复  |  直到 7 年前
        1
  •  0
  •   MrPako    7 年前

    如果希望以这种方式显示,您可以始终向后读取数组的输出。

    for (int e = p_elements-1; e >= 0; --e) {
        cout << array[e]<<" ";
    
    }