标签:
(一)记录表
(二)标准表
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define MAX 100
#define TIME_LEN 10
#define temperature_LEN 5
#define humidity_LEN 5
#define CHECK_STAND 2.0
double check_failtem[30];
char check_failtime[20][20];
struct TXT
{
char time[TIME_LEN];
char temperature[temperature_LEN];
char humidity[humidity_LEN];
}txt[30],check[30];
void print()
{
printf("++++++++++++++++++++++\n");
printf("1->读取记录表的数据\n");
printf("2->读取标准表的数据\n");
printf("3->求记录表温度的最大值\n");
printf("4->求记录表温度的平均值\n");
printf("5->检查某个时间下温度是否合格\n");
printf("++++++++++++++++++++++\n");
printf("\n请输入序号:");
}
void FileRead_Write()//读取记录表的数据
{
FILE *pfread = fopen("C:/Users/SAMSUNG/Documents/Visual Studio 2013/Projects/txtchuanganqi2015_6_24/Debug/sulijuan.txt", "rb+");
if (pfread == NULL)
{
perror("error");
exit(EXIT_FAILURE);
}
printf("时间 温度 湿度\n");
for (int i = 0; i < 5; i++)
{
fgets(txt[i].time, 5, pfread);
fseek (pfread, 1, SEEK_CUR);
fgets(txt[i].temperature, 5, pfread);
fseek(pfread, 1, SEEK_CUR);
fgets(txt[i].humidity, 6, pfread);
fseek(pfread, 1, SEEK_CUR);
printf("%s %s %s\n", txt[i].time, txt[i].temperature, txt[i].humidity);
}
printf("\n");
fclose(pfread);
}
void FileRead_Stadard()//读取标准表的数据
{
FILE *fcheck = fopen("C:/Users/SAMSUNG/Documents/Visual Studio 2013/Projects/txtchuanganqi2015_6_24/Debug/standardtemperature.txt", "rb+");
printf("时间 温度 湿度\n");
for (int i = 0; i < 5; i++)
{
fgets(check[i].time, 5, fcheck);
fseek(fcheck, 1, SEEK_CUR);
fgets(check[i].temperature, 5, fcheck);
fseek(fcheck, 1, SEEK_CUR);
fgets(check[i].humidity, 6, fcheck);
fseek(fcheck, 1, SEEK_CUR);
printf("%s %s %s\n", check[i].time, check[i].temperature, check[i].humidity);
}
printf("\n");
fclose(fcheck);
}
void temperature_max(struct TXT txt[])//求记录表温度的最大值
{
int i = 0;
double n,max = 0.0;
max = atof(txt[0].temperature);
for (i = 1; i < 5; i++)
{
n = atof(txt[i].temperature);
if (n>max)
{
max = n;
}
}
printf("temperature_max = %f\n",max);
printf("\n");
}
void temperature_avg(struct TXT txt[])//求记录表温度的平均值
{
double m,sum = 0.0;
int i;
sum = atof(txt[0].temperature);
for (i = 1; i <5; i++)
{
m = atof(txt[i].temperature);
sum += m;
}
printf("temperature_avg = %f\n",sum/5);
printf("%f", fabs(atof(check[3].temperature) - atof(txt[3].temperature)));
printf("\n");
}
void Check_temperature(struct TXT txt[], struct TXT check[])//检查温度是否合格
{
int i = 0,j = 0;
for (i = 0; i<5;i++)
{
if (strcmp(txt[i].time, check[i].time) == 0)
{
if ((fabs(atof(check[i].temperature) - atof(txt[i].temperature))) > CHECK_STAND)
{
strcpy(check_failtime[i],txt[i].time);
check_failtem[i] = atof(txt[i].temperature);
printf("%s ", check_failtime[i]);
printf("%f\n", check_failtem[i]);
//fwrite(&(txt[i].temperature), sizeof(TXT), 1, pf);
}
}
}
}
int main()
{
int input;
while (1)
{
print();
scanf("%d", &input);
switch (input)
{
case 1:
FileRead_Write();
break;
case 2:
FileRead_Stadard();
break;
case 3:
temperature_max(txt);
break;
case 4:
temperature_avg(txt);
break;
case 5:
Check_temperature(txt, check);
break;
default:
printf("序号无效\n");
break;
}
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/sulijuan66/article/details/46685547