1.高级语言程序设计实验大纲
1、课程编号:112ZB001
2、课程名称(中、英文):高级语言程序设计(Advanced Language Programming)
3、课程总学时/实验学时:75/30
4、课程总学分/实验学分:5/2
5、实验的目的与要求:掌握高级语言程序设计的基本方法及编写、调试程序的基本技能,培养学生实践能力和创新意识以及实事求是、严格认真的科学态度,为学习后续课程打下基础。
6、主要仪器设备及台(套)数:每人一台
7、主要实验教材(指导书)及参考用书:
教材:Al Kelley,Ira Pohl.C语言教程. 英文版第4版.北京:机械工业出版社,2005
参考书目:黄维通 著,C程序设计教程,清华大学出版社,2005
8、成绩考核方式与评定:实验报告。
9、实验项目内容:
序号 |
实验项目名称 |
学时 |
内容提要 |
每套仪 器人数 |
详细内容 |
01 |
Turbo C 集成开发环境的使用 |
2 |
目的: 掌握VC++6.0集成开发环境的使用,学会编写、调试简单的C语言程序。 内容: 1、介绍VC++6.0集成开发环境; 2、使用VC++6.0集成开发环境编写、调试一个简单的C语言程序。 注:都改为VC++6.0 |
1 |
P62Exercises 11 实验报告包括VC上机的步骤 |
02 |
简单程序的设计 |
4 |
目的: 掌握C语言程序的顺序结构,初步学会输入、输出函数的使用。 内容: 1、顺序程序设计 2、输入输出函数在程序中的使用 3、#include指令的使用 |
1 |
P99 14题 a,b,c用scanf从键盘上输入: Int a,b,c; Scanf(….); |
03 |
基本数据类型 |
4 |
目的: 熟练掌握输入、输出函数的使用,掌握C语言的基本数据类型。 内容: 1、顺序程序设计 2、输入输出函数在程序中的使用 3、基本数据类型在程序中的使用 |
1 |
??看原来实验,如没有,用练习10,解释输出结果的原因 |
04 |
选择结构 |
4 |
目的: 掌握选择结构程序的设计 内容: 1、 if结构 2、 if-else结构 3、 switch结构 |
1 |
P191 24 |
05 |
循环结构 |
4 |
目的: 掌握循环结构程序设计 内容: 1、 while语句 2、 do-while语句 3、 for语句 |
1 |
P188 第13题 |
06 |
函数 |
4 |
目的: 掌握函数的定义,函数的原型及函数的引用 内容: 1、 库函数的使用 2、 自定义函数的定义及引用 3、 函数原型 |
1 |
P38 第11题 |
07 |
数组、指针和字符串 |
4 |
目的: 掌握数组、指针和字符串的定义及引用 内容: 1、 数组的定义和引用 2、 指针的定义和引用 3、 字符串的定义和引用 |
1 |
P317 20 |
08 |
结构体与联合体 |
2 |
目的: 掌握结构体与联合体的定义及引用 内容: 1、 结构体的定义和引用 2、 联合体的定义和引用 |
1 |
P443 24 报告中写出解释 |
09 |
C程序的简单应用 |
2 |
目的: 学会使用C程序进行简单的程序开发 内容: 编写一个简单的游戏程序 |
1 |
|
C程序的简单应用综合性实验教学大纲
实验名称:C程序的简单应用
实验类型:综合性实验
实验类别: 基础
实验学时:2
所涉及的课程及知识点:
计算机基础、C语言程序设计
一、实验目的
1、调动学生学习的积极性和主动性。
2、提高学生发现问题,分析问题和解决问题的能力。
3、加深学生对C语言编程的认识,强化对C语言程序设计方法的掌握。
二、实验内容
1、使用C语言编写一个简单的游戏程序。或者其它综合性程序
三、实验仪器设备和材料清单
计算机硬件要求:CPU600MHz Pentinum以上,内存:256MB以上50台
软件要求:windows2000操作系统、Visual C++6.0
四、实验要求
1、实验前要有自己的程序设计流程图等。
2、实验中按要求应用C语言进行编程和调试。
3、总结实验过程中的实验成果和存在的问题并完成实验报告。
4、提交实验报告及所设计流程图和程序。
五、实验步骤及结果测试
1、设计程序流程图
2、编写程序代码
3、调试程序
4、程序测试
六、考核形式
流程图(30%)、程序(50%)以及程序调试(20%)综合考核
七、实验报告要求
按照程序设计要求提交流程图的设计、程序清单和小结。
八、思考题
1.在实验中运用到了本课程的哪些主要知识和技能?调试中遇到的主要问题和解决的方法是什么?
参考:
/*
学生成绩管理程序
编制一个统计学生考试分数的管理程序。
设学生成绩已以一个学生一个记录的形式存储在文件中,
每位学生记录包含的信息有:姓名,学号和各门功课的成绩。
程序具有以下几项功能:求出各门课程的总分,平均分,按姓名,
按学号寻找其记录并显示,浏览全部学生成绩和按总分由高到低显示学生信息等。
*/
#include <stdio.h>
#define SWN 3 /* 课程数*/
#define NAMELEN 20 /* 姓名最大字符数*/
#define CODELEN 10 /* 学号最大字符数*/
#define FNAMELEN 80 /* 文件名最大字符数*/
#define BUFLEN 80 /* 缓冲区最大字符数*/
/* 课程名称表*/
char schoolwork[SWN][NAMELEN+1] = {"Chinese","Mathematic","English"};
struct record
{
char name[NAMELEN+1]; /* 姓名*/
char code[CODELEN+1]; /* 学号*/
int marks[SWN]; /* 各课程成绩*/
int total; /* 总分*/
}stu;
struct node
{
char name[NAMELEN+1]; /* 姓名*/
char code[CODELEN+1]; /* 学号*/
int marks[SWN]; /* 各课程成绩*/
int total; /* 总分*/
struct node *next; /* 后续表元指针*/
}*head; /* 链表首指针*/
int total[SWN]; /* 各课程总分*/
FILE *stfpt; /* 文件指针*/
char stuf[FNAMELEN]; /* 文件名*/
/* 从指定文件读入一个记录*/
int readrecord(FILE *fpt,struct record *rpt)
{
char buf[BUFLEN];
int i;
if(fscanf(fpt,"%s",buf)!=1)
return 0; /* 文件结束*/
strncpy(rpt->name,buf,NAMELEN);
fscanf(fpt,"%s",buf);
strncpy(rpt->code,buf,CODELEN);
for(i=0;i<SWN;i++)
fscanf(fpt,"%d",&rpt->marks[i]);
for(rpt->total=0,i=0;i<SWN;i++)
rpt->total+=rpt->marks[i];
return 1;
}
/* 对指定文件写入一个记录*/
writerecord(FILE *fpt,struct record *rpt)
{
int i;
fprintf(fpt,"%s\n",rpt->name);
fprintf(fpt,"%s\n",rpt->code);
for(i=0;i<SWN;i++)
fprintf(fpt,"%d\n",rpt->marks[i]);
return ;
}
/* 显示学生记录*/
displaystu(struct record *rpt)
{
int i;
printf("\nName : %s\n",rpt->name);
printf("Code : %s\n",rpt->code);
printf("Marks :\n");
for(i=0;i<SWN;i++)
printf(" %-15s : %4d\n",schoolwork[i],rpt->marks[i]);
printf("Total : %4d\n",rpt->total);
}
/* 计算各单科总分*/
int totalmark(char *fname)
{
FILE *fp;
struct record s;
int count,i;
if((fp=fopen(fname,"r"))==NULL)
{
printf("Can't open file %s.\n",fname);
return 0;
}
for(i=0;i<SWN;i++)
total[i]=0;
count=0;
while(readrecord(fp,&s)!=0)
{
for(i=0;i<SWN;i++)
total[i]+=s.marks[i];
count++;
}
fclose(fp);
return count; /* 返回记录数*/
}
/* 列表显示学生信息*/
void liststu(char *fname)
{
FILE *fp;
struct record s;
if((fp=fopen(fname,"r"))==NULL)
{
printf("Can't open file %s.\n",fname);
return ;
}
while(readrecord(fp,&s)!=0)
{
displaystu(&s);
printf("\n Press ENTER to continue...\n");
while(getchar()!='\n');
}
fclose(fp);
return;
}
/* 构造链表*/
struct node *makelist(char *fname)
{
FILE *fp;
struct record s;
struct node *p,*u,*v,*h;
int i;
if((fp=fopen(fname,"r"))==NULL)
{
printf("Can't open file %s.\n",fname);
return NULL;
}
h=NULL;
p=(struct node *)malloc(sizeof(struct node));
while(readrecord(fp,(struct record *)p)!=0)
{
v=h;
while(v&&p->total<=v->total)
{
u=v;
v=v->next;
}
if(v==h)
h=p;
else
u->next=p;
p->next=v;
p=(struct node *)malloc(sizeof(struct node));
}
free(p);
fclose(fp);
return h;
}
/* 顺序显示链表各表元*/
void displaylist(struct node *h)
{
while(h!=NULL)
{
displaystu((struct record *)h);
printf("\n Press ENTER to continue...\n");
while(getchar()!='\n');
h=h->next;
}
return;
}
/* 按学生姓名查找学生记录*/
int retrievebyn(char *fname, char *key)
{
FILE *fp;
int c;
struct record s;
if((fp=fopen(fname,"r"))==NULL)
{
printf("Can't open file %s.\n",fname);
return 0;
}
c=0;
while(readrecord(fp,&s)!=0)
{
if(strcmp(s.name,key)==0)
{
displaystu(&s);
c++;
}
}
fclose(fp);
if(c==0)
printf("The student %s is not in the file %s.\n",key,fname);
return 1;
}
/* 按学生学号查找学生记录*/
int retrievebyc(char *fname, char *key)
{
FILE *fp;
int c;
struct record s;
if((fp=fopen(fname,"r"))==NULL)
{
printf("Can't open file %s.\n",fname);
return 0;
}
c=0;
while(readrecord(fp,&s)!=0)
{
if(strcmp(s.code,key)==0)
{
displaystu(&s);
c++;
break;
}
}
fclose(fp);
if(c==0)
printf("The student %s is not in the file %s.\n",key,fname);
return 1;
}
main()
{
int i,j,n;
char c;
char buf[BUFLEN];
FILE *fp;
struct record s;
clrscr();
printf("Please input the students marks record file's name: ");
scanf("%s",stuf);
if((fp=fopen(stuf,"r"))==NULL)
{
printf("The file %s doesn't exit, do you want to creat it? (Y/N) ",stuf);
getchar();
c=getchar();
if(c=='Y'||c=='y')
{
fp=fopen(stuf,"w");
printf("Please input the record number you want to write to the file: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Input the student's name: ");
scanf("%s",&s.name);
printf("Input the student's code: ");
scanf("%s",&s.code);
for(j=0;j<SWN;j++)
{
printf("Input the %s mark: ",schoolwork[j]);
scanf("%d",&s.marks[j]);
}
writerecord(fp,&s);
}
fclose(fp);
}
}
fclose(fp);
getchar();
/*clrscr();*/
puts("Now you can input a command to manage the records.");
puts("m : mean of the marks.");
puts("t : total of the marks.");
puts("n : search record by student's name.");
puts("c : search record by student's code.");
puts("l : list all the records.");
puts("s : sort and list the records by the total.");
puts("q : quit!");
while(1)
{
puts("Please input command:");
scanf(" %c",&c); /* 输入选择命令*/
if(c=='q'||c=='Q')
{
puts("\n Thank you for your using.");
break; /* q,结束程序运行*/
}
switch(c)
{
case 'm': /* 计算平均分*/
case 'M':
if((n=totalmark(stuf))==0)
{
puts("Error!");
break;
}
printf("\n");
for(i=0;i<SWN;i++)
printf("%-15s's average is: %.2f.\n",schoolwork[i],(float)total[i]/n);
break;
case 't': /* 计算总分*/
case 'T':
if((n=totalmark(stuf))==0)
{
puts("Error!");
break;
}
printf("\n");
for(i=0;i<SWN;i++)
printf("%-15s's total mark is: %d.\n",schoolwork[i],total[i]);
break;
case 'n': /* 按学生的姓名寻找记录*/
case 'N':
printf("Please input the student's name you want to search: ");
scanf("%s",buf);
retrievebyn(stuf,buf);
break;
case 'c': /* 按学生的学号寻找记录*/
case 'C':
printf("Please input the student's code you want to search: ");
scanf("%s",buf);
retrievebyc(stuf,buf);
break;
case 'l': /* 列出所有学生记录*/
case 'L':
liststu(stuf);
break;
case 's': /* 按总分从高到低排列显示*/
case 'S':
if((head=makelist(stuf))!=NULL)
displaylist(head);
break;
default: break;
}
}
}
9.面向对象程序设计实验大纲
1、课程编号:112ZX
2、课程名称(中、英文):面向对象程序设计(Object-Oriented Programming)
3、课程总学时/实验学时:48/16
4、课程总学分/实验学分:4/1
5、实验的目的与要求:掌握面向对象程序设计的基本方法及编写、调试程序的基本技能,培养学生面向对象设计的思维方式和严格认真的科学态度,为学习后续课程和进行项目开发打下基础。
6、主要仪器设备及台(套)数:计算机1台/人
7、主要实验教材(指导书)及参考用书:
教材:Richard Johnsonbaugh, Martin Kalin. C++面向对象程序设计. 第2版. 清华大学出版社. 2005年.
参考书目:Bjarne Stroustrup. C++程序设计语言(特别版)第一版. 机械工业出版社 2002年
8、成绩考核方式与评定:实验报告。
9、实验项目内容:
序号 |
实验项目名称 |
学时 |
内容提要 |
每套仪 器人数 |
详细内容 |
01 |
Visual C++ 集成开发环境的使用 |
2 |
目的: 掌握Visual C++集成开发环境的使用,学会编写、调试简单的C语言程序。 内容: 1、介绍Visual C++集成开发环境; 2、使用Visual C++集成开发环境编写、调试一个简单的C++程序。 |
1 |
以P22 四。2为例 |
02 |
文件 |
2 |
目的: 掌握文件的读写 内容: 1、编写一个C++程序进行文件的读写 |
1 |
P364 9.19 9.20 把两个主函数合成一个写 |
03 |
类 |
4 |
目的: 1、熟练掌握类的声明、及实现的方法 2、掌握对象声明及引用 3、掌握方法的引用 内容: 1、 创建一个应用类并给出类的实现 2、 对类进行测试 |
1 |
P199 第四题 复数类Complex |
04 |
继承 |
4 |
目的: 掌握类的继承结构及方法 内容: 1、创建一个类的层次结构 2、对该结构进行测试 |
1 |
定义一个基类Person类,以及两个派生类:学生类Student和教师类Teacher并简单应用(注1) |
05 |
多态 |
2 |
目的: 掌握类层次结构的多态机制 内容: 1、创建一个类的层次结构 2、设置virtual虚函数 3、测试运行时捆绑与编译时捆绑的不同效果 |
1 |
P322 但是改用用梯形公式计算积分() |
06 |
简单应用 |
2 |
目的: 掌握使用C++进行应用程序开发的基本方法 内容: 使用C++开发一个应用程序。 |
1 |
矩阵类Matrix (注2) |
简单应用综合性实验教学大纲
实验名称:简单应用
实验类型:综合性实验
实验类别: 基础
实验学时:2
所涉及的课程及知识点:
计算机基础、C++程序设计
一、实验目的
1、调动学生学习的积极性和主动性。
2、提高学生发现问题,分析问题和解决问题的能力。
3、加深学生对C++编程的认识,强化对C++程序设计方法的掌握。
二、实验内容
1、使用C++开发一个应用程序。
三、实验仪器设备和材料清单
计算机硬件要求:CPU600MHz Pentinum以上,内存:256MB以上50台
软件要求:windows2000操作系统、Visual C++6.0
四、实验要求
1、实验前要有自己的程序设计流程图等。
2、实验中按要求应用C++语言进行编程和调试。
3、总结实验过程中的实验成果和存在的问题并完成实验报告。
4、提交实验报告及所设计流程图和程序。
五、实验步骤及结果测试
1、设计程序流程图
2、编写程序代码
3、调试程序
4、程序测试
六、考核形式
流程图(30%)、程序(50%)以及程序调试(20%)综合考核
七、实验报告要求
按照程序设计要求提交流程图的设计、程序清单和小结。
八、思考题
1.在实验中运用到了本课程的哪些主要知识和技能?调试中遇到的主要问题和解决的方法是什么?
注1:person 类-teacher类-student类
请按下列要求编程:
编写一个教师相关信息输入和显示程序,教师信息有姓名、性别、年龄、职工号、职称和系别。为此,设计两个类:
(1)设计person 类,该类有两个构造函数,其中一个为缺省构造函数,一个为带参数的构造函数,用于对对象进行初始化;另有两个公有成员函数,分别为:输入函数void input ( ),用于从键盘输入姓名、性别和年龄;输出函数 void display ( ),用于显示输出姓名、性别和年龄。
(2)设计teacher类,该类是person类的派生类,它有两个构造函数,其中一个为缺省构造函数,一个为带参数的构造函数,用于对对象进行初始化;另有两个公有成员函数,分别为:输入函数void input ( ),用于从键盘输入教师的职工号、职称和系别,该函数中应包含对person类input ( ) 函数的调用;输出函数 void display ( ),用于显示输出教师的职工号、职称和系别,该函数中应包含对person类display ( ) 函数的调用。
在主函数中,定义两个教师,并对其中一个的信息进行初始化,另一个从键盘上输入相关的信息,并将两个教师的信息输出显示在屏幕上。
(3)设计student类,该类是person类的派生类,它有两个构造函数,其中一个为缺省构造函数,一个为带参数的构造函数,用于对对象进行初始化;另有两个公有成员函数,分别为:输入函数void input ( ),用于从键盘输入学生的学号、班级和成绩,该函数中应包含对person类input ( ) 函数的调用;输出函数 void display ( ),用于显示输出学生的学号、班级和成绩,该函数中应包含对person类display ( ) 函数的调用。
在主函数中,定义两个学生,并对其中一个的信息进行初始化,另一个从键盘上输入相关信息,并将两个学生的信息输出显示在屏幕上。
#include <string.h>
//person类
class TPerson
{
public:
TPerson(){strcpy(m_name,"");strcpy(m_sex,"");m_age=0;}
TPerson(char *name,char *sex;int age)
{
strcpy(m_name,name);strcpy(m_sex,sex);m_age=age;
}
void Input();
void Display();
private:
char m_name[20];
char m_sex[2];
int m_age;
};
void TPerson::Input()
{
cout<<"姓名:";
cin>>m_name;
cout<<"性别(女,男):";
cin>>m_sex;
cout<<"年龄:";
cin>>m_age;
}
void TPerson::Display()
{
cout<<"姓名:"<<m_name<<", 性别:"<<m_sex<<", 年龄:"<<m_age<<endl;
}
//students类
#include <iostream.h>
#include "tperson.cpp"
class TTeacher :public TPerson
{
public:
TTeacher(){m_teacherID=0;strcpy(m_post,"");strcpy(m_department,"");}
TTeacher(char *name,char *sex;int age,int teacherID,char *post,char department):TPerson(name,sex;age)
{
m_teacherID=teacherID;strcpy(m_post,post);strcpy(m_department,department);
}
void Input();
void Display();
private:
int m_teacherID;
char m_post[20];
char m_department[20];
};
void TTeacher::Input()
{
TPerson::Input();
cout<<"职工号:";
cin>>m_teacherID;
cout<<"职称:";
cin>>m_post;
cout<<"系别:";
cin>>m_department;
}
void TTeacher::Display()
{
TPerson::Display();
cout<<"职工号:"<<tm_eacherID<<", 职称:"<<m_post<<", 系别:"<<dm_epartment<<endl;
}
//students类
class TStudent :public TPerson
{
public:
TStudent(){m_teacherID=0;strcpy(m_post,"");strcpy(m_department,"");}
TStudent (char *name,char *sex;int age, char *stuID, char *classID, double sum):TPerson(name,sex;age)
{
strcpy(m_stuID, stuID);
strcpy(m_ classID, classID); m_sum=sum;
}
void Input();
void Display();
private:
char m_stuID[10];
char m_classID[20];
double m_sum;
};
void TStudent::Input()
{
TPerson::Input();
cout<<"学号:";
cin>>m_stuID;
cout<<"班级:";
cin>> m_classID;
cout<<"总分:";
cin>> m_sum;
}
void TStudent::Display()
{
TPerson::Display();
cout<<"学号:"<< m_stuID<<", 班级:"<< m_classID<<", 总分:"<< m_sum<<endl;
}
void main
{
TTeacher t1,t2(“小王”,”男”,20,1001,”讲师”,”数计系”);
t1.Input();
t1.Display();
t2. Display();
TStudent s1,s2(“小王”,”男”,20,”200301”,”03计本1”,500);
s1.Input();
s1.Display();
s2. Display();
}
注2:矩阵类Matrix:
矩阵编程说明书
目 录
1、矩阵的构造和析构
2、获取矩阵信息的基本操作
3、其它基本操作
4、取子矩阵
5、矩阵的运算
6、矩阵的显示
7、求解线性方程组
8、其它
*************************************************
有关文件:有关矩阵的程序代码主要位于matrix.h和matrix.cpp两个文件中。
有关类名:TMatrix(矩阵类)。
数据结构:
TAtom ** val; 指针数组,用来表示矩阵中的元素。
int v_dim, h_dim; 整型变量,分别用来表示矩阵的行数和列数。
1、矩阵的构造和析构
TMatrix(int v=1,int h=1);
TMatrix的构造函数,缺省情况下构造一个1行1列的矩阵;
TMatrix(int v,int h,TList * l);
TMatrix的构造函数,用列表中的元素来构造一个v行h列的矩阵;
TMatrix(TMatrix * tm);
TMatrix的构造函数(复制构造函数),用一个矩阵来构造另外一个矩阵;
TMatrix(TList* list,int direction);
TMatrix的构造函数,用一个列表来构造一个1维的矩阵,direction用来表示矩阵的方向;
~TMatrix();
TMatrix的析构函数;
2、获取矩阵信息的基本操作
TAtom * GetVal(int i,int j);
取得该矩阵第i行第j列的元素,操作之后这个元素本身也会变化;
TAtom * CopyVal(int i,int j);
复制该矩阵第i行第j列的元素,操作之后这个元素本身不会变化;
void SetVal(int i,int j,TAtom * value);
把value这个元素赋值到该矩阵第i行第j列;
int VSize() const;
求该矩阵的行数;
int HSize() const;
求该矩阵的列数;
int rowdim();
求该矩阵的行数;
int coldim();
求该矩阵的列数;
3、其它基本操作
复制:
TAtom * Copy();
复制该矩阵;
TAtom * Clone();
克隆该矩阵;
赋值:
TAtom *eval();
对矩阵进行赋值;
TMatrix* operator=(TMatrix* m);
矩阵的赋值运算符;
转置:
TMatrix* Tran();
说明:求该矩阵的转置;
行列式:
TAtom* det();
求该矩阵的行列式;
删除矩阵中的行/列:
TMatrix* delrow(int v1);
删除该矩阵的第v1行;
TMatrix* delcol(int h1);
删除该矩阵的第h1列;
交换矩阵中的行/列:
void swaprow(int v1,int v2);
把该矩阵的第v1行和v2行进行交换;
void swapcol(int h1,int h2);
把该矩阵的第h1列和h2列进行交换;
比较:
int SameSize(TMatrix *m);
判断两个矩阵的大小(即行数和列数)是否相等;
矩阵转化为列表:
TList * TranList();
把一个矩阵转化成为一个列表;
4、取子矩阵
TMatrix* row(int v1);
说明:取该矩阵的第vl行,组成一个行数为1的子矩阵;
TMatrix* row(int v1,int v2);
说明:取该矩阵的第vl行与第v2行之间的部分,组成一个行数为v2-v1的子矩阵;
TMatrix* col(int h1);
取该矩阵的第hl列,组成一个列数为1的子矩阵;
TMatrix* col(int h1,int h2);
取该矩阵的第hl列与第h2列之间的部分,组成一个列数为h2-h1的子矩阵;
TMatrix* SubMatrix(int v1,int v2,int h1,int h2);
求该矩阵的v1行到v2行,h1列到h2列之间的子矩阵,该矩阵的行数为v2-v1,列数为h2-h1;
TMatrix* SubMatrix(TList* Vlist,TList* Hlist);
求该矩阵的Vlist中的元素所组成的行,Hlist中的元素所组成的列所交叉的元素所组成的子矩阵,该子矩阵的行数为Vlist中元素的个数,列数为Hlist中元素的个数;
TMatrix* SubMatrix(TList* Vlist,int h1,int h2);
求该矩阵的Vlist中的元素所组成的行与h1列到h2列所交叉的元素所组成的子矩阵,该子矩阵的行数为Vlist中元素的个数,列数为h2-h1;
TMatrix* SubMatrix(int v1,int v2,TList* Hlist);
求该矩阵的v1行到v2行与Hlist中的元素所组成的列所交叉的元素所组成的子矩阵,该子矩阵的行数为v2-v1,列数为Vlist中元素的个数;
5、矩阵的运算
TMatrix* add(TMatrix *m);
说明:两个矩阵进行加法操作;
TMatrix* minus(TMatrix *m);
说明:两个矩阵进行减法操作;
TMatrix* mul(TMatrix *m);
说明:两个矩阵进行乘法操作;
TMatrix* uminus();
说明:矩阵的取负操作;
TMatrix* exp(int p);
说明:求该矩阵的p次方;
TMatrix* mdiff(int n = 1,TVariable * v = 0);
说明:对矩阵中的每一个元素都进行微分;
6、矩阵的显示
virtual void display(outtype& out =wout) const;
显示这个矩阵;
virtual void show(outtype& out =wout) const;
显示这个矩阵;
virtual void print(outtype& out =wout) const;
显示这个矩阵;
7、求解线性方程组
void lsolve_m();
求以该矩阵为增广矩阵的线性方程组的根;
TList* lsolve(TList * ps,TList * var);
求解以多项式列表ps和变量列表var所定义的线性方程组的根。
8、其它
virtual TRunType Run();
一个虚函数,为了保持和父类的兼容;
virtual DataType TypeID()const;
求Tmatrix的数据类型;
void* operator new(size_t);
为TMartix所重载的new函数;
void operator delete(void *);
为TMartix所重载的delete函数;
一个学生的Matrix例子:
#include <stdafx.h>
#include <iostream.h>
#include <stdlib.h>
class Matrix
{
private:
int nrows;//行数
int ncols;//列数
double **ets;//矩阵元素
void error(char *);//出错提示
public:
Matrix(int n,int m,double **);//构造函数(n乘m)
Matrix(int n,int m,double d=0);//所有元素等于
Matrix(const Matrix &);//拷贝构造函数
~Matrix();//析构函数
Matrix& operator=(const Matrix &);//重载=
Matrix& operator+=(const Matrix &);//重载+=
Matrix& operator-=(const Matrix &);//重载-=
double *operator[](int i) const {return ets[i];}
//下标,i行j列的元素是[i][j]
double &operator()(int i,int j){return ets[i][j];}
Matrix& operator+() ;//一元+,m1=+m2
Matrix operator+(const Matrix&);////二元+,m=m1+m2
Matrix& operator-() ;//一元-,m1=-m2
Matrix operator-(const Matrix&);////二元-,m=m1-m2
};
void Matrix::error(char *v)
{
cout<<v<<".program exited\n";//iostream.h
exit(1);//stdlib.h
}
Matrix::Matrix(int n,int m,double **dbp)//构造函数(n乘m)
{
nrows=n;
ncols=m;
ets=new double *[nrows];
for(int i=0 ;i<nrows;i++)
{
ets[i]=new double[ncols];
for(int j=0 ;j<ncols;j++)
ets[i][j]=dbp[i][j];
}
}
Matrix::Matrix(int n,int m,double d)//所有元素等于
{
nrows=n;
ncols=m;
ets=new double *[nrows];
for(int i=0 ;i<nrows;i++)
{
ets[i]=new double[ncols];
for(int j=0 ;j<ncols;j++)
ets[i][j]=d;
}
}
Matrix::Matrix(const Matrix &mat)//拷贝构造函数
{
nrows=mat.ncols;
ncols=mat.nrows;
ets=new double *[nrows];
for(int i=0 ;i<nrows;i++)
{
ets[i]=new double[ncols];
for(int j=0 ;j<ncols;j++)
ets[i][j]=mat[i][j];
}
}
Matrix::~Matrix()//析构函数
{
for(int i=0 ;i<nrows;i++)
delete[] ets[i];
delete[] ets;
}
Matrix& Matrix::operator=(const Matrix &mat)//重载=
{
if(this!=&mat)
{
if (nrows!=mat.nrows||ncols!=mat.ncols)
error("bad matrix sizes");
for(int i=0 ;i<nrows;i++)
{
for(int j=0 ;j<ncols;j++)
ets[i][j]=mat[i][j];
}
}
return *this;
}
Matrix& Matrix::operator+=(const Matrix &mat)//重载+=
{
if (nrows!=mat.nrows||ncols!=mat.ncols)
error("bad matrix sizes");
for(int i=0 ;i<nrows;i++)
{
for(int j=0 ;j<ncols;j++)
ets[i][j]+=mat[i][j];
}
return *this;
}
Matrix& Matrix::operator-=(const Matrix &mat)//重载-=
{
if (nrows!=mat.nrows||ncols!=mat.ncols)
error("bad matrix sizes");
for(int i=0 ;i<nrows;i++)
{
for(int j=0 ;j<ncols;j++)
ets[i][j]-=mat[i][j];
}
return *this;
}
Matrix& Matrix::operator+() //一元+,m1=+m2
{
return *this;
}
Matrix Matrix::operator+(const Matrix&mat)////二元+,m=m1+m2
{
if (nrows!=mat.nrows||ncols!=mat.ncols)
error("bad matrix sizes");
Matrix sum=*this;
sum+=mat;
return sum;
}
Matrix& Matrix::operator-() //一元-,m1=-m2
{
*this=Matrix(nrows,ncols)-*this;
return *this;
}
Matrix Matrix::operator-(const Matrix& mat)////二元-,m=m1-m2
{
if (nrows!=mat.nrows||ncols!=mat.ncols)
error("bad matrix sizes");
Matrix sum=*this;
sum-=mat;
return sum;
}
void main()
{
Matrix m1(5,6),m2(5,6);
for(int i=0;i<5;i++)
for(int j=0;j<6;j++)
m1(i,j)=i+j;
cout<<"m1:\n";
for( i=0;i<5;i++)
{
for(int j=0;j<6;j++)
cout<<m1(i,j);
cout<<endl;
}
cout<<"m2:\n";
for( i=0;i<5;i++)
{
for(int j=0;j<6;j++)
cout<<m2(i,j);
cout<<endl;
}
m1=m1+m2;
cout<<"m1+m2:\n";
for( i=0;i<5;i++)
{
for(int j=0;j<6;j++)
cout<<m1(i,j);
cout<<endl;
}
}