프로그래밍/C++2009. 5. 1. 13:06

input.txt 파일을 읽어와서 output.csv 파일을 만드는 프로그램이다

여기에 추가적으로 input.txt 파일에 보면 각 날짜의 시간별로 온도, 습도, 강수량 등이 기록되어 있다

이를 날짜별으로 평균 온도, 최대 최소 온도, 표준편차 등을 구해서 Reprot.csv 파일로 만드는 프로그램이다

#include 
#include 
#include 
#include 

typedef struct Weather
{
	char day[11];
	char time[6];
	float temperature;
	float humidty;
	float rainfall;
	float insolation;
	float sunshine_duration;
	char windy[5];
	float wind_velocity;
	float max_wind_velocity;
	struct Weather *next;
}Weather;

typedef struct 
{
	Weather* head;
}Weather_h;

Weather_h* CreateNode(void);
void PrintList();
void CreateCSVfile(Weather_h *L, Weather *p);
void CreateReportFile(Weather *p);
float Standard_Deviation(Weather *p, Weather* nextNdoe, float avgTmp, bool end, int count);
void FreeNode(Weather_h *L, Weather *p);

void main()
{
	Weather_h* L;

	L = CreateNode();


	PrintList();

}

Weather_h* CreateNode(void)
{
	Weather_h* L;
	L = (Weather_h *)malloc(sizeof(Weather_h));
	L->head = NULL;
	return L;
}

void PrintList()
{
	Weather* p;
	Weather* newData;
	Weather_h* L;
	L = (Weather_h *)malloc(sizeof(Weather_h));
	L->head = NULL;

	FILE *fp;

	if((fp = fopen("input.txt", "r")) == NULL)
	{
		printf("File is not found!\n");
	}

	p = L->head;

	fseek(fp, 110,SEEK_SET);

	puts("=========================================================================");
	puts("계측시간 온도(℃) 습도(%) 강수량(mm) 일사량(w/m2) 일조시간(hour) 풍향 풍속(m/s) 최대풍속(m/s)");

	while(!feof(fp))
	{
		newData = (Weather *)malloc(sizeof(Weather));

		fscanf(fp, "%s %s %f %f %f %f %f %s %f %f ", newData->day, newData->time, &newData->temperature, &newData->humidty, &newData->rainfall, &newData->insolation, &newData->sunshine_duration, &newData->windy, &newData->wind_velocity, &newData->max_wind_velocity);

		if(L->head == NULL)
		{
			L->head = newData;
			p = newData;
			newData->next = NULL;

		}
		else
		{
			p->next = newData;
			newData->next = NULL;
		}

		while(p->next != NULL)
		{

			p = p->next;
		}
	}

	p = L->head;
	CreateCSVfile(L, p);
	CreateReportFile(p);
	FreeNode(L, p);
	
	puts("\ncvs 파일과 report.csv 파일을 성공적으로 만드셨습니다.");
	puts("=========================================================================");

	fclose(fp);


}



void CreateCSVfile(Weather_h* L, Weather* p)
{
	FILE *fp;
	Weather* newData;
	newData = p;

	if((fp = fopen("output.csv", "w")) == NULL)
	{
		printf("File is not found!\n");
	}

	fprintf(fp, "계측날짜, 시간,온도(℃), 습도(%), 강수량(mm), 일사량(w/m2), 일조시간(hour), 풍향, 풍속(m/s), 최대풍속(m/s) \n");
	while(newData->next != NULL)
	{
		fprintf(fp, "%s,", newData->day);
		fprintf(fp, " %s,", newData->time);
		fprintf(fp, " %.1f,", newData->temperature);
		fprintf(fp, " %.1f,", newData->humidty);
		fprintf(fp, " %.1f,", newData->rainfall);
		fprintf(fp, " %.1f,", newData->insolation);
		fprintf(fp, " %.1f,", newData->sunshine_duration);
		fprintf(fp, " %s,", newData->windy);
		fprintf(fp, " %.1f,", newData->wind_velocity);
		fprintf(fp, " %.1f\n", newData->max_wind_velocity);


		newData = newData->next;
	}

	fclose(fp);
}

