排序函数:第二种做法(内有三种子方法)

第二种做法:内有三种子方法;

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#if !defined(AFX_STDAFX_H__E3A2A4D8_18B3_40A0_A613_E0CEA5245DC1__INCLUDED_)
#define AFX_STDAFX_H__E3A2A4D8_18B3_40A0_A613_E0CEA5245DC1__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers

#include <stdio.h>
#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<iomanip.h>
#include <stdlib.h>

// TODO: reference additional headers your program requires here

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before

the previous line.

#endif // !defined(AFX_STDAFX_H__E3A2A4D8_18B3_40A0_A613_E0CEA5245DC1__INCLUDE

D_)

**************************************

// stdafx.cpp : source file that includes just the standard includes
// GradeResort2.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information

#include "stdafx.h"

// TODO: reference any additional headers you need in STDAFX.H
// and not in this file

**************************************

#ifndef RECORD_RESORT
#define RECORD_RESORT

#include "stdAfx.h"

#define BUF_SIZE 1024
#define MAX_NUM 200

struct Grade {
char id[12];
char name[20];
int grade;
};

int compare(const void *arg1, const void* arg2);

#endif

***************************************

// GradeResort2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "GradeResort2.h"
#include <strstrea.h>

int compare(const void *arg1, const void* arg2)
{
Grade *g1, *g2;
g1 = (Grade *) arg1;
g2 = (Grade *) arg2;

if(g1->grade < g2->grade) return -1;
else if(g1->grade == g2->grade) return strcmp(g1->id, g2->id);
else return 1;
}

int main(int argc, char* argv[])
{
Grade gArray[MAX_NUM], tempGrade;
int count = 0;

///*
ifstream fin("oldgrade.txt");
char buf[BUF_SIZE];
while(fin.getline(buf, BUF_SIZE)) {
strstreambuf sbuf = strstreambuf(buf, BUF_SIZE);
istream in(&sbuf);
in >> tempGrade.id >> tempGrade.name >> tempGrade.grade;
gArray[count++] = tempGrade;
}
//*/

/*
ifstream fin("oldgrade.txt");
char buf[BUF_SIZE];
while(fin.getline(buf, BUF_SIZE)) {
sscanf(buf, "%s%s%d", tempGrade.id,
tempGrade.name, &tempGrade.grade);
gArray[count++] = tempGrade;
}
*/

/*
FILE *fin = fopen("oldgrade.txt", "r");
while(fscanf(fin, "%s%s%d", tempGrade.id,
tempGrade.name, &tempGrade.grade) == 3) {
gArray[count++] = tempGrade;
}
*/
qsort(gArray, count, sizeof(Grade), compare);

ofstream fout("sortedGrade.txt");
fout.setf(ios::left);
for(int i=0; i<count; i++) {
tempGrade = gArray[i];
fout << setw(12) << tempGrade.id << " "
<< setw(20) << tempGrade.name << " "
<< setw(4) << tempGrade.grade << endl;
}

return 0;
}

朋友的留言簿

作 者: isince2003
时 间: Sat Oct 9 18:16:59 2004

我问你蒹葭苍苍是不是在江湖,
你说水茫茫迷失了停泊的洲渚;
我问你彼黍离离是不是在江湖,
你说月悠悠捉不住流离的秦鹿;
我问你有没有英雄笑傲江湖,
你说五风烟挽不住寂寞的陶朱;
我问你有没有佳人遗世江湖,
你说空日暮找不到闲倚的修竹。

About "double" in C

昨天发现一个很有趣的现象,在 Turbo C 里 double 类型的变量无法用通常模式进行输入操作,即无法用 scanf() 进行赋值,程序举例:

void main()
{
  double a,b,c;
  scanf("%f%f%f",&a,&b,&c);
  printf("%f%f%f",a,b,c);
}

输出结果均是 0.000000,猜想 C 语言应该没有默认初始值的功能,之所以是 0.000000 可能是保留六位小数的原因,由于数值较小且跟地址有关,输入的数值没有传入到地址中。如果将 double 型改为 float 型,则能正常操作。是何道理?难道 C 里输入函数对 double 不兼容?还是另有其他输入方法?回去要查一下 MSDN,不知道 C 有没有专门的帮助文档。

还有要注意的是 TC2 和 TC3 之间对程序要求的变化,在 TC3 中如果不包含标准输入输出的头文件 stdio.h,程序中使用 scanf 和 printf 会报错,而且主函数也必须声明类型,不然会有警告。难道是 ANSI C 的标准变了?还是 TC 为了完善自己只适用于 TC?看来还是要注意编译软件的兼容性问题。 C 语言在好多地方还是不如 C++ 呀!

PS: 输出结果是 0.000000 的原因是因为 scanf() 如果取不到值就会把变量赋零。

