码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-09-14 14:56:42      阅读:317      评论:0      收藏:0      [点我收藏+]

标签:unknown   ++   浮点   precision   src   prime   hit   etl   tin   

程序清单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后应该按下回车键,然后再保存文件。

 

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

标签:unknown   ++   浮点   precision   src   prime   hit   etl   tin   

原文地址:http://www.cnblogs.com/little-monkey/p/7509597.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!