C++primer Ch1

本章主要工作是編寫了一個書店程序珠十,關(guān)于語句塊豌骏,main函數(shù)等非常基礎(chǔ)的知識不會再提汁蝶。我使用的IDE是code:blocks渐扮,由于VS2010不支持C++11就沒有用VS。書中的命令行運(yùn)行程序即在cmd中運(yùn)行掖棉,大家可以自己嘗試墓律,本文不作示范。

知識點(diǎn)1

  • 首先要認(rèn)識的iostream庫啊片,庫內(nèi)包含兩個基礎(chǔ)類型istream(輸入流)和ostream(輸出流)只锻。
  • 標(biāo)準(zhǔn)庫定義4個IO對象,cin(標(biāo)準(zhǔn)輸入), cout(標(biāo)準(zhǔn)輸出), cerr(輸出警告或錯誤), clog(輸出程序運(yùn)行時的信息)紫谷,>> 輸入運(yùn)算符齐饮,<<輸出運(yùn)算符,分別對流進(jìn)行輸入輸出操作笤昨。
  • 沒有using namespace std;時祖驱,使用命名空間內(nèi)的名字必須用std::(如std::cout)

習(xí)題1

1.1 (略)
1.2 運(yùn)行后顯示process returned -1
1.3

#include<iostream>
int main()
{
    std::cout <<" Hello, world." << std::endl;
    return 0;
}

1.4

#include<iostream>
int main()
{
    std::cout << "Enter two numbers:" << std::endl;
    int v1 = 0, v2 = 0;
    std::cin >> v1 >> v2;
    std::cout << "The product of " << v1 << " and " << v2
              << "is " << v1 * v2 <<std::endl;
    return 0;
}

1.5

#include<iostream>
int main()
{
    std::cout << "Enter two numbers:" ;
    std::cout << std::endl;
    int v1 = 0, v2 = 0;
    std::cin >> v1 >> v2;
    std::cout << "The sum of " ;
    std::cout << v1 ;
    std::cout << " and ";
    std::cout << v2 ;
    std::cout << " is " ;
    std::cout << v1 + v2 ;
    std::cout << std::endl;
    return 0;
}

1.6 不合法,去掉中間的分號瞒窒,后者后兩句最前面加std::cout

知識點(diǎn)2

  • 注釋捺僻,同C //單行 /* /多行,注意:注釋是不能嵌套的

習(xí)題2

1.7 自己試一下崇裁,應(yīng)該都會有這句
warning: "/" within comment [-Wcomment]|
1.8 第一個和第二個合法匕坯,后兩個均不合法,第三個最后加一個 " 就可以打印
/拔稳,第四個是<<后面全被注釋掉

知識點(diǎn)3

  • while循環(huán):while(condition){ statement }
  • for循環(huán):for(初始化計數(shù)變量;判斷條件;++)葛峻, 通常都用前++形式防止出錯
  • 讀取不定量數(shù)據(jù)常用while(cin >> val),輸入ctrl+z可終止輸入
  • if語句巴比,和C中if基本相同

習(xí)題3

1.9

#include<iostream>

int main()
{
    int sum = 0, val = 50;
    while(val <= 100)
    {
        sum += val;
        val++;
    }
    std::cout << "The sum of 50 to 100 is " << sum
              << std::endl;

    return 0;
}

1.10

#include<iostream>

int main()
{
    int i = 10;
    while(i >= 0)
    {
        std::cout << i-- << " ";
    }

    return 0;
}

1.11

#include<iostream>

int main()
{
    int i, j;
    std::cout << "Enter two numbers:" << std::endl;
    std::cin >> i >> j;
    std::cout << "The numbers between " << i << " and "
              << j << "is ";
    while(i < j)
    {
        std::cout << i << " ";
        i++;
    }
    while(i > j)
    {
        std::cout << j << " ";
        j++;
    }
    return 0;

}

1.12 從-100一直加到100 sum的終值是0
1.13

#include<iostream>
int main()
{
    //50到100相加
    int sum = 0;
    int v1, v2;

    for(int i = 50; i <= 100; ++i)
        sum += i;
    std::cout << "Sum of 50 to 100 inclusive is "
              << sum << std::endl;
    //輸出10到0
    for(int i = 10; i >= 0; i--)
        std::cout << i << " ";
    std::cout << std::endl;
    //輸出兩個整數(shù)范圍內(nèi)所有整數(shù)
    std::cout << "Enter two numbers: " << std::endl;
    std::cin >> v1 >> v2;
    for(v1; v1 < v2; v1++)
        std::cout << v1 << " ";
    for(v2; v2 < v1; v2++ )
        std::cout << v2 << " ";

    return 0;
}

