软件技术基础实验二报告

1.采用邻接表存储结构,编写一个求无向图的连通分量个数的算法。

#include<stdio.h>

#include<malloc.h>

int n;

struct VNode{               //顶点

int position;

struct VNode* next;

};

struct ArcNode{                //弧

int mark;

struct VNode* first;

};

void DFS(struct ArcNode* v,struct ArcNode* w){             //深度优先搜索

struct VNode* L;

w->mark=1;

L=w->first;

while(L!=NULL){

if((v+(L->position))->mark==0){

DFS(v,(v+L->position));               //递归调用

}

L=L->next;

}

}

int main(){

int i,j,k;

int num=0;

struct ArcNode* p;

struct VNode* temp;

struct VNode* flag;

printf("\n请输入顶点个数 n:");

scanf("%d",&n);

while(n<1){

printf("你输入的值不合理,请重新输入:\n");

scanf("%d",&n);

}

p=(struct ArcNode*)malloc(n*sizeof(struct ArcNode));

/*说明:1.Vi表示第i个顶点,它在表中的位置为i-1,如V3在表中的位置为2;

2.如果输入以V1为弧尾的所有弧(假设存在弧<V1,V3>和<V1,V2>)

  则输入:2 1 -1(只需输入弧头的位置,并用-1表示结束)*/

for(i=0;i<n;i++){           //创建无向图

printf("\n请输入以V%d为弧尾的所有弧,并以-1结束输入\n",i+1);

scanf("%d",&k);

if(k==-1){

p[i].mark=0;

p[i].first=NULL;

}

else{

temp=(struct VNode*)malloc(sizeof(struct VNode));

temp->position=k;

temp->next=NULL;

p[i].first=temp;

p[i].mark=0;

flag=temp;

scanf("%d",&k);

while(k!=-1){

temp=(struct VNode*)malloc(sizeof(struct VNode));

temp->position=k;

temp->next=NULL;

flag->next=temp;

flag=temp;

scanf("%d",&k);

}

}

}

i=0;

while(p[i].mark==0){               //计算连通分量的个数

DFS(p,(p+i));

num++;

i=0;

while(p[i].mark!=0&&i<n){

i++;

}

}

printf("此图的连通分量个数为:%d\n",num);

return 0;

}


2.试基于图的深度优先搜索策略编写一程序,判别以邻接表方式存储的有向图中是否存在有顶点Vi到Vj顶点的路径(i≠j)。

#include "stdio.h"

#include "malloc.h"

#include "conio.h"

typedef struct ArcNode{

int adjvex;//该弧指向的顶点

struct ArcNode *nextarc;//下一个弧

int in;//判断是否遍历过该顶点

}ArcNode;

typedef struct VNode{

int data;//顶点信息

ArcNode *firstarc;//指向第一条弧

}VNode,AdjList[20];

typedef struct ALGraph{

AdjList vertices;

int vexnum,arcnum;//顶点数和弧数

int kind;//图的种类标志感觉没什么用

}ALGraph;

int n,i,j;

int m=0;

int s=1;

ALGraph *graph;

void read(ArcNode *firstarc){

int x;

char y;

scanf("%c",&y);

while(y==' ')

scanf("%c",&y);

firstarc->nextarc=NULL;

if(y=='\n'){

return;

}

else{

x=(int)y-48;

ArcNode *t;

t=new ArcNode;

t->nextarc=NULL;

t->in=0;

t->adjvex=x;

firstarc->nextarc=t;

read(firstarc->nextarc);

}

}//输入该顶点出发的弧直到回车

void mkgraph(ALGraph *graph){

char rub;

ArcNode *t;

VNode *list;

scanf("%c",&rub);

for(;s<=n;s++){

printf("%d   ",s);

t=new ArcNode;

graph->vertices[s].firstarc=t;

read(graph->vertices[s].firstarc);

}

}//用循环控制输入每个顶点的信息

void run(ArcNode *firstarc){

ArcNode *t;

t=firstarc;

if(t->adjvex==j){

m=1;//若找到则m=1

return;

}

if(t->in==0&&m==0&&t->adjvex>0&&t->adjvex<=n&&graph->vertices[t->adjvex].firstarc->nextarc!=NULL){

t->in=1;

run(graph->vertices[t->adjvex].firstarc->nextarc);

}

if(m==0&&t->nextarc!=NULL)

run(t->nextarc);

}//深度优先搜索

void main(){

graph=new ALGraph;

printf("图中有几个顶点:");

scanf("%d",&n);

printf("请以邻接表方式依次输入各顶点的关系以回车作为结束标志:\n");

mkgraph(graph);

printf("已为您生成图.\n\n请输入您要寻找路径的起点i和终点j(i!=j):");

scanf("%d%d",&i,&j);

while(i==j||i<=0||i>n||j<=0||j>n){

printf("对不起,您输入的数据有误,请重新输入:");

scanf("%d%d",&i,&j);

}//控制输入的数据

run(graph->vertices[i].firstarc->nextarc);

if(m==1)

printf("存在从%d到%d的路径.",i,j);

else

printf("不存在从%d到%d的路径.",i,j);

getch();

}


