|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 notwolf 于 2017-8-10 20:50 编辑
题目的要求如下:
描述
在一个学生信息处理程序中,要求实现一个代表学生的类,并且所有成员变量都应该是私有的。
(注:评测系统无法自动判断变量是否私有。我们会在结束之后统一对作业进行检查,请同学们严格按照题目要求完成,否则可能会影响作业成绩。)
输入
姓名,年龄,学号,第一学年平均成绩,第二学年平均成绩,第三学年平均成绩,第四学年平均成绩。
其中姓名、学号为字符串,不含空格和逗号;年龄为正整数;成绩为非负整数。
各部分内容之间均用单个英文逗号","隔开,无多余空格。
输出
一行,按顺序输出:姓名,年龄,学号,四年平均成绩(向下取整)。
各部分内容之间均用单个英文逗号","隔开,无多余空格。
样例输入:
Tom,18,7817,80,80,90,70
样例输出:
Tom,18,7817,80
为了实现样例输入的要求,我使用的c语言的扫描集,输入是没错的,其他的就很迷了,求指点一下
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- class stu
- {
- private:
- char *name = new char[20]; //姓名
- unsigned int age; //年龄
- char *stuid = new char[15]; //学号
- unsigned int score1; //第一年的平均成绩
- unsigned int score2; //第二年的平均成绩
- unsigned int score3;
- unsigned int score4; //第三年的平均成绩
- public:
- int init(char *in_name, char * in_stuid, unsigned int in_age, unsigned int in_score1, unsigned int in_score2, unsigned int in_score3, unsigned int in_score4)
- {
- if (strlen(in_name)>20 || strlen(in_stuid)>15)
- {
- return 0; //输入的名字或者学号超过特定的长度则返回false;
- }
- else
- {
- strcpy(name, in_name);
- strcpy(stuid, in_stuid);
- age = in_age;
- score1 = in_score1;
- score2 = in_score2;
- score3 = in_score3;
- score4 = in_score4;
- }
- return 1;
- }
- int average()
- {
- return (score1 + score2 + score3 + score4) / 3;
- }
- void pritinfo()
- {
- printf("\n%s,d%,%s,%d",name,age,stuid,average()); //姓名,年龄,学号,四年平均成绩(向下取整)。
- }
- };
- int main(void)
- {
- stu xiaoming; //实例化出小明这个对象
- unsigned int score_1;
- unsigned int score_2;
- unsigned int score_3;
- unsigned int score_4;
- unsigned int age_;
- char *name_ = new char[20];
- char *stuid_ = new char[15];
- printf("请您以下面的固定的格式输入:\n姓名,年龄,学号,第一学年平均成绩,第二学年平均成绩,第三学年平均成绩,第四学年平均成绩输入\n");
- scanf("%[^,],%d,%[^,],%d,%d,%d,%ud", name_, &age_, stuid_, &score_1, &score_2, &score_3, &score_4);
- if (xiaoming.init(name_, stuid_, age_, score_1, score_2, score_3, score_4) == 0)
- {
- printf("请您按固定格式输入,否则程序默认退出!");
- exit(-1);
- }
- else
- {
- xiaoming.pritinfo();
- }
- return 0;
- }
复制代码 |
|