2009年6月11日15:40
只因当时年纪小呀,这篇博客现在让我看都快要笑死了。为了不误导读者,特更正如下:
1. 之所以读取错误,是因为本文中程序是错的,%f 匹配浮点类型,%lf 才能匹配 double 类型,这不是 scanf 的错。
2. TC3 对程序的检查是正确的,C 标准确实有变化,我当时是受一本错误的 C 语言教科书误导。
3. C 虽然很多地方不如 C++,但是 C 有简洁性的长处,C++ 是不能比的。

Something About Scanf() In C (1)

一. scanf 函数输入格式中的字符串

scanf 函数输入格式中也可以含有普通字符串,但他的含义是这些字符必须在输入中出现,例如:

int num;
scanf("hello %d", &num);

他的含义是首先要求输入一个hello字符串,然后再输入一个十进制数。 注意在等待输入时忽略hello与要输入的数之间的空格,制表符,回车。

因此这两种输入都是正确的: hello 1234 hello1234

二. scanf函数的返回值

程序:

{
  int num, result=0;
  printf("please input the student’s score: ");
  while(result==0) {
    /* 清空输入缓冲区。 */
    fflush(stdin);
    if(result!=1) printf("Please input a digital score: ");
    result=scanf("%d",&num);
  }
}

一切OK!

三. scanf函数中一个参数的应用

在 scanf 函数中,我们可以使用 %c 来读取一个字符,使用 %s 读取一个字符串。 但是读取字符串时不忽略空格,读字符串时忽略开始的空格,并且读到空格为止,因此我们只能读取一个单词,而不是整行字符串。因此一般使用 fgets 来读取一个字符串。

其实 scanf 函数也可完成这样的功能,而且还更强大。 这里主要介绍一个参数:%[] ,这个参数的意义是读入一个字符集合。 [] 是个集合的标志,因此 %[] 特指读入此集合所限定的那些字符, 比如 %[A-Z] 是输入大写字母,一旦遇到不在此集合的字符便停止。 如果集合的第一个字符是"^", 这说明读取不在 "^" 后面集合的字符,既遇到 "^" 后面集合的字符便停止。注意此时读入的字符串是可以含有空格的。 Eg: 输入一个字符串, 这个字符串只含有小写字符。遇到第一个不是小写字符时停止, scanf("%[a-z], str); Eg: 想输入一个字符串, 遇到 "." 停止,可设计如下: scanf("%[^.]", str); 使用这个参数,你可以完成许多强大的功能呦!

通常来讲,scanf 函数和他的一些参数并不是很常用,主要是因为:

1. 许多系统的 scanf 函数都有漏洞。 (典型的就是TC再输入浮点型时有时会出错)。

2. 用法复杂,容易出错。

3. 编译器作语法分析时会很困难,从而影响目标代码的质量和执行效率。

三次方程求法

#include <iostream.h>
#include <math.h>
#include <iomanip>
#include <fstream.h>
#include <stdlib.h>

#define f(x) (1*x*x*x-3*x*x-6*x+8)

double bisection(double x,double y,double z)
{
double a=x,b=y,p;
while(((b-a)/2)>=z)
{ p=(a+b)/2;
if(f(p)*f(b)<=0)
a=p;
else b=p;
}
return p;

}

void main()
{
double a[5];
char filename[256];
cout.setf(ios::fixed);
cout<<"请输入误差容限:";
cin>>a[0];
cout<<"输入目的文件全路径:";
cin>>filename;
ofstream outfile(filename);
a[2]=(3-sqrt(21))/2;
a[3]=(3+sqrt(21))/2;
a[1]=a[2];
a[4]=a[3];
for(int i=1; ;i++)
{
if(f(a[1])>=0) a[1]--;

else if(f(a[4])<=0) a[4]++;
else break;
}
for(i=1;i<4;i++)

outfile<<"函数f(x)在区间("<<a[i]<<’,’<<a[i+1]<<")上的根为"<<bisection(a[i],a[i+1],a[0])<<endl;

cout<<"已经将结果写入:"<<filename<<endl;
cout<<"误差在"<<a[0]<<"之内."<<endl;
outfile.close();
}

input=============

f:output.txt //可以是任何路径
1e-7

output============

函数f(x)在区间(-2.79129,-0.791288)上的根为-2
函数f(x)在区间(-0.791288,3.79129)上的根为1
函数f(x)在区间(3.79129,4.79129)上的根为4

按比例列选主元Gauss消去法求线性方程组的解

实习二

用按比例列选主元Gauss消去法求下列线性方程组Ax=b的解。

系数矩阵A:
15 7 6 5 1
7 10 8 7 2
6 8 10 9 3
5 7 9 10 4
1 2 3 4 5

右端向量b:

-4.0000
21.0000
13.2000
5.0000
-9.0000

要求:输出每次选取的主元的行号,解向量x.
输出时要有一定的文字说明,以使输出结果一目了然。

#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <math.h>
void main()
{
char filename1[256],filename2[256];
cout<<"Enter the path to input file:";
cin>>filename1;
cout<<"Enter the path to output file:";
cin>>filename2;
ifstream infile(filename1,ios::in|ios::nocreate);
ofstream outfile(filename2);
if(!infile){
cout<<"Can’t open input file:"<<filename1<<’
’;
exit(1);
}

int k,m,i,j,n,w;
infile>>n;
w=n+1;

double *p,*x;
double t;

p=new double[w*w];
x=new double[w];
for(i=0;i<w;i++) p[i]=0;
for(i=w;i<w*w;i++)
infile>>p[i];

for(k=1;k<n;k++) //step 1

{
t=p[k*w+k-1];
m=k;
for(i=k;i<w;i++) //step 2
{ if(fabs(p[i*w+k-1])>fabs(t))
{ t=p[i*w+k-1];
m=i;
}
}
if(t<1e-8) //step 3
{ cout<<"A is singular"<<endl;
exit(2);
}
outfile<<"第"<<k<<"列的主元行号为:"<<m<<’
’;
//----------------------------------------
if(m!=k) //step 4
for(j=k;j<w+1;j++)
{ t=p[k*w+j-1];
p[k*w+j-1]=p[m*w+j-1];
p[m*w+j-1]=t;
}
for(i=k+1;i<w;i++) //step 5
{ p[i*w+k-1]=p[i*w+k-1]/p[k*w+k-1]; //step 6
for(j=k+1;j<w+1;j++) //step 7
p[i*w+j-1]=p[i*w+j-1]-p[i*w+k-1]*p[k*w+j-1];
}

}
x[n]=p[n*w+n]/p[n*w+n-1]; //step 8

for(k=n-1;k>0;k--) //step 9
{ t=0;
for(j=k+1;j<n+1;j++) t+=p[k*w+j-1]*x[j];
x[k]=(p[k*w+n]-t)/p[k*w+k-1];
}
outfile<<"方程组的根为:("; //step 10
for(i=1;i<n;i++)
outfile<<x[i]<<’,’;
outfile<<x[n]<<’)’;
infile.close();
outfile.close();

}
input===========