3.在上述例题中,如改用邻接表的方式存储图,试编一程序实现上述算法。

顶点表nodelist的每个元素包含四个字段:

其中mark为布尔类型,用来标记顶点是否被访问过。开始时,所有元素的mark字段为false,每访问过一个顶点,则mark字段置为true。info为顶点值,pre为访问路径上该顶点的前驱顶点的序号,out指向该顶点的出边表。

#include<stdio.h>

#include<stdlib.h>

#define MAXV 100

typedef struct//邻接矩阵存储结构

{

int no;

int info;

}VertexType;

typedef struct

{

int edges[MAXV][MAXV];

int n,e;

VertexType vexs[MAXV];

}MGraph;

typedef struct ANode //邻接表存储结构

{

int adjvex;

struct ANode *nextarc;

int info;

}ArcNode;

typedef struct Vnode

{

int data;

ArcNode *firstarc;

}VNode;

typedef VNode AdjList[MAXV];

typedef struct

{

AdjList adjlist;

int n,e;

}ALGraph;

typedef struct node

{

int data;

struct node *next;

}List;

void MatToList(MGraph g,ALGraph *&G)

{

int i,j,n=g.n;

ArcNode *p;

G=(ALGraph *)malloc(sizeof(ALGraph));

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

G->adjlist[i].firstarc=NULL;

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

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

if(g.edges[i][j]!=0)

{

p=(ArcNode *)malloc(sizeof(ArcNode));

p->adjvex=j;

p->nextarc=G->adjlist[i].firstarc;

G->adjlist[i].firstarc=p;

}

G->n=n;

G->e=g.e;

}

static int s1=0;

void Find(ALGraph *G,int x,int l,int k,int visited[],int path[],int d)

{

int i,node;

ArcNode *r;

visited[x]=1;

d++;

path[d]=x;

if(x==l&&d==k)

{

s1++;

if(s1==1)

printf("\n给定的两个顶点%d,%d之间存在长度为%d的简单路径.\n所有路径为:\n",path[0],l,k);

for(i=0;i<=k;i++)

printf("%d ",path[i]);

printf("\n");

}

r=G->adjlist[x].firstarc;

while(r!=NULL)

{

node=r->adjvex;

if(visited[node]==0)

Find(G,node,l,k,visited,path,d);

r=r->nextarc;

}

visited[x]=0;

d--;

}

int main()

{

int path[MAXV],visited[MAXV],A[MAXV][MAXV],i,j,m,l,k;

MGraph g;

ALGraph *G;

printf("请输入顶点个数:");

scanf("%d",&g.n);

g.e=0;

//printf("\n请输入路径条数:");

//scanf("%d",&g.e);

printf("\n请输入邻接矩阵:(若(vi,vj)属于E(G),A[i][j]=1;其他,A[i][j]=0)\n\n" );

for(i=0;i<g.n;i++)

{

for(j=0;j<g.n;j++)

scanf("%d",&A[i][j]);

getchar();

}

for(i=0;i<g.n;i++)

for(j=0;j<g.n;j++)

{

g.edges[i][j]=A[i][j];

if(A[i][j]==1)g.e++;

}

MatToList(g,G);

printf("\n请选择顶点(0~%d):",g.n-1);

scanf("%d",&m);

printf("\n请选择顶点(0~%d):",g.n-1);

scanf("%d",&l);

printf("\n请选择路径长度:");

scanf("%d",&k);

for(i=0;i<G->n;i++)

visited[i]=0;

Find(G,m,l,k,visited,path,-1);

printf("\n");

system("pause");

return 0;

}

 

第二篇:软件技术基础实验报告

       

实验一  简易计算器实验... 4

一、实验目的... 4

二、实验设备及器件... 4

三、实验内容... 4

1.对象... 4

2.对象的属性... 4

3.事件... 4

四、实验代码... 4

1. 创建新工程... 4

2. 设计窗体... 4

3.运行调试程序... 4

4. 保存文件... 4

5. 生成可执行文件... 4

五、实验代码... 4

实验二  成绩录入系统... 8

一、实验目的... 8

二、实验设备及器件... 8

三、实验内容... 8

四、实验步骤... 8

1. 创建新工程... 8

2. 设计窗体... 8

3. 运行调试程序... 8

4. 保存文件... 8

5. 生成可执行文件... 8

五、实验代码... 8

实验三  控件... 14

一、实验目的... 14

二、实验设备及器件... 14

三、实验内容... 14

四、实验步骤... 14

1. 创建新工程... 14

2. 设计窗体及功能说明... 14

3. 运行调试程序... 14

4. 保存文件... 14

5. 生成可执行文件... 14

