當前位置:首頁 » 編程語言 » C語言fw

C語言fw

發布時間: 2023-12-26 14:41:15

① C語言編程實現

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#defineinput"input.txt"
#defineoutput"output.txt"
#defineMax20
typedefstruct_Word_{
charwd[Max];
intn;
}sword;
intmain(intargc,char*argv[])
{
FILE*fr,*fw;
if((fr=fopen(input,"r"))==NULL)
printf("openerror");
charword[Max];
charc;
inti=0,j=0,num=0,flag=0;
sword*list=malloc(sizeof(sword));
do{
c=fgetc(fr);
if(c!=' '&&c!=' '&&c!=''&&c!=','&&c!='.'&&c!=EOF){
word[i++]=c;
flag=0;
}
else{
if(0==flag){
flag=1;
word[i]='';
i=0;
for(j=0;j<num;j++)
if(strcmp((list+j)->wd,word)==0){
(list+j)->n++;
break;
}
if(j==num){
list=realloc(list,(num+1)*sizeof(sword));
strcpy((list+num)->wd,word);
(list+num)->n=1;
++num;
}
}
}
}while(c!=EOF);
fclose(fr);
fw=fopen(output,"w");
for(i=0;i<num;i++){
if((list+i)->n>=5)
fprintf(fw,"%s %d ",(list+i)->wd,(list+i)->n);
}
fclose(fw);
return0;
}

② 求C語言高手

Numerical Recipes in C
一書及所附程序,有完整程序。不過我封裝了它的C++版本,可以對但參數或多參數求極值,完整的頭文件為:

#ifndef __OPTIMIZATION_H__
#define __OPTIMIZATION_H__

//////////////////鄭戚////////////////////////////////////////////////////////
// class TOptimization
//
// $ 求函數一個或多個參數的最小值
//
// 該類默認對一個參數優化,一般只要輸入優化參數數目
// 優化目標函數就可以使用。
//
// ...主要代碼來自:
// Numerical Recipes in C++
// The Art of Scientific Computing
// Second Edition
// William H. Press Saul A. Teukolsky
// William T. Vetterling Brian P. Flannery
//
// 中譯本:
// C++ 數值演算法(第二版) 胡健偉 趙志勇 薛運華 等譯
// 電子工業出版社 北京游碼 (2005)
//
// Author: Jian Feng
// Email: [email protected]
// Dec. 9, 2006
//
//////////////////////////////////////////////////////////////////////////
//
// 輸入函數:
//
// @MaxIterationStep: 最大迭代次數, 默認 1000
// @ParameterNumbers: 優化參數數目, 默認 1
// @InitMatrix: 初始化矩陣參數(N*N), 默認
// @Tolerance: 容許公差, 默認 1E-7
//
// 執行函數:
//
// @ExecutePowell: 利用powell方法進行多參數優化
// @ExecuteBrent: 利用brent方法進行單參數優化
//
// 輸出函數:
//
// @OptimizatedParameters: 優化結果數據
// @ObjectiveFunctionValue: 目標函數在優化值處的值
//
// 使用示例:
//
// 1. 單參數
// double objfun(double a){
// double sum = 0;
// for(int i = 0; i < DataPoints; ++i)
// sum += SQR(Exps[i] - Theo(a));
// }
// double value
// TOptimization opt;
// if(opt.ExecuteBrent(objfun, -10, -1)) opt.OptimizatedParameters(&value);
//
// 2. 多參數
// double objfun(double *a){
// double sum = 0;
// for(int i = 0; i < DataPoints; ++i)
// sum += SQR(Exps[i] - Theo(a));
// }
// double value[3]
// TOptimization opt(3);
// double ival[3] = ;
// if(opt.ExecutePowell(objfun, ival)) opt.OptimizatedParameters(value);
//
namespace{
static int ncom; //公用變數
static double *pcom_p; //公用變數
static double *xicom_p; //公用變數
static double (*nrfunc)(double*); //公用函數指針
}