5
15 7 6 5 1 -4.0000
7 10 8 7 2 21.0000
6 8 10 9 3 13.2000
5 7 9 10 4 5.0000
1 2 3 4 5 -9.0000

output==========

第1列的主元行号为:1
第2列的主元行号为:2
第3列的主元行号为:3
第4列的主元行号为:4
方程组的根为:(-2.01506,3.73891,2.00518,-1.87662,-2.59436)

修改的LDLt分解

#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>

int main()
{
char filename1[256],filename2[256];
cout<<"Enter the path to input file:";
cin>>filename1;
cout<<"Enter the path to output file:";
cin>>filename2;
ifstream infile(filename1,ios::in|ios::nocreate);
ofstream outfile(filename2);
if(!infile)
{
cout<<"Can not open input file:"<<filename1<<’
’;
exit(1);
}

int n,w,k,i,j;
infile>>n;
w=n+1;

double *a,*x,*b,*g;
double t;

a=new double[n*n];
g=new double[w];
x=new double[w];
b=new double[w];
for(i=0;i<n*n;i++)
infile>>a[i];
for(i=1;i<w;i++)
infile>>b[i];

if(a[0]<1e-8) //Step1
{
cout<<"Method failed!";
exit(2);
}
/*其实这里可以定义:#define a(int u,int v) a[u*n-n+v-1]
这样引用起来比较直观!但是运算是一样的.*/
g[1]=a[n]; //Step2
a[n]=g[1]/a[0];
a[n+1]-=g[1]*a[n];
if(a[n+1]<1e-8) //Step3
{
cout<<"Method failed!";
exit(3);
}
for(i=3;i<w;i++) //Step4
{
g[1]=a[i*n-n]; //Step5
for(j=2;j<i;j++) //Step6
{
for(t=0,k=1;k<j;k++)
t+=g[k]*a[j*n-n+k-1];
g[j]=a[i*n-n+j-1]-t;
}
a[i*n-n]=g[1]/a[0]; //Step7
for(j=2;j<i;j++) //Step8
a[i*n-n+j-1]=g[j]/a[j*n-n+j-1];
for(t=0,k=1;k<i;k++) //Step9
t+=g[k]*a[i*n-n+k-1];
a[i*n-n+i-1]-=t;
if(a[i*n-n+i-1]<1e-8) //Step10
{
cout<<"Method failed!";
exit(4);
}
}
for(i=2;i<w;i++) //Step11
{
for(t=0,k=1;k<i;k++)
t+=a[i*n-n+k-1]*b[k];
b[i]-=t;
}
x[n]=b[n]/a[n*n-1]; //Step12
for(i=n-1;i>0;i--) //Step13
{
for(t=0,k=i+1;k<w;k++)
t+=a[k*n-n+i-1]*x[k];
x[i]=b[i]/a[i*n-n+i-1]-t;
}

for(i=1;i<w;i++)
outfile<<"g("<<i<<")="<<g[i]<<’
’;

outfile<<"y=(";
for(i=1;i<n;i++)
outfile<<b[i]<<’,’;
outfile<<b[n]<<");
";

outfile<<"diag(D)=(";
for(i=1;i<n;i++)
outfile<<a[i*n-n+i-1]<<’,’;
outfile<<a[n*n-1]<<");
";

outfile<<"L=
";
for(i=1;i<w;i++)
{ for(j=1;j<w;j++)
{ if(j<=i)
outfile<<’ ’<<a[i*n-n+j-1];
else outfile<<’ ’<<’0’;
}
outfile<<’
’;
}

outfile<<"The roots are:("; //Step14
for(i=1;i<n;i++)
outfile<<x[i]<<’,’;
outfile<<x[n]<<");";
delete a;
delete g;
delete b;
delete x;
infile.close();
outfile.close();
return 0;
}
input=============

