《C++大学教程》学习笔记(四)

《C++大学教程》学习笔记(四)

1.零零散散

条件运算符可以这么用:
grade >= 60 ? cout << “Passed” : cout << “Failed”;

else摇摆问题,简而言之就是编译器始终会把else同它之前最近的if联系起来,例子可以看书。

if或者if…else语句的任一部分只能包含“一条”语句,如果想要包含多条语句,那就要用花括号{ }括起来,使其构成“复合语句”/“语句块”。

所有的程序都可以用三种控制结构编写,分别是顺序、选择和循环。嵌套的控制语句是实际编程中最为常见的。

2.计数器控制的循环代码

GradeBook.h文件

#include "GradeBook.h"

int main(int argc, const char * argv[]) {

    GradeBook myGradeBook("CS Programming Learning");

    myGradeBook.displayMessage();
    myGradeBook.determineClassAverage();

    return 0;
}

GradeBook.cpp文件

#include "GradeBook.h"

GradeBook::GradeBook(string name){
    setCourseName(name);
}

void GradeBook::setCourseName(string name){

    if (name.size() <= 25)
        courseName = name;
    else{
        courseName = name.substr(0,25);            //复制子字符串:从下表0开始,长度为25
        cerr << "More than 25!\n" ;                //cerr用来输出错误信息
    }
}

string GradeBook::getCourseName() const{
    return courseName;
}

void GradeBook::displayMessage() const {
    cout << "Welcome to:" << getCourseName() << endl;
}

void GradeBook::determineClassAverage() const{
    int total = 0;
    unsigned int gradeCount = 1;

    while (gradeCount <= 10) {
        cout << "Enter grade: ";
        int grade = 0;
        cin >> grade;
        total += grade;
        gradeCount++;
    }

    int average = total / 10;

    cout << "Total: " << total << endl;
    cout << "Average: " << average << endl;
}

main.cpp文件

#include "GradeBook.h"

int main(int argc, const char * argv[]) {

    GradeBook myGradeBook("CS Programming Learning");

    myGradeBook.displayMessage();
    myGradeBook.determineClassAverage();

    return 0;
}

3.标记控制的循环

有两个新学到的内容,第一个是通过static_cast来显式类型转换,比如:

int total = 10;
double average = static_cast<double>(total) / 3;

执行后total仍然是整形,但average是3.3333333

另一个则是在C++中通过iomanip头文件中的setprecision(设置精度)来对浮点数格式化,比如说:

cout << setprecision(2) << fixed;
cout << average << end;

这个时候输出的average就是保留小数点后两位浮点,即3.33。

除此之外,就是较为基本的编程,其中GradeBook.h文件以及main.cpp文件与之前一样,故不重复贴出。

有变化的GradeBook.cpp文件:

#include "GradeBook.h"
#include 

GradeBook::GradeBook(string name){
    setCourseName(name);
}

void GradeBook::setCourseName(string name){

    if (name.size() <= 25)
        courseName = name;
    else{
        courseName = name.substr(0,25);            //复制子字符串:从下表0开始,长度为25
        cerr << "More than 25!\n" ;                //cerr用来输出错误信息
    }
}

string GradeBook::getCourseName() const{
    return courseName;
}

void GradeBook::displayMessage() const {
    cout << "Welcome to:" << getCourseName() << endl;
}

void GradeBook::determineClassAverage() const{
    int total = 0;
    unsigned int gradeCount = 0;

    cout << "Enter grade or -1 to quit: ";
    int grade = 0;
    cin >> grade;

    while (grade != -1) {
        total += grade;
        gradeCount++;

        cout << "Enter grade or -1 to quit: ";
        cin >> grade;
    }

    if (gradeCount != 0){
        double average = static_cast<double>(total) / gradeCount;
        //static_cast显式类型转换,不改变原total的类型(int),仅生成一个total的浮点数(double)副本

        cout << "Total is: " << total << endl;
        cout << setprecision(2) << fixed;               //C++中格式化浮点数,setprecision属于iomanip头文件
        cout << "Average is: " << average << endl;
        //printf("Average is: %.2f\n",average);         //传统C式保留小数点后两位
    }
    else
        cout << "No grades were entered!" << endl;
}

你可能感兴趣的:(C++)