五、实验代码... 14

实验四  对话框程序设计... 20

一、实验目的... 20

二、实验设备及器件... 20

三、实验内容... 20

四、实验步骤... 20

1. 创建新工程... 20

2. 设计窗体及功能说明... 20

3. 运行调试程序... 20

4. 保存文件... 20

5. 生成可执行文件... 20

五、实验代码... 20

实验五  文件操作... 22

一、实验目的... 22

二、实验设备及器件... 22

三、实验内容... 22

四、实验步骤... 22

1. 创建新工程... 22

2. 设计窗体及功能说明... 22

3. 运行调试程序... 22

4. 保存文件... 22

5. 生成可执行文件... 22

五、实验代码... 22

实验六  员工管理系统设计案例... 24

一、实验目的... 24

二、实验设备及器件... 24

三、实验内容... 24

四、实验步骤... 24

1. 设计数据库... 24

2. 设计窗体... 24

3. 运行调试程序... 24

4. 保存文件... 24

5. 生成可执行文件... 24

五、实验代码... 24

实验一  简易计算器实验

一、实验目的

l  初步学会VB集成开发环境的使用;

l  初步掌握VB语言的基本语法及数据类型;

l  掌握VB可视化编程的设计方法;

l  初步掌握应用VB应用程序的开发步骤。

二、实验设备及器件

l  硬件:PC机

l  软件:Visual Basic 6.0集成开发环境

三、实验内容

    编写一段程序实现一个简易计算器(可进行加、减、乘、除)的功能。

1.对象

    对象(Object)是对具有某些特性的具体事物的抽象。每个对象都具有描述其特征

的属性及附属于它的行为。在可视化的编程中,常见的对象有:窗体、标签、文本框等。

2.对象的属性

属性(Property)是指对象的一项描述内容,用来描述对象的一个特性,不同的对象具有不同的属性,而每个对象又有若干属性描述。

 3.事件

事件(Event)是对象触发的行为描述,事件是预先定义的动作,由用户或者系统激活。VB中的事件通常包括键盘事件和鼠标事件等,本实验中应用到的就是鼠标的单击事即“Click”事件。为了响应事件,可以为事件加入响应的代码。

四、实验代码

1. 创建新工程       

2. 设计窗体

3.运行调试程序

4. 保存文件

5. 生成可执行文件

    “文件”菜单中选择“生成<工程名>.exe”命令即可。

五、实验代码


Dim s As String

Dim a As Double

Dim b As Double

Dim p As Double

Dim q As Variant

Dim x As Variant

Dim y As Variant

Private Sub Command1_Click(Index As Integer)

y = "0" + "."

If Command1(Index).Caption = "." Then           '限制小数点的个数,按小数点怎代表"0."

    If Command1(Index).Caption = "." And s = "" Then

    s = "0"

    s = "0" + Command1(Index).Caption

    ElseIf s Or Not s And Command1(Index).Caption = "." Then

       If s = x Or s = y Then

            Text1.Text = s

       ElseIf s <> s + "." Then

            s = s + "."

            x = s

       End If

    End If

    Text1.Text = s

ElseIf (Command1(Index).Caption Or Not Command1(Index).Caption) Then     

    Cls                                                          '清空屏幕

    If q = "/" And Command1(Index).Caption = 0 Then

     s = 0

     Text1.Text = 0                                              '二次判断b=0,则出错

    ElseIf Command1(Index).Caption Or Not Command1(Index).Caption Then   

    s = s + Command1(Index).Caption                             '继续输入数字

    Text1.Text = s

    End If

End If

End Sub

Private Sub Command2_Click(Index As Integer)

q = Command2(Index).Caption & ""                                  '运算符

If s = "" Then                                                   '等号后的累运算

    s = Text1.Text

    a = CDbl(s)

ElseIf 1 Then                                                    '运算判断,  “数字 + 运算符 + 数字” 累运算的实现

    a = CDbl(s)

        Select Case q

        Case "+"

        p = p + a

        s = ""

        Text1.Text = ""

        Text1.Text = p       

        Case "-"

        If p = 0 Then

        p = a

        ElseIf p Then

        p = p - a

        End If

        s = ""

        Text1.Text = ""

        Text1.Text = p       

        Case "*"

             If p = 0 Then

                 p = 1 * a

             ElseIf p Then

                 p = p * a

            End If

         s = ""

        Text1.Text = ""

        Text1.Text = p        

        Case "/"                                                 '分母为0时出错 判断

        If a = 0 Then

            If a = 0 And p Then

                p = 0

                Cls

                Print Text1.Text = ""

            ElseIf a = 0 And Not p Then

                 p = 0

                 Cls

                 Print Text1.Text = ""

                End If                     

        ElseIf a Then

            If p = 0 Then

                p = a               

            ElseIf p Then

                p = p / a

                End If               

        End If

        s = ""

        Text1.Text = ""

        Text1.Text = p

        End Select