10
1 1 1 1 1 1 1 1 1 1
1 2 2 2 2 2 2 2 2 2
1 2 3 3 3 3 3 3 3 3
1 2 3 4 4 4 4 4 4 4
1 2 3 4 5 5 5 5 5 5
1 2 3 4 5 6 6 6 6 6
1 2 3 4 5 6 7 7 7 7
1 2 3 4 5 6 7 8 8 8
1 2 3 4 5 6 7 8 9 9
1 2 3 4 5 6 7 8 9 10
8 2 -12 4 -45 6 -5 9 10 30

output=============

g(1)=1
g(2)=1
g(3)=1
g(4)=1
g(5)=1
g(6)=1
g(7)=1
g(8)=1
g(9)=1
g(10)=-6.27744e+066
y=(8,-6,-14,16,-49,51,-11,14,1,20);
diag(D)=(1,1,1,1,1,1,1,1,1,1);
L=
1 0 0 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0 0 0
1 1 1 0 0 0 0 0 0 0
1 1 1 1 0 0 0 0 0 0
1 1 1 1 1 0 0 0 0 0
1 1 1 1 1 1 0 0 0 0
1 1 1 1 1 1 1 0 0 0
1 1 1 1 1 1 1 1 0 0
1 1 1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1 1 1
The roots are:(14,8,-30,65,-100,62,-25,13,-19,20);

三对角方程组的追赶法求解方程组的解

#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>

int main()
{
char /*filename1[256],*/filename2[256];
// cout<<"Enter the path to input file:";
// cin>>filename1;
cout<<"Enter the path to output file:";
cin>>filename2;
// ifstream infile(filename1,ios::in|ios::nocreate);
ofstream outfile(filename2);
// if(!infile)
// {
// cout<<"Can not open input file:"<<filename1<<’
’;
// exit(1);
// }
int n,w,k;
// infile>>n;
n=50;
w=n+1;
double *a,*b,*d,*c,*p,*q,*x,*y,*Iy,*Ix;

a=new double[w];
d=new double[w];
c=new double[w];
b=new double[w];
p=new double[w];
q=new double[w];
x=new double[w];
y=new double[w];
Iy=new double[w];
Ix=new double[w];

//输入======================================================================================

/*

int i;
infile>>d[1]>>c[1];
// infile.seekg((n-3)*sizeof(double));
// infile.seekg((n-3)*sizeof(int),ios::cur);
for(k=0;k<n-3;k++)
infile>>a[0];
infile>>a[1];

for(k=2;k<n;k++)
{
// infile.seekg((k-2)*sizeof(int),ios::cur);
for(i=0;i<k-2;i++)
infile>>a[0];
infile>>a[k]>>d[k]>>c[k];
// infile.seekg((n-k-1)*sizeof(int),ios::cur);
for(i=0;i<n-k-1;i++)
infile>>a[0];
}
infile>>c[n];
// infile.seekg((n-3)*sizeof(int),ios::cur);
for(i=0;i<n-3;i++)
infile>>a[0];
infile>>a[n]>>d[n];
for(k=1;k<w;k++)
infile>>b[k];
*/
for(k=1;k<w;k++)
{
a[k]=1;
c[k]=1;
d[k]=4;
b[k]=k+1;
}
d[1]=2;d[n]=4;

//算法===========================================================================
if(d[1]<1e-8)
{
cout<<"Method failed!";
exit(1);
}
p[1]=d[1];
q[1]=c[1]/d[1];
for(k=2;k<n-1;k++)
{
p[k]=d[k]-a[k]*q[k-1];
if(p[k]<1e-8)
{
cout<<"Method failed!";
exit(2);
}
q[k]=c[k]/p[k];
}
p[n-1]=d[n-1]-a[n-1]*q[n-2];
if(p[n-1]<1e-8)
{
cout<<"Method failed!";
exit(3);
}
y[1]=b[1]/d[1]; Iy[1]=-a[1]/d[1];
for(k=2;k<n-1;k++)
{
y[k]=(b[k]-a[k]*y[k-1])/p[k];
Iy[k]=(-a[k]*Iy[k-1])/p[k];
}
y[n-1]=(b[n-1]-a[n-1]*y[n-2])/p[n-1];
Iy[k]=(-c[n-1]-a[k]*Iy[k-1])/p[k];
x[n-1]=y[n-1]; Ix[n-1]=Iy[n-1];
for(k=n-2;k>0;k--)
{
x[k]=y[k]-q[k]*x[k+1];
Ix[k]=Iy[k]-q[k]*Ix[k+1];
}
x[n]=(b[n]-c[n]*x[1]-a[n]*x[n-1])/(c[n]*Ix[1]+a[n]*Ix[n-1]+d[n]);
for(k=1;k<n;k++)
x[k]+=Ix[k]*x[n];
outfile<<"The roots are:(";
for(k=1;k<n;k++)
outfile<<x[k]<<’,’;
outfile<<x[n]<<");
";
delete a,d,c,p,q,x,y,Iy,Ix;
// infile.close();
outfile.close();
return 0;
}
output============

