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

4.4 KiB
Raw Permalink 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**********/
	for(i=0;i<50;i++){
		//p指针变量 存着数组array首元素地址
		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;
	*newLen = 1;
	//合并
	for(i=0;i<6;i++){
		mergedArray[j++] = arr1[i];
		mergedArray[j++] = arr2[i];
	}
	//排序
	for(i=0;i<size1*2-1;i++){
		for(j=0;j<size1*2-1-i;j++){
			if(mergedArray[j] < mergedArray[j+1]){
				temp = mergedArray[j];
				mergedArray[j] = mergedArray[j+1];
				mergedArray[j+1] = temp;
			}
		}
	}
	//去重
	for(i=1;i<size1*2;i++){
		if(mergedArray[(*newLen)- 1] != mergedArray[i]){
			mergedArray[(*newLen)++] =  mergedArray[i];
		}
	}

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

数据库-1

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

#2. 插入记录
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 ;

数据库-2