Как вы можете хранить значения с помощью указателя и как вы можете показать все значения, которые хранятся? - PullRequest
0 голосов
/ 05 марта 2020

Итак, у меня есть эта программа для хранения всех данных, которые будет вводить пользователь. Я использую указатель, и я немного запутался, как хранить их все и как отображать их все. Вот мой код:

#include<iostream>
using namespace std;
int main(){
    int num1, num2, result, range;
    int *ptr1 = &num1, *ptr2 = &num2;
    char operation, answer;
    char *ptrop = &operation;
    while(true){
    cout<<"ENTER TWO NUMBERS: \n";
    cin>>*ptr1>>*ptr2;
    cout<<endl;
    cout<<"CHOOSE OPERATION: ";
    cin>>operation;
    switch(operation){
        case '+':
            result = *ptr1 + *ptr2;
            cout<<"Asnwer is "<<result<<endl;
            break;
        case '-':
                result = *ptr1 - *ptr2;
                cout<<"Asnwer is "<<result<<endl;
            break;
        default:
            cout<<"NONE\n\n";
    }
    cout<<"\nWANT TO TRY AGAIN? ";
    cin>>answer;
    switch(answer){
        case 'Y':
        case 'y':
            ptr1++;
            ptr2++;
            range ++;
            system("cls");
            continue;
        default:
            cout<<"VIEW HISTORY?";
            cin>>answer;
                switch(answer){
                    case 'Y':
                    case 'y':
                        for(int i=0;i<=range;i++){
                            cout<<"ADDRESS OF "<<*ptr1<<" is "<<ptr1<<endl;
                            cout<<"ADDRESS OF "<<*ptr2<<" is "<<ptr2<<endl<<endl;
                        }
                        break;
                    default:
                        return 0;
                }
    }
    }
}

Я пытаюсь сохранить все данные, но при просмотре истории отображаются те же цифры. Я в замешательстве, я не знаю, как это исправить

1 Ответ

0 голосов
/ 05 марта 2020

Вы должны немного пересмотреть логи c, но идея в том, что вы определяете динамические c массивы, в которых будут храниться ваши значения. Для каждой операции вы сохраняете значения в позиции этого массива, а затем увеличиваете указатель. Чтобы увидеть историю, вы начинаете с первой позиции и проходите до конца.

#include<iostream>
using namespace std;
int main(){

    int result;
    int range = 0; 
    int max_range = 10; // maximum number of operations to do, you could ask this to the user...
    int* num1 = new int[max_range]; // arrays instead of simple ints
    int* num2 = new int[max_range];
    int *ptr1 = &num1[0]; // initialize the pointers to the first position
    int *ptr2 = &num2[0];

    char operation, answer;
    char *ptrop = &operation;
    while(true){
        cout<<"ENTER TWO NUMBERS: \n";
        cin>>*ptr1>>*ptr2;
        cout<<endl;
        cout<<"CHOOSE OPERATION: ";
        cin>>operation;
        switch(operation){
        case '+':
            result = *ptr1 + *ptr2;
            cout<<"Asnwer is "<<result<<endl;
            break;
        case '-':
            result = *ptr1 - *ptr2;
            cout<<"Asnwer is "<<result<<endl;
            break;
        default:
            cout<<"NONE\n\n";
        }
        cout<<"\nWANT TO TRY AGAIN? ";
        cin>>answer;
        switch(answer){
        case 'Y':
        case 'y':
            ptr1++; // the pointers are updated to the next position
            ptr2++;
            range ++; // increase the number of operations done.
            system("cls");
            continue;
        default:
            cout<<"VIEW HISTORY?";
            cin>>answer;
            switch(answer){
            case 'Y':
            case 'y':
                ptr1 = &num1[0]; // reset the pointers to the beginning of the arrays
                ptr2 = &num2[0];
                for(int i=0;i<=range;i++){
                    cout<<"ADDRESS OF "<<*ptr1<<" is "<<ptr1<<endl;
                    cout<<"ADDRESS OF "<<*ptr2<<" is "<<ptr2<<endl<<endl;
                    ptr1++; // VERY IMPORTANT!! This moves the pointer to the next value.
                    ptr2++;
                }
                break;
            default:
                return 0;
            }
        }
    }
    delete [] num1; // always delete dynamic arrays.
    delete [] num2;
    return 0;
}
...