The roots are:(-6.4282,2.31175,0.18121,0.963411,0.965146,1.17601,1.33083,1.50067,1.66649,1.83338,1.99999,2.16667,2.33333,2.5,2.66667,2.83333,3,3.16667,3.33333,3.5,3.66667,3.83333,4,4.16667,4.33333,4.5,4.66667,4.83333,5,5.16667,5.33333,5.5,5.66667,5.83333,6,6.16667,6.33333,6.5,6.66666,6.83334,6.99997,7.16677,7.33293,7.5015,7.66108,7.85418,7.92219,8.45706,7.24957,12.5447);

Gauss-Jordan列主元消去法

#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <math.h>
#define a(u,v) a[(u)*n+(v)-w]

void main()
{//-----------------------文件流----------------------------
char filename1[256],filename2[256];
cout<<"Enter the path to input file:";
cin>>filename1;
cout<<"Enter the path to output file:";
cin>>filename2;
ifstream infile(filename1,ios::in|ios::nocreate);
ofstream outfile(filename2);
if(!infile)
{
cout<<"Can not open input file:"<<filename1<<’
’;
exit(1);
}
//----------------------变量定义----------------------------
int n,w,k,i,j;
int *p;

double *a;
double t;
//------------------------输入------------------------------
infile>>n;
w=n+1;

p=new int[w];
a=new double[n*n];

for(i=0;i<n*n;i++)
infile>>a[i];

//------------------------算法------------------------------
for(k=1;k<w;k++) //step 1
{ p[k]=k;
for(i=k;i<w;i++) //step 2
{
if(fabs(a(i,k))>fabs(a(p[k],k))) p[k]=i;
}

if(a(p[k],k)<1e-10) //step 3
{
cout<<"A is singular!"<<endl;
exit(1);
}
if(p[k]!=k) //step 4
for(i=1;i<w;i++)
{
t=a(k,i);
a(k,i)=a(p[k],i);
a(p[k],i)=t;
}
a(k,k)=1/a(k,k); //step 5
for(i=1;i<w;i++)
if(i!=k)
a(i,k)*=(-a(k,k)); //step 6
for(i=1;i<w;i++) //step 7
if(i!=k)
for(j=1;j<w;j++)
if(j!=k)
a(i,j)+=a(i,k)*a(k,j);
for(j=1;j<w;j++) //step 8
if(j!=k)
a(k,j)*=a(k,k);
}
for(k=n;k>0;k--) //step 9
if(k!=p[k])
for(i=1;i<w;i++)
{
t=a(i,k);
a(i,k)=a(i,p[k]);
a(i,p[k])=t;
}
outfile<<"各步主行行号依次为::";
for(i=1;i<w;i++)
outfile<<p[i]<<’ ’;

outfile<<"
A的逆矩阵为:
"; //step 10
for(i=1;i<w;i++)
{
for(j=1;j<w;j++)
outfile<<a(i,j)<<’ ’;
outfile<<’
’;
}

delete a,p;

infile.close();
outfile.close();
}
input=================

10
1 2 3 4 5 6 7 8 9 10
2 3 4 5 6 7 8 9 10 1
3 4 5 6 7 8 9 10 1 2
4 5 6 7 8 9 10 1 2 3
5 6 7 8 9 10 1 2 3 4
6 7 8 9 10 1 2 3 4 5
7 8 9 10 1 2 3 4 5 6
8 9 10 1 2 3 4 5 6 7
9 10 1 2 3 4 5 6 7 8
10 1 2 3 4 5 6 7 8 9

output=================

