![]() |
#2
mmmmmmmmmmmm2012-11-26 22:15
用这个using namespace std;
![]() /* **accelerated c++ 例子3.1加强版 **计算学生成绩,家庭成绩以中值表示 */ #include <algorithm> #include <iomanip> #include <ios> #include <iostream> #include <string> #include <vector> using namespace std; int main() { //请求输入并读入学生的姓名 cout << "Please enter your first name: "; string name; cin >> name; //请求输入并读入学生的其中和期末成绩 cout << "Please enter your midterm and final exam grades: "; double midterm, final; cin >> midterm >> final; //请求输入家庭作业成绩 cout << "Enter all your homework grades, " "followed by end-of-file: "; vector<double> homework; double x; //不变式:homework 包含了所以的家庭作业成绩 while (cin >> x) { homework.push_back(x); } //检查homework是否为空 typedef vector<double>::size_type vec_sz; vec_sz size = homework.size(); if (size == 0){ cout << endl << "You must enter your grades. " << "Please try again." <<endl; return 1; } //对程序排序 sort(homework.begin(), homework.end()); //计算家庭作业成绩的中值 vec_sz mid = size/2; double median; median = (size % 2 == 0) ? (homework[mid] + homework[mid-1])/2 : homework[mid]; //计算并输出总成绩 streamsize prec = cout.precision(); cout << "Your final grade is " << setprecision(3) << 0.2*median + 0.4*final + 0.4*median //这里midterm拼错了 << setprecision(prec) << endl; return 0; } |
如题 , 不知道为啥这个书上的例子在vc++ 6.0下编译会提示错误
错误如下:
--------------------Configuration: 例子3-1加强版 - Win32 Debug--------------------
Compiling...
例子3-1加强版.cpp
d:\users\sakuralove\documents\my program\c++\accelerated.c++\例子3-1加强版.cpp(16) : error C2039: 'precision' : is not a member of 'std'
d:\users\sakuralove\documents\my program\c++\accelerated.c++\例子3-1加强版.cpp(16) : error C2873: 'precision' : symbol cannot be used in a using-declaration
d:\users\sakuralove\documents\my program\c++\accelerated.c++\例子3-1加强版.cpp(43) : error C2653: 'vector<double,class std::allocator<double> >' : is not a class or namespace name
d:\users\sakuralove\documents\my program\c++\accelerated.c++\例子3-1加强版.cpp(43) : error C2146: syntax error : missing ';' before identifier 'vec_sz'
d:\users\sakuralove\documents\my program\c++\accelerated.c++\例子3-1加强版.cpp(43) : error C2065: 'vec_sz' : undeclared identifier
d:\users\sakuralove\documents\my program\c++\accelerated.c++\例子3-1加强版.cpp(44) : error C2146: syntax error : missing ';' before identifier 'size'
d:\users\sakuralove\documents\my program\c++\accelerated.c++\例子3-1加强版.cpp(44) : error C2065: 'size' : undeclared identifier
d:\users\sakuralove\documents\my program\c++\accelerated.c++\例子3-1加强版.cpp(55) : error C2146: syntax error : missing ';' before identifier 'mid'
d:\users\sakuralove\documents\my program\c++\accelerated.c++\例子3-1加强版.cpp(55) : error C2065: 'mid' : undeclared identifier
d:\users\sakuralove\documents\my program\c++\accelerated.c++\例子3-1加强版.cpp(63) : error C2065: 'midian' : undeclared identifier
执行 cl.exe 时出错.
例子3-1加强版.obj - 1 error(s), 0 warning(s)
完整程序:
/*
**accelerated c++ 例子3.1加强版
**计算学生成绩,家庭成绩以中值表示
*/
#include <algorithm>
#include <iomanip>
#include <ios>
#include <iostream>
#include <string>
#include <vector>
using std::cin; using std::sort;
using std::cout; using std::streamsize;
using std::endl; using std::string;
using std::precision; using std::vector;
using std::setprecision;
int main()
{
//请求输入并读入学生的姓名
cout << "Please enter your first name: ";
string name;
cin >> name;
//请求输入并读入学生的其中和期末成绩
cout << "Please enter your midterm and final exam grades: ";
double midterm, final;
cin >> midterm >> final;
//请求输入家庭作业成绩
cout << "Enter all your homework grades, "
"followed by end-of-file: ";
vector<double> homework;
double x;
//不变式:homework 包含了所以的家庭作业成绩
while (cin >> x) {
homework.push_back(x);
}
//检查homework是否为空
typedef vector<double>::size_type vec_sz;
vec_sz size = homework.size();
if (size == 0){
cout << endl << "You must enter your grades. "
<< "Please try again." <<endl;
return 1;
}
//对程序排序
sort(homework.begin(), homework.end());
//计算家庭作业成绩的中值
vec_sz mid = size/2;
double median;
median = (size % 2 == 0) ? (homework[mid] + homework[mid-1])/2 : homework[mid];
//计算并输出总成绩
streamsize prec = cout.precision();
cout << "Your final grade is " << setprecision(3)
<< 0.2*midterm + 0.4*final + 0.4*midian
<< setprecision(prec) << endl;
return 0;
}