关闭

object c 学生信息管理系统

835人阅读 评论(0) 收藏 举报
分类:

//  main.m
//  StuMIS

#import <Foundation/Foundation.h>
#import "Student.h"
#import "StuMIS.h"
int main(int argc, const char * argv[])
{

    @autoreleasepool {
     printf("欢迎进入学生信息管理系统\n");
     printf("pk国际2013,@copyright2013-2014\n\n");
        char c;
        StuMIS * sm=[[StuMIS alloc]init];
        while (YES) {
            printf("\n1 添加学生");
            printf("\n2 打印所有学生");
            printf("\n3 删除学生");
            printf("\n4 查找学生");
            printf("\n5 修改学生");
            printf("\n6 保存学生");
            printf("\n7 读取学生");
            printf("\n8 退出");
            scanf(" %c",&c);
            switch (c) {
                case '1':
                {Student * tmp=[[Student alloc]init];
                [tmp inputStudent];
                [sm addNewStu:tmp];
                }
                    break;
                case '2':
                    [sm printAllstu];
                    break;
                case '3':
                { int num;
                  printf("\n请输入学号");
                  scanf(" %d",&num);

                  [sm deleteStu:num];
                }
                    break;
                case '4':
                {int num;
                    printf("\n请输入学号");
                    scanf(" %d",&num);
                    Student * tmp;
                    tmp=[sm findStu:num];
                    [tmp printInfo];
                }
                    break;
                case '5':
                {   int num;
                    printf("\n请输入学号");
                    scanf(" %d",&num);
                    Student * tmp;
                    tmp=[sm findStu:num];
                    [tmp inputStudent];
                }
                    break;
                case '6':
                    [sm saveStu];
                    break;
                case '7':
                    [sm readStu];
                    break;
                case '8':
                    return 0;
                    break;
                default:
                    break;
            }
       
        }
   
    }
    return 0;
}
//
//  Student.h
//  StuMIS

#import <Foundation/Foundation.h>

@interface Student : NSObject<NSCoding>
{   NSString * name;
    int number;
    int age;
    int score;
}
@property NSString * name;
@property int number;
@property int age;
@property int score;
-(void)inputStudent;
-(void)printInfo;
-(void)modify:(Student *) stu;
@end

//  Student.m
#import "Student.h"

@implementation Student
@synthesize name;
@synthesize number;
@synthesize age;
@synthesize score;
//输入学生信息
-(void)inputStudent{
    printf("\n请输入学号 姓名 年龄 成绩\n");
    char nam[20];
    scanf("%d %s %d %d",&number,nam,&age,&score);
    name=[NSString stringWithFormat:@"%s",nam];
}
//打印学生信息
-(void)printInfo{
    printf("\n学号%d 姓名%s 年龄%d 成绩%d",number,[name UTF8String],age,score);
   
}
//修改信息
-(void)modify:(Student *) stu{
    self.name=stu.name;
    self.age=stu.age;
    self.score=stu.score;
}

-(void)encodeWithCoder:(NSCoder *)aCoder
{   [aCoder encodeInt:number forKey:@"number"];
    [aCoder encodeInt:age forKey:@"age"];
    [aCoder encodeObject:name forKey:@"name"];
    [aCoder encodeInt:score forKey:@"score"];
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
    if (self=[super init]) {
        self.number=[aDecoder decodeIntForKey:@"number"];
        self.age=[aDecoder decodeIntForKey:@"age"];
        self.name=[aDecoder decodeObjectForKey:@"name"];
        self.score=[aDecoder decodeIntForKey:@"score"];
        } 
        return self;
}
@end
//
//  StuMIS.h
//  StuMIS

#import <Foundation/Foundation.h>
#import "Student.h"
@interface StuMIS : NSObject
{
    NSMutableArray * allStu;//保存所有学生
}
-(id)init;
-(void)addNewStu:(Student *)stu;
-(void)printAllstu;
-(Student *)findStu:(int)num;
-(void)deleteStu:(int)num;
-(void)saveStu;
-(void)readStu;
@end
//
//  StuMIS.m

#import "StuMIS.h"
@implementation StuMIS
//初始化
-(id)init{
 if(self=[super init])
 {
 allStu=[[NSMutableArray alloc]init];
 }else
 {
 return nil;
 }
    return self;
}
//添加学生信息
-(void)addNewStu:(Student *)stu{
    [allStu addObject:stu];
}
//打印所有学生信息
-(void)printAllstu{
    for(int i=0;i<[allStu count];i++)
    { Student * nows=[allStu objectAtIndex:i];
        [nows printInfo];
    }
}
//按学号查找学生信息
-(Student *)findStu:(int)num{
    for(int i=0;i<[allStu count];i++)
        { Student * nows=[allStu objectAtIndex:i];
            if(nows.number==num){
                return nows;
            }
        }
    return nil;
}
//删除学生信息
-(void)deleteStu:(int)num{
    for(int i=0;i<[allStu count];i++)
        { Student * nows=[allStu objectAtIndex:i];
            if(nows.number==num){
                [allStu removeObjectAtIndex:i];
                return;
            }
        }

}
//保存学生信息
-(void)saveStu{
    NSData *data1=[[NSData alloc]init];
    data1=[NSKeyedArchiver archivedDataWithRootObject:allStu];
    [data1 writeToFile:@"//Users//huangzhidong//Desktop//stud.txt" atomically:YES];
    NSLog(@"保存成功");
}
//读取学生信息
-(void)readStu{
    [allStu removeAllObjects];
     NSData *data2=[NSData dataWithContentsOfFile:@"//Users//huangzhidong//Desktop//stud.txt"];
NSArray *a=[NSKeyedUnarchiver unarchiveObjectWithData:data2];
    [allStu addObjectsFromArray:a];
   
        NSLog(@"读取成功");
}
@end

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:23032次
    • 积分:485
    • 等级:
    • 排名:千里之外
    • 原创:24篇
    • 转载:13篇
    • 译文:0篇
    • 评论:0条
    文章分类