各步主行行号依次为::10 9 8 7 6 6 7 8 9 10
A的逆矩阵为:
-0.0981818 0.00181818 0.00181818 0.00181818 0.00181818 0.00181818 0.00181818 0.00181818 0.00181818 0.101818
0.00181818 0.00181818 0.00181818 0.00181818 0.00181818 0.00181818 0.00181818 0.00181818 0.101818 -0.0981818
0.00181818 0.00181818 0.00181818 0.00181818 0.00181818 0.00181818 0.00181818 0.101818 -0.0981818 0.00181818
0.00181818 0.00181818 0.00181818 0.00181818 0.00181818 0.00181818 0.101818 -0.0981818 0.00181818 0.00181818
0.00181818 0.00181818 0.00181818 0.00181818 0.00181818 0.101818 -0.0981818 0.00181818 0.00181818 0.00181818
0.00181818 0.00181818 0.00181818 0.00181818 0.101818 -0.0981818 0.00181818 0.00181818 0.00181818 0.00181818
0.00181818 0.00181818 0.00181818 0.101818 -0.0981818 0.00181818 0.00181818 0.00181818 0.00181818 0.00181818
0.00181818 0.00181818 0.101818 -0.0981818 0.00181818 0.00181818 0.00181818 0.00181818 0.00181818 0.00181818
0.00181818 0.101818 -0.0981818 0.00181818 0.00181818 0.00181818 0.00181818 0.00181818 0.00181818 0.00181818
0.101818 -0.0981818 0.00181818 0.00181818 0.00181818 0.00181818 0.00181818 0.00181818 0.00181818 0.00181818

逐次线性

#include<iostream.h>
#include<fstream.H>
#include<math.h>
#include<stdlib.h>

#define q(a,b) q[(a)*(a+1)/2+b]
#define p(a,b) q[a+1+(b)*(b+1)/2]

ifstream infile("in.txt");
ofstream outfile("out.txt");

double *x,*f,*q,root;
int n,i,j,k;

void indata()
{
infile>>n;
x=new double[n];
f=new double[n];
q=new double[(n+2)*(n+1)/2];
for(i=0;i<=n;i++) infile>>x[i]>>f[i];
infile>>root;
}

void neville()
{
for(i=0;i<=n;i++)
{
q(i,0)=f[i];
for(k=1;k<=i;k++)
q(i,k)=((root-x[i-k])*q(i,k-1)-(root-x[i])*q(i-1,k-1))/(x[i]-x[i-k]);
if(fabs(q(i,i)-q(i-1,i-1))<1e-10)
break;
}
if(i>n) i--;
outfile<<"Neville:"<<q(i,i)<<endl;
}
void aitken()
{
for(i=1;i<=n;i++)
{
p(0,i)=f[0]+(root-x[0])*(f[i]-f[0])/(x[i]-x[0]);
for(j=1;j<i;j++)
p(j,i)=p(j-1,j)+(root-x[j])*(p(j-1,i)-p(j-1,j))/(x[i]-x[j]);
if(fabs(p(i,i)-p(i-1,i-1))<1e-10)
break;
}
if(i>n) i--;
outfile<<endl<<"Aitken:"<<p(i-1,i)<<endl;
}

void mine()
{

q(0,0)=f[0];
for(i=1;i<=n;i++)
{ q(i,0)=f[i];
q(i,1)=1/(x[0]-x[i])*((root-x[i])*f[0]-(root-x[0])*f[i]);

for(k=2;k<=i;k++)
q(i,k)=-1/(x[i-k+1]-x[i])*((root-x[i-k+1])*q(i,k-1)-(root-x[i])*q(i-1,k-1));
if(fabs(q(i,i)-q(i-1,i-1))<1e-10)
break;
}
if(i>n) i--;
outfile<<endl<<"mine:"<<q(i,i)<<endl;
}

int main()
{
indata();
neville();
aitken();
mine();
return 0;
}
input===============

4
-2 0.04
-1 0.2
0 1
1 5
2 25
0.5

output==============

Neville:2.04

Aitken:2.04

mine:2.04

Newton均匀插值多项式

#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>

#define Q(a,b) Q[(a)*(a+1)/2+b]

ifstream infile("in.txt");
ofstream outfile("out.txt");

int n,i,j;
double *x,*root,*Q,*b;

void input()
{
infile>>n;
x=new double[n];
root=new double[n-1];
b=new double[n];
Q=new double[n*(n+1)/2];

for(i=0;i<n;i++)
{
infile>>x[i]>>Q(i,0);
}
}

void setroot()
{
double t;
for(i=0;i<n;i++)
for(j=i;j<n;j++)
if(x[j]<x[i])
{
t=x[j];
x[j]=x[i];
x[i]=t;
t=Q(j,0);
Q(j,0)=Q(i,0);
Q(i,0)=t;
}
for(i=0;i<n-1;i++)
{
root[i]=(x[i]+x[i+1])/2;
}

}

