[C++ Primer Plus] 7、分支语句和逻辑运算符(一)程序清单

程序清单6.2

#include<iostream>
using namespace std;

void main() {
    char ch;
    cout << "Type, and I shall repeat.\n";
    cin.get(ch);
    while(ch != ‘.‘)
    {
        if (ch == ‘\n‘)
            cout << ch;
        else
            cout << ++ch;
        cin.get(ch);
    }
    system("pause");
}

程序清单6.5

#include<iostream>
using namespace std;

const int Size = 6;
void main() {
    float naaq[Size];
    int i = 0;
    float temp;
    cout << "First value:";
    cin >> temp;
    while (i < Size&&temp >= 0) {
        naaq[i] = temp;
        ++i;
        if (i<Size)
        {
            cout << "Next value:";
            cin >> temp;
        }
    }
    if (0 == i)
        cout << "No data!" << endl;
    else {
        cout << "Enter your NAAQ:";
        float you;
        cin >> you;
        int count = 0;
        for (int j = 0; j < i; j++)
        {
            if (naaq[j] > you)
                ++count;
        }
        cout << count << "个数字比你的大" << endl;
    }
    system("pause");
}

程序清单6.8(字符函数库cctype)

#include<iostream>
#include<cctype>
using namespace std;

void main() {
    cout << "Enter text for analysis,and type @ to terminate input."<<endl;
    char ch;
    int space = 0, digit = 0, chars = 0, punct = 0, others = 0;

    cin.get(ch);
    while (ch!=‘@‘)
    {
        if (isalpha(ch))        chars++;
        else if (isspace(ch))    space++;
        else if (isdigit(ch))    digit++;
        else if (ispunct(ch))    punct++;
        else    others++;
        cin.get(ch);
    }
    cout << chars << " letters,"
        << space << " whitespace,"
        << digit << " digit,"
        << punct << " punctuations,"
        << others << " others." << endl;
    system("pause");
}

程序清单6.13

#include<iostream>
using namespace std;
const int Max = 5;
void main() {
    double fish[Max];
    cout << "Enter the weights of your fish.\nYou may enter up to " << Max << " fish<q to terminate>." << endl;
    cout << "fish #1: ";
    int i = 0;
    while (i<Max&&cin >> fish[i])
    {
        if (++i < Max)
            cout << "fish #" << i + 1 << ": ";//i+1和++i不同,i+1对i的值没有影响
    }
    double total = 0.0;
    for (int j = 0; j < i; j++)//i=5
        total += fish[j];
    if (i == 0)
        cout << "No fish!" << endl;
    else
        cout << total / i << "=average weight of " << i << " fish" << endl;
    system("pause");
}

根据自己的习惯重新编写

#include<iostream>
using namespace std;
const int Max = 5;
void main() {
    double fish[Max],sum=0;
    cout << "Enter the weights of your fish.\nYou may enter up to " << Max << " fish<q to terminate>." << endl;
    int i;
    for (i = 0; i < Max; i++)
    {
        cout << "fish #" << i + 1 << ": ";
        if (cin >> fish[i])//输入成功返回true
            sum += fish[i];
        else
            break;
    }
    if (i == 0)
        cout << "No fish!" << endl;
    else
        cout << sum/i<< "=average weight of " << i << " fish" << endl;
    system("pause");
}

程序清单6.14

#include<iostream>
using namespace std;
const int Max = 5;
void main() {
    double golf[Max],sum=0;
    cout << "Enter your golf scores.\nYou must enter " << Max << " rounds." << endl;
    int i;
    for (i = 0; i < Max; i++)
    {
        cout << "round #" << i + 1 << ": ";
        while (!(cin>>golf[i]))
        {
            cin.clear();
            while (cin.get() != ‘\n‘)
                continue;
            cout << "Please enter a number:";
        }
        sum += golf[i];
    }
    cout << sum/Max<< "=average  score " <<Max<< " rounds" << endl;
    system("pause");
}

程序清单6.15(文本I/O)

//文件输出(对程序而言)

#include<iostream>
#include<fstream>
using namespace std;

void main() {
    char automobile[50];
    int year;
    double a_price, d_price;

    //声明ofstream对象并将其同文件关联起来
    ofstream outFile;
    outFile.open("first.txt");

    cout << "Enter the make and model of automobile:";
    cin.getline(automobile, 50);//cin.getline:不断读取,直到遇到换行符(少于50个字符),在末尾加上一个空字符,换行符被丢弃
    cout << "Enter the model year:";
    cin >> year;
    cout << "Enter the original asking price:";
    cin >> a_price;
    d_price = 0.913*a_price;

    cout << fixed;//表示用一般的方式输出浮点数,比如num=0.00001,cout输出为1e-005,加了fixed后再输出就为0.000010
    cout.precision(2);//第一位精确,第二位四舍五入,比如num = 318.15,precision(2)为3.2e+02,precision(4)为318.2
    cout.setf(ios_base::showpoint);//强制显示小数点
    cout << "Make and model: " << automobile << endl;
    cout << "Year: " << year << endl;
    cout << "Was asking $" << a_price << endl;
    cout << "Now asking $" << d_price << endl;

    outFile << fixed;
    outFile.precision(2);
    outFile.setf(ios_base::showpoint);
    outFile << "Make and model: " << automobile << endl;
    outFile << "Year: " << year << endl;
    outFile << "Was asking $" << a_price << endl;
    outFile << "Now asking $" << d_price << endl;

    outFile.close();
    system("pause");
}

程序清单6.16

//文件读入(对程序而言)

#include<iostream>
#include<fstream>//文件I/O
#include<cstdlib>//exit()
using namespace std;
const int SIZE = 90;