class TOptimization
{
private:
typedef double (*Reff)(double *);
typedef double (*Ptrf)(double );

public:
TOptimization(int n = 1);
~TOptimization()

//主要方法
void ParameterNumbers(int n)

//喊磨陵利用powell方法對一個或多個參數優化
bool ExecutePowell(Reff obj, double *a = 0);

//利用brent方法對一個參數優化,需給出參數所在的區間
bool ExecuteBrent(Ptrf obj, double vFrom = 0, double vTo = 1);

void OptimizatedParameters(double *a)

void OptimizatedParameters(double &a)

//void OptimizatedParameters(double *a){
// if(method) for(int i=0; i<num; ++i) a[i]=coef[i];
// else *a = vmin;
//}

//其它方法
void InitMatrix(double **m)
{
for(int i=0; i<num; ++i)
for(int j = 0; j<num; ++j)
matx[i][j]=m[i][j];
setm = true;
}

void MaxIterationStep(int s)

void Tolerance(double eps)

double ObjectiveFunctionValue()const

private:

double brent(double ax, double bx, double cx, Ptrf f, double tol, double &xmin, int &flag);
void mnbrak(double &ax, double &bx, double &cx, double &fa, double &fb, double &fc, Ptrf func);
void linmin(double *p, double *xi, double &fret, Reff func);
bool powell(double *p, double **xi, double ftol, int &iter, double &fret, Reff func);

void shft2(double &a, double &b, const double c)

void shft3(double &a, double &b, double &c, const double d)

double SQR(double x)

void SWAP(double &a, double &b)

double SIGN(const double &a, const double &b)

double MAX(const double &a, const double &b)

void AllocateMemory();

void FreeMemory();

static double f1dim(double x)
{
int j;
double *xt = new double [ncom];
//Vec_Dp &pcom=*pcom_p,&xicom=*xicom_p;
double *pcom = pcom_p, *xicom = xicom_p;
for (j=0;j<ncom;j++)
xt[j]=pcom[j]+x*xicom[j];
//delete []xt;
double val = nrfunc(xt);
delete []xt;
return val;
}

bool setm; //是否設置優化方向初始矩陣

int num; //優化參數
int ITMAX; //最大迭代數
int iter; //實際迭代步數
int method; //優化方法 0: 1-D brent, 2: M-D Powell

double vmin; //一維優化參數
double ftol; //容許差
double fret; //目標函數值

double *coef; //多維優化參數值
double **matx; //多維優化參數方向的初始值

};

//////////////////////////////////////////////////////////////////////////

inline TOptimization::TOptimization(int n )
{
num = n;
ftol = 1e-7;
ITMAX = 1000;
iter = 0;
fret = 0.;
vmin = 0.;
method = 0;
setm = false;

AllocateMemory();
}

inline void TOptimization::AllocateMemory()
{
pcom_p = new double [num];
xicom_p = new double [num];
coef = new double [num];
matx = new double *[num];
for(int i = 0; i < num; ++i)
{
coef[i] = 0.;
matx[i] = new double [num];
for(int j = 0; j < num; ++j)
matx[i][j]=(i == j ? 1.0 : 0.0);
}
}

inline void TOptimization::FreeMemory()
{
for(int i = 0; i < num; ++i)
{
delete []matx[i];
}
delete []matx;
delete []pcom_p;
delete []xicom_p;
delete []coef;
}

inline bool TOptimization::ExecutePowell(Reff obj, double *a)
{
method = 1;
if(a)
for(int i = 0; i < num; ++i) coef[i] = a[i];
return powell(coef, matx, ftol, iter, fret, obj);
}

inline bool TOptimization::ExecuteBrent(Ptrf obj, double vFrom, double vTo)
{
method = 0;
int flag;
double cx, fa, fb, fc;
mnbrak(vFrom,vTo,cx,fa,fb,fc,obj);
fret = brent(vFrom,vTo,cx,obj, ftol,vmin, flag);
return flag ? true : false;
}

inline void TOptimization::mnbrak(double &ax, double &bx, double &cx, double &fa,
double &fb, double &fc, Ptrf func)
{
const double GOLD=1.618034,GLIMIT=100.0,TINY=1.0e-20;
double ulim,u,r,q,fu;

fa=func(ax);
fb=func(bx);
if (fb > fa) {
SWAP(ax,bx);
SWAP(fb,fa);
}
cx=bx+GOLD*(bx-ax);
fc=func(cx);
while (fb > fc) {
r=(bx-ax)*(fb-fc);
q=(bx-cx)*(fb-fa);
u=bx-((bx-cx)*q-(bx-ax)*r)/
(2.0*SIGN(MAX(fabs(q-r),TINY),q-r));
ulim=bx+GLIMIT*(cx-bx);
if ((bx-u)*(u-cx) > 0.0) {
fu=func(u);
if (fu < fc) {
ax=bx;
bx=u;
fa=fb;
fb=fu;
return;
} else if (fu > fb) {
cx=u;
fc=fu;
return;
}
u=cx+GOLD*(cx-bx);
fu=func(u);
} else if ((cx-u)*(u-ulim) > 0.0) {
fu=func(u);
if (fu < fc) {
shft3(bx,cx,u,cx+GOLD*(cx-bx));
shft3(fb,fc,fu,func(u));
}
} else if ((u-ulim)*(ulim-cx) >= 0.0) {
u=ulim;
fu=func(u);
} else {
u=cx+GOLD*(cx-bx);
fu=func(u);
}
shft3(ax,bx,cx,u);
shft3(fa,fb,fc,fu);
}
}

