class-notes/2207/习题答案/12-18习题答案.md
2025-02-06 22:43:19 +08:00

6.0 KiB
Raw Blame History

C语言-1

/*------------------------------------------------------------------------------
1【程序设计】定义一个大小为50的整型数组将数字1~50存入该数组利用指针将数组中的值输出每行5个每个数字占4个符号位左对齐。
------------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
	int i,*p,array[50];
	for(i=0;i<50;i++)
	{
		array[i]=i+1;
	}
	p=array;
/**********Program**********/
	//p 一级指针 可以指向整型的一维数组
	for(i=0;i<50;i++){//0~49
		printf("%-4d ", p[i]);
		if((i+1)%5==0){
			printf("\n");
		}
	}

/**********  End  **********/
}

C语言-2

/*----------------------------------------------------------------------
【程序设计】
------------------------------------------------------------------------
题目:要求实现一个函数,能够合并两个整数数组,合并后进行降序排序并删除重复元素。
示例1
输入输出如下所示:
请输入第一个数组的6个元素每个元素之间用空格隔开1 3 5 7 10 6
请输入第二个数组的6个元素每个元素之间用空格隔开10 4 2 6 11 30
合并后降序排列的数组30 11 10 7 6 5 4 3 2 1
------------------------------------------------------------------------
注意:请勿改动程序中的其他内容。
----------------------------------------------------------------------*/
#include <stdio.h>

int* mergeArrays(int *arr1, int size1, int *arr2, int size2,int *newLen);

int main() {

        int size1=6;
	int size2=6;
	int arr1[6];
	int arr2[6];
	int i;

	int newLen = 0;
	int* mergedArray;

	printf("请输入第一个数组的6个元素每个元素之间用空格隔开");
	for ( i = 0; i < size1; i++) {
            scanf("%d", &arr1[i]);
        }
	printf("请输入第二个数组的6个元素每个元素之间用空格隔开");
	for ( i = 0; i < size2; i++) {
            scanf("%d", &arr2[i]);
        }

	mergedArray = mergeArrays(arr1, size1, arr2, size2, &newLen);

	printf("合并后降序排列的数组:");
	for (i = 0; i < newLen; i++) {
	    printf("%d ", mergedArray[i]);
	}
	printf("\n");
	return 0;
}


int* mergeArrays(int *arr1,int size1,int *arr2,int size2,int *newLen) {
	static int mergedArray[20];
	/**********Program**********/
	int i, j=0, temp;
	//合并数组
	for(i=0;i<size1;i++){
		mergedArray[j++] = arr1[i];
		mergedArray[j++] = arr2[i];
	}

	//降序排序
	for(i=0;i<size1*2-1;i++){//已经有序的个数
		for(j=0;j<size1*2-i-1;j++){//i每+1 代表最前面已经有一个最小的元素
			if(mergedArray[j] < mergedArray[j+1]){ //无序的元素两两比较小值后移
				temp = mergedArray[j];
				mergedArray[j] = mergedArray[j+1];
				mergedArray[j+1] = temp;
			}
		}
	}
	//去重
	/*
		默认第一个数字不用判断,从数组中第二个元素开始判断,
		是否等于前一项如果不等于则进行赋值操作,然后元素个数加一
	*/
	*newLen = 1;//当前数组中已经过滤后的有效数字个数
	for(i=1;i<size1*2;i++){//使用i变量从1下标元素开始 遍历数组中的所有数字
		if(mergedArray[(*newLen) - 1] != mergedArray[i]){
			//如果当前判断的元素不等于前一个元素则插入到有效数字位置
			mergedArray[(*newLen)++] = mergedArray[i];
		}
	}



	/**********  End  **********/
	return mergedArray;
}

MySQL-1

#1. 设置字段主键+自动增长
ALTER TABLE t_user MODIFY uid INT(11) PRIMARY KEY AUTO_INCREMENT;

#只设置主键
ALTER TABLE t_user ADD PRIMARY KEY(uid);

#2. 插入记录
#DEFAULT 自增/默认值/NULL
INSERT INTO t_user()
VALUES(DEFAULT, 'mike', 'mm123', '小明', DEFAULT);

#3. 更新记录
UPDATE t_user u
SET u.`password` = '888888'
WHERE u.username = 'Dennis';

#4. tj11 条件查询+多表联查
SELECT u.nickname, c.mid, c.score, c.content
FROM t_user u, t_comment c
WHERE u.uid = c.uid AND u.nickname = '白色';

#5. tj12 分组查询+多表联查
SELECT m.mname, ROUND(AVG(c.score), 2)
FROM t_movie m, t_comment c
WHERE m.mid = c.mid
GROUP BY m.mid;

#6. tj13 分组查询+多表联查
SELECT m.mname, COUNT(*)
FROM t_movie m, t_comment c
WHERE m.mid = c.mid
GROUP BY m.mid;

#7 视图 多表联查
CREATE VIEW v_movie AS
SELECT m.mname, u.nickname, c.score, c.content
FROM t_user u, t_movie m , t_comment c
WHERE u.uid = c.uid AND m.mid = c.mid;

#8 存储过程
DELIMITER $$
DROP PROCEDURE IF EXISTS pro_getUserNumber;
CREATE PROCEDURE pro_getUserNumber(IN in_uid INT, OUT out_number INT)
BEGIN
	SET out_number = (
		SELECT COUNT(*) 
		FROM t_user u, t_comment c
		WHERE u.uid = c.uid AND u.uid = in_uid
	);
END $$
DELIMITER ;

#9 触发器
DELIMITER $$
DROP TRIGGER IF EXISTS tri_userlog;
CREATE TRIGGER tri_userlog 
AFTER INSERT ON t_user
FOR EACH ROW
BEGIN
	INSERT INTO t_log()
	VALUES(DEFAULT, NEW.uid, '新增用户', '2024-01-01');
END $$
DELIMITER ;

MySQL-2

#1 修改表结构
ALTER TABLE t_examinee MODIFY sex CHAR(2);
DESC t_examinee;#查看表结构

#2 插入记录
INSERT INTO t_room()
VALUES(DEFAULT, '202教室', '2号教学楼');

#3 删除记录
DELETE FROM t_teacher 
WHERE rid IS NULL;

#4 更新记录
UPDATE t_examinee
SET tel = '1396668888'
WHERE ename = '龚致远';

#5 tj11 条件查询+多表联查
SELECT e.ename, r.rname,  r.site
FROM t_examinee e, t_room r
WHERE e.rid = r.rid AND e.ename = '陶宇宁';

#6 tj12
SELECT r.rname, r.site, t.tname, t.tel
FROM t_teacher t, t_room r
WHERE t.rid = r.rid AND r.rname = '402教室';

#7
CREATE VIEW v_exam AS
SELECT e.ename, e.sex, e.tel, e.examnum, r.rname, r.site 
FROM t_examinee e, t_room r
WHERE e.rid = r.rid;

#8 存储过程
DELIMITER $$
DROP PROCEDURE IF EXISTS pro_getExamNumber;
CREATE PROCEDURE pro_getExamNumber(IN in_rid INT, OUT out_number INT)
BEGIN
	SELECT COUNT(*) INTO out_number
	FROM t_examinee e
	WHERE e.rid = in_rid;
END $$
DELIMITER ;

#9 触发器
DELIMITER $$
DROP TRIGGER IF EXISTS tri_examlog;
CREATE TRIGGER tri_examlog
BEFORE INSERT ON t_examinee
FOR EACH ROW
BEGIN
	INSERT INTO t_log()
	VALUES(DEFAULT, NEW.eid, '新增考生', '2024-01-01');
END $$
DELIMITER ;