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;

    }




  • 相关阅读:
    python-裴波那契数列
    python-装饰器
    登录权限,认证
    sevlet面试题总结
    ssm整合
    配置文件sshmvc
    利用反射来实现动态代理
    springmvc spring hibernate整合
    中期项目总结
    web servlet 网址http://www.cnblogs.com/mengdd/tag/Servlet/
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4471028.html
Copyright © 2011-2022 走看看