鱼C论坛

 找回密码
 立即注册
查看: 1406|回复: 0

做了一道c++的题,一运行输入就蹦

[复制链接]
发表于 2017-8-10 20:26:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 notwolf 于 2017-8-10 20:50 编辑

题目的要求如下:
描述

在一个学生信息处理程序中,要求实现一个代表学生的类,并且所有成员变量都应该是私有的。

(注:评测系统无法自动判断变量是否私有。我们会在结束之后统一对作业进行检查,请同学们严格按照题目要求完成,否则可能会影响作业成绩。)

输入

姓名,年龄,学号,第一学年平均成绩,第二学年平均成绩,第三学年平均成绩,第四学年平均成绩。

其中姓名、学号为字符串,不含空格和逗号;年龄为正整数;成绩为非负整数。

各部分内容之间均用单个英文逗号","隔开,无多余空格。

输出

一行,按顺序输出:姓名,年龄,学号,四年平均成绩(向下取整)。

各部分内容之间均用单个英文逗号","隔开,无多余空格。

样例输入:
Tom,18,7817,80,80,90,70
样例输出:
Tom,18,7817,80



为了实现样例输入的要求,我使用的c语言的扫描集,输入是没错的,其他的就很迷了,求指点一下



  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. class stu
  5. {
  6. private:
  7.         char *name = new char[20];   //姓名
  8.         unsigned int age;         //年龄
  9.         char *stuid = new char[15];   //学号
  10.         unsigned int score1;                //第一年的平均成绩
  11.         unsigned int score2;                        //第二年的平均成绩         
  12.         unsigned int score3;
  13.         unsigned int score4;                //第三年的平均成绩
  14. public:
  15.         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)
  16.         {
  17.                 if (strlen(in_name)>20 || strlen(in_stuid)>15)
  18.                 {
  19.                         return 0;               //输入的名字或者学号超过特定的长度则返回false;
  20.                 }
  21.                 else
  22.                 {
  23.                         strcpy(name, in_name);
  24.                         strcpy(stuid, in_stuid);
  25.                         age = in_age;
  26.                         score1 = in_score1;
  27.                         score2 = in_score2;
  28.                         score3 = in_score3;
  29.                         score4 = in_score4;
  30.                 }
  31.                 return 1;
  32.         }

  33.          int  average()
  34.         {
  35.                 return (score1 + score2 + score3 + score4) / 3;
  36.         }
  37.         void pritinfo()
  38.         {
  39.                 printf("\n%s,d%,%s,%d",name,age,stuid,average());         //姓名,年龄,学号,四年平均成绩(向下取整)。
  40.         }

  41. };


  42. int main(void)
  43. {
  44.         stu xiaoming;   //实例化出小明这个对象
  45.         unsigned int score_1;
  46.         unsigned int score_2;
  47.         unsigned int score_3;
  48.         unsigned int score_4;
  49.         unsigned int age_;
  50.         char *name_ = new char[20];
  51.         char *stuid_ = new char[15];
  52.         printf("请您以下面的固定的格式输入:\n姓名,年龄,学号,第一学年平均成绩,第二学年平均成绩,第三学年平均成绩,第四学年平均成绩输入\n");
  53.         scanf("%[^,],%d,%[^,],%d,%d,%d,%ud", name_, &age_, stuid_, &score_1, &score_2, &score_3, &score_4);
  54.         if (xiaoming.init(name_, stuid_, age_, score_1, score_2, score_3, score_4) == 0)
  55.         {
  56.                 printf("请您按固定格式输入,否则程序默认退出!");
  57.                 exit(-1);
  58.         }
  59.         else
  60.         {
  61.                 xiaoming.pritinfo();
  62.         }



  63.         return 0;
  64. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-24 11:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表