1

Создать интерактивное приложение с возможностью выбора команды: а) ввод данных в стек(и) из стандартного или нестандартного текстового файла(ов); б) вывод данных из всех стеков на экран (стандартный текстовый файл); в) решение задачи; г) освобождение стеков; д) выход.

Сама задача: Из двух стеков с целыми числами создать новый стек из элементов первого стека, которых нет во втором стеке.

Я так понимаю, ошибка в той части, где сравниваются элементы стеков и создается третий(процедура List* combination), помогите пожалуйста. Вот код:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>

struct List
{
    int x;
    List *Next;
};
//-----------------------------------------------------------------------------
List* input(List *MyList1,int &n1)
{
    char name[40];
    printf("\n name dat ");
    scanf("%s",name);
    FILE *tf1=fopen(name,"r");

    int num1;
    fscanf(tf1,"%i",&n1);
    for(int i=0;i<n1;i++)
    {
        fscanf(tf1,"%i",&num1);
        if(i==0)
        MyList1->x=num1;
        else
        {
            List *tmp=new List;
            tmp->x=num1;
            tmp->Next=MyList1;
            MyList1=tmp;
        }

    }
    return MyList1;
}
//-----------------------------------------------------------------------------
void print(List *MyList1, List *MyList2, List *MyList3,int &n1, int &n2,int &n3)
{
    List *temp1=MyList1;
    List *temp2=MyList2;
    List *temp3=MyList3;
    printf("\n Показать стек \n");
    printf(" 1 - стек: \n");
    for(int i=0;i<n1;i++)
    {
        printf(" %i",temp1->x);
        temp1=temp1->Next;
    }
    printf("\n 2 - стек: \n");
    for(int i=0;i<n2;i++)
    {
        printf(" %i",temp2->x);
        temp2=temp2->Next;
    }
    printf("\n 3 - стек: \n");
    for(int i=0;i<n3;i++)
    {
        printf(" %i",temp3->x);
        temp3=temp3->Next;
    }
    printf("\n");
}
//-----------------------------------------------------------------------------
void clear(List *MyList1, List *MyList2, List *MyList3,int &n1, int &n2,int &n3)
{
    MyList1=new List;
    MyList2=new List;
    MyList3=new List;
    n1=0;n2=0;n3=0;
}
//-----------------------------------------------------------------------------
List* combination(List *MyList1, List *MyList2, List *MyList3,int &n1, int &n2,int &n3)

{
    List *temp1=MyList1;
    List *temp2=MyList2;
    List *temp3=MyList3;
    for(int i=0;i<n1;i++)
    {
        bool flag=false;
        temp1->x;
        for(int i=0;i<n1;i++)
        {
            temp2->x;
            if (temp1==temp2)
            {
                flag=true;
            }
        }
        if (flag!=true)
        {
            MyList3->Next=temp1;
            n3+=1;
        }
    }
    return MyList3;
}

//-----------------------------------------------------------------------------
int main()
{
    List *MyList1=new List;
    List *MyList2=new List;
    List *MyList3=new List;
    SetConsoleOutputCP(1251);
    int n1,n2,n3=0;
    char c;
    while (1)
    { //clrscr();
        printf("\n");
        printf("--------------------------------------------------------------\n");
        puts(" A - INPUT");
        puts(" B - SHOW STACKS");
        puts(" C - CREATE NEW STACK");
        puts(" D - CLEAR");
        puts(" E - EXIT");
        c=getch();
        switch(c)
        {
            case 'A':MyList1=input(MyList1,n1);MyList2=input(MyList2,n2);break;
            case 'B':print(MyList1, MyList2 ,MyList3,n1,n2,n3);break;
            case 'C':MyList3=combination(MyList1, MyList2 ,MyList3,n1,n2,n3);break;
            case 'D':clear(MyList1, MyList2 ,MyList3,n1,n2,n3);break;
            case 'E':return 0;
            default : puts(" Ошибка!");
        }
    }
    return 0;
}
1
  • есть еще такой вариант проблемного куска.. результат близок к нужному, но все же что-то не так List* combination(List *MyList1, List *MyList2, List *MyList3,int &n1, int &n2,int &n3) { List *temp1=MyList1; List *temp2=MyList2; List *temp3=MyList3; for(int i=0;i<n1;i++) { bool flag=false; temp1->x; for(int i=0;i<n1;i++) { temp2->x; if (temp1==temp2) { flag=true; } } if (flag!=true) { temp1=MyList1; MyList1=MyList1-> Next; temp1->Next=MyList3; MyList3=temp1; n3+=1; } } return MyList3; }
    – maxkosh
    21 мая 2014 в 20:38

1 ответ 1

1

Ой, а зачем так много кода? Если вы считаете, что проблема в combination, то включите в пример только combination плюс хардкодированные тестовые данные, воспроизводящие проблему.

Код наподобие printf("--------------------------------------------------------------\n"); точно не обязателен для понимания проблемы.

И ещё: вас не затруднит писать лесенкой?


Кстати: http://www.sscce.org/

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.