inline double TOptimization::brent(double ax, double bx, double cx,
Ptrf f, double tol, double &xmin, int &flag)
{
flag = 1;
const double CGOLD=0.3819660;
const double ZEPS=1.0e-20;
int iter;
double a,b,d=0.0,etemp,fu,fv,fw,fx;
double p,q,r,tol1,tol2,u,v,w,x,xm;
double e=0.0;

a=(ax < cx ? ax : cx);
b=(ax > cx ? ax : cx);
x=w=v=bx;
fw=fv=fx=f(x);
for (iter=0;iter<ITMAX;iter++) {
xm=0.5*(a+b);
tol2=2.0*(tol1=tol*fabs(x)+ZEPS);
if (fabs(x-xm) <= (tol2-0.5*(b-a))) {
xmin=x;
return fx;
}
if (fabs(e) > tol1) {
r=(x-w)*(fx-fv);
q=(x-v)*(fx-fw);
p=(x-v)*q-(x-w)*r;
q=2.0*(q-r);
if (q > 0.0) p = -p;
q=fabs(q);
etemp=e;
e=d;
if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x))
d=CGOLD*(e=(x >= xm ? a-x : b-x));
else {
d=p/q;
u=x+d;
if (u-a < tol2 || b-u < tol2)
d=SIGN(tol1,xm-x);
}
} else {
d=CGOLD*(e=(x >= xm ? a-x : b-x));
}
u=(fabs(d) >= tol1 ? x+d : x+SIGN(tol1,d));
fu=f(u);
if (fu <= fx) {
if (u >= x) a=x; else b=x;
shft3(v,w,x,u);
shft3(fv,fw,fx,fu);
} else {
if (u < x) a=u; else b=u;
if (fu <= fw || w == x) {
v=w;
w=u;
fv=fw;
fw=fu;
} else if (fu <= fv || v == x || v == w) {
v=u;
fv=fu;
}
}
}
flag = 0;
xmin=x;
return fx;
}

inline void TOptimization::linmin(double *p, double *xi, double &fret, Reff func)
{
int j, flag;
const double TOL=1.0e-8;
double xx,xmin,fx,fb,fa,bx,ax;

int n=num;
ncom=n;
//pcom_p=new Vec_Dp(n);
//xicom_p=new Vec_Dp(n);
nrfunc=func;
//Vec_Dp &pcom=*pcom_p,&xicom=*xicom_p;
double *pcom = pcom_p, *xicom = xicom_p;
for (j=0;j<n;j++) {
pcom[j]=p[j];
xicom[j]=xi[j];
}
ax=0.0;
xx=1.0;
mnbrak(ax,xx,bx,fa,fx,fb,f1dim);
fret=brent(ax,xx,bx,f1dim,TOL,xmin, flag);
for (j=0;j<n;j++) {
xi[j] *= xmin;
p[j] += xi[j];
}
//delete xicom_p;
//delete pcom_p;
}

inline bool TOptimization::powell(double *p, double **xi, double ftol, int &iter,
double &fret, Reff func)
{
const int ITMAX=500;
const double TINY=1.0e-20;
int i,j,ibig;
double del,fp,fptt,t;

int n=num;
//Vec_Dp pt(n),ptt(n),xit(n);
double *pt, *ptt, *xit;
for(i = 0; i < n; ++i)
{
pt = new double [n];
ptt = new double [n];
xit = new double [n];
}
fret=func(p);
for (j=0;j<n;j++) pt[j]=p[j];
for (iter=0;;++iter) {
fp=fret;
ibig=0;
del=0.0;
for (i=0;i<n;i++) {
for (j=0;j<n;j++) xit[j]=xi[j][i];
fptt=fret;
linmin(p,xit,fret,func);
if (fptt-fret > del) {
del=fptt-fret;
ibig=i+1;
}
}
if (2.0*(fp-fret) <= ftol*(fabs(fp)+fabs(fret))+TINY) {
delete []pt;
delete []ptt;
delete []xit;
return true;
}
if (iter == ITMAX)
{
delete []pt;
delete []ptt;
delete []xit;
return false;
//cerr<<"powell exceeding maximum iterations.";
}
for (j=0;j<n;j++) {
ptt[j]=2.0*p[j]-pt[j];
xit[j]=p[j]-pt[j];
pt[j]=p[j];
}
fptt=func(ptt);
if (fptt < fp) {
t=2.0*(fp-2.0*fret+fptt)*SQR(fp-fret-del)-del*SQR(fp-fptt);
if (t < 0.0) {
linmin(p,xit,fret,func);
for (j=0;j<n;j++) {
xi[j][ibig-1]=xi[j][n-1];
xi[j][n-1]=xit[j];
}
}
}
}
}

#endif
另外,站長團上有產品團購,便宜有保證

熱點內容
去角質皮面膜怎麼樣配置 發布:2024-11-30 10:44:22 瀏覽:808
證券首次開戶後為什麼沒密碼 發布:2024-11-30 10:41:57 瀏覽:316
玩具廠資料庫 發布:2024-11-30 10:41:57 瀏覽:786
學校考試伺服器地址 發布:2024-11-30 10:35:30 瀏覽:683
nas無盤伺服器搭建教程 發布:2024-11-30 10:27:07 瀏覽:156
觸摸精靈腳本解密 發布:2024-11-30 10:27:04 瀏覽:328
如何解鎖密碼鎖上的密碼用數字解 發布:2024-11-30 10:07:55 瀏覽:454
文件夾選項怎麼找 發布:2024-11-30 10:05:50 瀏覽:378
安卓手機界面下面返回鍵怎麼設置 發布:2024-11-30 09:59:12 瀏覽:65
名ftp是 發布:2024-11-30 09:59:12 瀏覽:294