Code:
#include <stdio.h>
#include <string.h>
#define CIRCLE "Circle"
#define SQUARE "Square"
#define RECTANGLE "Rectangle"
#define TRIANGLE "Triangle"
void main()
{
const double PI = 3.15;
char shapeName[100];
float x, y, h, ls, rs;
FILE *inFile = fopen("input1.txt", "r");
FILE *outFile = fopen("output1.txt", "w");
while (fscanf(inFile, "%s", shapeName) != EOF)
{
if ( 0 == strcmp(shapeName, SQUARE) )
{
fscanf(inFile, "%f", &x);
printf("%s: Side=%6.2f Perimeter=%6.2f Area=%6.2f\n"
, shapeName, x ,x * 4, x*x );
fprintf(outFile,
"%s: Side=%6.2f Perimeter=%6.2f Area=%6.2f\n"
, shapeName, x ,x * 4, x*x );
}
else if ( 0 == strcmp(shapeName, RECTANGLE) )
{
fscanf(inFile, "%f%f", &x, &y);
printf ("%s: Width=%6.2f Height=%6.2f Perimeter=%6.2f Area=%6.2f\n"
, shapeName, x , y, (x+y)*2, x*y );
fprintf (outFile,
"%s: Width=%6.2f Height=%6.2f Perimeter=%6.2f Area=%6.2f\n"
, shapeName, x , y, (x+y)*2, x*y );
}
else if ( 0 == strcmp(shapeName, CIRCLE))
{
fscanf(inFile, "%f", &x);
printf("%s: radius=%6.2f circumference=%6.2f Area=%6.2f\n"
, shapeName, x ,2 * PI * x, 2 * PI * x * x );
fprintf(outFile, "%s: radius=%6.2f circumference=%6.2f Area=%6.2f\n"
, shapeName, x ,2 * PI * x, 2 * PI * x * x );
}
else if ( 0 == strcmp(shapeName, TRIANGLE))
{
fscanf(inFile, "%f %f %f %f", &x, &h, &ls, &rs);
printf("%s: perimeter=%6.2f area=%6.2f\n"
, shapeName, x + ls + rs, (x*h)/2 );
fprintf(outFile, "%s: perimeter=%6.2f area=%6.2f\n"
, shapeName, x + ls + rs, (x*h)/2 );
}
else
{
printf("%s", shapeName);
fgets(shapeName, 99, inFile);
printf("%s\n", shapeName);
}
}
fclose(inFile);
fclose(outFile);
}