End If

s = ""


End Sub

Private Sub Command3_Click()

If s = "" Then                                       ' 连击等号的 累计算

    Text1.Text = p

ElseIf s Then

    Text1.Text = s

    b = CDbl(s)

End If

Select Case q                                          '运算判断

Case "+"

p = p + b

s = ""

Text1.Text = ""

Text1.Text = p

Case "-"

p = p - b

s = ""

Text1.Text = ""

Text1.Text = p

Case "*"

p = p * b

s = ""

Text1.Text = ""

Text1.Text = p

Case "/"

If b = 0 Then

Cls

Print Text1.Text = ""

p = 0

ElseIf b Then

p = p / b

End If

s = ""

Text1.Text = ""

Text1.Text = p

End Select

b = 0

End Sub

Private Sub Command4_Click()                          '  AC的彻底清空

p = 0

s = ""

Text1.Text = 0

Cls

End Sub

Private Sub Command5_Click()                             '退格键

If Len(Text1.Text) > 1 Then

Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1)

Else

Text1.Text = "0 "                                   '没有数字时清0

End If

End Sub

Private Sub Command6_Click()

End

End Sub


           实验二  成绩录入系统

一、实验目的

l  掌握VB的基本控制语句;

l  掌握通过VB的调试程序来进行错误查找;

l  掌握VB的工程管理及事件机制。

l  熟练掌握VB数组的使用

二、实验设备及器件

l  硬件:PC机

l  软件:Visual Basic 6.0集成开发环境

三、实验内容

    VB实现应用程序实现成绩录入的功能。分为如下三个模块

Ø  登录模块

Ø  成绩录入模块

Ø  成绩显示模块

四、实验步骤

1. 创建新工程      

2. 设计窗体 

3. 运行调试程序

4. 保存文件

5. 生成可执行文件

    “文件”菜单中选择“生成<工程名>.exe”命令即可。

五、实验代码


Dim s(10) As typmembers

Dim str As String

Private Sub Command1_Click()

Dim t As String

Dim q As String

'Kill "e:\matal__vb\成绩录入器\save.txt"

'str = Dir("e:\matal__vb\成绩录入器\save.txt")

'If str <> "" Then                                                         '说明文件存在

        Open "C:\Documents and Settings\Administrator\桌面\save.txt" For Input As #1                       '读出文件

                                                                      

        Do While Not EOF(1)                                              '循环至文件尾

        Line Input #1, t                        '读出一行并赋给t       

        Loop

        Shell "explorer C:\Documents and Settings\Administrator\桌面\save.txt", vbNormalFocus   '以窗口的形式显示文本

        Close #1

   ' ElseIf str = "" Then                 '说明文件不存在

       ' Print t;                         '打印在窗体上   

Command1.Enabled = False

End Sub

Private Sub Command2_Click()

frmcheck.Hide

frminput.Show

Command1.Enabled = True

End Sub

Private Sub Command3_Click()

frmcheck.Hide

frminput.Show

Command1.Enabled = True

End Sub

Private Sub Dir1_Change()

dir1sorted = True

End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

If KeyCode = 32 Then

Command1.Visible = True

Command2.Visible = True

Command3.Visible = True

End If

End Sub

Dim s(100) As typmembers

Dim a As Double, m As Integer, n As Integer, p As Integer, q As Integer, b As String, c As String, d As String, e As String, X As String

Dim i As Integer

Private Sub Command1_Click()

Dim t As String

Dim q As String

'Kill "e:\matal__vb\成绩录入器\save.txt"

'str = Dir("e:\matal__vb\成绩录入器\save.txt")

'If str <> "" Then                '说明文件存在

   Open "C:\Documents and Settings\Administrator\桌面\save.txt" For Input As #1                       '读出文件                                                                     

        Do While Not EOF(1)                '循环至文件尾

        Line Input #1, t                  '读出一行并赋给t       

        Loop

        Shell "explorer C:\Documents and Settings\Administrator\桌面\save.txt", vbNormalFocus   '以窗口的形式显示文本

        Close #1

   ' ElseIf str = "" Then                 '说明文件不存在

       ' Print t;                '打印在窗体上   

Command1.Enabled = False

End Sub

Private Sub Command2_Click()

Command1.Enabled = True

i = 1

e = member(0).Text

b = member(1).Text

c = member(2).Text

d = member(3).Text

If IsNumeric(member(1).Text) And IsNumeric(member(2).Text) And IsNumeric(member(3).Text) Then    

        s(i).姓名 = member(0).Text

        s(i).数学 = CDbl(b)

        s(i).英语 = CDbl(c)

        s(i).政治 = CDbl(d)

        s(i).平均分 = (s(i).数学 + s(i).英语 + s(i).政治) / 3            

         m = LenB(e)

        'n = LenB(b)

        'n = m \ 2 + n

        'p = LenB(c)

        'p = p + n

        'q = LenB(d)

        'q = q + p

 If Asc(s(i).姓名) > 0 Then                                                                                '判断是否是字母                    

