前言
学生管理系统和C语言学生管理系统要求相似,需要实现增删改查排序功能,这个仿写比较简单,但通过这个仿写中的限制输入的要求,初步了解了正则表达式。
注册、登陆界面
这个界面和3G share相同,这里就不多做解释了。链接:【iOS】 3G shaere
首页
这个界面中,由一个UITableView和按钮组成,最上方的标题是三个UILabel。整体来说比较简单,这里就不多做解释了。
效果图:
增加
这里先遍历数组,寻找学生是否已经存在,我这里允许异班同名,所以需要对班级也进行判断。
-(void) presstui
{
BOOL N = NO;
BOOL C = NO;
BOOL G = NO;
NSString* s1 = self.text1.text;
NSString* s2 = self.text2.text;
NSString* s3 = self.text3.text;
for(int i = 0; i < self.arrName.count; i++) {
if([s1 isEqual:self.arrName[i]] && [s2 isEqual:self.arrClass[i]]) {
N = YES;
C = YES;
break;
}
if(s3.integerValue < 0 || s3.integerValue > 150) {
G = YES;
break;
}
}
if(s1.length > 0 && s2.length > 0 && s3.length > 0) {
self.text1.text = nil;
self.text2.text = nil;
self.text3.text = nil;
if(N && C) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"该名学生已经存在,请勿重复注册" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cofirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){}];
[alert addAction:cofirm];
[self presentViewController:alert animated:YES completion:nil];
} else if (G) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"请输入正确的成绩" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cofirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){}];
[alert addAction:cofirm];
[self presentViewController:alert animated:YES completion:nil];
} else {
[self.arrName addObject:s1];
[self.arrClass addObject:s2];
[self.arrGrade addObject:s3];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"该名学生已成功添加" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cofirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){
[self.delegate AddStudentName:self.arrName andClass:self.arrClass andGrade:self.arrGrade];
[self dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:cofirm];
[self presentViewController:alert animated:YES completion:nil];
}
} else {
self.text1.text = nil;
self.text2.text = nil;
self.text3.text = nil;
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"内容不能为0" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cofirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){}];
[alert addAction:cofirm];
[self presentViewController:alert animated:YES completion:nil];
}
}
效果图:
删除
删除也是先遍历数组找到这名学生,在将他从数组中移除,要同时移除成绩和班级,否则会导致信息错行,而后协议传值传会给首页即可。
BOOL N = NO;
BOOL C = NO;
NSString* s1 = self.text1.text;
NSString* s2 = self.text2.text;
int index = 0;
for(int i = 0; i < self.arrName.count; i++) {
if([s1 isEqual:self.arrName[i]] && [s2 isEqual:self.arrClass[i]]) {
N = YES;
C = YES;
index = i;
break;
}
}
if(s1.length>0&& s2.length>0) {
if(N && C) {
[self.arrName removeObjectAtIndex:index];
[self.arrClass removeObjectAtIndex:index];
[self.arrGrade removeObjectAtIndex:index];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"该名学生已成功删除" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cofirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){
[self.delegate shanStudentName:self.arrName andClass:self.arrClass andGrade:self.arrGrade];
[self dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:cofirm];
[self presentViewController:alert animated:YES completion:nil];
} else {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"未查询到该名学生信息" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cofirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){
self.text1.text = @"";
self.text2.text = @"";
//[self.tableview removeFromSuperview];
}];
[alert addAction:cofirm];
[self presentViewController:alert animated:YES completion:nil];
}
} else {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"你没有输入全部信息,无法查询" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cofirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){
self.text1.text = @"";
self.text2.text = @"";
//[self.tableview removeFromSuperview];
}];
[alert addAction:cofirm];
[self presentViewController:alert animated:YES completion:nil];
}
效果图:
查询
查询的逻辑是根据输入的名字遍历整个数组,将查找出来的放进一个新的数组,最后交给tableView让他显示出来。
-(void) presstui
{
BOOL N = NO;
self.arrName1 = [NSMutableArray array];
self.arrClass2 = [NSMutableArray array];
self.arrGrade3 = [NSMutableArray array];
NSString* s1 = self.text1.text;
int index = 0;
for(int i = 0; i < self.arrName.count; i++) {
if([s1 isEqual:self.arrName[i]]) {
N = YES;
index = i;
[self.arrName1 addObject:self.arrName[i]];
[self.arrClass2 addObject:self.arrClass[i]];
[self.arrGrade3 addObject:self.arrGrade[i]];
//break;
}
}
if(s1.length==0)
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"你没有输入信息,请别玩" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cofirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){}];
[alert addAction:cofirm];
[self presentViewController:alert animated:YES completion:nil];
}
if(N) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"已经查询到该名学生" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cofirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){
//[self.delegate shanddStudentName:self.arrName andClass:self.arrClass andGrade:self.arrGrade];
[self UseTableView];
}];
[alert addAction:cofirm];
[self presentViewController:alert animated:YES completion:nil];
} else {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"该名学生不存在" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cofirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){
self.tableview.hidden = YES;
self.text1.text = @"";
}];
[alert addAction:cofirm];
[self presentViewController:alert animated:YES completion:nil];
}
}
-(void) UseTableView
{
self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width, 300) style:UITableViewStylePlain ];
self.tableview.delegate = self;
self.tableview.dataSource = self;
self.tableview.hidden = NO;
[self.view addSubview:self.tableview];
[self.tableview registerClass:[Cellshitu class] forCellReuseIdentifier:@"cellshitu"];
}
更改学生信息
更改学生信息的逻辑是先找到这名学生,所以需要班级和姓名都确定,而后更改班级和成绩,但是这名学生的更改后班级必须要进行查重,不然在这里会出现同名同班的情况。
-(void)presstui
{
NSString* s1 = self.text1.text;
NSString* s2 = self.text2.text;
NSString* s3 = self.text3.text;
NSString* s4 = self.text4.text;
BOOL N = NO;
BOOL C = NO;
int index = 0;
for(int i = 0; i < self.arrName.count; i++) {
if([s1 isEqual:self.arrName[i]] && [s2 isEqual:self.arrClass[i]]) {
N = YES;
C = YES;
index = i;
break;
}
}
for(int i = 0; i < self.arrName.count; i++) {
if([s1 isEqual:self.arrName[i]] && [s3 isEqual:self.arrClass[i]]) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"这个班级已经存在这个学生" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cofirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){
return;
}];
[alert addAction:cofirm];
[self presentViewController:alert animated:YES completion:nil];
}
}
if(s1.length > 0 && s2.length > 0 && s3.length > 0 && s4.length > 0) {
self.text1.text = nil;
self.text2.text = nil;
self.text3.text = nil;
self.text4.text = nil;
if(s4.integerValue < 0 || s4.integerValue > 150) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"请输入正确的成绩" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cofirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){}];
[alert addAction:cofirm];
[self presentViewController:alert animated:YES completion:nil];
}
if(N && C) {
[self.arrClass replaceObjectAtIndex:index withObject:s3];
[self.arrGrade replaceObjectAtIndex:index withObject:s4];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"添加成功" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cofirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){
[self.delegate GaiStudentName:self.arrName andClass:self.arrClass andGrade:self.arrGrade];
[self dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:cofirm];
[self presentViewController:alert animated:YES completion:nil];
} else {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"未找到该名学生" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cofirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){}];
[alert addAction:cofirm];
[self presentViewController:alert animated:YES completion:nil];
}
} else
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请输入全部内容" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cofirm = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){
self.text1.text = nil;
self.text2.text = nil;
self.text3.text = nil;
self.text4.text = nil;
}];
[alert addAction:cofirm];
[self presentViewController:alert animated:YES completion:nil];
}
}
排序
这里排序我使用的是C语言中的冒泡排序,较为简单,不多做解释。
for(int i = 0; i < self.arrName.count; i++) {
for(int j = 0; j < self.arrName.count - i - 1; j++) {
if([self.arrGrade[j] intValue] > [self.arrGrade[j + 1] intValue]) {
NSString* tmp1 = @"";
NSString* tmp2 = @"";
NSString* tmp3 = @"";
tmp1 = [NSString stringWithFormat:@"%@", self.arrName[j]];
tmp2 = [NSString stringWithFormat:@"%@", self.arrClass[j]];
tmp3 = [NSString stringWithFormat:@"%@", self.arrGrade[j]];
self.arrName[j] = [NSString stringWithFormat:@"%@", self.arrName[j+1]];
self.arrClass[j] = [NSString stringWithFormat:@"%@", self.arrClass[j+1]];
self.arrGrade[j] = [NSString stringWithFormat:@"%@", self.arrGrade[j+1]];
self.arrName[j+1] = [NSString stringWithFormat:@"%@", tmp1];
self.arrClass[j+1] = [NSString stringWithFormat:@"%@", tmp2];
self.arrGrade[j+1] = [NSString stringWithFormat:@"%@", tmp3];
}
}
}