void Newton()
{
for(i=1;i<n;i++)
for(j=1;j<=i;j++)
Q(i,j)=(Q(i,j-1)-Q(i-1,j-1))/(x[i]-x[i-j]);
b[n-1]=Q(n-1,n-1);
}

void f(double x0)
{
for(i=n-1;i>0;i--)
b[i-1]=Q(i-1,i-1)+b[i]*(x0-x[i-1]);
outfile<<"f("<<x0<<")的近似值为:"<<b[0]<<endl;
}

void main(void)
{
input();
setroot();
Newton();
outfile<<"差分表为:"<<endl;
for(i=0;i<n;i++)
{ outfile<<i<<’ ’<<x[i]<<’ ’;
for(j=0;j<=i;j++)
outfile<<Q(i,j)<<’ ’;
outfile<<endl;
}

for(j=0;j<n-1;j++)
f(root[j]);

delete x,b,Q,root;
infile.close();
outfile.close();
}

input===============

7
-3 -4.5
6 2.1
-1 -3.2
5 2.8
-2 -1.8
3 1.7
0 0.5

output==============

差分表为:
0 -3 -4.5
1 -2 -1.8 2.7
2 -1 -3.2 -1.4 -2.05
3 0 0.5 3.7 2.55 1.53333
4 3 1.7 0.4 -0.825 -0.675 -0.368056
5 5 2.8 0.55 0.03 0.1425 0.116786 0.0606052
6 6 2.1 -0.7 -0.416667 -0.0744444 -0.0309921 -0.0184722 -0.00878638
f(-2.5)的近似值为:-1.06517
f(-1.5)的近似值为:-3.0675
f(-0.5)的近似值为:-1.86002
f(1.5)的近似值为:5.90552
f(4)的近似值为:-0.577778
f(5.5)的近似值为:4.59237

变步长的Simpson求积法

#include<iostream.h>
#include<fstream.h>
#include<math.h>
#include<stdlib.h>

ifstream infile("in.dat");
ofstream outfile("out.out ");

double a[3],b[3];
int i,m[3],k;

void indata()
{
for(i=0;i<3;i++)
infile>>a[i]>>b[i]>>m[i];
m[i]/=2;
}

double cal0(double x)
{
return (6*exp(-x*x));
}

double cal1(double x)
{
return (3*log(x));
}

double cal2(double x)
{
return (sin(x)/x);
}

void simpson(double a,double b,int m,double (*g)(double))
{
double h[3],q[3],p[3],s[3],t;

h[0]=(b-a)/(2*m);
t=0;
for(i=1;i<m+1;i++)
t+=(g(a+(2*i-1)*h[0])+g(a+2*i*h[0]));
p[0]=g(a)-g(b)+2*t;
t=0;
for(i=1;i<m+1;i++)
t+=g(a+(2*i-1)*h[0]);
s[0]=h[0]*(p[0]+2*t)/3;

h[1]=h[0]/2;
q[0]=0;
for(i=1;i<2*m+1;i++)
q[0]+=g(a+(2*i-1)*h[1]);
s[1]=h[1]*(p[0]+4*q[0])/3;
h[2]=h[1]/2;
q[1]=0;
for(i=1;i<pow(2,2)*m+1;i++)
q[1]+=g(a+(2*i-1)*h[2]);
p[1]=p[0]+2*q[0];
s[2]=h[2]*(p[1]+4*q[1])/3;
for(k=2;;k++)
{
h[(k+1)%3]=(b-a)/(pow(2,k+2)*m);
q[k%3]=0;
for(i=1;i<pow(2,k+1)*m+1;i++)
q[k%3]+=g(a+(2*i-1)*h[(k+1)%3]);
p[k%3]=p[(k-1)%3]+2*q[(k-1)%3];
s[(k+1)%3]=h[(k+1)%3]*(p[k%3]+4*q[k%3])/3;
if(s[(k+1)%3]-s[k%3]<1e-7)
{ t=s[(k+1)%3];
break;
}
}
outfile<<t<<endl;
outfile<<"积分区间数目为:"<<pow(2,k+1)*m<<endl;
}

void main()
{
indata();

outfile<<"I("<<1<<")=";
simpson(a[0],b[0],m[0],cal0);

outfile<<"I("<<2<<")=";
simpson(a[1],b[1],m[1],cal1);

outfile<<"I("<<3<<")=";
simpson(a[2],b[2],m[2],cal2);

infile.close();
outfile.close();
}

input==================

0 1 2
1 2 2
1 4 2

output=================

I(1)=4.48094
积分区间数目为:16
I(2)=1.15888
积分区间数目为:32
I(3)=0.81212
积分区间数目为:32

Exam!

昨天考完了六级,明天又有概率论的考试,复习的好辛苦。

到期末了才发现原来以为很难的东西并不是不能克服,大数定律、中心极限定理那么不清不楚的家伙其实也是很明了的。唉,不知道为什么正常上课的时候看不懂。也许没那个环境吧,平时看不懂就可以放掉,到考试了,看不懂就要挂,当然要努力理解了。