1.14 for循環(huán)指定次數(shù)术奖,while循環(huán)可不固定次數(shù)
1.15 14頁指出了語法錯誤,聲明錯誤轻绞,類型錯誤采记。
1.16

#include<iostream>
int main()
{
    int sum = 0, value = 0;
    while(std::cin >> value)
        sum += value;
    std::cout << "Sum is: " << sum << std::endl;

    return 0;
}

1.17 輸出結(jié)果為輸入的同樣的數(shù)出現(xiàn)了多少次,例如都是1的話
1 occurs 7 times
1.18

#include<iostream>

int main()
{
    int currVal = 0, val = 0;
    if (std::cin >> currVal){
        int cnt = 1;
        while (std::cin >> val){
            if (val == currVal)
                ++cnt;
            else{
                std::cout << currVal << " occurs "
                          << cnt << "times" <<std::endl;
                currVal = val;
                cnt = 1;

            }
        }//while
        std::cout << currVal << " occurs "
                  <<cnt << " times " << std::endl;
    }//if

    return 0;
}

1.19

#include<iostream>

int main()
{
    int i, j;
    std::cout << "Enter two numbers:" << std::endl;
    std::cin >> i >> j;
    std::cout << "The numbers between " << i << " and "
              << j << "is ";
    if (i < j){

        while(i <= j)
        {
            std::cout << i << " ";
            i++;
        }
    }
    else{
        while(i >= j)
        {
            std::cout << j << " ";
            j++;
        }
    }
    return 0;

}

知識點(diǎn)4

  • 類政勃,定義了一個數(shù)據(jù)結(jié)構(gòu)和與其關(guān)聯(lián)的一組操作唧龄。類類型是面向?qū)ο蟪绦蛘Z言一個的特征
  • 成員函數(shù)(類中的函數(shù)),通過點(diǎn)運(yùn)算符訪問奸远。

習(xí)題4

Sales_item.h


#ifndef SALESITEM_H
#define SALESITEM_H
#include <iostream>
#include <string>
class Sales_item
{
public:
    Sales_item(const std::string &book):isbn(book),units_sold(0),revenue(0.0){}
    Sales_item(std::istream &is){ is >> *this;}
    friend std::istream& operator>>(std::istream &,Sales_item &);
    friend std::ostream& operator<<(std::ostream &,const Sales_item &);
public:
    Sales_item & operator+=(const Sales_item&);
public:
    double avg_price() const;
    bool same_isbn(const Sales_item &rhs)const
    {
        return isbn == rhs.isbn;
    }
    Sales_item():units_sold(0),revenue(0.0){}
public:
    std::string isbn;
    unsigned units_sold;
    double revenue;
};

using std::istream;
using std::ostream;
Sales_item operator+(const Sales_item &,const Sales_item &);
inline bool operator==(const Sales_item &lhs,const Sales_item &rhs)
{
    return lhs.units_sold == rhs.units_sold && lhs.revenue == rhs.revenue && lhs.same_isbn(rhs);
}
inline bool operator!=(const Sales_item &lhs,const Sales_item &rhs)
{
    return !(lhs == rhs);
}

inline Sales_item & Sales_item::operator +=(const Sales_item &rhs)
{
    units_sold += rhs.units_sold;
    revenue += rhs.revenue;
    return *this;
}
inline Sales_item operator+(const Sales_item &lhs,const Sales_item &rhs)
{
    Sales_item ret(lhs);
    ret += rhs;
    return ret;
}
inline istream& operator>>(istream &in,Sales_item &s)
{
    double price;
    in >> s.isbn >> s.units_sold >> price;
    if(in)
        s.revenue = s.units_sold * price;
    else
        s = Sales_item();
    return in;
}
inline ostream& operator<<(ostream &out,const Sales_item &s)
{
    out << s.isbn << "\t" <<s.units_sold << "\t" << s.revenue << "\t" << s.avg_price();
    return out;
}
inline double Sales_item::avg_price() const
{
    if(units_sold)
        return revenue/units_sold;
    else
        return 0;
}
#endif

1.20

#include<iostream>
#include"Sales_item.h"
int main()
{
    Sales_item book;
    while(std::cin >> book){
            std::cout << book << std::endl;
    }


    return 0;
}

