zoukankan      html  css  js  c++  java
  • 依据波形的转折点文件,转换成波形文件

    注:输入的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
    #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 != ' ')
    {
    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 ", Point->Coor.x_Index, Point->Percent, Point->PosFlag);
    printf("%s", WriteBuf);
    printf("the strlen(Writebuf) is %d ", 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;

    }




  • 相关阅读:
    Something about the "BSTR" and "SysStringLen"
    关于 i = i ++ 的问题
    duilib写个三国杀?
    关于WM_GETTEXT的应用
    hoops暂时用过的一些方法
    Hoops随便记的
    C++ win32线程数上限
    windows系统时间(SYSTEMTIME)
    Form表单提交的那些事
    多行文字溢出...
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4471028.html
Copyright © 2011-2022 走看看