考完复变晚上头很痛,上了会儿网,QQ上人倒是不少,没有聊天的欲望。一个哥们向我诉苦,说复变又没考好,觉得很失败。我何尝不是,好象对分析学没有感觉似的,错的莫名其妙,老师改的又严,不知道结果会怎么样,不过期末比期中感觉好多了,希望分数不要太差。

学校有空调的地方少的可怜,自习热的不行,教室里风扇风挺大,可居然连蚊子都扇不走,咬得身上很多包。无奈就回来洗洗澡,降一下温。

唉,苦命的数学系学生,非常想学好,可数学这个东西对我来说怎么那么艰苦呢。

还有三门,24号就能考完回家了。虽然有些讨厌在家那种无所事事的感觉,可不用操心那么多事还是挺让人向往的。开学就该大三了,离自己要独立越来越近了,中午睡觉的时候想了一下,还是蛮吓人的。过惯了有人照顾的生活,很难想象自己一个人奋斗的艰苦。可是,路,还是得走下去呀!

Shit exam!And,bless me exam!

接着去自习~~

我的太阳

我把我生命中珍贵的、易失去的东西都称做我的太阳。因为它们,我的生命充满了快乐的光芒,虽然阴霾并不是荡然无存,但是,快乐过,就足够庆幸了。
不可否认的是,我应该非常感激上天赐给了我很多的幸福。人生像一个找寻幸福的过程,前方完全是个未知数,也没有可以估计的期望,渐渐地由孩子而成人而老去,似乎没人有觉得自己的幸福是全世界的最大值。我也一样,人性的本质给了我一颗蒙尘的心,永远不知道满足。常常在想,我到底需要什么呢?

也许这是一个非常沉重的话题,这个月的《萌芽》上有一篇文章说,一个男孩告诉别人,他追求的是爱情,但是在有爱情之前得先打好基础。但他却是清华的学生,已经保送研究生,也不知道他指的打好基础是什么。文章说,等你准备好一切然后去追求你的女孩时,她也许已经在别人的怀抱了。这句话让我想了好久。

我曾经天真的以为我喜欢的女孩和我的想法一样,可以平平淡淡的分享一份稳定的爱情,可是事实证明,像好朋友的个性签名说的,这个世界远不像你想的那样。然后,我渐渐明白了,一个男人还应该有自己的责任,应该有一个能给得起承诺的基础,就开始吝惜自己的感情了。既然给不起别人,就不要期望可以有回报。换用我自称帅哥室友的一句话,可能极端了点,这年头,帅顶个屁用,男人要有钱。

直至现在我也不知道我到底做得对不对。大一的时候日子过得很平淡,由于对数学不怎么感兴趣,心中也有一份期待,学习以外的爱好就是运动运动,看看闲书。我的朋友就说,很羡慕你的,有那么随心的生活。等到军训后的那个暑假,受到打击之后才发现,自己什么也不是,虽然从小功利心就不是那么的强,还是被一种强烈的自卑撞击得体无完肤。就连暑假也过得不顺利,倒霉到能在马路上碰巧撞到一个篮球,而且伤口到开学才长好。大二为了让自己生活中的一块被填补上,便把自己投入到各种活动中去,把本来可以大一就应该体验到的东西尝试了一遍。时间是疗伤很好的工具,在忙碌中渐渐淡忘。虽然不可以完全无动于衷了,可是至少可以做到平静的生活,偶尔会听到一些消息,全当作一个好朋友罢默默的祝福。

整个大学时代已经没有什么好期待的了,我认为我的感情生活在大学已经成了死水一潭,没可能再有什么波澜。为了不让它那么索然无味的过去,也是受了一位学长的鼓动,就打定了决心出国,虽然成绩并不是很理想,但是还是过得去那种,可以试试,况且还有一年多时间。然后在系里做得还行,因为学生工作的缘故也认识了许多朋友,再次恢复了往日那种自信,虽然学习也没什么起色。生活也渐渐地趋于规则化,况且有学校的身处市郊,与世无争的清净,也可以说是安逸了。

无论如何是舍不得的,我也不想回避这一点。但是一切真能像我期待的那样走下去吗?我常常问自己。我不知道,不知道将来,不知道该如何去做。记得好象是柳传志说过,如果不知道下面该做什么,先把手头的事情做好。可是我发现自己居然是那么的幼稚,连现在该怎么做都不知道。在感情方面,我还只是个不谙世事的孩子,把一切看得像童话般简单,可事实把理想一个一个撞得粉碎。现在的我,依然过着原来的生活,依然渴望平平淡淡波澜不惊,只是某些时候加了一些期待,加了一些对未来的憧憬。正如志摩之言:“我将于茫茫人海中访我唯一灵魂之伴侣;得之,我幸;不得,我命,如此而已。”