当前位置: 首页 > news >正文

依据波形的转折点文件,转换成波形文件

注:输入的input.csv文件末尾必须有换行符结束,最后一行的index必须是200,不然程序无法正常执行。

相应的測试文件在博客的最后


#include "stdafx.h"

#include <string.h>
#include <math.h>
#include <stdlib.h>

#define PEAK_VOLT 33.8
#define WAVE_END_FLAG 5
#define WAVE_MIDDLE_FLAG 0 
#define MAX_INDEX 200

typedef unsigned char BYTE;

typedef struct tagPointCoor{
int x_Index;
double y_Volt;
}PointCoor;


typedef struct tagPointInfo{
PointCoor Coor;
double Percent;
int PosFlag;
}PointInfo;


char WriteFilePath[] = "c:\\my_path\\output.csv";
char ReadFilePath[] = "c:\\my_path\\input.csv";

int WritePointInfo(FILE *fp, PointInfo *Point);
int WriteLine(FILE *fp, const PointCoor *StartPoint, const PointCoor *EndPoint);
int GetPosFlag(const PointCoor *Point);
void WriteWave(FILE *Input, FILE *Output);
void GetNextPoint(FILE *Input, PointCoor *Point);
void WritePoint(FILE *fp, PointCoor *Point);

int _tmain(int argc, _TCHAR* argv[])
{
FILE *Output = NULL;
FILE *Input = NULL;

//PointInfo RandomPoint;
//PointCoor StartPoint;
//PointCoor EndPoint;
//PointCoor Point;
//
//StartPoint.x_Index = 1;
//StartPoint.y_Volt = 33;
//EndPoint.x_Index = 100;
//EndPoint.y_Volt = 33;


//RandomPoint.Coor.x_Index = 2;
//RandomPoint.Coor.y_Volt = 20;
//RandomPoint.Percent = 0.4;
//RandomPoint.PosFlag = 5;

Input = fopen(ReadFilePath, "r");
Output = fopen(WriteFilePath, "w");

if((Input == NULL)||(Output == NULL))
{
return -1;
}

WriteWave(Input, Output);

system("pause");
}

void GetNextPoint(FILE *Input, PointCoor *Point)
{
char ch;
char StrBuf[10] = {0};
int Cnt = 0;
int CommaPos = 0;


ch = fgetc(Input);
while(ch != '\n')
{
switch(CommaPos)
{
case 0: //index
{
if(ch == ',')
{
Point->x_Index = atoi(StrBuf);
memset(StrBuf, 0, 10);
CommaPos++;
Cnt = 0;
break;
}
StrBuf[Cnt++] = ch;
}
break;
case 1: //voltage
{
StrBuf[Cnt++] = ch;
}
break;
default:
break;
}
ch = fgetc(Input);
}
Point->y_Volt = atof(StrBuf);
}

void WriteWave(FILE *Input, FILE *Output)
{
PointCoor StartPoint;
PointCoor NextPoint;


int PosFlag;

GetNextPoint(Input, &StartPoint);

do{
GetNextPoint(Input, &NextPoint);
WriteLine(Output, &StartPoint, &NextPoint);
StartPoint = NextPoint;

if((PosFlag = GetPosFlag(&NextPoint)) == WAVE_END_FLAG)
{
WritePoint(Output, &NextPoint);
}
}while(PosFlag != WAVE_END_FLAG);
}


void WritePoint(FILE *fp, PointCoor *Point)
{
PointInfo RandomPoint;


RandomPoint.Coor.x_Index = Point->x_Index;
RandomPoint.Coor.y_Volt = Point->y_Volt;
RandomPoint.Percent = RandomPoint.Coor.y_Volt / PEAK_VOLT;
RandomPoint.PosFlag = GetPosFlag(Point);

WritePointInfo(fp, &RandomPoint);
}


int WritePointInfo(FILE *fp, PointInfo *Point)
{
char WriteBuf[20] = {0};


if(fp == NULL)
return -1;
sprintf(WriteBuf, "%d,%.2lf,%d\n", Point->Coor.x_Index, Point->Percent, Point->PosFlag);
printf("%s", WriteBuf);
printf("the strlen(Writebuf) is %d \n ", strlen(WriteBuf));


fwrite(WriteBuf, strlen(WriteBuf), 1, fp);
return 0;
}