void CreateReportFile(Weather* p)
{
	FILE *fp;
	Weather *nextNode, *temp, *startNode;
	float avgTem = 0;
	float sumTem = 0;
	int count=0;
	float Max = -1000;
	float Min = 1000;
	float sumHumidty = 0;
	float avgHumidty = 0;
	float sumRainfall = 0;
	float avgRainfall = 0;
	float sumSunshine_duration = 0;
	float avgSunshine_duration = 0;

	temp = p;
	startNode = p;
	nextNode = p->next;

	if((fp = fopen("Report.csv", "w")) == NULL)
	{
		printf("File is not found!\n");
	}

	fprintf(fp, "[일별기상통계표] 계측 지역:신촌 \n");
	fprintf(fp, "일자, ,기온(Temperature), , , 습도, 강수량, 일조시간\n");
	fprintf(fp, " , 평균, 표준편차, 최고, 최저, [%%], [mm], [hour]\n");


	while(p->next != NULL)
	{
		temp = startNode;

		if(strcmp(p->day, nextNode->day) == 0)
		{
			sumTem = sumTem + p->temperature;
			sumHumidty = sumHumidty + p->humidty;
			sumRainfall = sumRainfall + p->rainfall;
			sumSunshine_duration = sumSunshine_duration + p->sunshine_duration;

			count++;

			if(p->temperature > Max)
				Max = p->temperature;

			if(p->temperature < Min)
				Min = p->temperature;

			p = nextNode;
			nextNode = nextNode->next;


			if(p->next == NULL)
			{
				sumTem = sumTem + p->temperature;
				sumHumidty = sumHumidty + p->humidty;
				sumRainfall = sumRainfall + p->rainfall;
				sumSunshine_duration = sumSunshine_duration + p->sunshine_duration;
				count++;

				avgTem = sumTem / count;
				avgHumidty = sumHumidty / count;
				avgRainfall= sumRainfall / count;
				avgSunshine_duration = sumSunshine_duration / count;

				fprintf(fp, "%s, ", p->day);
				fprintf(fp, "%.1f, " , avgTem );
				fprintf(fp, "%.4f,", Standard_Deviation(temp, temp, avgTem, true, count));
				fprintf(fp, "%.1f, " , Max);
				fprintf(fp, "%.1f, " , Min);
				fprintf(fp, "%.1f, " , avgHumidty);
				fprintf(fp, "%.1f, " , avgRainfall);
				fprintf(fp, "%.1f\n " , avgSunshine_duration);

			}

		}

		else if(strcmp(p->day, nextNode->day) != 0)
		{
			sumTem = sumTem + p->temperature;
			sumHumidty = sumHumidty + p->humidty;
			sumRainfall = sumRainfall + p->rainfall;
			sumSunshine_duration = sumSunshine_duration + p->sunshine_duration;
			count++;

			avgTem = sumTem / count;
			avgHumidty = sumHumidty / count;
			avgRainfall= sumRainfall / count;
			avgSunshine_duration = sumSunshine_duration / count;

			if(p->temperature > Max)
				Max = p->temperature;

			fprintf(fp, "%s, ", p->day);
			fprintf(fp, "%.1f, " , avgTem);
			fprintf(fp, "%.4f,", Standard_Deviation(temp, nextNode, avgTem, false, count));
			fprintf(fp, "%.1f, " , Max);
			fprintf(fp, "%.1f, " , Min);
			fprintf(fp, "%.1f, " , avgHumidty);
			fprintf(fp, "%.1f, " , avgRainfall);
			fprintf(fp, "%.1f\n " , avgSunshine_duration);

			p = nextNode;
			nextNode = nextNode->next;
			startNode = p;
			avgTem = 0;
			sumTem = 0;
			sumRainfall = 0;
			sumHumidty = 0;
			sumSunshine_duration = 0;
			count = 0;
			Max = -1000;
			Min = 1000;

		}


	}



}

float Standard_Deviation(Weather* p, Weather* nextNode, float avgTmp, bool end, int count)
{
	Weather *temp = p;
	Weather *tempNext = nextNode;
	float sd = 0;
	float squaresum = 0;


	if(end == false)
	{
		while(strcmp(temp->day, tempNext->day) != 0)
		{
			squaresum = squaresum + (avgTmp - temp->temperature)*(avgTmp - temp->temperature);
			temp = temp->next;
		}
		squaresum = squaresum + (avgTmp - temp->temperature)*(avgTmp - temp->temperature);
		temp = temp->next;
	}
	else
	{
		while(temp->next != NULL)
		{
			squaresum = squaresum + (avgTmp - temp->temperature)*(avgTmp - temp->temperature);
			temp = temp->next;

		}
		squaresum = squaresum + (avgTmp - temp->temperature)*(avgTmp - temp->temperature);
		temp = temp->next;
	}
	sd = sqrt(squaresum) / count;
	return sd;	

}

void FreeNode(Weather_h *L, Weather *p)
{
	Weather *temp = L->head;


	if(L->head == NULL)
		return ;
	else
	{
		free(L);
		while(temp->next != NULL)
		{
			temp = p->next;
			free(p);
			p = temp;
		}
	}

}

'프로그래밍 > C++' 카테고리의 다른 글

C++ 디버깅시 TRACE 사용하기  (0) 2009.12.10
STL - List 사용하기  (0) 2009.07.16
C/C++ 로깅 Facility  (0) 2009.04.25
Abstract Factory Pattern  (0) 2009.04.22
PVOID  (0) 2009.04.22
Posted by 마블(이환문)