void main()
{
    char filename[SIZE];
    ifstream inFile;//声明ifstream对象
    cout << "Enter name of data file:";
    cin.getline(filename, SIZE);
    inFile.open(filename);//关联文件

    if (!inFile.is_open())//文件打开失败
    {
        cout << "Could not open the file " << filename << endl;
        exit(EXIT_FAILURE);
    }
    double value, sum = 0.0;
    int count = 0;

    inFile >> value;
    while (inFile.good())//输入正确
    {
        ++count;
        sum += value;
        inFile >> value;
    }
    if (inFile.eof())
        cout << "End of file reached." << endl;
    else if (inFile.fail())
        cout << "Input terminated by data misamatch." << endl;
    else
        cout << "Input terminated for unknown reason." << endl;
    if (count == 0)
        cout << "No data processed." << endl;
    else {
        cout << "Items read: " << count << endl;
        cout << "Sum: " << sum << endl;
        cout << "Average: " << sum / count << endl;
    }
    inFile.close();

    system("pause");
}

要想正确运行,首先在源代码文件夹中创建一个包含double数字的文本文件。

为何会少了最后一个数字17.5呢?

在文本文件中,输入最后的文本17.5后应该按下回车键,然后再保存文件。

时间: 2024-10-06 06:29:19

[C++ Primer Plus] 7、分支语句和逻辑运算符(一)程序清单的相关文章

C++ primer plus读书笔记——第6章 分支语句和逻辑运算符

第6章 分支语句和逻辑运算符 1. 逻辑运算符的优先级比关系运算符的优先级低. 2. &&的优先级高于||. 3. cctype中的函数P179. 4. switch(integer-expression)括号里必须是一个整数表达式,最常见的是int或char,也可以是枚举量. 5. P190~P197复习简单文件输入输出 inFile.is_open()判断文件是否成功地打开. inFile.good()当文件无法打开或输入数据不匹配或到达文件尾时返回false.

c++primerplus(第六版)编程题&mdash;&mdash;第6章(分支语句和逻辑运算符)

声明:作者为了调试方便,每一章的程序写在一个工程文件中,每一道编程练习题新建一个独立文件,在主函数中调用,我建议同我一样的初学者可以采用这种方式,调试起来会比较方便. (具体方式参见第3章模板) 1.编写一个小程序,读取键盘输入,直到遇到@符号为止,并回显输入(数字除外),同时将大写字符转换为小写,将小写字符转换为大写(别忘了cctype函数系列). #include <iostream> #include <cctype> using namespace std; void cp

python中的条件语句,分支语句以及逻辑运算符和比较运算符

python中的if与else语句可执行简单测试 如: a = 3; b = 5; if a<b:     print('a小b大') else:     print('a大b小') 答案输出: a小b大 我对条件判断if的理解:     所谓条件判断其实就是 如果 a<b 我们做相应的处理 否则(a大于b的情况) 我们对a>b做出相应的处理  我们在判断某个变量或者某个表达式 只对真的时候作出相应的处理时else是可选的 比如: name = '张三' isMarry = 'true'

[C++ Primer Plus] 8、分支语句和逻辑运算符(二)课后习题

一.复习题 3. #include<iostream> using namespace std; void main() { char ch; int c1, c2; c1 = c2 = 0; while ((ch=cin.get())!='$') { cout << ch; c1++; if (ch = '$') //注意是=,不是== c2++; cout << ch; } cout << "c1=" << c1 <

C++ Primer Plus 6th 读书笔记 - 第6章 分支语句和逻辑运算符

1. cin读取错误时对换行符的处理 1 #include <iostream> 2 3 using namespace std; 4 5 int main() { 6 double d; 7 char c; 8 cin >> d; 9 if(!cin) { 10 cout << "x" << endl; 11 cin.clear(); 12 cout << cin.get() << endl; 13 } 14 /

C++ Primer Plus第六版编程练习---第6章 分支语句和逻辑运算符

1. 1 #include <iostream> 2 #include <string> 3 #include <cctype> 4 5 int main() 6 { 7 std::string inputStr; 8 std::cout<<"Enter your character list. enter @ to end." << std::endl; 9 char ch; 10 std::string srcStr; 1

第六章 分支语句和逻辑运算符

cctype是从C语言继承来的一个与字符相关的函数原型的头文件,可以简化确定字符是否为大小写字母.数字.标点符号等工作.  ? : 运算符 cin类型不匹配或到达文件尾时将导致错误,istream对象cin返回false,用cin.clear()清除错误位,以便继续输入. 判断输入是否出错,可以用`!(cin >> a)`或`!cin`表示,失效位判断函数cin.fail() == 1也可以判断输入错误. 1 #include<iostream> 2 3 int main(){ 4

C语言学习 02运算符和分支语句

BOOL类型: 是一种表示非真即假的数据类型.只有两个初始值.(YES 1)真和假(NO 0).eg:BOOL flag = YES/NO; (注意BOOL 也是整形占4个字节,一般不赋值为整形数) 作用:用来存储关系运算符和逻辑运算符的结果,用来存储分支语句的判断条件,用来存储循环的判断条件. c语言中 非0即为真.#define YES 1 #define NO 0 运算符: a.关系运算符: >     >=     <     <=    ==   != b.逻辑运算符:&

运算符 与 分支语句:if ,else if,else;switch case

分支语句: if        else if       else      :    switch          case --如何使用 if  else if  else: Console.WriteLine("请你出拳"); --输出[请你出拳]            int a = Convert.ToInt32(Console.ReadLine()); --定义一个变量a            Random i = new Random(); --随机数