0
grade.h头文件
#ifndef ACM_GRADE_H
#define ACM_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
Student_info.h头文件
#ifndef ACM_Student_info
#define ACM_Student_info
#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_hw(std::istream&, std::vector<double>&);
std::istream& read(std::istream&, Student_info&);
#endif
主函数
#include <algorithm>
#include <iomanip>
#include <ios>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
#include "grade.h"//grade函数重载声明
#include "Student_info.h"//与Student_info结构和相关的函数组装起来
using std::cin; using std::setprecision;
using std::cout; using std::sort;
using std::domain_error; using std::streamsize;
using std::string; using std::vector;
using std::max; using std::endl;
using std::istream;
//按照字母顺序排列学生
bool compare(const Student_info& x, const Student_info& y){
return x.name < y.name;
}
//求中位数
double median(vector<double> vec){
typedef 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];
}
//对Stundet_info类型的对象进行处理
double grade(const Student_info& s){
return grade(s.midterm, s.final, s.homework);//调用grade2
}//grade1
//根据期中、期末以及保存家庭作业的向量来计算学生的总成绩
double grade(double midterm, double final, const vector<double>& hw){
if(hw.size() == 0)
throw domain_error("student has done no homework");
return grade(midterm, final, median(hw));//如果家庭作业非空,则调用grade3
}//grade2
//根据期中、期末、平时成绩计算总成绩
double grade(double midterm, double final, double homework){
return 0.2 * midterm + 0.4 * final + 0.4 * homework;
}//garde3
//读入学生姓名、期中、期末成绩、家庭作业成绩
istream& read(istream& is, Student_info& s){
is >> s.name >> s.midterm >> s.final;
//如果输入的期中、期末成绩不是数字,则会把输入流标记为失败状态
read_hw(is, s.homework);//读入家庭作业
return is;
}
//从输入流中将家庭作业的成绩读入到一个vector<double>中
istream& read_hw(istream& in, vector<double>& hw){
if(in){//当前学生的姓名、期中期末成绩非法时(输入流标记为失败状态),不会读入家庭作业
//清除原先内容
hw.clear();//清除之前可能存在的成绩,重新再次拥有空向量
double x;
while(in >> x){//输入的不是成绩时,库把输入流标记为失败状态
hw.push_back(x);
}
//清除流使得输入动作对下个学生有效
in.clear();//清除所有错误标记使得输入动作可以继续,并不清除下一个存储在输入流中的姓名
}//如正常读完in,则返回in正确标记
return in;
}
int main(){
vector<Student_info> students;
Student_info record;
string::size_type maxlen = 0;//最长姓名的长度
//读入并存储所有学生的数据
while(read(cin,record)){//遇到非法输入,则结束输入,例如读入两个姓名
//找出最长姓名的长度
maxlen = max(maxlen, record.name.size());
students.push_back(record);
}
//按字母顺序排列学生记录
sort(students.begin(), students.end(), compare);
//输出学生姓名和成绩
for (vector<Student_info>::size_type i = 0; i != students.size() ; ++i) {
//输出姓名,将姓名填充值maxlen+1个字符的长度
cout << students[i].name
<< string(maxlen + 1 - students[i].name.size(),' ');
//计算并输出成绩
try {
double final_grade = grade(students[i]);//调用grade2
streamsize pre = cout.precision();
cout << setprecision(3) << final_grade
<< setprecision(pre);
}catch (domain_error e){
cout << e.what();
}
cout << endl;
}
return 0;
}
1
- 错误类型:
error: no matching function for call to ‘max’
- 原因:
max比较的两个参数的类型不匹配,一个是string::size_type (无符号整数型)
一个是int型
- 解决方法:
把maxlen的类型改为string::size_type
2
#include <iostream>
#include <iomanip>
using std::endl; using std::setw;
using std::cout; using std::cin;
int main(){
for (int i = 1; i != 101; ++i)
{
cout << setw(3) << i
<< setw(6) << i * i <<endl;
}
return 0;
}
3
出现为问题:
数字和数字的平方混在了一起
解决代码:
#include <iostream>
#include <iomanip>
#include <cmath>
using std::endl; using std::setw;
using std::cout; using std::cin;
int Get_Width(int max){
return (log10(max) + 1) + 1;//返回数值的数位数 + 1,如1000的数位数是4,加一后变成5
}
int main(){
int max;
cin >> max;
for (int i = 1; i != max; ++i)
{
cout << setw(Get_Width(max)) << i
<< setw(Get_Width(max * max)) << i * i <<endl;
}
return 0;
}
4
#include <iostream>
#include <iomanip>
#include <cmath>
using std::endl; using std::setw;
using std::cout; using std::cin;
using std::string; using std::streamsize;
using std::setprecision;
#define N 5 // 保存有效位数
#define INCREASENUM 0.6 //增加的数字步数
int GetWidth(int max){
return (log10(max) + 1);//返回数值的数位数,如1000的数位数是4
}
int GetSquareWidth(int max){
//一个数的平方的位数 <= 这个数的位数 *2
return (GetWidth(max) * 2);//返回平方数值的数位数,如1000的数位数是4
}
int main(){
double max;
cin >> max;
// cout<< GetWidth(max) << " "
// << GetSquareWidth(max) << endl;
streamsize pre;
for (double i = 1.0; i < max; i += INCREASENUM)
{
pre = cout.precision();
cout << setprecision(N)
<< setw(GetWidth(max) + 1) << i
<< setw(GetSquareWidth(max) + 1) << i * i
<<endl;
}
cout << setprecision(pre);
return 0;
}
5
#include <iostream>
#include <string>
#include <vector>
using std::cin; using std::string;
using std::endl; using std::vector;
using std::cout; using std::istream;
istream& ReadWord(istream& is, vector<string>& wd, vector<int>& cnt){
string s;
while(cin >> s){
bool found = false;
for (vector<string>::size_type i = 0; i != wd.size() ; ++i)
{
if(s == wd[i]){
found = true;
++cnt[i];
break;
}
}
if(found == false){
wd.push_back(s);
cnt.push_back(1);
}
}
return is;
}
int main(int argc, char const *argv[])
{
vector<string> word;
vector<int> count;
int wordCount = 0;
ReadWord(cin, word, count);
cout << "The word nums are " << word.size() << endl;
for (vector<string>::size_type i = 0; i != word.size() ; ++i)
{
cout << word[i] << " "
<< count[i] << " times!" << endl;
}
return 0;
}
6
Student_info.h头文件
//Student_info.h
#ifndef ACM_Student_info
#define ACM_Student_info
#include <iostream>
#include <string>
#include <vector>
struct Student_info{
std::string name;
// double midterm, final;
// std::vector<double> homework;
double score;
};
bool compare(const Student_info&, const Student_info&);
std::istream& read_hw(std::istream&, std::vector<double>&);
std::istream& read(std::istream&, Student_info&);
#endif
grade.h头文件
//grade.h
#ifndef ACM_GRADE_H
#define ACM_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
主函数
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <stdexcept>
#include <string>
#include <vector>
#include "grade.h"//grade函数重载声明
#include "Student_info.h"//与Student_info结构和相关的函数组装起来
using std::cin; using std::setprecision;
using std::cout; using std::sort;
using std::domain_error; using std::streamsize;
using std::string; using std::vector;
using std::max; using std::endl;
using std::istream;
//按照字母顺序排列学生
bool compare(const Student_info& x, const Student_info& y){
return x.name < y.name;
}
//求中位数
double median(vector<double> vec){
typedef 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];
}
//对Stundet_info类型的对象进行处理
double grade(const Student_info& s){
return s.score;
}//grade1
//根据期中、期末以及保存家庭作业的向量来计算学生的总成绩
double grade(double midterm, double final, const vector<double>& hw){
if(hw.size() == 0)
throw domain_error("student has done no homework");
return grade(midterm, final, median(hw));//如果家庭作业非空,则调用grade3
}//grade2
//根据期中、期末、平时成绩计算总成绩
double grade(double midterm, double final, double homework){
return 0.2 * midterm + 0.4 * final + 0.4 * homework;
}//garde3
//读入学生姓名、期中、期末成绩、家庭作业成绩
istream& read(istream& is, Student_info& s){
double midterm,final;
is >> s.name >> midterm >> final;
//如果输入的期中、期末成绩不是数字,则会把输入流标记为失败状态
vector<double> homework;
read_hw(is, homework);//读入家庭作业
if(cin){
s.score = grade(midterm, final, homework);
}
return is;
}
//从输入流中将家庭作业的成绩读入到一个vector<double>中
istream& read_hw(istream& in, vector<double>& hw){
if(in){//当前学生的姓名、期中期末成绩非法时(输入流标记为失败状态),不会读入家庭作业
//清除原先内容
hw.clear();//清除之前可能存在的成绩,重新再次拥有空向量
double x;
while(in >> x){//输入的不是成绩时,库把输入流标记为失败状态
hw.push_back(x);
}
//清除流使得输入动作对下个学生有效
in.clear();//清除所有错误标记使得输入动作可以继续,并不清除下一个存储在输入流中的姓名
}//如正常读完in,则返回in正确标记
return in;
}
int main(){
vector<Student_info> students;
Student_info record;
string::size_type maxlen = 0;//最长姓名的长度
//读入并存储所有学生的数据
while(read(cin,record)){//遇到非法输入,则结束输入,例如读入两个姓名
//找出最长姓名的长度
// maxlen = max(maxlen, record.name.size());
maxlen = max(record.name.size(),maxlen);
students.push_back(record);
}
//按字母顺序排列学生记录
sort(students.begin(), students.end(), compare);
//输出学生姓名和成绩
for (vector<Student_info>::size_type i = 0; i != students.size() ; ++i) {
//输出姓名,将姓名填充值maxlen+1个字符的长度
cout << students[i].name
<< string(maxlen + 1 - students[i].name.size(),' ');
//计算并输出成绩
try {
double final_grade = grade(students[i]);//调用grade2
streamsize pre = cout.precision();
cout << setprecision(3) << final_grade
<< setprecision(pre);
}catch (domain_error e){
cout << e.what();
}
cout << endl;
}
return 0;
}
7
#include <iostream>
#include <vector>
using std::cin; using std::cout;
using std::endl; using std::vector;
int main(int argc, char const *argv[])
{
vector<double> grade;
double num;
while(cin >> num){
grade.push_back(num);
}
double res = ((grade.size() % 2 == 0) ? ((grade[grade.size() / 2] + grade[(grade.size() -1) / 2]) / 2)
: grade[grade.size() / 2]) ;
cout << res << endl;
return 0;
}
8
合法
#include <iostream>
double *f()
{
static double value[] = {1,2,3,4,5};
return value;
}
int main(int argc, char const *argv[])
{
int n = 2;
double d = f() [n];//返回指向double类型value数组的指针
std::cout << "d = " << d << '\n';
return 0;
}