计算机软件技术基础实验报告_软件技术基础实验报告
计算机软件技术基础实验报告由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“软件技术基础实验报告”。
计算机软件技术基础
实验报告
自动化0901班
郝万福
学号:20092395 实验1:线性表的排序与查找
目的:
掌握数据结构的基本概念、线性表的基本操作以及查找和排序方法的设计。内容:
键盘输入一组无序数据,添加到线性表中; 排序线性表并输出排序结果;
键盘输入一个数,并插入到排好序的线性表中(要求插入后的表仍为有序表),输出结果;
键盘输入一个数,并从线性表中删除相应的数据,输出结果。要求:
程序清单;
运行过程及运行结果。
实验代码:
/*
*/
#include “stdafx.h” #include “iostream” #include
// 程序实现有各种方法,这里给出一个实例。
// 定义一个线性表
const int nMaxSize = 15;// 最大值 int nLen = 0;
// 定义操作 void LSort();void LOut();void LInsert(int n);void LDelete(int n);
int main(){
// 输入数据并放入线性表中
printf(“Please input datan”);// std::cout
// 表中元素个数
int nLinearList[nMaxSize];班级:自动化 0901 班 姓名:郝万福 学号:20092395
编译环境: Visual Studio 2011 Windows 7 x86-64 旗舰版
} for(int i = 0;i
}
LSort();// 排序线性表 LOut();// 输出结果 scanf(“%d”,&nIn);nLen++;
//
std::cin >> nIn;
nLinearList[i] = nIn;printf(“Please input a data to insert n”);scanf(“%d”,&nIn);LInsert(nIn);LOut();printf(“Please input a data to delete n”);scanf(“%d”,&nIn);LDelete(nIn);LOut();char chTmp;printf(“Please input a char to finish this program.”);chTmp = getch();return 0;// 输入一个数字,并从线性表中删除 // 输入一个数字,并插入到线性表中
void LSort(){
}
void LOut(){
// 冒泡排序,由大到小
for(int j=0;j
} for(int i=0;i
if(nLinearList[i]
int temp=nLinearList[i];nLinearList[i]=nLinearList[i+1];nLinearList[i+1]=temp;} printf(“n”);for(int i = 0;i
} } printf(“%d, ”, nLinearList[i]);printf(“n”);void LInsert(int n){ nLen++;nLinearList[nLen] = n;for(int k=0;k
实验结果:
实验2:栈与队列的应用
目的:
掌握栈与队列的基本概念、基本操作及设计与应用。内容:
键盘输入算数表达式,并放入队列当中; 应用栈的概念设计表达式求值算法; 输出表达式求值结果; 要求:
程序清单;
运行过程及运行结果。
要求:
实现实验内容的SQL语句,以及输出结果
为检验实验内容的有效性而执行的SQL语句,例如向数据表中插入数据; 检验运行结果(例如插入、删除)的SQL语句,以及输出的结果。
实验代码:
/*
*/
#include “stdafx.h” #include #include #include #include using namespace std;
const int MAX_LEN = 10;
// 定义一个队列的结构
struct QUEUE {
int nMaxSize;int nCount;int nFront;// 最大值 // 个数 // 头
// 字符串的长度
const int MAX_SIZE = 30;// 栈或队的最大元素个数 班级:自动化 0901 班 姓名:郝万福 学号:20092395
编译环境: Visual Studio 2011 Windows 7 x86-64 旗舰版
int nRear;// 尾
char szQueue[MAX_SIZE][MAX_LEN];} QQ;//定义一个栈的结构
struct STACK {
// 队列的操作
void InitQueue(QUEUE *q,int nMaxSize){
} void InQueue(QUEUE *q, char *pItem){
} void OutQueue(QUEUE *q, char *pItem){
if(q->nCount == 0){
} q->nCount--;int i;printf(“队列已空n”);getch();return 0;if(q->nMaxSize == q->nCount){
} q->nCount++;int i;for(i=0;inFront ++;return;q->szQueue[q->nFront][i]= pItem[i];printf(“队列已满n”);getch();return 0;q->nMaxSize=nMaxSize;q->nCount=0;q->nFront=0;q->nRear=0;int nMaxSize;int nTop;// 最大值 // 栈顶
char szStack[MAX_SIZE][MAX_LEN];} ST;
}
for(i=0;inRear ++;return;pItem[i] = q->szQueue[q->nRear][i];//栈的操作
void InitStack(STACK *s,int nMaxSize){
} void PushStack(STACK *s, char *pItem){
} void PopStack(STACK *s, char *pItem){ if(s->nTop == 0){
} printf(“堆栈已空n”);getch();return 0;if(s->nTop == s->nMaxSize-1){
} for(int i=0;inTop++;s->szStack[s->nTop][i]= pItem[i];printf(“堆栈已满n”);getch();return 0;s->nMaxSize=nMaxSize;s->nTop=0;// 栈顶
// 最大值
s->nTop--;for(int i=0;iszStack[s->nTop][i];}
} for(int i=0;iszStack[s->nTop-1][i];{ } int Priority(char *op);式的值
int main(){ int i=0;
char c,temp[10];
// 声明一个队列 QUEUE qq;
// 声明OS栈和NS栈 STACK ns,os;
// 获得操作符的优先级
// 计算表达void Compute(char *num1, char *num2, char *op, char *chResult);
InitQueue(&qq,MAX_SIZE);InitStack(&ns,MAX_SIZE);char x[MAX_LEN];char op[MAX_LEN];
// 定义扫描的表达式
// 栈顶运算符 // 运算结果 InitStack(&os,MAX_SIZE);char num1[MAX_LEN], num2[MAX_LEN];// 两个操作数 char chResult[MAX_LEN];printf(“Input expreion please:n”);while((c=getchar())!='n'){
InQueue(&qq,temp);temp[0]=c;InQueue(&qq,temp);i=0;
if(isdigit(c)){ } else {
for(;i
};
OutQueue(&qq, x);while(true){ if(isdigit(x[0]))
// 是数
// 扫描表达式 // ;压栈 PushStack(&os,“;”);// 表达式p求值 } { PushStack(&ns,x);OutQueue(&qq, x);
// 认为是运算符,没有考虑空格等
// 获得OS栈顶运算符
// 运算符的优先级〉栈顶运算符 } else
{
GetTopStack(&os, op);if(Priority(x)> Priority(op))
} else if(x[0]== ';' && op[0] == ';')
} else if(Priority(x)
// 不大于栈顶运算符 printf(“最终的答案是%s”,chResult);break;
// 扫描结束 PushStack(&os,x);OutQueue(&qq, x);{ { { PopStack(&ns,num1);PopStack(&ns,num2);PopStack(&os,op);Compute(num1, num2, op, chResult);PushStack(&ns,chResult);} }
}
} getch();return 0;int Priority(char *op){
} void Compute(char *num1, char *num2, char *op, char *chResult){
fNum1 = atof(num1);fNum2 = atof(num2);switch(op[0]){ case '^':
fResult = pow(fNum2,fNum1);break;fResult = fNum1*fNum2;break;fResult = fNum2/fNum1;break;fResult = fNum1+fNum2;break;double fNum1,fNum2;double fResult = 0;switch(op[0]){ case '^': nPriority = 3;break;int nPriority = 0;case '*': case '/': nPriority = 2;break;case '+': case '-':
} return nPriority;nPriority = 1;break;nPriority = 0;case ';': case '*': case '/': case '+': case '-':
}
} fResult = fNum2-fNum1;break;
//把计算的结果转化为字符串 sprintf(chResult,“%.4f”,fResult);return;实验结果:
实验3:关系数据语言的应用
目的:
掌握SQL基本语句,实现数据库、表的创建,以及增删改和查询操作。内容:
创建数据库MyDB;
创建学生表Stu,内容包括:Sno、4个字符、主键,Sname、10个字符,Sex、2个字符,Age、整数,Birthday、日期型,Cla、10个字符; 创建课程表Course,内容包括:Cno、2个字符、主键,Cname、10个字符,Chour、整型;
创建成绩表Score,内容包括: Sno、4个字符、主键,Cno、2个字符、主键,Grade、整型;
向各个数据表中插入多条实验数据,并查看操作后的结果;
实验环境:
操作系统: Linux Ubuntu 10.04 数据库: Mysql 5.1.62 使用字符集: utf-8 实验步骤:
1.$>mysql-u root 进入mysql 命令行。2.显示当前数据库
mysql> show databases;+----------------------+ | Database | +----------------------+ | information_schema | | mysql | | new-mato_development | | new-mato_test | +----------------------+ 4 rows in set(0.03 sec)
3.创建数据库 MyDB
mysql> create database MyDB;Query OK, 1 row affected(0.02 sec)
mysql> show databases;+----------------------+ | Database | +----------------------+ | information_schema | | MyDB | | mysql | | new-mato_development | | new-mato_test | +----------------------+ 5 rows in set(0.00 sec)
成功建立了数据库 MyDB。
4.创建学生表Stu
mysql> create table Stu(-> Sno char(4)primary key,-> Sname char(10),-> Sex char(2),-> Age int(4),-> Birthday datetime,-> Cla char(10));Query OK, 0 rows affected(0.08 sec)
mysql> desc Stu;+----------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+----------+------+-----+---------+-------+ | Sno | char(4)| NO | PRI | NULL | | | Sname | char(10)| YES | | NULL | | | Sex | char(2)| YES | | NULL | | | Age | int(4)| YES | | NULL | | | Birthday | datetime | YES | | NULL | | | Cla | char(10)| YES | | NULL | | +----------+----------+------+-----+---------+-------+ 6 rows in set(0.00 sec)
5.创建课程表 Course
mysql> create table Course(-> Cno char(2)primary key,-> Cname char(10),-> Chour int(4));Query OK, 0 rows affected(0.06 sec)
mysql> desc Course;+-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | Cno | char(2)| NO | PRI | NULL | | | Cname | char(10)| YES | | NULL | | | Chour | int(4)| YES | | NULL | | +-------+----------+------+-----+---------+-------+ 3 rows in set(0.00 sec)
6.创建成绩表 Score
mysql> create table Score(-> Sno char(4)primary key,-> Cno char(2),-> Grade int);Query OK, 0 rows affected(0.05 sec)
mysql> desc Score;+-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | Sno | char(4)| NO | PRI | NULL | | | Cno | char(2)| YES | | NULL | | | Grade | int(11)| YES | | NULL | | +-------+---------+------+-----+---------+-------+ 3 rows in set(0.00 sec)
7.向各个数据表中插入多条实验数据,查看操作结果。
mysql> insert into Stu values('1','Jobs','man',22,'1991-04-13','G2C2');Query OK, 1 row affected, 2 warnings(0.09 sec)
mysql> insert into Stu values('2','Susan','feman',23,'1988-02-03','G2C2');Query OK, 1 row affected, 2 warnings(0.00 sec)
mysql> insert into Stu values('3','Bob','man',23,'1979-01-03','G2C2');Query OK, 1 row affected, 2 warnings(0.00 sec)
mysql> insert into Score values('1','1',23);Query OK, 1 row affected(0.01 sec)
mysql> insert into Score values('1','2',43);Query OK, 1 row affected(0.01 sec)
mysql> insert into Score values('1','3',63);Query OK, 1 row affected(0.01 sec)
mysql> insert into Score values('1','4',93);Query OK, 1 row affected(0.01 sec)
mysql> insert into Score values('2','1',56);Query OK, 1 row affected(0.00 sec)
mysql> insert into Score values('3','1',66);Query OK, 1 row affected(0.00 sec)
mysql> insert into Course values('1','G2C2',22);Query OK, 1 row affected, 1 warning(0.02 sec)
8.查询学生出生日期
mysql> select Birthday from Stu where Sno='2' OR Sname='Jobs';+---------------------+ | Birthday | +---------------------+ | 1991-04-13 00:00:00 | | 1988-02-03 00:00:00 | +---------------------+ 2 rows in set(0.00 sec)
9.按照学号顺序查询一个班级所有学生:
+-------+ | Sname | +-------+ | Jobs | | Susan | | Bob | +-------+ 3 rows in set(0.02 sec)
10.列出学生选择各门课程的成绩。
mysql> select r.Grade,s.Sname,c.Cname from Score r inner join Stu s on r.Sno = s.Sno inner join Course c on c.Cname = s.Cla;
+-------+-------+-------+ | Grade | Sname | Cname | +-------+-------+-------+ | 23 | Jobs | G2C2 | | 56 | Susan | G2C2 | | 66 | Bob | G2C2 | +-------+-------+-------+ 3 rows in set(0.00 sec)
11.列出有过不及格成绩的学生名单
mysql> select r.Grade,s.Sname,c.Cname from Score r inner join Stu s on r.Sno = s.Sno inner join Course c on c.Cname = s.Cla where r.Grade
12.求学生的平均成绩和总成绩。
mysql> select t.Sname, SUM(s.Grade)as ZCJ,AVG(s.Grade)as PJCJ from Score s inner join Stu t on t.Sno = s.Sno where Sname = 'Jobs';+-------+------+---------+ | Sname | ZCJ | PJCJ | +-------+------+---------+ | Jobs | 222 | 55.5000 | +-------+------+---------+ 1 row in set(0.00 sec)
13.找到所有成绩都在85以上的学生。mysql> select t.Sname from Stu t inner join Score s on t.Sno = s.Sno having count(s.Grade >85)= 4;
Empty set(0.00 sec)
14.将课程号为01的课程改名为“软件技术”
mysql> update Course set Cname = '软件技术' where Cno = 1;Query OK, 1 row affected, 1 warning(0.26 sec)Rows matched: 1 Changed: 1 Warnings: 1
+-----+------------+-------+ | Cno | Cname | Chour | +-----+------------+-------+ | 1 | 软件技术 | 22 | +-----+------------+-------+ 1 row in set(0.04 sec)
15.修改一名学生的姓名/性别/年龄
mysql> update Stu set Sname = 'Ruby',sex = 'f',Age = 31 where Sno = 3;Query OK, 1 row affected(0.03 sec)Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from Stu where Sname ='Ruby';+-----+-------+------+------+---------------------+-------+ | Sno | Sname | Sex | Age | Birthday | Cla | +-----+-------+------+------+---------------------+-------+ | 3 | Ruby | f | 31 | 1978-02-03 00:00:00 | G2C2 | +-----+-------+------+------+---------------------+-------+ 1 row in set(0.00 sec)
16.将成绩为 55-59分的男生的成绩修改为60分。
mysql> update Score s inner join Stu t on t.Sno = s.Sno set s.grade = 60 where t.Sex = 'man' AND s.grade BETWEEN 55 AND 59;
17.删除90年以后,80年以前出生的学生的所有信息成绩。
mysql> DELETE FROM Stu WHERE Birthday > '1990-01-01 00:00:00' OR Birthday
18.删除一个班上的所有学生
mysql> DELETE FROM Stu WHERE Cla = 'G2C2';
19.删除所有数据表和数据库
mysql> drop table Stu;Query OK, 0 rows affected(0.08 sec)
mysql> drop table Course;Query OK, 0 rows affected(0.12 sec)
mysql> drop table Score;Query OK, 0 rows affected(0.02 sec)
mysql> drop database MyDB;Query OK, 0 rows affected(0.27 sec)
mysql> show databases;+----------------------+ | Database | +----------------------+ | information_schema | | mysql | | new-mato_development | | new-mato_test | +----------------------+ 4 rows in set(0.03 sec)
20.退出
mysql> quit Bye