C 检测增加或减少的数据
我得到了一个数据文件,它由一个标题名为temperature的列组成,下面的行只是一系列记录的温度。我可以使用以下命令成功地(也许)将其读入C程序:C 检测增加或减少的数据,c,C,我得到了一个数据文件,它由一个标题名为temperature的列组成,下面的行只是一系列记录的温度。我可以使用以下命令成功地(也许)将其读入C程序: #include <stdio.h> #include <cstdlib> int main() { FILE *fpt; /*define a pointer to predefined structure type FILE*/ fpt = fopen("temperature.dat","r"); ch
#include <stdio.h>
#include <cstdlib>
int main()
{
FILE *fpt; /*define a pointer to predefined structure type FILE*/
fpt = fopen("temperature.dat","r");
char temp[10];
float t[7];
int i;
fscanf(fpt, "%s",temp);
printf("%s",temp);
for(i=0;i<7;++i)
{
fscanf(fpt, "%f",&t[i]);
printf("%.2f",t[i]);
}
printf("%f",t[3]); /*just testing whether the program is reading correctly*/
fclose(fpt);
system("pause");
}
#包括
#包括
int main()
{
FILE*fpt;/*定义指向预定义结构类型文件的指针*/
fpt=fopen(“温度数据”,“r”);
煤焦温度[10];
浮动t[7];
int i;
fscanf(fpt,“%s”,温度);
printf(“%s”,温度);
对于(i=0;i添加一个变量(例如,inctemp
)来计算一行中看到的增加,如果有增加,则在循环中增加。如果没有增加,则将其重置为0。在循环结束时,您知道一行中有多少个(至少在数据集结束时)
修改为任意读取次数
不需要使用额外的循环,只需执行以下操作即可
totalInc = 0;
for(i=0;i<7;++i) {
fscanf(fpt, "%f",&t[i]);
printf("%.2f",t[i]);
if (i > 0) {
if (t[i] > t[i-1]) totalInc += 1;
else totalInc -= 1;
}
}
totalInc=0;
对于(i=0;i=0){
如果(t[i]>t[i-1])totalInc+=1;
else totalInc-=1;
}
}
totalInc
将告诉您当前值大于上一个值的次数。对于您的情况,您可以只检查totalInc==6
,但实际上,您可以只检查任意数量的增量。正数表示总体增量趋势,负数表示总体增量趋势总体下降趋势。找到温度之间的差值将有助于您
#include <stdio.h>
#include <cstdlib>
int main()
{
FILE *fpt; /*define a pointer to predefined structure type FILE*/
fpt = fopen("temperature.dat","r");
char temp[10];
float t[7];
int i, loweringdelta;
fscanf(fpt, "%s",temp);
printf("%s",temp);
loweringdelta = 1;
for (i=0; i<7; ++i)
{
fscanf(fpt, "%f", &t[i]);
printf("%.2f", t[i]);
if (i > 0 && (t[i]-t[i-1]<= 0))
{
loweringdelta = t[i]-t[i-1];
}
}
if (loweringdelta > 0)
{
// Your error message here
}
printf("%f", t[3]); /*just testing whether the program is reading correctly*/
fclose(fpt);
system("pause");
}
#包括
#包括
int main()
{
FILE*fpt;/*定义指向预定义结构类型文件的指针*/
fpt=fopen(“温度数据”,“r”);
煤焦温度[10];
浮动t[7];
int i,下降三角洲;
fscanf(fpt,“%s”,温度);
printf(“%s”,温度);
loweringdelta=1;
对于(i=0;i0&(t[i]-t[i-1]0)
{
//您的错误信息在这里
}
printf(“%f”,t[3]);/*只是测试程序是否正确读取*/
fclose(fpt);
系统(“暂停”);
}
要检测浮点文件在一行中是否至少有6个递增值,可以执行以下操作:
#include <stdio.h>
#define IN_A_ROW 6
int main() {
FILE *f = fopen("temps.txt", "r");
float x, last_x;
int inc = 0;
fscanf(f, "%f", &last_x);
while (fscanf(f, "%f", &x) == 1) {
if (x > last_x) { // or maybe >=
if (++inc >= IN_A_ROW) {
printf("Found %d increases in a row\n", IN_A_ROW);
return -1;
}
}else
inc = 0;
last_x = x;
}
fclose(f);
return 0;
}
#包括
#在第6行定义
int main(){
文件*f=fopen(“temps.txt”、“r”);
浮动x,最后的x;
int inc=0;
fscanf(f、%f、&last_x);
而(fscanf(f,%f,&x)==1){
如果(x>last_x){//或者可能>=
如果(++inc>=在行中){
printf(“发现一行增加了%d”,在一行中);
返回-1;
}
}否则
inc=0;
最后的x=x;
}
fclose(f);
返回0;
}
您将需要某种计数器来查看温度升高的次数。此外,请在while循环中读取该文件:
#include <stdio.h>
int main()
{
FILE *fpt; /*define a pointer to predefined structure type FILE*/
fpt = fopen("temperature.dat","r");
char temp[10];
int count = 0;
int i;
float prev_temp = -999.00;
float current_temp;
int threshold = 6;
fscanf(fpt, "%s",temp); // header?
printf("Header: %s\n",temp);
while(!feof(fpt)) {
fscanf(fpt, "%f", ¤t_temp);
if (current_temp > prev_temp) count++;
else count = 0;
prev_temp = current_temp;
if (count > threshold) printf("Saw %d consecutive increases\n", count);
}
fclose(fpt);
}
#包括
int main()
{
FILE*fpt;/*定义指向预定义结构类型文件的指针*/
fpt=fopen(“温度数据”,“r”);
煤焦温度[10];
整数计数=0;
int i;
浮动前温度=-999.00;
浮动电流温度;
int阈值=6;
fscanf(fpt,“%s”,temp);//头?
printf(“标题:%s\n”,临时);
而(!feof(fpt)){
fscanf(fpt、%f、&当前温度);
如果(当前温度>上一个温度)计数++;
else计数=0;
上一个温度=当前温度;
如果(计数>阈值)printf(“看到%d个连续增加,\n”,计数);
}
fclose(fpt);
}
因此,如果在一行中发现6个递增值,您需要读取未知数量的值并打印错误消息,对吗?是的,先生,操作说明是“假设数据的总输入数量不固定”。实际上,根本不需要数组。我同意。我可能会使用curtemp和prevtemp变量并一直读取,直到输入用尽。但是,我在提供的示例代码的上下文中提供了一个示例。@JohnH那么,这是另一种读取所有数据而不提及的方法,就像我编辑它以执行任意数量的读取一样。我没有为您编写整个代码,只是对内部循环进行了修改。嗯,我可以知道为什么您必须在编码中输入return-1和return 0吗?程序返回0表示成功,非零表示失败(-1是一个好选择,宏退出\u failure
from
是一个更好的选择)。
#include <stdio.h>
int main()
{
FILE *fpt; /*define a pointer to predefined structure type FILE*/
fpt = fopen("temperature.dat","r");
char temp[10];
int count = 0;
int i;
float prev_temp = -999.00;
float current_temp;
int threshold = 6;
fscanf(fpt, "%s",temp); // header?
printf("Header: %s\n",temp);
while(!feof(fpt)) {
fscanf(fpt, "%f", ¤t_temp);
if (current_temp > prev_temp) count++;
else count = 0;
prev_temp = current_temp;
if (count > threshold) printf("Saw %d consecutive increases\n", count);
}
fclose(fpt);
}