Open "C:\Documents and Settings\Administrator\桌面\save.txt" For Append As #1                                                                'Append方式打开文件  依次连接到文件尾部

            Print #1, s(i).姓名; Tab(12); s(i).数学; Tab(24); s(i).英语; Tab(36); s(i).政治; Tab(48); s(i).平均分

                'save.txt.SelAlignment = 0

            Close #1                            

'Print "姓名", " 数学", "英语", "政治", "平均分"           '连接

                'Print s(i).姓名, s(i).数学, s(i).英语, s(i).政治, s(i).平均分

            w = "添加成功"

            X = MsgBox(w)

        ElseIf Asc(s(i).姓名) Then           

Open "C:\Documents and Settings\Administrator\桌面\save.txt" For Append As #1                                                                  'Append方式打开文件  依次连接到文件尾部

            Print #1, s(i).姓名; Tab(12 - m / 2); s(i).数学; Tab(24 - m / 2); s(i).英语; Tab(36 - m / 2); s(i).政治; Tab(48 - m / 2); s(i).平均分

                'save.txt.SelAlignment = 0

            Close #1                               

'Print "姓名", " 数学", "英语", "政治", "平均分"                  '连接

                'Print s(i).姓名, s(i).数学, s(i).英语, s(i).政治, s(i).平均分

            w = "添加成功"

            X = MsgBox(w)

            End If

ElseIf 1 Then

        w = "成绩输入错误!"

        X = MsgBox(w)

    End If

End Sub       

Private Sub Command3_Click()                                                        ' 重置

Command1.Enabled = True

member(0).Text = ""

member(1).Text = ""

member(2).Text = ""

member(3).Text = ""

End Sub

Private Sub Command4_Click()

Command1.Enabled = True

frminput.Hide

frmmain.Show

End Sub                            

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

If KeyCode = 32 Then

Frame1.Visible = True

Frame2.Visible = True

End If

End Sub

Private Sub Com登录_Click()

Open "C:\Documents and Settings\Administrator\桌面\save.txt" For Append As #1            'append方式打开文

Print #1, "姓名"; Tab(10); " 数学"; Tab(21); "英语"; Tab(31); "政治"; Tab(41); "平均分", Chr(13);   '固定输出模板

Close #1

If 账号(1).Text <> "admin" Or 密码.Text <> "123" Then       '登录设置   

    w = "请输入正确的账号和密码!"

    X = MsgBox(w) 

ElseIf 账号(1).Text = "admin" And 密码.Text = "123" Then

    Com登录.Enabled = False

    Com重置.Enabled = False

    Com退出.Enabled = True

    录入.Enabled = True

    查看.Enabled = True

    账号(1).Enabled = False

    密码.Enabled = False

    w = "登陆成功"

    X = MsgBox(w)

    End If

End Sub

Private Sub Com退出_Click()                 '退出登录

    Com登录.Enabled = True

    Com重置.Enabled = True

    账号(1).Enabled = True

    密码.Enabled = True

     录入.Enabled = False

    查看.Enabled = False   

End Sub

Private Sub Com重置_Click()

账号(1).Text = ""

密码.Text = ""

End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

If KeyCode = 32 Then

Frame1.Visible = True

Frame2.Visible = True

End If

     录入.Enabled = False

    查看.Enabled = False

End Sub

Private Sub Form_Load()

   录入.Enabled = False

    查看.Enabled = False

End Sub

Private Sub 查看_Click()

frmmain.Hide

frminput.Hide

frmcheck.Show

End Sub

Private Sub 录入_Click()

frmmain.Hide

frmcheck.Hide

frminput.Show

End Sub

Private Sub 退出_Click()                '退出系统

w = "是否退出成绩录入器?"

X = MsgBox(w, 1, s)

If X = 1 Then

   w = "是否清空存档?"

    X = MsgBox(w, 1, s)   

    If X = 1 Then

        Kill "C:\Documents and Settings\Administrator\桌面\save.txt"

        e = "感谢您的使用"

        X = MsgBox(e)

         End

    ElseIf X Then

        End

        End If

ElseIf X Then

        frmmain.Show   

End If

End Sub


           实验三  控件

一、实验目的

*  掌握VB基本控件的使用方法

二、实验设备及器件

²  硬件:PC机

²  软件:Visual Basic 6.0集成开发环境

三、实验内容

    应用所学VB控件实现如下三个阶段的功能。

n  最爱电影调查

n  学生就业

n  景点排序

四、实验步骤     

1. 创建新工程     

2. 设计窗体及功能说明    

3. 运行调试程序

4. 保存文件

5. 生成可执行文件

    “文件”菜单中选择“生成<工程名>.exe”命令即可。

五、实验代码


Dim i As Integer

