// VC6.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "windows.h"
#include "conio.h"
int Menu()
{
printf("\nPress button to continue:\n");
printf("1 - Input new array\n");
printf("2 - Find Y by X\n");
printf("3 - Find all X by Y\n");
printf("4 - Load Pairs from file\n");
printf("5 - Save Pairs to file\n");
printf("6 - Print array here\n");
printf("any key - Exit program\n");
printf("\n");
return getch();
}
class MyClass{
private:
int n;
int x[100], y[100];
char st[200];
public:
MyClass() {n = 0;};
void Input();
void FindYByX();
void FindXByY();
void LoadFromFile();
void SaveToFile();
void Print();
};
void MyClass::Input()
{
printf("\nInput count of pairs: ");
scanf("%d", &n);
for (int i=0; i<n; i++)
{
printf("Pair %d X: ", i+1);
scanf("%d", &(x[i]));
printf("Pair %d Y: ", i+1);
scanf("%d", &(y[i]));
printf("\n");
}
}
void MyClass::FindYByX()
{
int xx;
printf("Input X: ");
scanf("%d", &xx);
for (int i=0; i<n; i++)
if (x[i] == xx)
printf("(%d,%d)", x[i], y[i]);
st[0] = 0;
printf("\nType filename to save (input 'q' to skip):\n");
scanf("%s", st);
if ((st[0] != 'q') || (st[1] != 0))
{
FILE *f = fopen(st,"w");
fprintf(f, "%d\n", n);
for (i=0; i<n; i++)
if (x[i] == xx)
fprintf(f, "(%d,%d)", x[i], y[i]);
fclose(f);
}
}
void MyClass::FindXByY()
{
int yy;
printf("Input Y: ");
scanf("%d", &yy);
for (int i=0; i<n; i++)
if (y[i] == yy)
printf("(%d,%d)\n", x[i], y[i]);
st[0] = 0;
printf("\nType filename to save (input 'q' to skip):\n");
scanf("%s", st);
if ((st[0] != 'q') || (st[1] != 0))
{
FILE *f = fopen(st,"w");
fprintf(f, "%d\n", n);
for (i=0; i<n; i++)
if (y[i] == yy)
fprintf(f, "(%d,%d)\n", x[i], y[i]);
fclose(f);
}
}
void MyClass::LoadFromFile()
{
st[0] = 0;
printf("\nType filename to load array from (input 'q' to skip):\n");
scanf("%s", st);
if ((st[0] != 'q') || (st[1] != 0))
{
FILE *f = fopen(st,"r");
fscanf(f, "%d\n", &n);
for (int i=0; i<n; i++)
fscanf(f, "(%d,%d)\n", &(x[i]), &(y[i]));
fclose(f);
}
}
void MyClass::SaveToFile()
{
st[0] = 0;
printf("\nType filename to save array to (input 'q' to skip):\n");
scanf("%s", st);
if ((st[0] != 'q') || (st[1] != 0))
{
FILE *f = fopen(st,"w");
fprintf(f, "%d\n", n);
for (int i=0; i<n; i++)
fprintf(f, "(%d,%d)\n", x[i], y[i]);
fclose(f);
}
}
void MyClass::Print()
{
printf("\nPrinting array\n%d\n", n);
for (int i=0; i<n; i++)
printf("(%d,%d)\n", x[i], y[i]);
printf("-----------------\n");
getch();
}
int main(int argc, char* argv[])
{
MyClass Sample;
ShowMenu:
switch (Menu())
{
case '1':
Sample.Input();
goto ShowMenu;
case '2':
Sample.FindYByX();
goto ShowMenu;
case '3':
Sample.FindXByY();
goto ShowMenu;
case '4':
Sample.LoadFromFile();
goto ShowMenu;
case '5':
Sample.SaveToFile();
goto ShowMenu;
case '6':
Sample.Print();
goto ShowMenu;
}
printf("\n\n\nDone.\nPress any key to close window...");
getch();
return 0;
}