#include <stdio.h>
#include <stdlib.h>
int _Read(char *path, int *m, int *b)
{
FILE *fp = fopen(path, "r");
if (fp==NULL)
{
printf("Cannot open the file!!");
return 0;
}
if (fscanf(fp,"m=%d b=%d",m,b) != 2)
{
fclose(fp);
printf("Cannot read the data!!");
return 0;
}
fclose(fp);
return 1;
}
int _BitTest(unsigned int *a, int n, int bit)
{
return (a[n]>>bit)&1;
}
void _BitSet(unsigned int *a, int n, int bit)
{
a[n] |= 1<<bit;
}
void _Coordinates(unsigned int *c, int m, int b)
{
int y, x;
for (x=0; x<20; ++x)
{
y = m*x+b;
if (y>=0 && y<20)
_BitSet(c,y,(19-x));
}
}
int _Write(char *path, unsigned int *c, int m, int b)
{
FILE *fp = fopen(path,"w");
if (fp==NULL)
{
printf("Cannot open the file!!");
return 0;
}
fprintf(fp,"filename:%s\n\n", path);
fprintf(fp,"Linear equation:y=mx+b,which m=%d,b=%d,Output1\n",m,b);
fprintf(fp,"-------------------------------------------------\n\n");
fprintf(fp,"The coordinates of the linear equation :");
int i, j;
for(i=19; i>=0; --i)
{
j = m*i+b;
if (j>=0 && j<20)
fprintf(fp, "(%d,%d)", i, j);
}
fprintf(fp, "\n\n\n\n");
for (i=19; i>=0; --i)
{
for (j=19; j>=0; --j)
fprintf(fp, "%c", _BitTest(c,i,j)?'*':'_');
fprintf(fp, "\n");
}
fclose(fp);
return 1;
}
int _test(char *inPath, char *outPath)
{
int m, b;
if (!_Read(inPath, &m, &b))
return 0;
unsigned int c[20]= {0};
_Coordinates(c, m, b);
_Write(outPath, c, m, b);
return 1;
}
int main(void)
{
_test("Input1.txt", "Output1.txt");
_test("Input2.txt", "Output2.txt");
_test("Input3.txt", "Output3.txt");
printf("The data is written to the end!!\n");
system("PAUSE");
return 0;
}