代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C
】
简易学生信息系统
作者:
mobilefzb
/ 发布于
2013/1/8
/
750
利用了list容器,然后进一步的封装了排序的比较方法使其可以支持成员之间按照不同的优先级进行排序~~重载运算符果然很方便……
#include <iostream> #include <list> #include <cstdio> #include <cstdlib> #include <cstring> #include <cstdarg> //设置成员的名称方便设定优先级 typedef enum { STU_NULL_MEMBER = 0, STU_NO = 1, STU_NAME = 2, STU_AGE = 3, STU_SEX = 4 } stuMemberName; #define STU_NUM 10 #define STU_AGE_START 16 #define STU_AGE_RANGE 4 #define STU_NO_START 1000 #define MAILE 0 #define FEMALE 1 #define STU_MEMBER_NUM 4 #define STU_NAME_LEN 16 typedef struct { unsigned int stuNo; char stuName[STU_NAME_LEN]; unsigned short stuAge; unsigned char stuSex; unsigned char reverse; } stuInfoS; static int memberCompareOrder[STU_MEMBER_NUM] =\ {STU_NULL_MEMBER,STU_NULL_MEMBER,STU_NULL_MEMBER,STU_NULL_MEMBER}; class stuInfo { private: unsigned int stuNo; char stuName[STU_NAME_LEN]; unsigned short stuAge; unsigned char stuSex; inline bool isMmeberEqual(int,const stuInfo &); inline bool isMemberCanCompare(int,const stuInfo &); public: stuInfo(unsigned int,const char *, unsigned short,unsigned char); ~stuInfo(); //复制构造函数 stuInfo(const stuInfo &); //转换构造函数 stuInfo(const stuInfoS &); //重载几个运算符 stuInfo &operator =(const stuInfo &); bool operator <(const stuInfo &); bool operator >(const stuInfo &); bool operator ==(const stuInfo &); bool operator >=(const stuInfo &); bool operator <=(const stuInfo &); friend std::ostream &operator <<(std::ostream &,const stuInfo &); }; stuInfo::stuInfo(unsigned int stuNo,const char *stuName, unsigned short stuAge,unsigned char stuSex) { this->stuNo = stuNo; this->stuAge = stuAge; this->stuSex = stuSex; std::memset(this->stuName,0,STU_NAME_LEN); if(NULL == stuName) std::memcpy(this->stuName,"null",4); else if(std::strlen(stuName) < STU_NAME_LEN - 1) std::strcpy(this->stuName,stuName); else std::memcpy(this->stuName,stuName,STU_NAME_LEN - 1); } stuInfo::~stuInfo() { } stuInfo::stuInfo(const stuInfoS &sis) { this->stuNo = sis.stuNo; this->stuAge = sis.stuAge; this->stuSex = sis.stuSex; std::memset(this->stuName,0,STU_NAME_LEN); std::strcpy(this->stuName,sis.stuName); } stuInfo::stuInfo(const stuInfo &si) { this->stuNo = si.stuNo; this->stuAge = si.stuAge; this->stuSex = si.stuSex; std::memset(this->stuName,0,STU_NAME_LEN); std::strcpy(this->stuName,si.stuName); } //注意变参的参数个数不定,结尾标志 //必须要自行指定 static void setMemberCompareOrder(int n,...) { va_list arg_ptr; va_start(arg_ptr,n); #if 0 //这里要注意一个问题,就是从arg_ptr //开始并不是n而是n后面的参数,所以 //说明变参至少都要传一个进去 int arg_num = va_arg(arg_ptr,int); std::cout << "arg_num: " << arg_num << std::endl; #endif for(int i = 0;i < n;i++) memberCompareOrder[i] = va_arg(arg_ptr,int); va_end(arg_ptr); } stuInfo & stuInfo::operator =(const stuInfo &si) { if(this != &si) { this->stuNo = si.stuNo; this->stuAge = si.stuAge; this->stuSex = si.stuSex; std::memset(this->stuName,0,STU_NAME_LEN); std::strcpy(this->stuName,si.stuName); } return *this; } //这里就对成员进行优先级判断 //要确保高优先级相等的情况下 //才能比较 inline bool stuInfo::isMmeberEqual(int curOrder,const stuInfo &comparedStuInfo) { switch(curOrder) { case STU_NO: if(this->stuNo == comparedStuInfo.stuNo) return true; else return false; case STU_NAME: { int ret = std::strcmp(this->stuName,comparedStuInfo.stuName); if(0 != ret) return false; else { for(int i = 0;i < STU_NAME_LEN;i++) { if(this->stuName[i] < \ comparedStuInfo.stuName[i]) return false; else if(this->stuName[i] > \ comparedStuInfo.stuName[i]) return false; } return true; } } case STU_AGE: if(this->stuAge == comparedStuInfo.stuAge) return true; else return false; case STU_SEX: if(this->stuSex == comparedStuInfo.stuSex) return true; else return false; default: break; } return false; } inline bool stuInfo::isMemberCanCompare(int orderNum,const stuInfo &comparedStuInfo) { if(0 == orderNum) return true; else if(1 == orderNum) { return isMmeberEqual(memberCompareOrder[0], comparedStuInfo); } else if(2 == orderNum) { return (isMmeberEqual(memberCompareOrder[0], comparedStuInfo) && \ isMmeberEqual(memberCompareOrder[1], comparedStuInfo)); } else if(3 == orderNum) { return (isMmeberEqual(memberCompareOrder[0], comparedStuInfo) && \ isMmeberEqual(memberCompareOrder[1], comparedStuInfo) && \ isMmeberEqual(memberCompareOrder[2], comparedStuInfo)); } return false; } bool stuInfo::operator <(const stuInfo &si) { if(this == &si) return false; else { //几个优先级 //按照优先级的顺序来比较 bool cmp_res = false; for(int i = 0;((i < STU_MEMBER_NUM) \ && (memberCompareOrder[i] \ != STU_NULL_MEMBER));i++) { switch(memberCompareOrder[i]) { case STU_NO: if(!this->isMemberCanCompare(i,si)) goto COMPARE_EXIT; if(this->stuNo < si.stuNo) cmp_res = true; else cmp_res = false; break; case STU_NAME: { if(!this->isMemberCanCompare(i,si)) goto COMPARE_EXIT; //《c++大学自学教程第7版》上说goto语句的后面 //不能包含隐式或显式初始化的变量声明语句但是 //g++里面似乎没有这样的限制 int ret = std::strcmp(this->stuName,si.stuName); //stuInfo myStu(*this); if(ret < 0) cmp_res = true; else if(ret > 0) cmp_res = false; else { int i = 0; for(;i < STU_NAME_LEN;i++) { if(this->stuName[i] < si.stuName[i]) { cmp_res = true; break; } else if(this->stuName[i] > si.stuName[i]) { cmp_res = false; break; } } //equal if(STU_NAME_LEN == i) cmp_res = false; } } break; case STU_AGE: if(!this->isMemberCanCompare(i,si)) goto COMPARE_EXIT; if(this->stuAge < si.stuAge) cmp_res = true; else cmp_res = false; break; case STU_SEX: if(!this->isMemberCanCompare(i,si)) goto COMPARE_EXIT; if(this->stuSex < si.stuSex) cmp_res = true; else cmp_res = false; break; default: break; } } COMPARE_EXIT: return cmp_res; } } bool stuInfo::operator >(const stuInfo &si) { if(this == &si) return false; else { bool cmp_res = false; for(int i = 0;((i < STU_MEMBER_NUM) \ && (memberCompareOrder[i] \ != STU_NULL_MEMBER));i++) { switch(memberCompareOrder[i]) { case STU_NO: if(!this->isMemberCanCompare(i,si)) goto COMPARE_EXIT; if(this->stuNo > si.stuNo) cmp_res = true; else cmp_res = false; break; case STU_NAME: { if(!this->isMemberCanCompare(i,si)) goto COMPARE_EXIT; int ret = std::strcmp(this->stuName,si.stuName); if(ret > 0) cmp_res = true; else if(ret < 0) cmp_res = false; else { int i = 0; for(;i < STU_NAME_LEN;i++) { if(this->stuName[i] > si.stuName[i]) { cmp_res = true; break; } else if(this->stuName[i] < si.stuName[i]) { cmp_res = false; break; } } //equal if(STU_NAME_LEN == i) cmp_res = false; } } break; case STU_AGE: if(!this->isMemberCanCompare(i,si)) goto COMPARE_EXIT; if(this->stuAge > si.stuAge) cmp_res = true; else cmp_res = false; break; case STU_SEX: if(!this->isMemberCanCompare(i,si)) goto COMPARE_EXIT; if(this->stuSex > si.stuSex) cmp_res = true; else cmp_res = false; break; default: break; } } COMPARE_EXIT: return cmp_res; } } bool stuInfo::operator >=(const stuInfo &si) { if(this == &si) return true; else if(*this == si) return true; else if(*this > si) return true; else return false; } bool stuInfo::operator <=(const stuInfo &si) { if(this == &si) return true; else if(*this == si) return true; else if(*this < si) return true; else return false; } bool stuInfo::operator ==(const stuInfo &si) { if(this == &si) return true; else { if((this->stuNo == si.stuNo) && \ (this->stuAge == si.stuAge) && \ (this->stuSex == si.stuSex)) { if(0 == std::strcmp(this->stuName, si.stuName)) { int i = 0; for(;i < STU_NAME_LEN;i++) if(this->stuName[i] != si.stuName[i]) return false; return true; } else return false; } else return false; } } std::ostream & operator <<(std::ostream &out,const stuInfo &si) { out << "------------------------------------\n" \ << "stu no: " << si.stuNo << std::endl \ << "stu name: " << si.stuName << std::endl \ << "stu age: " << si.stuAge << std::endl \ << "stu sex: " << ((si.stuSex) ? "female" : "male") << std::endl \ << "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n"; return out; } static bool stuListSortFromLow2High(stuInfo &cur,stuInfo &compare) { if(cur < compare) return true; else return false; } static bool stuListSortFromHigh2Low(stuInfo &cur,stuInfo &compare) { if(cur > compare) return true; else return false; } int main(int argc,char *argv[]) { std::list<stuInfo> stuInfoList; srand(time(NULL)); int i,j; stuInfoS newStuInfo; //stuInfo stuInfoTmp; for(i = 0;i < STU_NUM;i++) { std::memset(&newStuInfo,0,sizeof(newStuInfo)); newStuInfo.stuNo = i + STU_NO_START; newStuInfo.stuAge = STU_AGE_START + \ std::rand() % STU_AGE_RANGE; newStuInfo.stuSex = rand() % 2; newStuInfo.stuName[7] = '\0'; for(j = 0;j < 7;j++) newStuInfo.stuName[j] = 'a' + rand() % 26; stuInfoList.push_back(newStuInfo); } //打印 std::list<stuInfo>::iterator iter; for(iter = stuInfoList.begin();iter != stuInfoList.end();iter++) std::cout << *iter; //在使用之前不设置比较优先级则比较不会进行 //等于排序是输入进去的是啥,输出的也就是啥 setMemberCompareOrder(3,STU_SEX,STU_AGE,STU_NO); //进行排序升序 //stuInfoList.sort(stuListSortFromLow2High); //进行排序降序 stuInfoList.sort(stuListSortFromHigh2Low); //打印 std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"; for(iter = stuInfoList.begin();iter != stuInfoList.end();iter++) std::cout << *iter; //建立两个stuInfo然后判断它们是否相等 stuInfo stu1(1044,"张三a",17,0); stuInfo stu2(1044,"张三",17,0); if(stu1 == stu2) std::cout << "stu1 == stu2\n"; //大小比较的结果满足最近一次使用setMemberCompareOrder设置的 //优先级规则 if(stu1 >= stu2) std::cout << "stu1 >= stu2\n"; if(stu1 <= stu2) std::cout << "stu1 <= stu2\n"; std::cout << "printf list:\n"; std::cout << stu1; std::cout << stu2; return 0; }
试试其它关键字
信息系统
同语言下
.
获取手机通讯录 iOS去除数字以外的所有字符
.
异步加载音乐等资源
.
交通罚单管理系统
.
freemark实现,简单的替换
.
计算斐波那契数列
.
base64解码 包括解码长度
.
图像显示
.
冒泡排序
.
输入十进制数,输出指定进制
.
链式栈
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
mobilefzb
贡献的其它代码
(
1
)
.
简易学生信息系统
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3