Dim a As Integer

Dim b As String

Dim c As Integer

Dim s As jingdian

Private Sub Form_Load()

 For i = 0 To List2.ListCount

     s.x = List1.List(i)

     s.y = List2.List(i)

     s.z = List3.List(i)

    q(i).x = s.x

    q(i).y = s.y

    q(i).z = s.z

     Next

End Sub

Private Sub List1_Click()

   Dim c As Integer

   c = List1.ListIndex

   List2.ListIndex = c

   List3.ListIndex = c

End Sub

Private Sub List2_Click()

 Dim c As Integer

   c = List2.ListIndex

   List1.ListIndex = c

   List3.ListIndex = c

End Sub

Private Sub List3_Click()

 Dim c As Integer

   c = List3.ListIndex

   List1.ListIndex = c

   List2.ListIndex = c

End Sub

Private Sub List4_Click()

Dim c As Integer

   c = List4.ListIndex

   List5.ListIndex = c

   List6.ListIndex = c

End Sub

Private Sub List5_Click()

Dim c As Integer

   c = List5.ListIndex

   List6.ListIndex = c

   List4.ListIndex = c

End Sub

Private Sub List6_Click()

Dim c As Integer

   c = List6.ListIndex

   List5.ListIndex = c

   List4.ListIndex = c

End Sub

Private Sub Option1_Click()    

     List4.Clear

     List5.Clear

     List6.Clear   

    For j = 0 To List2.ListCount - 2

     For i = 0 To List2.ListCount - 2 - j

      If Val(List2.List(i)) > Val(List2.List(i + 1)) Then

           a = List2.List(i)

          List2.List(i) = List2.List(i + 1)

          List2.List(i + 1) = a

           b = List1.List(i)

          List1.List(i) = List1.List(i + 1)

          List1.List(i + 1) = b

           c = List3.List(i)

          List3.List(i) = List3.List(i + 1)

          List3.List(i + 1) = c

       End If

       Next i

    Next j

    For i = 0 To List2.ListCount - 1

        List4.AddItem (List1.List(i))

        List5.AddItem (List2.List(i))

        List6.AddItem (List3.List(i))

    Next i

   For i = 0 To List2.ListCount - 1

    List1.List(i) = q(i).x

    List2.List(i) = q(i).y

    List3.List(i) = q(i).z

    Next

End Sub

Private Sub Option2_Click()

 List4.Clear

     List5.Clear

     List6.Clear

    For j = 0 To List2.ListCount - 2

     For i = 0 To List2.ListCount - 2 - j

      If Val(List2.List(i)) < Val(List2.List(i + 1)) Then

           a = List2.List(i)

          List2.List(i) = List2.List(i + 1)

          List2.List(i + 1) = a

           b = List1.List(i)

          List1.List(i) = List1.List(i + 1)

          List1.List(i + 1) = b

           c = List3.List(i)

          List3.List(i) = List3.List(i + 1)

          List3.List(i + 1) = c

       End If

       Next i

    Next j

    For i = 0 To List2.ListCount - 1

        List4.AddItem (List1.List(i))

        List5.AddItem (List2.List(i))

        List6.AddItem (List3.List(i))

    Next i

For i = 0 To List2.ListCount - 1

    List1.List(i) = q(i).x

    List2.List(i) = q(i).y

    List3.List(i) = q(i).z

    Next

End Sub

Private Sub Option3_Click()

     List4.Clear

     List5.Clear

     List6.Clear

    For j = 0 To List2.ListCount - 2

     For i = 0 To List2.ListCount - 2 - j

      If Val(List3.List(i)) > Val(List3.List(i + 1)) Then

           a = List3.List(i)

          List3.List(i) = List3.List(i + 1)

          List3.List(i + 1) = a

           b = List1.List(i)

          List1.List(i) = List1.List(i + 1)

          List1.List(i + 1) = b

           c = List2.List(i)

          List2.List(i) = List2.List(i + 1)

          List2.List(i + 1) = c

       End If

       Next i

    Next j

    For i = 0 To List2.ListCount - 1

        List4.AddItem (List1.List(i))

        List5.AddItem (List2.List(i))

        List6.AddItem (List3.List(i))

    Next i

For i = 0 To List2.ListCount - 1

    List1.List(i) = q(i).x

    List2.List(i) = q(i).y

    List3.List(i) = q(i).z

    Next

End Sub

Private Sub Option4_Click()

List4.Clear

     List5.Clear

     List6.Clear

    For j = 0 To List2.ListCount - 2

     For i = 0 To List2.ListCount - 2 - j

      If Val(List3.List(i)) < Val(List3.List(i + 1)) Then

           a = List3.List(i)

          List3.List(i) = List3.List(i + 1)

          List3.List(i + 1) = a

           b = List1.List(i)

          List1.List(i) = List1.List(i + 1)

          List1.List(i + 1) = b

           c = List2.List(i)

          List2.List(i) = List2.List(i + 1)

          List2.List(i + 1) = c

       End If

       Next i

    Next j

    For i = 0 To List2.ListCount - 1

        List4.AddItem (List1.List(i))

        List5.AddItem (List2.List(i))

        List6.AddItem (List3.List(i))

    Next i

