*UVa 12504 - Updating a Dictionary

12504 - Updating a Dictionary


http://blog.youkuaiyun.com/zyq522376829/article/details/46484575


错误代码:

#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <map>
#include <set>

using namespace std;

const int MAXN = 100 + 5;

int main(){
	ios::sync_with_stdio(false);
	int t;
	cin >> t;
	while (t--) {
		map<string, string> ma;
		string dictOld, dictNew;
		string add[MAXN], del[MAXN], cha[MAXN];
		int addCount = 0;
		int delCount = 0;
		int chaCount = 0;

		cin >> dictOld >> dictNew;

		int a = 0;
		while (dictOld.find(',', a + 1) < dictOld.size()) {
			//截取
			string s(dictOld, a + 1, dictOld.find(',', a + 1) - a - 1);
			string key(s, 0, s.find(':', 0));
			string value(s, s.find(':', 0) + 1, s.size() - s.find(':', 0));
			ma.insert(pair<string, string>(key, value));
			a = dictOld.find(',', a + 1);
		}
		string s(dictOld, a + 1, dictOld.find('}', a + 1) - a - 1);
		string key(s, 0, s.find(':', 0));
		string value(s, s.find(':', 0) + 1, s.size() - s.find(':', 0));
		if (key.size()) {
			ma.insert(pair<string, string>(key, value));
		}

		a = 0;
		while (dictNew.find(',', a + 1) < dictNew.size()) {
			string s(dictNew, a + 1, dictNew.find(',', a + 1) - a - 1);
			string key(s, 0, s.find(':', 0));
			string value(s, s.find(':', 0) + 1, s.size() - s.find(':', 0));

			if (ma.find(key) == ma.end()) {
				add[addCount++] = key;
			}
			else {
				string temp = ma.find(key)->second;
				if (temp != value) {
					cha[chaCount++] = key;
				}
				ma.erase(key);
				//erase为了之后得到del
			}
			a = dictNew.find(',', a + 1);
		}
		string s1(dictNew, a + 1, dictNew.find('}', a + 1) - a - 1);
		string key1(s1, 0, s1.find(':', 0));
		string value1(s1, s1.find(':', 0) + 1, s1.size() - s1.find(':', 0));

		if (key1.size()) {
			if (ma.find(key1) == ma.end()) {
				add[addCount++] = key1;
			}
			else {
				string temp = ma.find(key1)->second;
				if (temp != value1) {
					cha[chaCount++] = key1;
				}
				ma.erase(key1);
			}
		}

		while (ma.begin() != ma.end()) {
			string temp = ma.begin()->first;
			del[delCount++] = temp;
			ma.erase(key1);
		}

		if (addCount == 0 && delCount == 0 && chaCount == 0) {
			cout << "No changes" << endl;
		}
		else {
			if (addCount) {
				sort(add, add + addCount);
				cout << "+";
				for (int i = 0; i < addCount; i++) {
					if (i) {
						cout << ",";
					}
					cout << add[i];
				}
				cout << endl;
			}
			if (delCount) {
				sort(del, del + delCount);
				cout << "-";
				for (int i = 0; i < delCount; i++) {
					if (i) {
						cout << ",";
					}
					cout << del[i];
				}
				cout << endl;
			}
			if (chaCount) {
				sort(cha, cha + chaCount);
				cout << "*";
				for (int i = 0; i < chaCount; i++) {
					if (i) {
						cout << ",";
					}
					cout << cha[i];
				}
				cout << endl;
			}
		}
		cout << endl;
	}
	return 0;
}


我们已经为你建好了数据库与数据表,并添加了相应的数据内容。 你只需: 补全右侧代码片段中 create trigger_insert_score 下的 Begin-End 区域间的代码,向 score 表建立一个插入触发器。保证向 score 表中插入的学生信息的学号,必须在 student 表中存在; 补全右侧代码片段 create trigger_delete_student 下的 Begin-End 区域间的代码,向 student 表插入删除触发器,实现 student 表和 score 表的级联删除; 补全右侧代码片段 create trigger_protect_grade 下的 Begin-End 区域间的代码,向 score 表建立触发器,使 grade 列不能手工修改。 表 student 如下所示,其中 birthday 是 date 类型,其余字段均为 varchar 类型。 sno sname sex birthday discipline school 1001 Tom male 2019-06-01 computer information 1002 Bob male 2019-06-01 software information 1003 Alice female 2019-05-02 computer information 表 score 如下所示,字段 grade 为 float 类型,其余均为 varchar 类型。 sno cno grade 1001 101 89 1001 102 78 1002 101 88 1002 102 80USE studentdb go SET NOCOUNT ON go --********** create trigger_insert_score **********-- --********** Begin **********-- --********** End **********-- go delete from score go insert into score values('1001','2001','89.5') go insert into score values('1002','2001','95') go insert into score values('1011','2001','88') go select * from score go --********** create trigger_delete_student **********-- --********** Begin **********-- --********** End **********-- go delete from student where sno='1001' go select * from score go --********** create trigger_protect_grade **********-- --********** Begin **********-- --********** End **********-- go Update score set cno = 2021,grade = 99.0 where sno = '1002'; go select * from score go
05-24
### 插入触发器:验证 `score` 表中的 `sno` 是否存在于 `student` 表 为了确保插入到 `score` 表中的 `sno` 存在于 `student` 表中,可以创建如下触发器: ```sql CREATE TRIGGER trg_Validate_Score_Insert ON score INSTEAD OF INSERT AS BEGIN IF NOT EXISTS ( SELECT 1 FROM inserted i WHERE NOT EXISTS ( SELECT 1 FROM student s WHERE s.Sno = i.sno ) ) BEGIN INSERT INTO score (sno, course_id, grade) SELECT sno, course_id, grade FROM inserted; END ELSE BEGIN RAISERROR ('Insert failed: The Sno does not exist in the Student table.', 16, 1); END END; GO ``` 此触发器通过检查 `inserted` 虚拟表来确认每条新记录的 `sno` 是否存在於 `student` 表中[^3]。 --- ### 删除触发器:实现 `student` 和 `score` 的级联删除 当从 `student` 表中删除一条记录时,关联的 `score` 记录也需要一并删除。以下是该触发器的定义: ```sql CREATE TRIGGER trg_Cascade_Delete_Student ON student AFTER DELETE AS BEGIN DELETE FROM score WHERE sno IN (SELECT deleted.Sno FROM deleted); PRINT 'Related records in Score have been successfully deleted.'; END; GO ``` 在此触发器中,利用 `deleted` 虚拟表获取已删除的学生编号,并将其作为条件删除对应的 `score` 数据[^4]。 --- ### 更新触发器:阻止手动修改 `score` 表中的 `grade` 列 如果希望防止用户直接更改 `score` 表中的 `grade` 值,可以通过以下触发器实现: ```sql CREATE TRIGGER trg_Prevent_Grade_Update ON score FOR UPDATE AS BEGIN IF UPDATE(grade) BEGIN ROLLBACK TRANSACTION; RAISERROR('Updating Grade column is prohibited!', 16, 1); END END; GO ``` 这个触发器会在检测到 `grade` 列被更新时回滚事务,并抛出错误提示消息[^5]。 --- #### 注意事项 - 上述触发器均需在 SQL Server 环境下运行。 - 如果数据库设计允许外键约束,则建议优先考虑使用外键而非触发器完成类似的逻辑操作,因为外键通常更高效且易于维护。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值