c数据结构学习随笔
#include <stdio.h> #include <stdlib.h> #include "PublicDS.h" #include<Windows.h> typedef struct stack{ int top = -1; int maxSize = 100; //用于存放二进制位 int *data = (int *)malloc(100 * sizeof(int)); int pop(){ if (!isEmpty()) return data[top--]; } void push(int n){ if (!isFull()) data[++top] = n; } bool isEmpty(){ return top < 0; } bool isFull(){ return top >= maxSize; } } stack; int ReadData(FILE *, int *&); void WriteData(FILE *, int *, int); void main(){ int *data; FILE *fin, *fout; fin = fopen("input.txt", "r"); fout = fopen("output.txt", "w"); MALLOC(data, 100 * sizeof(int), int *); int tmp = ReadData(fin, data); WriteData(fout, data, tmp); FREE(data); fclose(fin); fclose(fout); system("pause"); } int ReadData(FILE *fin, int * &data){ int i = 1; int n = 0; int tmp; while (!feof(fin)){ fscanf(fin, "%d", &tmp); if (tmp != -1){ data[n++] = tmp; // printf("%d\n", tmp); if (n % 100 == 0){ int *p = (int *)realloc(data, 100 * (++i)*sizeof(int)); if (!p){ printf("error"); MessageBox(GetActiveWindow(), L"1", L"2", MB_OK); } else{ data = p; // free(p); // p = NULL; } } } } return n; } void WriteData(FILE *fout, int *data, int n) { for (int i = 0; i < n; i++) { fprintf(fout, "%8d--->", data[i]); stack sc; while (data[i] / 2 != 0) { sc.push(data[i] % 2); data[i] /= 2; } sc.push(data[i]); for (int j = 0; j <= sc.top;){ fprintf(fout, "%d", sc.pop()); } fprintf(fout, "\n"); } // printf("%d\n", n); }