西科人BBS_西安科技大学学生论坛

 找回密码
 立即注册
楼主: windyuwen

寻找C++高手

[复制链接]
发表于 2006-3-28 00:02:39 | 显示全部楼层
倒~~果然,还不只一个错误。。。。
明天再说。。。早上还上课的。
发表于 2006-3-28 00:08:02 | 显示全部楼层
哥哥~~你的是啥编译器?太神奇了。
发表于 2006-3-28 16:29:54 | 显示全部楼层
先给你指出你的那三处语法错误吧。
第一个就像我前面说的那样,students.name是个错误的用法。因为你的students是一个vectoer对象,对象的成员中没有name,name更不是vector模版类的成员。name是Student_info这个结构类型的成员。你如何从此对象直访问用彼对象的成员呢?我暂时改成students.name。
第二个  double final_grade = grade(students);students是什么类型的前面已经说过了,可看你的头文件,那里有对应的函数声明?如果里面是一个结构体,或者是students这结果就对了,在或者是students.back()之类的也对,这就要看你程序的意思了。我暂时改成最后一种。
第三个,你在调用一个students的成员函数时忘记了给后面加括号,也就是没有参数列表(尽管它的参数列表是空的,但有了括号编译器才知道这是个函数而不是其他什么东西)。这个错在哪里我就不说了。
还有个警告以后再说吧。
如此处理这些错误之后,才能得到你上面说的那种状况。现在我看看你的逻辑吧。回头再说。
发表于 2006-3-28 16:32:02 | 显示全部楼层
靠,这个论坛的显示太扯淡了。
发表于 2006-3-28 16:36:42 | 显示全部楼层
[s:22] 看到cout<<setw(maxlen+1)<<students.name;后面都变成斜体字,我明白了,这两个[]被帖子自动解释成wind code了。
发表于 2006-3-28 16:37:08 | 显示全部楼层
[s:36]你把你那段代码重新编辑一下,勾掉Wind Code自动转换。
发表于 2006-3-28 21:29:49 | 显示全部楼层
而且当输入c @ @后,因为@为字符类型,而s.midterm为double类型,会使输入流is读取失败,程序本应正常退出的,但运行时出现了最后6条语句,而且程序结束不了。

把read_hw中第一个in.clear去掉就可以结束了。。。。。
发表于 2006-3-28 21:37:06 | 显示全部楼层
至于,你那个导致输入失败的部分,我就不管了。
 楼主| 发表于 2006-3-28 23:37:22 | 显示全部楼层
首先谢谢楼上的答复!
关于你的答复有几个问题:
1、我使用的编译器是VC++6.0,由于这个版本并没有完全支持标准C++,所以编译时会出现
E:\C & C++\C++程序\Accelerated C++\4_sample2.cpp(25) : error C2065: &#39;max&#39; :

undeclared identifier
Error executing cl.exe.
4_sample2.obj - 1 error(s), 0 warning(s)
这个错误,只要在VC的安装目录Microsoft Visual Studio\VC98\Include中,找到algorithm这个文件,把以下代码加进去后再编译就不会出现错误了。
template <class T> inline T max(const T& a, const T& b)
{
return (a > b) ? a : b;
}

template <class T> inline T min(const T& a, const T& b)
{
return (a < b) ? a : b;
}

2、不好意思,没怎么用过这个论坛,所以导致了student.name的问题,我在下面重新粘了
一下代码。

3、如果将in.clear()删掉的话,会导致in输入流一直处于错误的状态,从而无法录入下一
个学生的数据,所以删除in.clear()是不可行的,而且也改变了程序的意图。

4、我的问题仍是
在标有//*的地方,按程序的意图来说是不该出现的,但不知为什么会出现这两条语句。
而且当输入c @ @后,因为@为字符类型,而s.midterm为double类型,会使输入流is读取失败(我的理解,不知道对不对),程序本应正常退出的,但运行时出现了最后6条语句,而且程序结束不了。
希望有人帮我解答,谢谢了。
 楼主| 发表于 2006-3-28 23:42:37 | 显示全部楼层
第一个文件:grade.h
#ifndef  GUARD_grade_h
#define GUARD_grade_h

//grade.h
#include<vector>
#include "Student_info.h"

