{
cout"can't get N"endl;
return false;
}
else
{
cout"N = "ulNendl;
}
//读取元素
complexdouble c;
for(unsigned long i = 0; iulN; i++)
{
loadfilec;
if(!loadfile)
{
cout"can't get enough infomation"endl;
return false;
}
else
cout"x("i") = "cendl;
vecList.push_back(c);
}
//关闭文件
loadfile.close();
return true;
}
bool saveResultToFile(unsigned longulN, vectorcomplexdoublevecList) //保存结果至文件中
{
//询问是否需要将结果保存至文件
char chChoose = ' ';
cout"Do you want to save the result to file? (y/n):";
chChoose = _getch();
if(chChoose != 'y'chChoose != 'Y')
{
return true;
}
//输入文件名
string strfilename;
cout"\nInput file name:" ;
cinstrfilename;
cout"Save result to file "strfilename"......"endl;
//打开文件
ofstream savefile(strfilename.c_str());
if(!savefile)
{
cout"can't open file"endl;
return false;
}
//写入N
savefileulNendl;
//写入元素
for(vectorcomplexdouble ::iterator i = vecList.begin(); ivecList.end(); i++)
{
savefile*iendl;
}
//写入完毕
cout"save succeed."endl;
//关闭文件
savefile.close();
return true;
}
void FFT(unsigned longulN, vectorcomplexdoublevecList)
{
//得到幂数
unsigned long ulPower = 0;//幂数
unsigned long ulN1 = ulN - 1;
while(ulN10)
{
ulPower++;
ulN1 /= 2;
}
//反序
bitsetsizeof(unsigned long) * 8 bsIndex;//二进制容器
unsigned long ulIndex;//反转后的序号
unsigned long ulK;
for(unsigned long p = 0; pulN; p++)
{
ulIndex = 0;
ulK = 1;
bsIndex = bitsetsizeof(unsigned long) * 8(p);
for(unsigned long j = 0; julPower; j++)
{
ulIndex += bsIndex.test(ulPower - j - 1) ? ulK : 0;
ulK *= 2;
}
if(ulIndexp)
{
complexdouble c = vecList[p];
vecList[p] = vecList[ulIndex];
vecList[ulIndex] = c;
}
}
//计算旋转因子
vectorcomplexdoublevecW;
for(unsigned long i = 0; iulN / 2; i++)
{
vecW.push_back(complexdouble(cos(2 * i * PI / ulN) , -1 * sin(2 * i * PI / ulN)));
}
for(unsigned long m = 0; mulN / 2; m++)
{
cout "\nvW["m"]="vecW[m];
}
//计算FFT
unsigned long ulGroupLength= 1;//段的长度
unsigned long ulHalfLength = 0;//段长度的一半
unsigned long ulGroupCount = 0;//段的数量
complexdouble cw;//WH(x)
complexdouble c1;//G(x) + WH(x)
complexdouble c2;//G(x) - WH(x)
for(unsigned long b = 0; bulPower; b++)
{
ulHalfLength = ulGroupLength;
ulGroupLength *= 2;
for(unsigned long j = 0; julN; j += ulGroupLength)
{
for(unsigned long k = 0; kulHalfLength; k++)
{
cw = vecW[k * ulN / ulGroupLength] * vecList[j + k + ulHalfLength];
c1 = vecList[j + k] + cw;
c2 = vecList[j + k] - cw;
vecList[j + k] = c1;
vecList[j + k + ulHalfLength] = c2;
}
}
}
}
void display(unsigned longulN, vectorcomplexdoublevecList)
{
cout"\n\n===========================Display The Result========================="endl;
for(unsigned long d = 0; dulN;d++)
{
cout"X("d")\t\t\t = "vecList[d]endl;
}
}
下面为STDAFX.H文件:
// stdafx.h : 标准系统包含文件的包含文件,
// 或是常用但不常更改的项目特定的包含文件
#pragma once
#include iostream
#include tchar.h
// TODO: 在此处引用程序要求的附加头文件
下面为STDAFX.CPP文件:
// stdafx.cpp : 只包括标准包含文件的源文件
推荐阅读
- 虚拟机下载软件后蓝屏死机,一安装虚拟机就蓝屏
- c语言0到100中所有奇数相加,c语言1到100所有奇数的和
- redis长连接正确使用,redis长连接和短连接
- 国外服务器地址二维码,外国服务器地址账号密码
- MySQL库文件怎么设置 mysql怎么设置保存点
- ios打字怎么取消声音,ios打字声音怎么调
- jquery主动触发click,jquery手动触发事件
- 2016ios好玩的赛车游戏,ios赛车类手游排行榜
- C语言调用函数加减乘除 c语言用函数实现加减乘除