1.21

#include<stdio.h>
#include"Sales_item.h"

int main()
{
    Sales_item book1, book2;
    std::cin >> book1 >> book2;
    if(book1.isbn == book2.isbn)
        std::cout << book1 + book2 << std::endl;
    else
        std::cout << "False input!";

    return 0;
}

1.22

#include<stdio.h>
#include"Sales_item.h"
int main()
{
    Sales_item book, total;
    std::cin >> total;
    while(std::cin >> book && (total.isbn == book.isbn))
    {
        total += book;
    }                                                   //不太懂兩次ctrl+z才可退出
    std::cout << total << std::endl;

    return 0;
}

1.23选侨,24

#include<stdio.h>
#include"Sales_item.h"
int main()
{
    Sales_item currbook, book;
    if (std::cin >> currbook){
        int cnt = 1;
        while(std::cin >> book){
            if (currbook.isbn == book.isbn)
                cnt++;
            else{
                std::cout << currbook.isbn << " has sold " <<cnt
                          << "times" << std::endl;
                currbook = book;
                cnt = 1;
            }
        }
        std::cout << currbook.isbn << " has sold " <<cnt
                          << "times" << std::endl;
    }

    return 0;
}

1.25

#include<iostream>
#include"Sales_item.h"
int main()
{
    Sales_item total;

    if(std::cin >> total){
        Sales_item trans;
        while(std::cin >> trans){
            if(total.isbn == trans.isbn)
                total += trans;
            else{
                std::cout << total << std::endl;
                total = trans;
            }
        }
        std::cout << total << std::endl;
    }
    else{
            std::cerr << "No data!" << std::endl;
            return -1;
    }

    return 0;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末掖鱼,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子援制,更是在濱河造成了極大的恐慌戏挡,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,651評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件晨仑,死亡現(xiàn)場離奇詭異褐墅,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)洪己,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,468評論 3 392
  • 文/潘曉璐 我一進(jìn)店門妥凳,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人答捕,你說我怎么就攤上這事逝钥。” “怎么了拱镐?”我有些...
    開封第一講書人閱讀 162,931評論 0 353
  • 文/不壞的土叔 我叫張陵艘款,是天一觀的道長。 經(jīng)常有香客問我沃琅,道長哗咆,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,218評論 1 292
  • 正文 為了忘掉前任益眉,我火速辦了婚禮晌柬,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘郭脂。我一直安慰自己年碘,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,234評論 6 388
  • 文/花漫 我一把揭開白布展鸡。 她就那樣靜靜地躺著盛泡,像睡著了一般。 火紅的嫁衣襯著肌膚如雪娱颊。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,198評論 1 299
  • 那天凯砍,我揣著相機(jī)與錄音箱硕,去河邊找鬼。 笑死悟衩,一個胖子當(dāng)著我的面吹牛剧罩,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播座泳,決...
    沈念sama閱讀 40,084評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼惠昔,長吁一口氣:“原來是場噩夢啊……” “哼幕与!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起镇防,我...
    開封第一講書人閱讀 38,926評論 0 274
  • 序言:老撾萬榮一對情侶失蹤肩狂,失蹤者是張志新(化名)和其女友劉穎侨歉,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,341評論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡杭措,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,563評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了镀迂。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片屁柏。...
    茶點(diǎn)故事閱讀 39,731評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖扑毡,靈堂內(nèi)的尸體忽然破棺而出胃榕,到底是詐尸還是另有隱情,我是刑警寧澤瞄摊,帶...
    沈念sama閱讀 35,430評論 5 343
  • 正文 年R本政府宣布勋又,位于F島的核電站,受9級特大地震影響泉褐,放射性物質(zhì)發(fā)生泄漏赐写。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,036評論 3 326
  • 文/蒙蒙 一膜赃、第九天 我趴在偏房一處隱蔽的房頂上張望挺邀。 院中可真熱鬧,春花似錦跳座、人聲如沸端铛。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,676評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽禾蚕。三九已至,卻和暖如春狂丝,著一層夾襖步出監(jiān)牢的瞬間换淆,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,829評論 1 269
  • 我被黑心中介騙來泰國打工几颜, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留倍试,地道東北人。 一個月前我還...
    沈念sama閱讀 47,743評論 2 368
  • 正文 我出身青樓蛋哭,卻偏偏與公主長得像县习,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,629評論 2 354

推薦閱讀更多精彩內(nèi)容