oc实现小型学生管理系统
首先,创建一个工程,然后加入两个cocoaclass,分别命名为Student 和 StudentSystem.
然后就可以开始写代码喽
1.Student类--Student.h文件
```
#import <Foundation/Foundation.h>
@interface Student : NSObject<NSCoding>
{
@protected
int stuID;
NSString * name;
int age;
int score;
}
@property int stuID;
@property NSString * name;
@property int age;
@property int score;
-(id)init;
-(void)inputStudent;
-(void)printStudent;
//-(void)modifyStudent;
@end
```
```
#import "Student.h"
int allgrades=0;
@implementation Student
//extern int allgrades;
@synthesize stuID,name,age,score;
-(id)init//构造函数
{
if (self = [super init]) {
stuID = 0;
name = nil;
age = 0;
score = 0;
}
else
{
self = nil;
}
return self;
}
-(void)inputStudent
{
NSLog(@"请依次输入学生的姓名,学号,年龄,成绩(格式:ganyu 10086 18 100)");
char CharName[20];
int intId;
int intAge,intScore;
scanf("%s %d %d %d",CharName,&intId,&intAge,&intScore);
allgrades+=intScore;
[self setName:[[NSString alloc]initWithUTF8String:CharName]];
[self setStuID:intId];
[self setAge:intAge];
[self setScore:intScore];
}
-(void)printStudent
{
NSLog(@"学号:%d,姓名:%@,年龄:%d,成绩: %d",self.stuID,self.name,self.age,self.score);
}
-(void)encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeInt:stuID forKey:@"id"];
[aCoder encodeObject:name forKey:@"name"];
[aCoder encodeInt:age forKey:@"age"];
[aCoder encodeInt:score forKey:@"score"];
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
if (self = [super init]) {
self.stuID = [aDecoder decodeIntForKey:@"id"];
self.name = [aDecoder decodeObjectForKey:@"name"];
self.age = [aDecoder decodeIntForKey:@"age"];
self.score = [aDecoder decodeIntForKey:@"score"];
}
return self;
}
@end
3.StudentSystem类--StudentSystem.h 文件
#import <Foundation/Foundation.h>
#import "Student.h"
extern int allgrades;
@interface StudentSystem : NSObject
@property NSMutableArray *studentArray;
-(id)init;
-(void)printStudentArray;
-(void)addStudentArray:(Student *)stu;
-(void)findStudentByStuID;
-(void)removeStudentArrayByStuID;
-(void)writeToFile;
-(void)readFromFile;
-(void)countStudent;
-(void)SortStudentArray;// >0升序,<0降序
-(void)motifyStudent;
@end
#import "StudentSystem.h"
extern int allgrades;
@implementation StudentSystem
@synthesize studentArray;
-(id)init
{
if (self = [super init]) {
studentArray = [[NSMutableArray alloc]init];
}
else
self = nil;
return self;
}
-(void)countStudent
{
unsigned long int a=[self.studentArray count];
NSLog(@"一共有%lu 位同学",a);
double b=allgrades/a;
NSLog(@"allgrades的值为:%d",allgrades);
NSLog(@"大家的平均成绩是%lf",b);
}
-(void)printStudentArray
{
for (Student *stu in studentArray) {
[stu printStudent];
}
NSLog(@"array 打印完毕.");
}
-(void)addStudentArray:(Student *)stu
{
[[self studentArray] addObject:stu];
NSLog(@"添加完成");
}
-(void)findStudentByStuID
{
NSLog(@"请输入你要查找的同学的学号:");
int studentID;
scanf("%d",&studentID);
for (Student *stu in studentArray) {
if (stu.stuID == studentID) {
[stu printStudent];
return ;
}
}
NSLog(@"未找到学号为%d的学生.",studentID);
}
-(void)removeStudentArrayByStuID
{
NSLog(@"请输入你要删除的同学的学号:");
int studentID;
scanf("%d",&studentID);
for (Student *stu in studentArray) {
if (stu.stuID == studentID) {
[studentArray removeObject:stu];
allgrades-=stu.score;
NSLog(@"删除完成.");
return ;
}
}
NSLog(@"未找到学号为%d的学生.",studentID);
}
-(void)SortStudentArray// >0升序,<0降序
{
NSLog(@"按照学号排序请输入 (id)");
NSLog(@"按姓名排序请输入(name)");
NSLog(@"按照年龄排序请输入(age)");
NSLog(@"按照成绩排序请输入(score)");
char charKey[10] ;
scanf("%s",charKey);
NSString *tempkey = [NSString stringWithUTF8String:charKey];
NSString *key = [tempkey lowercaseString];
BOOL ascending = NO;
if ([key isEqualToString:@"id"]) {
NSSortDescriptor *sortByID = [NSSortDescriptor sortDescriptorWithKey:@"stuID" ascending:ascending];
[studentArray sortUsingDescriptors:[NSArray arrayWithObject:sortByID]];
}
else if([key isEqualToString:@"name"])
{
NSSortDescriptor *sortByName= [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:ascending];
[studentArray sortUsingDescriptors:[NSArray arrayWithObject:sortByName]];
}
else if([ key isEqualToString:@"age"])
{
NSSortDescriptor *sortByAge = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:ascending];
[studentArray sortUsingDescriptors:[NSArray arrayWithObject:sortByAge]];
}
else if ([key isEqualToString:@"score"])
{
NSSortDescriptor *sortByScore = [NSSortDescriptor sortDescriptorWithKey:@"score" ascending:ascending];
[studentArray sortUsingDescriptors:[NSArray arrayWithObject:sortByScore]];
}
}
-(void)writeToFile
{
NSString *path = [NSString stringWithFormat:@"/Users/len/Desktop"];
NSLog(@"请输入你所要写入的文件名字:");
char sfilename[20];
scanf("%s",sfilename);
NSString *filename = [NSString stringWithUTF8String:sfilename];
NSString *filepath = [path stringByAppendingPathComponent:filename];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:studentArray ];
[data writeToFile:filepath atomically:YES];
}
-(void)readFromFile
{
NSString *path = [NSString stringWithFormat:@"/Users/len/Desktop"];
NSLog(@"请输入你所要读取的文件名字:");
char sfilename[20];
scanf("%s",sfilename);
NSString *filename = [NSString stringWithUTF8String:sfilename];
NSString *filepath = [path stringByAppendingPathComponent:filename];
//NSMutableData *data = [[NSMutableData alloc]initWithContentsOfFile:filepath];
NSMutableData *Data = [NSMutableData dataWithContentsOfFile:filepath];
self.studentArray = [NSKeyedUnarchiver unarchiveObjectWithData:Data];
}
-(void)motifyStudent
{
NSLog(@"请输入你要修改的同学的学号:");
int studentID;
scanf("%d",&studentID);
for (Student *stu in studentArray) {
if (stu.stuID == studentID) {
NSLog(@"修改学号为%d的姓名,年龄,成绩(格式:ganyu 10086 18 100)",studentID);
allgrades-=[stu score];
char CharName[20];
int intId;
int intAge,intScore;
scanf("%s %d %d %d",CharName,&intId,&intAge,&intScore);
[stu setName:[[NSString alloc]initWithUTF8String:CharName]];
[stu setStuID:intId];
[stu setAge:intAge];
[stu setScore:intScore];
allgrades+=[stu score];
return ;
}
}
NSLog(@"未找到学号为%d的学生.",studentID);
}
@end
5.main文件
#import <Foundation/Foundation.h>
#import "Student.h"
#import "StudentSystem.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSLog(@"欢迎来到计科某班考试成绩管理系统");
StudentSystem *class= [[StudentSystem alloc]init];
while (YES) {
NSLog(@"1:增加 2:打印");
NSLog(@"3:删除 4:查找");
NSLog(@"5:写入 6:读出");
NSLog(@"7:统计 8:排序");
NSLog(@"9:修改 0:退出");
int c;
scanf("%d",&c);
switch (c) {
case 1:
{
Student *stuTemp = [[Student alloc]init];
[stuTemp inputStudent];
[class addStudentArray:stuTemp];
break;
}
case 2:
[class printStudentArray];
break;
case 3:
[class removeStudentArrayByStuID];
break;
case 4:
[class findStudentByStuID];
break;
case 5:
[class writeToFile];
break;
case 6:
[class readFromFile];
break;
case 7:
[class countStudent];
break;
case 8:
[class SortStudentArray];
NSLog(@"已排序");
break;
case 9:
[class motifyStudent];
break;
case 0:
return 0;
default:
break;
}
}
}
return 0;
}
//关于上面还有一些功能没有实现,有兴趣的话可以尝试一下呦
oc实现小型学生管理系统的更多相关文章
- Java 小型学生管理系统心得
这个学生管理系统相对来说比较简单,主要就是复习下java怎么连接数据库,然后你怎么来实现这个功能,我简单的说下思路吧. 首先你要构思好这个界面,他包括增删查改这些基本功能,然后你去分析这些功能都能怎么 ...
- 【IOS开发笔记02】学生管理系统
端到端的机会 虽然现在身处大公司,但是因为是内部创业团队,产品.native.前端.服务器端全部坐在一起开发,大家很容易做零距离交流,也因为最近内部有一个前端要转岗过来,于是手里的前端任务好像可以抛一 ...
- 【IOS开发笔记01】学生管理系统(上)
端到端的机会 虽然现在身处大公司,但是因为是内部创业团队,产品.native.前端.服务器端全部坐在一起开发,大家很容易做零距离交流,也因为最近内部有一个前端要转岗过来,于是手里的前端任务好像可以抛一 ...
- C程序范例(2)——学生管理系统”链表“实现
1.对于学生管理系统,能够实现的方法有许多,但是今天我们用链表的方法来实现.虽然初学者很可能看不懂,但是不要紧,这是要在整体的系统的学习完C语言之后,我才编写出的程序.所以大家不必要担心.在这里与大家 ...
- jsp学习之基于mvc学生管理系统的编写
mvc开发模式:分别是 model层 view层 Control层 在学生管理系统中,model层有学生实体类,数据访问的dao层,view层主要是用于显示信息的界面,Control层主要是servl ...
- java版本的学生管理系统
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Frame; import java.awt.event.Ac ...
- 学生管理系统-火车订票系统 c语言课程设计
概要: C 语言课程设计一---学生管理系统 使使用 C 语言实现学生管理系统.系统实现对学生的基本信息和考试成绩的 管理.采用终端命令界面,作为系统的输入输出界面.采用文件作为信息存储介质. 功能描 ...
- Java学生管理系统项目案例
这是一个不错的Java学生管理系统项目案例,希望能够帮到大家的学习吧. 分代码如下 package com.student.util; import java.sql.Connection; impo ...
- Java+Mysql+学生管理系统
最近正在学java和数据库,想起以前写的学生管理系统,都是从网上下载,敷衍了事.闲来无事,也就自己写了一个,不过功能实现的不是很多. 开发语言:java: 开发环境:Mysql, java: 开发工具 ...
随机推荐
- Hadoop之HDFS读写原理
一.HDFS基本概念 HDFS全称是Hadoop Distributed System.HDFS是为以流的方式存取大文件而设计的.适用于几百MB,GB以及TB,并写一次读多次的场合.而对于低延时数据访 ...
- ef+Npoi导出百万行excel之踩坑记
最近在做一个需求是导出较大的excel,本文是记录我在做需求过程中遇到的几个问题和解题方法,给大家分享一下,一来可以帮助同样遇到问题的朋友,二呢,各位大神也许有更好的方法可以指点小弟一 ...
- Nginx服务器安装及配置解释
nginx是高性能的轻量级web服务器. 特性: 1.http代理 2.反向代理 3.负载均衡 4.缓存机制 一,安装及启动(centos7,nginx 1.14.0) 1.下载 wget http: ...
- 数据类型转换&运算符
基本数据类型注意事项 整型类型:long>int>short>byte java默认类型为int型 long a = 123 表示将int类型的数值赋给更大范围的long类型,当数据 ...
- su root
1. 命令行方式 ansible zabbix_agents --become --become-method=su -K -m shell -a 'whoami' 2. 变量方式 [zabbix_a ...
- PHP中的服务容器与依赖注入的思想
依赖注入 当A类需要依赖于B类,也就是说需要在A类中实例化B类的对象来使用时候,如果B类中的功能发生改变,也会导致A类中使用B类的地方也要跟着修改,导致A类与B类高耦合.这个时候解决方式是,A类应该去 ...
- 【Luogu P3834】可持久化线段树(主席树)
Luogu P3834 可持久化数据结构就是支持在历史版本上进行查询和修改操作的数据结构. 主席树就是对线段树的改进,使之可持久化. 前置知识:动态开点线段树 我们利用权值(值域)线段树统计区间内的数 ...
- Linux LVM 配置
本文出自 “www.kisspuppet.com” 博客,请务必保留此出处http://dreamfire.blog.51cto.com/418026/1084729 许多Linux使用者安装操作系统 ...
- Android最大方法数和解决方案
转载请标明出处:http://blog.csdn.net/shensky711/article/details/52329035 本文出自: [HansChen的博客] 什么是64K限制和Linear ...
- 在Asp.Net Core MVC 开发过程中遇到的问题
1. Q: Razor视图中怎么添加全局模型验证消息 #### A:使用ModelOnly <div asp-validation-summary="ModelOnly" c ...