double grade(double,double,double);
double grade(double,double,const std::vector<double>&);
double grade(const Student_info&);

#endif


第二个文件:median.h
#ifndef GUARD_median_h
#define GUARD_median_h

//median.h
#include<vector>
double median(std::vector<double>);

#endif


第三个文件:Student_info.h
#ifndef  GUARD_Student_info
#define GUARD_Student_info

//Student_info.h
#include<iostream>
#include<string>
#include<vector>

struct Student_info
{
  std::string name;
  double midterm,final;
  std::vector<double> homework;
};

bool compare(const Student_info&,const Student_info&);
std::istream& read(std::istream&,Student_info&);
std::istream& read_hw(std::istream&,std::vector<double>&);
#endif


第四个文件:grade.cpp
//grade.cpp
#include <stdexcept>
#include <vector>
#include "grade.h"
#include "median.h"
#include "Student_info.h"

using namespace std;

double grade(double midterm,double final,double homework)
{
  return 0.2 * midterm + 0.4 * final + 0.4 * homework;
}

double grade(double midterm,double final,const std::vector<double>& hw)
{
  if(hw.size==0)
    throw domain_error("Student has done no homework");
  return grade(midterm,final,median(hw));
}

double grade(const Student_info& s)
{
  return grade(s.midterm,s.final,s.homework);
}

第五个文件:median.cpp
//median.cpp
#include <algorithm>
#include <stdexcept>
#include <vector>

using namespace std;

double median(std::vector<double> vec)
{
  typedef std::vector<double>::size_type vec_sz;

  vec_sz size = vec.size();
  if(size==0)
    throw domain_error("median of an empty vector");

  sort(vec.begin(),vec.end());

  vec_sz mid = size/2;

  return size%2 == 0 ? (vec[mid] + vec[mid-1]) / 2 : vec[mid];
}

第六个文件:Student_info.cpp
//Student_info.cppp
#include "Student_info.h"

using namespace std;

bool compare(const Student_info& x,const Student_info& y)
{
  return x.name < y.name;
}

istream& read(istream& is,Student_info& s)
{
  cout<<"Please enter the student&#39;s name,"
    "midterm grade and fianl grade:"<<endl;
  //读入并存储学生的姓名以及期中、期末考试成绩
  is>>s.name>>s.midterm>>s.final;

  read_hw(is,s.homework);

  return is;
}

//从输入流中将家庭作业的成绩读入到一个vector<double>中
istream& read_hw(istream& in,vector<double>& hw)
{
  in.clear();
  if(in)
  {
    //清除原先的内容
    hw.clear();

    cout<<"Please enter your homework grades:"<<endl;
    //读家庭作业成绩
    double x;
    while(in>>x)
      hw.push_back(x);

    //清除流以使输入动作对下一个学生有效
    in.clear();
  }
  return in;
}

主文件:main.cpp
#include <algorithm>
#include <iomanip>
#include <ios>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
#include "grade.h"
#include "Student_info.h"

using namespace std;

int main()
{
  std::vector<Student_info> students;
  Student_info record;
  string::size_type maxlen = 0;

  //读并存储所有学生的数据
  //students包含了所有学生记录
  //max包含了students中最长的姓名
  while (read(cin,record))
  {
    //找出最长的姓名的长度
    maxlen = max(maxlen,record.name.size());
    students.push_back(record);
  }

  //按字母顺序排列记录
  sort(students.begin(),students.end(),compare);

  //输出姓名和成绩
  for (std::vector<Student_info>::size_type i=0;
    i!=students.size();++i)
    {
      //输出姓名,把姓名填充至maxlen+1个字符的长度
      cout<<setw(maxlen+1)<<students.name;

      //计算并输出成绩
      try
      {
        double final_grade = grade(students);
        streamsize prec = cout.precision();
        cout<<setprecision(3)<<final_grade
          <<setprecision(prec);
      }
      catch (domain_error e)
      {
        cout<<e.what();
      }
      cout<<endl;
    }
    return 0;
}
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|手机版|Archiver|西科人BBS ( 粤ICP备20049523号-3 )

GMT+8, 2026-3-23 22:05 , Processed in 0.042135 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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