For i = 0 To List2.ListCount - 1

    List1.List(i) = q(i).x

    List2.List(i) = q(i).y

    List3.List(i) = q(i).z

    Next

End Sub

Dim s As String

Dim j As String

Dim i As Integer

Private Sub Command1_Click()  

       x = MsgBox(s)   

End Sub

Private Sub Command2_Click()

    If Check1.Value = 1 And Check2.Value = 1 Then

        j = Text1.Text & " 选择的专业:英语计算机!"

    ElseIf Check1.Value = 1 Then

        j = Text1.Text & " 选择的专业是:英语"

    ElseIf Check2.Value = 1 Then

        j = Text1.Text & " 选择的专业是:计算机"

    Else

        j = Text1.Text & " 请选择专业"

    End If

       x = MsgBox(j)    

End Sub

Private Sub Form_Load()

    s = Text1.Text & "请选择专业!"

End Sub

Private Sub Option1_Click(Index As Integer)

    If Option1(Index).Value Then

       s = Text1.Text & "选择的行业是:" & Option1(Index).Caption   

    End If

End Sub

Dim s As String

Private Sub Command1_Click()

    If Text1.Text = "" Then

        x = MsgBox("请输入姓名!", 48, "提示")

    ElseIf Text2.Text = "" Then

        x = MsgBox("请输入电影名!", 48, "提示")

    ElseIf Combo1.Text = "" Then

        x = MsgBox("请输入颜色!", 48, "提示")

    ElseIf List1.ListIndex = -1 Then

        x = MsgBox("请选择交通工具!", 48, "提示")

    Else

        s = Text1.Text & "您好!您喜爱的电影是" & Text2.Text & "," & "最喜欢的颜色是" & Combo1.Text & "," & "最喜欢的交通方式是" & List1.List(List1.ListIndex)

        x = MsgBox(s, 64, "提示")   

    End If

End Sub

实验四  对话框程序设计

一、实验目的

u  掌握VB通用对话框控件的属性与方法

u  熟练掌握通用对话框的程序设计方法

二、实验设备及器件

l  硬件:PC机

l  软件:Visual Basic 6.0集成开发环境

三、实验内容

Ø  打开对话框

Ø  另存为对话框

Ø  颜色对话框

Ø  字体对话框

四、实验步骤

1. 创建新工程      

2. 设计窗体及功能说明

3. 运行调试程序

4. 保存文件

5. 生成可执行文件

    “文件”菜单中选择“生成<工程名>.exe”命令即可。

五、实验代码


Private Sub Command1_Click()

    End

End Sub

Private Sub Command2_Click()

    Dim dname As String

    '得到所选择的系别

    dname = List1.Text

    If Option1.Value = True Then

        Select Case dname

            Case "计算机系"

                Text1.Text = "计算机系情况"

            Case "环境工程系"

                Text1.Text = "环境工程系情况"

            Case "建筑学系"

                Text1.Text = "建筑学系情况"

            Case "电力系"

                Text1.Text = "电力工程系情况"

            Case "水利系"

                Text1.Text = "水利工程系情况"

            End Select

    Else

        If Option2.Value = True Then

        Select Case dname

            Case "法律系"

                Text1.Text = "法律系情况"

            Case "外语系"

                Text1.Text = "外语系情况"

            Case "工商管理系"

                Text1.Text = "工商管理系情况"

            Case "艺术系"

                Text1.Text = "艺术系情况"

            End Select

        End If

    End If

End Sub

Private Sub Form_Load()

    '设置时间

    Label2.Caption = Date$

    '设置理工学院为默认选择

    Option1.Value = True

    '添加理工学院系别

    AddD1

End Sub

Public Sub AddD1()

    List1.Clear

    List1.AddItem ("计算机系")

    List1.AddItem ("环境工程系")

    List1.AddItem ("建筑学系")

    List1.AddItem ("电力工程系")

    List1.AddItem ("水利工程系")

End Sub

Public Sub AddD2()

    List1.Clear

    List1.AddItem ("法律系")

    List1.AddItem ("外语系")

    List1.AddItem ("工商管理系")

    List1.AddItem ("艺术系")

End Sub

Private Sub Option1_Click()

    AddD1

    Text1.Text = ""

End Sub

Private Sub Option2_Click()

    AddD2

    Text1.Text = ""    End Sub


    实验五  文件操作

       一、实验目的

l  掌握文件系统控件的常用属性

l  理解文件的有关概念

l  熟练顺序文件与随机文件的找开、关闭、写操作与读操作

二、实验设备及器件

n  硬件:PC机