int GetPosFlag(const PointCoor *Point)
{
int PosFlag;


if(Point->x_Index >= MAX_INDEX)
{
PosFlag = WAVE_END_FLAG;
}
else
{
PosFlag = WAVE_MIDDLE_FLAG;
}
return PosFlag;
}

int WriteLine(FILE *fp, const PointCoor *StartPoint, const PointCoor *EndPoint)
{
PointInfo RandomPoint;
double Slope;


if(fp == NULL)
{
return -1;
}


RandomPoint.Coor.x_Index = StartPoint->x_Index;
Slope = (EndPoint->y_Volt - StartPoint->y_Volt)/(EndPoint->x_Index - StartPoint->x_Index);


while(RandomPoint.Coor.x_Index < EndPoint->x_Index)
{
RandomPoint.Coor.y_Volt = Slope*(RandomPoint.Coor.x_Index - StartPoint->x_Index) + StartPoint->y_Volt;
RandomPoint.Percent = RandomPoint.Coor.y_Volt / PEAK_VOLT;
RandomPoint.PosFlag = GetPosFlag(&RandomPoint.Coor);
WritePointInfo(fp, &RandomPoint);

RandomPoint.Coor.x_Index++;
}
return 0;

}




相关文章:

  • springMvc 入门学习(自动生成 springmvc 单表 两关联表 生成 及显示)
  • HealthKit开发教程之HealthKit的主要类型数据
  • java.io.FileNotFoundException: /exapp/hadoop/name/current/VERSION (Permission denied)
  • 北漂到底要不要回老家发展?
  • openwrt开源系统LUCI配置界面
  • Apache benchmark对网站进行压力测试
  • mybatis generator生成代码工具的使用
  • SharePoint 2016 的新特性概览(二)(What's New for IT Professionals in SharePoint Server 2016)...
  • 后缀数组 --- HDU 3518 Boring counting
  • C++语言基础 例程 基类与派生类的转换
  • CDA数据分析师认证考试模拟题库
  • CDH使用之CM 5.3.x安装
  • STM32 ~ 如何从ST网站找到对应的固件库
  • [裴礼文数学分析中的典型问题与方法习题参考解答]5.1.12
  • [裴礼文数学分析中的典型问题与方法习题参考解答]5.1.16
  • [nginx文档翻译系列] 控制nginx
  • 【划重点】MySQL技术内幕:InnoDB存储引擎
  • Django 博客开发教程 16 - 统计文章阅读量
  • express.js的介绍及使用
  • Golang-长连接-状态推送
  • Hibernate最全面试题
  • JSDuck 与 AngularJS 融合技巧
  • js算法-归并排序(merge_sort)
  • JS题目及答案整理
  • Mithril.js 入门介绍
  • NLPIR语义挖掘平台推动行业大数据应用服务
  • node入门
  • Python 反序列化安全问题(二)
  • SegmentFault 2015 Top Rank
  • 分布式任务队列Celery
  • 欢迎参加第二届中国游戏开发者大会
  • 简析gRPC client 连接管理
  • 使用Gradle第一次构建Java程序
  • 携程小程序初体验
  • LevelDB 入门 —— 全面了解 LevelDB 的功能特性
  • 湖北分布式智能数据采集方法有哪些?
  • ​创新驱动,边缘计算领袖:亚马逊云科技海外服务器服务再进化
  • !$boo在php中什么意思,php前戏
  • #大学#套接字
  • (10)工业界推荐系统-小红书推荐场景及内部实践【排序模型的特征】
  • (3)llvm ir转换过程
  • (C++)八皇后问题
  • (附源码)springboot建达集团公司平台 毕业设计 141538
  • (十二)devops持续集成开发——jenkins的全局工具配置之sonar qube环境安装及配置
  • (一)基于IDEA的JAVA基础10
  • (源码版)2024美国大学生数学建模E题财产保险的可持续模型详解思路+具体代码季节性时序预测SARIMA天气预测建模
  • ***利用Ms05002溢出找“肉鸡
  • .md即markdown文件的基本常用编写语法
  • .net core IResultFilter 的 OnResultExecuted和OnResultExecuting的区别
  • .net 调用php,php 调用.net com组件 --
  • .NET/C# 使用 #if 和 Conditional 特性来按条件编译代码的不同原理和适用场景
  • .NET/C# 使用反射注册事件
  • .NET/C# 异常处理:写一个空的 try 块代码,而把重要代码写到 finally 中(Constrained Execution Regions)
  • .NET程序员迈向卓越的必由之路
  • .net和jar包windows服务部署