西北工业大学动力与能源学院上机实习报告

更新时间:2024-06-17 08:56:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

西北工业大学动力与能源学院

上机实习报告

班 级: 02031002 专 业: 自动化 学 号: 2010301740 姓 名: 王宏业 课程名称: 上机实习 成 绩:

2012年7月4日

1.熟悉VC编程环境,用选择法对n个数字进行排序。 程序编制要点(知识点、程序框图):

开始 输入数字个数n和需要排序的数字 j=0 N j

源程序代码:

#include #define n 7

using namespace std; int main() {

int i,j; double tmp; double a[n];

for (i=0;i> a[i];

for(j=n-1;j>0;j--) for(i=0;i

if (a[i]

tmp=a[i+1];a[i+1]=a[i];a[i]=tmp; } }

for (i=0;i

程序执行结果(拷屏):

源程序文件名: 第一天第一题.cpp

1.利用梯形法计算?sinxdx,?cosxdx,?exdx。

0?10113程序编制要点(知识点、程序框图):

开始 定义变量 用梯形法计算 N 满足精度 Y 输出结果 结束

源程序代码:

1?0sinxdx的源程序

#include #include

using namespace std;

double f(double x) {

return sin (x); }

int main() {

double a,b,h,k,l,s; int i;

long n=10000;

cout << \请输入要积分sin的上下限\ cin >> a >> b;

if (a>b)

}

k=a,a=b,b=k; h=(b-a)/n;

for (i=1,s=f(a)+f(b);i

cout << l << endl; return 0;

?1?1cosxdx的源程序

#include #include

using namespace std; #define e 1e-6

#define f(x) (cos(x)) int main() {

int i,n,a,b,k; double h,ll,l,g; n=1;

cout << \请输入要积分cos x的上下限\ cin >> a >> b;

if (a>b) k=a,a=b,b=k; //判断输入的数值是否是上下限 h=(double)(b-a)/2; l=h*(f(a)+f(b)); do {

ll=l; g=0;

for (i=1;i<=n;i++)

g+=f((a+(2*i-1)*h)); l=(ll/2)+(h*g); n*=2; h/=2; }

while (fabs(l-ll)>e); cout<

return 0; }

?30edx的源程序

x#include

#include

using namespace std; #define e 1e-6

#define f(x) (exp(x)) //定义f(x) int main() {

int i,n,a,b,k; double h,ll,l,g;

cout << \请输入要积分exp x的上下限\ cin >> a >> b;

if (a>b) k=a,a=b,b=k; //判断输入的数值是否是上下限 n=1;

h=(double)(b-a)/2; l=h*(f(a)+f(b)); do {

ll=l; g=0;

for (i=1;i<=n;i++)

g+=f((a+(2*i-1)*h)); l=(ll/2)+(h*g); n*=2; h/=2; }

while (fabs(l-ll)>e); //判断是否满足误差限e cout<

程序执行结果(拷屏):

源程序文件名:

第一天第二题1.cpp,第一天第二题2.cpp,第一天第二题3.cpp 2.将一个mxn的整型矩阵转置。 程序编制要点(知识点、程序框图):

开始 输入数组元素 输出原矩阵 按列输出即为转置矩阵 结束

源程序代码:

#include using namespace std;

class A {

public:

int **a; void input(); void output(); void swap(); int m,n; private:

int b[100][100]; };

void A::input() {

cout<<\矩阵的转置\

cout<<\ cout << \输入m n:\ cin>>m>>n;

a=new int*[m]; for(int i=0;i

a[i]=new int[n];

cout<<\输入\矩阵:\ for( i=0; i

for(int j=0; j> a[i][j];

cout<<\ cout<<\输入矩阵A为:\}

void A::output() {

for(int i=0;i

for(int j=0;j

cout<

cout<

cout<<\}

void A::swap() {

cout<<\转置矩阵B为:\ for(int i=0;i

for(int j=0;j

b[j][i]=a[i][j]; }

cout<

for( i=0;i

for(int j=0;j

void main() {

A a; a.input(); a.output(); a.swap(); }

程序执行结果(拷屏):

源程序文件名: 第一天第三题.cpp

3.用二分法(对分法)求方程f(x)=x3-2x-5=0在[2, 3]内的根的近似值,绝对误差要求小于0.001。(x=2.0945515) 程序编制要点(知识点、程序框图):

开始 定义变量 确定循环次数k=(log(b-a)-log(r))*1.0/log(2) c=(a+b)/2 i=0 N i

源程序代码:

#include #include #include using namespace std;

double f(double x,double a,double b,double c,double d) {

double y;

y=a*x*x*x+b*x*x+c*x+d; return y; }

int main() {

double x1,x2,f1,f2,z,a,b,c,d,k;

cout<<\从高到低输入方程的系数\ cin>>a>>b>>c>>d;

cout<<\输入隔根区间的端点x1,x2:\ cin>>x1>>x2;

f1=f(x1,a,b,c,d); f2=f(x2,a,b,c,d);

cout<<\输入精度:\ cin>>k;

if (f1*f2>0)

cout<<\隔根区间内没有根或不止一个根\ else {

while (fabs(x1-x2)>=k) {

if(f(x1,a,b,c,d)*f((x1+x2)/2,a,b,c,d)>0) x1=(x1+x2)/2; else

x2=(x1+x2)/2; } }

z=(x1+x2)/2;

cout<<\方程的根为\ return 0; }

程序执行结果(拷屏):

源程序文件名: 第二天第一题.cpp

4.用迭代法解线性方程组(参考答案:x1=1,x2=2,x3=1)

11x1 -3x2 -2x3 =3 -23x1 +11x2+x3 =0 x1 -2x2 +2x3=-1

程序编制要点(知识点、程序框图):

开始 定义变量 x11=3.0/11+3.0/11*x2+2.0/11*x3; x21=23.0/11*x1-1.0/11*x3; x31=-0.5*x1+x2-0.5; fabs(x11-x1)>10e-10||fabs(x21-x2)>10e-10||fabs(x31-x3)>10N 输出结果 结束 源程序代码:

#include using namespace std;

int main() {

double x[3]={1,1,1},y,m=1; int i;

for (;m>=0.0000001;) {

y=x[0];

x[0]=(3+3*x[1]+2*x[2])/11;

Y

x[1]=(0+23*x[0]-x[2])/11; x[2]=(-1-x[0]+2*x[1])/2; if ((y-x[0])>0) m=y-x[0]; else m=x[0]-y; }

for (i=0;i<3;i++) {

cout << \ }

return 0; }

程序执行结果(拷屏):

源程序文件名: 第二天第二题.cpp

5.输入某年某月某日,指出这一天是这一年的第几天。 程序编制要点(知识点、程序框图):

开始 输入年月日 判断是否为闰年 按月选择计算 输出结果 结束

源程序代码:

#include

int ping[] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; int run[] = {0,31,29,31,30,31,30,31,31,30,31,30,31};

bool f(int year) {

if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0) return true; return false; }

int main() {

int year,month,day,i,j;

while(~scanf(\ {

if((year == 0 || month == 0 || day == 0) || (month == 2 && day>29) ||

(run[month]

(!(year%4 == 0 && year0 != 0 ||year@0 == 0)&&month == 2 &&

day == 29) ) {

printf(\ continue; }

if(f(year))

for(j = 0,i = 1;i

for(j = 0,i = 1;i

printf(\ }

return 0; }

程序执行结果(拷屏):

源程序文件名: 第二天第三题.cpp

6.用最小二乘法求解方程组的近似解

2x+4y =11 3x-5y=3 x+2=6 4x+2y=14

程序编制要点(知识点、程序框图):

开始 输入矩阵形式的各项系数 运用最小二乘法得到正则方程 用雅可比迭代法解正则方程组 输出方程组的解 结束 源程序代码:

#include #include using namespace std;

int main () {

start:

int m,n,i,j,k;

double sum ,cha,cha1,jing=0.000001; char ch;

cout << \输入方程个数\ cin >> m;

cout << \输入未知数个数\ cin >> n;

double * a = new double [m*n];

double * at = new double [m*n]; double * ata = new double [n*n]; double * b = new double [m]; double * atb = new double [n]; double * x = new double [n];

cout << \请输入化简后系数矩阵,不包含常数项\for (i=0;i> a[i] ;

cout << \请输入化简后右侧常数项\for (i=0;i> b[i];

for (i=0;i

at[j*m+i]=a[i*n+j]; }

for (i=0;i

for (k=0,sum=0;k

sum = sum + at[i*m+k] * a[k*n+j]; ata[i*n+j] = sum; }

for (i=0;i

for (j=0,sum=0;j

sum = sum + at[i*m+j] * b [j]; atb[i] = sum; }

for (i=0;i

x[i]=1; }

for (cha=1;cha>jing;) {

cha1= x[0];

for (i=0;i

for (j=0,sum=0;j

if (i==j) continue;

sum = sum + ata [i*n+j]*x[j]; }

x[i]=(atb[i]-sum)/ata[i*n+i]; }

if (cha1 > x[0]) cha = cha1 -x[0]; else cha = x[0] - cha1; }

if (n==2) {

cout << \ } else {

for (i=0;i

cout << \ } }

delete []a; delete []at; delete []ata; delete []atb; delete []b; delete []x;

cout << \是否继续?y/n\ cin >> ch;

if (ch=='y') goto start; return 0; }

程序执行结果(拷屏):

源程序文件名: 最小二乘法.cpp

7.有n个人围成一圈,顺序排号。从第一个人开始报数(从1到m报数),凡报到m的人退出圈子,问最后留下的是原来第几号。 程序编制要点(知识点、程序框图):

开始 输入总人数和报的数n 当报到n时,数组元素位赋0值 检索非零数即可 输出结果 结束

源程序代码:

#include

#include

#define MAX 100 main() {

int a[MAX]; int m,n;

int i,k,length;

printf(\请输入n和m(n>m),中间以空格结束!\\n当只剩余一个人时候,自动结束,例如输入 2 1;不会把2个人都删除了.\\n当删除了第一个人之后,则先结束程序,于是输出2\\n\ scanf(\

if(n>=MAX||n

printf(\您的输入不符合要求!\\n\ return; }

for(i=1;i<=n;i++)//开始赋值 初始化 {

a[i]=i; }

length=n;//初始化 i=1; k=1;

while(1) {

if(a[i]>0) {

if(length==1) {

break; }

if(k>=m) {

a[i]=0; k=1;

length--; }

else//k

k++; }

} i++;

i=i>n?1:i; }

printf(\最后剩余的下标为%d\\n\ }

程序执行结果(拷屏):

源程序文件名: 8报数.cpp

8.构建一个基本的学生成绩信息档案管理系统,学生信息包括:学号、姓名、性别、三门课程成绩及三门课程平均成绩。要求:(I)打印出每位学生三门课的平均成绩和总平均成绩及;(II) 各门课程所有学生的平均成绩。(提示:考虑使用结构体)

程序编制要点(知识点、程序框图):

开始 输入学生信息 对数据进行求均值处理 输出结果 结束

源程序代码:

#include #include

using namespace std; struct Student {

string name[100]; char sex[100]; int num[100];

double score[100][3]; double avg[100]; double sum_avg; double Avg[3]; };

int main() {

Student stu; int i,j,n;

double sum=0,Avg_sum=0; cout<<\输入学生人数:\ cin>>n;

for (i=0;i

cout<<\输入学生\学号、姓名、性别,三门课的成绩信息:\

cin>>stu.num[i]>>stu.name[i]>>stu.sex; for(j=0;j<3;j++)

cin>>stu.score[i][j]; }

for (i=0;i

stu.avg[i]=(stu.score[i][0]+stu.score[i][1]+stu.score[i][2])/3; }

for (i=0;i

sum=sum+stu.avg[i]; }

stu.sum_avg=sum/n; for (i=0;i<3;i++) {

for (j=0;j

Avg_sum=Avg_sum+stu.score[j][i]; }

stu.Avg[i]=Avg_sum/n; Avg_sum=0; }

cout<<\每位学生的三门课的平均成绩\ for (i=0;i

cout<<\输出学生\的三门课平均成绩:\ }

cout<<\总平均成绩\ cout<

cout<<\各门课程所有学生的平均成绩\ for (i=0;i<3;i++) {

cout<<\输出课程\所有学生的平均成绩:\ }

return 0; }

程序执行结果(拷屏):

源程序文件名: 教务系统.cpp

10. 用欧拉方法与改进的欧拉方法求初值问题

2x?dy??23y ?dx??y(0)?1在区间[0,1]上取步长h=0.1的数值解。要求:显示各x值下(0、0.1、0.2… 0.9、

1)两种方法计算的y值。

程序编制要点(知识点、程序框图):

开始 输入区间 欧拉公式 梯形欧拉预估校正公式 输出结果 结束

源程序代码:

#include using namespace std;

float f(float x, float y) {

float z;

z=2.0/3*x/(y*y); return (z); }

int main() {

float y1,y2,x1,x3,y3,y4; float h=0.1; int i,a,b;

cout<<\请输入区间:\ cin>>a>>b; x1=0; y1=1;

cout<<\欧拉方法计算得到的y值:\ for(i=1;i<(b-a)/h+1;i++) {y2=y1+h*f(x1,y1);

cout<<\ y1=y2; x1=x1+h;

}

cout<<\改进的欧拉方法计算得到的y值:\ x3=0; y3=1;

for(i=1;i<(b-a)/h+1;i++)

{y4=y3+h/2*(f(x3,y3)+f(x3+h,y3+h*f(x3,y3))); cout<<\ y3=y4; x3=x3+h; }

return 0; }

程序执行结果(拷屏):

源程序文件名: 欧拉公式.cpp

本文来源:https://www.bwwdw.com/article/p9k3.html

微信扫码分享

《西北工业大学动力与能源学院上机实习报告.doc》
将本文的Word文档下载到电脑,方便收藏和打印
推荐度:
点击下载文档
下载全文
范文搜索
下载文档
Top