n  软件:Visual Basic 6.0集成开发环境

三、实验内容

u  通迅录

u  简易记事本

四、实验步骤     

1. 创建新工程       

2. 设计窗体及功能说明

3. 运行调试程序

4. 保存文件

5. 生成可执行文件

    “文件”菜单中选择“生成<工程名>.exe”命令即可。

五、实验代码


Dim i As Integer

Private Sub Command1_Click()

    If Text3.Text = "" Then

     MsgBox "请输入地址"

     ElseIf Text1.Text = "" Then

     MsgBox "请输入姓名"

     ElseIf Text2.Text = "" Then

     MsgBox "请输入电话号码"

    Else

     s(i).name = Text1.Text

     s(i).num = Text2.Text

     s(i).add = Text3.Text

     i = i + 1

    End If

End Sub

Private Sub Command2_Click()

    Dim j As Integer

     For j = 0 To i - 1

      List1.List(j) = Label1.Caption & s(j).name & Label2.Caption & s(j).num & Label4.Caption & s(j).add

     Next j

End Sub

Private Sub Text2_Change()

     If Text2.Text = "" Then

       Label3.Caption = ""

      Label3.Caption = "请输入电话号码"

      End If

End Sub

Private Sub Text2_KeyPress(KeyAscii As Integer)

 If KeyAscii > 57 Or KeyAscii < 48 Then

    Label3.Caption = ""

    Label3.Caption = "电话号码只能为数字"

     KeyAscii = 0

 Else: Label3.Caption = ""       

End If

End Sub

Private Sub Text4_Change()

End Sub

Modules

Type mas


 name As String

 num As String

 add As String

 End Type

 Public s(20) As mas


实验六  员工管理系统设计案例

一、实验目的

l  理解数据库概念;

l  掌握数据库管理器的使用方法;

l  掌握Data与ADO数据控件访问数据库的方法

l  掌握数据窗体向导的使用方法

二、实验设备及器件

l  硬件:PC机

l  软件:Visual Basic 6.0集成开发环境

三、实验内容

Ø  数据库设计

Ø  代码实现:某公司需要开发一个员工管理系统,用来记录员工信息。要求实现对员工信息的增加,删除,和修改,和显示员工数据。   

四、实验步骤     

1. 设计数据库       

2. 设计窗体

3. 运行调试程序

4. 保存文件

5. 生成可执行文件

    “文件”菜单中选择“生成<工程名>.exe”命令即可。

五、实验代码


Private Sub cmdexit_Click()

    mainform.Adodc3.Refresh

    mainform.DataGrid1.Refresh

    mainform.Show

    updataform.Hide

End Sub

Private Sub cmdok_Click()

    Dim id As String, name As String, subject As String, address As String

        id = Trim(txtid.Text)

        name = Trim(txtname.Text)

        subject = Trim(txtsubject.Text)

        address = Trim(txtaddress.Text)

        If Len(id) = 0 Then

            MsgBox "请输入学号"

            txtid.SetFocus

        ElseIf Len(name) = 0 Then

            MsgBox "请输入姓名"

            txtname.SetFocus

        ElseIf Len(subject) = 0 Then

            MsgBox "请输入专业"

            txtsubject.SetFocus

        ElseIf Len(address) = 0 Then

            MsgBox "请输入家庭住址"

            txtaddress.SetFocus

        End If

        Dim cn As New ADODB.Connection

        Dim Rs As ADODB.Recordset

        Dim conn As String

            conn = "Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source=C:\Documents and Settings\Administrator\桌面\考试\VBstudent.mdb"

            cn.Open conn

        Dim sql As String

            sql = "update studentinf set 姓名='" & Trim(txtname.Text) & "',专业='" & Trim(txtsubject.Text) & "',家庭住址='" & Trim(txtaddress) & "'where 学号='" & Trim(txtsearch) & "'"

            Set Rs = cn.Execute(sql)

            MsgBox "修改成功"           

End Sub

Private Sub Command1_Click()

    Dim id As String

        id = Trim(txtsearch.Text)

    Dim sql As String

        sql = "select * from studentinf where 学号='" & id & " '"

        Adodc3.RecordSource = sql

        Adodc3.Refresh

        If Adodc3.Recordset.RecordCount = 0 Then

            MsgBox "对不起,没有该学生记录"

        Else

            txtid.Text = Adodc3.Recordset.Fields("学号")

            txtname.Text = Adodc3.Recordset.Fields("姓名")

            txtsubject.Text = Adodc3.Recordset.Fields("专业")

            txtaddress.Text = Adodc3.Recordset.Fields("家庭住址")

        End If

           End Sub

Private Sub Form_Load()

End Sub

Private Sub subject_Click()

End Sub

Private Sub cmdadd_Click()

    addform.Show

    mainform.Hide

End Sub

Private Sub cmdupdate_Click()

    updataform.Show

    Me.Hide

End Sub


相关推荐