| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 249 人关注过本帖
标题:结构体动态内存分配问题
只看楼主 加入收藏
荆棘之心
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2015-3-23
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
结构体动态内存分配问题
代码及错误如下,,求大神指点一二,感激不尽,,楼主不是特别深入,很多东西理解不透彻,,主要是红字部分,,

1,输入N,f,fs;
2,sig(i)=exp(j*2*PI*f/fs*i)=cos(-x)+sin(-x)*j;
3,s(w)=FFT(sig)=∑(i=0~N-1)[exp(-j*2PI*i*w/N)*sig(i)];
4,求取模|s(w)|  w=(0~N-1);
5,求取|s(w)|的最大值及相应坐标index;
6,fest=index/N*fs;

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#define PI 3.1415926
struct complex
{
    double Real;
    double Imag;
};
struct complex Plus(struct complex, struct complex);
struct complex Multi(struct complex, struct complex);
double Modulo(struct complex);
void sig(struct complex, int, double, double);
void FFT(struct complex, int, struct complex);
void Msw(struct complex, int, double);
double Max(double, int, int);

int main()
{
    int N,index=0;
    double f, fs,Smax,fest;

    printf("请输入需要测试的N,f,fs的值\n");
    scanf_s("%d,%1f,%1f", &N, &f, &fs);

    struct complex *p;
    p = (struct complex*)malloc(512*sizeof(struct complex));
    struct complex *p1;
    p1 = (struct complex*)malloc(512*sizeof(struct complex));
    double *pm;
    pm = (double*)malloc(512*sizeof(double));

    sig(p,N,f,fs);
    FFT(p,N,p1);
    Msw(p1,N,pm);
    Smax=Max(pm,N,index);

    fest = index / N*fs;
    printf("|sw|的最大值是%f\n与之对应的坐标是%d\n与之对应的fest是%f\n",Smax,index,fest);

    return 0;
}

void sig(struct complex *p, int N, double f, double fs)
{
    int i;
    for (i = 0; i < N; i++)
        *(p + i) = { cos(2 * PI*f*i / fs), sin(2 * PI*f*i / fs) };
}
void FFT(struct complex *p, int N, struct complex *p1)
{
    int i, w;
    struct complex *p2;
    p2 = (struct complex*)calloc(512 , sizeof(struct complex));
    for (w = 0; w < N; w++)
    {
        for (i = 0; i <= N; i++)
        {
            *(p2 + i) = { cos(-2 * PI*w*i / N), sin(2 * PI*w*i / N) };
            *(p1 + w) = Plus(Multi(*(p2 + i), *(p + i)), *(p1 + w));
        }
    }
}
void Msw(struct complex *p1, int N, double *pm)
{
    int i;
    for (i = 0; i < N; i++)
    {
        *(pm + i) = Modulo(*(p1 + i));
    }

}
double Max(double *pm, int N, int index)
{
    int i;
    double Smax = *pm;

    for (i = 1; i < N; i++)
    {
        if (*(pm + i)>Smax) Smax = *(pm + i);
        index = i;
    }
    return Smax;
}

struct complex Plus(struct complex a, struct complex b)
{
    struct complex answer;
    answer.Real = a.Real + b.Real;
    answer.Imag = a.Imag + b.Imag;
    return answer;
}
struct complex Multi(struct complex a, struct complex b)
{
    struct complex answer;
    answer.Real = a.Real * b.Real - a.Imag * b.Imag;
    answer.Imag = a.Imag * b.Real + b.Imag * a.Real;
    return answer;
}
double Modulo(struct complex a)
{
    double m;
    m = sqrt(a.Real*a.Real + a.Imag*a.Imag);
    return m;
}

1>c:\users\kylin\source\repos\c语言学习\discrete fourier transform\source.cpp(38): error C2664: “void sig(complex,int,double,double)”: 无法将参数 1 从“complex *”转换为“complex”
1>          无构造函数可以接受源类型,或构造函数重载决策不明确
1>c:\users\kylin\source\repos\c语言学习\discrete fourier transform\source.cpp(40): error C2664: “void FFT(complex,int,complex)”: 无法将参数 1 从“complex *”转换为“complex”
1>          无构造函数可以接受源类型,或构造函数重载决策不明确
1>c:\users\kylin\source\repos\c语言学习\discrete fourier transform\source.cpp(42): error C2664: “void Msw(complex,int,double)”: 无法将参数 1 从“complex *”转换为“complex”
1>          无构造函数可以接受源类型,或构造函数重载决策不明确
1>c:\users\kylin\source\repos\c语言学习\discrete fourier transform\source.cpp(44): error C2664: “double Max(double,int,int)”: 无法将参数 1 从“double *”转换为“double”
1>          没有使该转换得以执行的上下文

[ 本帖最后由 荆棘之心 于 2015-3-23 16:33 编辑 ]
2015-03-23 16:17
longwu9t
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:6
帖 子:732
专家分:2468
注 册:2014-10-9
收藏
得分:20 
你在程序头就声明的函数原型与下文中的函数定义存在矛盾的地方
参数类型不一致
多个函数都有这样的问题

你按提示慢慢改吧

Only the Code Tells the Truth             K.I.S.S
2015-03-23 16:31
荆棘之心
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2015-3-23
收藏
得分:0 
回复 2楼 longwu9t
已找到错误更正,,感谢!!!
2015-03-23 16:43
快速回复:结构体动态内存分配问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014619 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved