Satck Input/Output files

Create a program that contains the following functions:
-create two dynamic stacks with integer data contained in an external file.
-Sort items in stacks by the selection sort method
-Record outputs in an external file.
Main function main ()-menu selection functions and check the status of the data

How to write input and output function for the stack?

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
#include "stdlib.h"
#include "memory.h"
#include "stdio.h"
#include "fstream"
#include "iostream"
#include "iomanip"
#include "string"

typedef struct elem { int key; elem *next; } *pelem;
typedef struct stack { pelem start; } *pstack;

void push(pstack s, int n) {
	pelem p = s->start;
	s->start = (pelem)malloc(sizeof elem);
	s->start->key = n;
	s->start->next = p;
}

int pop(pstack s) {
	int n = -1;
	pelem p = s->start;
	if (p != NULL) {
		n = p->key;
		s->start = p->next;
		free(p);
	}
	return n;
}

int is_empty(pstack s) {
	return s->start == NULL;
}

pstack create_stack() {
	pstack t = (pstack)malloc(sizeof stack);
	memset(t, 0, sizeof stack);
	return t;
}

void selection_sort(pstack s) {
	pstack ret = create_stack();
	pstack temp = create_stack();
	while (!is_empty(s)) {
		int current = pop(s);
		while (!is_empty(s)) {
			int n = pop(s);
			if (n < current) {
				int swap = current;
				current = n;
				n = swap;
			}
			push(temp, n);
		}
		push(ret, current);
		s->start = temp->start;
		temp->start = NULL;
	}
	s->start = ret->start;
}

void file_input(){???}

void file_output(){???}

void display_file(int type){
	int n;
	std::string name;           
	switch (type){               
	case 1:
		name = "inputf.txt";
		break;
	case 2:
		name = "outputf.txt";
		break;
	}
	std::ifstream file;
	file.open(name.c_str(), std::ios::in);
	if (file){               
		int i(1);          
		std::cout << "Conten of file " << name << " is:" << std::endl;
		std::cout << std::setw(8) << "Element";
		std::cout << std::setw(14) << "Value";
		while (file >> n){          
			std::cout << std::endl << std::setw(5) << i;   
			std::cout << std::setw(14) << n;         
			i++;      
		}
		file.close();      
		if (i == 1)std::cout << std::endl << "Failat e prazen." << std::endl;
	}
	else std::cout << std::endl << "Greshka pri otvarianeto na faila!";
}

int main(){
	/* MENU */
	std::cout << "MENU" << std::endl;
	std::cout << "[1] - Input data infile ( inputf.txt )." << std::endl;
	std::cout << "[2] - Sort " << std::endl;
	std::cout << "[3] - Save file ( outputf.txt )." << std::endl;
	std::cout << "[4] - Print input.txt" << std::endl;
	std::cout << "[5] - Print output.txt" << std::endl;
    std::cout << "[0] - Izhod ot programata." << std::endl;
	char chc;
	while (true){
		std::cout << "Choose operation -> ";
		std::cin >> chc;
		switch (chc)
		{
		case '0':
		{
					exit(0);
					break;
		}
		case '1':
		{
					file_input();
					break;
		}
		case '2':
		{          pstack s1 = create_stack();
					selection_sort(s1);
					while (!is_empty(s1))
						printf("%d ", pop(s1));
		}

		case '3':
		{
					file_output();
					break;
		}

		case '4':
		{
					display_file(1);
					break;
		}

		case '5':
				   display_file(2);
				    break;

		default: std::cout << "Invalid choice!!!" << std::endl;
		}
		std::cout << std::endl;
	}
	return 1;
}
Topic archived. No new replies allowed.