Introduction to Programming with C++ Comprehension Version

《Introduction to Programming with C++ Comprehension Version》

 (美) Y.Daniel Liang 阿姆斯特朗亚特兰大州大学    普度大学

这本书主要分为三部分:

Part 1. 程序设计基础 

Part 2. 面向对象程序设计

Part 3. 数据结构


初期打算一两天搞定第一部分,然后慢慢地搞定第二三部分,所以不急,慢慢来吧( 持续更新吧 )



Part 1. 程序设计基础


第一章 计算机、程序和C++语言简介

主要讲的是编译环境什么的,这本书的程序都是在Dev-C++写的

然后扯到一些UNIX平台和Windows命令行开发C++





第二章 基本数据类型和运算

讲的还是和其他书一样的东西,如题,而且英文版的书翻译过来,总感觉程序 里的一些变量名啊很长,很无语

看到一个比较有意思的实例研究:


目的:显示当前时间

如果在 totalsecond 后面加上一个 8*3600 最后得到的就是北京时间的 (GMT+8:00 )

//****************************
//      输出当前时间
// 形式: GMT
// 北京时间: GMT +8:00
//****************************
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
    int totalsecond=time(0);
    //int totalsecond=time(0)+8*60*60; // 北京时间
    int currentsecond=totalsecond%60;
    int totalmimute=totalsecond/60;
    int currentmimute=totalmimute%60;
    int totalhour=totalmimute/60;
    int currenthour=totalhour%24;
    cout<<"Current time is "<<currenthour<<":"
    <<currentmimute<<":"<<currentsecond<<" GMT"<<endl;
    return 0;
}






第三章 分支语句

主要讲的是if和else if以及switch,case等语句,然后就是各种运算符了

还是觉得里面的实例研究比较有意思:

实例1:猜生日

方法:简单的 if 语句

发现其实还是很有技巧的,写这个代码的人,膜拜之

//*********************
//     猜生日
//主要: 简单的if语句
//*********************
#include <iostream>
using namespace std;
int main()  
{
    int date=0;
    char answer;

    cout<<"Is your birth date in Set1?"<<endl;
    cout<<"16 17 18 19\n"<<
          "20 21 22 23\n"<<
          "24 25 26 27\n"<<
          "28 29 30 31"<<endl;
    cout<<"Enter N for NO and Y for Yes: ";
    cin>>answer;

    if(answer=='Y')
    date+=16;

    cout<<"\nIs your birth date in Set2?"<<endl;
    cout<<" 8  9 10 11\n"<<
          "12 13 14 15\n"<<
          "24 25 26 27\n"<<
          "28 29 30 31"<<endl;
    cout<<"Enter N for NO and Y for Yes: ";
    cin>>answer;

    if(answer=='Y')
    date+=8;

    cout<<"\nIs your birth date  in Set3?"<<endl;
    cout<<" 1  3  5  7\n"<<
          " 9 11 13 15\n"<<
          "17 19 21 23\n"<<
          "25 27 29 31"<<endl;
    cout<<"Enter N for NO and Y for Yes: ";
    cin>>answer;

    if(answer=='Y')
    date+=1;

    cout<<"\nIs your birth date  in Set4?"<<endl;
    cout<<" 2  3  6  7\n"<<
          "10 11 14 15\n"<<
          "18 19 22 23\n"<<
          "26 27 30 31"<<endl;
    cout<<"Enter N for NO and Y for Yes: ";
    cin>>answer;

    if(answer=='Y')
    date+=2;

    cout<<"\nIs your birth date  in Set5?"<<endl;
    cout<<" 4  5  6  7\n"<<
          "12 13 14 15\n"<<
          "20 21 22 23\n"<<
          "28 29 30 31"<<endl;
    cout<<"Enter N for NO and Y for Yes: ";
    cin>>answer;

    if(answer=='Y')
    date+=4;

    cout<<"Your birth date is "<<date<<endl;
    return 0;
}


还有一个比较有意思的实例研究题:

实例2: 一个数学学习工具

问题:随机生成一个简单算数题,重点在随机(没用过)

还是比较简单的;

//****************************************
//        一个数学学习工具
//       主要:应用随机函数
//    种子值的地方用到了time(0)
//这样不同时间就可以产生不同的问题,妙啊
//****************************************
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
    srand(time(0));// 用返回格林威治的时间产生随机数。。。妙啊
    int number1=rand()%10;
    int number2=rand()%10;
    if(number1<number2)
    {
        int temp=number1;
        number1=number2;
        number2=temp;
    }
    cout<<"What is "<< number1<<" - "<<number2<<" ?"<<endl;
    int answer;
    cin>>answer;
    if(number1-number2==answer)
        cout<<"You are correct!";
    else
        cout<<"Your answer is wrong.\n"<<number1<<"-"<<
        number2<<"should be"<<(number1-number2)<<endl;
    return 0;
}





第四章 循环

这章讲的东西当然离不开while,do-while,和for循环语句的,以及各种嵌套啊,还有break和continue语句的应用

基础知识还是不累赘了,还是写写比较有意思的实例吧

1.实例: 一个改进的数学学习工具

在前一个用随机数产生的数学问题上加了一个确定次数的循环,然后统计测试时间

//****************************************
//        一个改进的数学学习工具
//     主要:统计测试,正确数及时间
//****************************************
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cmath>
using namespace std;
int main()
{
    int correctcount=0;
    int count=0;
    long starttime=time(0);
    while(count<10)
    {
        srand(time(0));
        int number1=rand()%10;
        int number2=rand()%10;
        if(number1<number2)
        {
            int tmp=number1;
            number1=number2;
            number2=tmp;
        }
        cout<<"What is "<<number1<<" - "<<number2<<" ?"<<endl;
        int answer;
        cin>>answer;
        if(number1-number2==answer)
        {
            cout<<"You are correct!\n";
            correctcount++;
        }
        else
        cout<<"Your answer is wrong.\n"<<number1<<" - "<<
        number2<<" should be "<<(number1-number2)<<endl;
        count++;
    }
    long endtime=time(0);
    long testtime=endtime-starttime;
    cout<<"Corret cout is "<<correctcount<<"\nTest time is "
    <<testtime<<" second\n";
    return 0;
}



然后之后的几个循环的应用,个人觉得他写得不怎么滴,尤其是关于素数的随便丢个模版都比他写得好

后面几个关于简单文件的输入输出,之前学C的时候没有好好学,所以在这里还是写一写:

//*************************
//   SimpleFileOutput.cpp
//
//*************************
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    ofstream output;// Create a file
    output.open("number.txt");//指定写入的文件
    output<<95<<" "<<56<<" "<<34;
    output.close();// 关闭文档
    cout<<"Done"<<endl;
    return 0;
}

//************************
//   SimpleFileInput.cpp
//
//************************
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    ifstream input;
    input.open("number.txt");
    int score1,score2,score3;
    input>>score1;
    input>>score2;
    input>>score3;
    cout<<"Total score is "<<score1+score2+score3<<endl;
    input.close();
    cout<<"Done"<<endl;
    return 0;
}


//************************
//   TestEndOfFile.cpp
//
//************************
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
    ifstream input;
    input.open("number.txt");
    int score;
    int total=0;
    while(!input.eof())
    {
        input>>score;
        cout<<score<<endl;
        total+=score;
    }
    cout<<"Total score is "<<total<<endl;
    input.close();
    cout<<"Done"<<endl;
    return 0;
}





第五章 函数

主要还是瞎扯,基础知识略过


实例研究:生成随机字符
重点还是随机,以及不大熟悉的字符串的啥

//****************************************
//    实例研究:生成随机字符
//
// a+rand()%b为返回a~(a+b)之间的随机值
//
//****************************************
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;
//主函数只用到一个函数,另外几个丢出来玩哇
char getRandomCharacter(char ch1,char ch2)
{
    return static_cast<char>(ch1+rand()%(ch2-ch1+1));
}

char getRandomLowerCaseLetter()
{
    return getRandomCharacter('a','z');
}

char getRandomUpperCaseLetter()
{
    return getRandomCharacter('A','Z');
}

char getRandomDigitCharacter()
{
    return getRandomCharacter('0','9');
}

char getRandomCharacter()
{
    return getRandomCharacter(0,127);
}

int main()
{
    const int NUMBER_OF_CHARS=175;
    const int CHARS_PER_LINE=25;
    srand(time(0));
    for(int i=0;i<NUMBER_OF_CHARS;i++)
    {
        char ch=getRandomLowerCaseLetter();
        if((i+1)%CHARS_PER_LINE==0)
        cout<<ch<<endl;
        else
        cout<<ch;
    }
    return 0;
}

然后看到一个用关于LeapYear的函数的代码,笑了,不解释

看到和教材上提到的内联函数,做个笔记先:内联函数是不会被调用的,实际上,编译器将其代码复制到了每个调用点上。其实也没多大的意义,呵呵...





第六章 数组

主要讲了一些数组的简单操作,以及顺序搜索、二分搜索,快速排序,选择排序和插入排序

二分主要还是思想吧,丢个代码先:
//********************
//    二分搜索
//********************
int binarySearch(int list[],int key,int arraySize)
{
    int low=0;
    int high=arraySize-1;
    while(high>=low)
    {
        int mid=(low+high)/2;
        if(key<list[mid])
            high=mid-1;
        else if(key==list[mid])
            return mid;
        else
            low=mid+1;
    }
    return -low-1;
}

排序神马的一般书上都有些,所以就挂个以前没看过的,插入排序(按算法导论写的)

#include <iostream>
#define ElemType int
#define maxn 1000000
using namespace std;
int a[maxn];
void InsertionSort(ElemType a[],int n)
{
    int i,j;
    ElemType tmp;
    for(i=1;i<=n;i++)
    {
        tmp=a[i];
        for(j=i;j>0&&a[j-1]>tmp;j--)
        a[j]=a[j-1];
        a[j]=tmp;
    }
}
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    cin>>a[i];
    InsertionSort(a,n);
    for(int i=1;i<=n-1;i++)
    cout<<a[i]<<" ";
    cout<<a[n]<<endl;
    return 0;
}

看下有趣的实例研究:
实例研究:和前面的一个一样,猜生日
不过这次是开了一个三维数组,觉得有趣,所以贴上代码:
//***********************************
//   (三维数组)猜生日
//
//***********************************
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    int date=0;
    char answer;
    int dates[5][4][4]={
        {{16,17,18,19},
         {20,21,22,23},
         {24,25,26,27},
         {28,29,30,31}},
        {{ 8, 9,10,11},
         {12,13,14,15},
         {24,25,26,27},
         {28,29,30,31}},
        {{ 1, 3, 5, 7},
         { 9,11,13,15},
         {17,19,21,23},
         {25,27,29,31}},
        {{ 2, 3, 6, 7},
         {10,11,14,15},
         {18,19,22,23},
         {26,27,30,31}},
        {{ 4, 5, 6, 7},
         {12,13,14,15},
         {20,21,22,23},
         {28,29,30,31}}};
    for(int i=0;i<5;i++)
    {
        cout<<"Is your birth date in Set"<<(i+1)<<"?"<<endl;
        for(int j=0;j<4;j++)
        {
            for(int k=0;k<4;k++)
            cout<<dates[i][j][k]<<" ";
            cout<<endl;
        }
        cout<<"\nEnter N for No and Y for Yes: ";
        cin>>answer;
        if(answer=='Y')
        date+=dates[i][0][0];
    }
    cout<<"Your birth date is "<<date<<endl;
    return 0;
}




第七章 指针和C字符串
介绍了一下指针的基础使用以及访问地址之类的东西,还有动态分配和字符串的处理
与C不同的提出一些,分配内存空间用new,释放由new分配的内存空间,只需在指针前使用delete,若是数组,则用delete [ ] list 的形式
实例分析:随机产生字符,统计每个字母出现的个数
//************************************************
//    统计随机产生字符串中字母出现的次数
//
//    基本是按书上敲的,结果可以,但是
//程序结束的时候还是有点问题,谁闲的话帮忙看下吧
//
//*************************************************
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;
//虽然主函数只用到一个函数,但还是丢几个出来玩哇
char getRandomCharacter(char ch1,char ch2)
{
    return static_cast<char>(ch1+rand()%(ch2-ch1+1));
}

char getRandomLowerCaseLetter()
{
    return getRandomCharacter('a','z');
}

char getRandomUpperCaseLetter()
{
    return getRandomCharacter('A','Z');
}

char getRandomDigitCharacter()
{
    return getRandomCharacter('0','9');
}

char getRandomCharacter()
{
    return getRandomCharacter(0,127);
}

const int NUMBER_OF_LETTERS=100;
char *createArray();
void displayArray(char []);
int *countLetters(char []);
void displayCounts(int []);

int main()
{
    char *chars=createArray();
    cout<<"The lowercase letters are: "<<endl;
    displayArray(chars);
    int *counts=countLetters(chars);
    cout<<endl;
    cout<<"The occurrences of each letter are: "<<endl;
    displayCounts(counts);
    return 0;
}

char *createArray()
{
    char *chars=new char[NUMBER_OF_LETTERS];
    srand(time(0));
    for(int i=0;i<NUMBER_OF_LETTERS;i++)
        chars[i]=getRandomLowerCaseLetter();
    return chars;
}

void displayArray(char chars[])
{
    for(int i=0;i<NUMBER_OF_LETTERS;i++)
    {
        if((i+1)%20==0)
            cout<<chars[i]<<" "<<endl;
        else
            cout<<chars[i]<<" ";
    }
}

int *countLetters(char chars[])
{
    int *counts=new int[26];
    for(int i=0;i<NUMBER_OF_LETTERS;i++)
        counts[i]=0;
    for(int i=0;i<26;i++)
        counts[chars[i]-'a']++;
    return counts;
}

void displayCounts(int counts[])
{
    for(int i=0;i<26;i++)
    {
        if((i+1)%10==0)
            cout<<counts[i]<<" "<<static_cast<char>(i+'a')<<endl;
        else
            cout<<counts[i]<<" "<<static_cast<char>(i+'a')<<" ";
    }
}


一些主要的字符串函数:
int strlen(char *s) // 返回长度

char *strcpy(char *s1,const char *s2)
//将字符串s2复制到s1,返回s1中的地址值

char *strncpy(char *s1,const char *s2,size_t n)
//将字符串s2复制到s1,最多复制n个字符,返回s1中地址值

char *strcat(char *s1,const int *s2)
//将字符串s2附加到s1之后,返回s1中地址值

char *strncat(char *s1,const int *s2,size_t n)
//将字符串s2附加到s1之后,最多附加n个字符,返回s1中地址值

int *strcmp(char *s1,const int *s2)
//比较字符串,对于s1大于,等于,小于s2中字符串分别返回,正数,0,负数

int *strncmp(char *s1,const int *s2,size_t n)
//比较前n个,分别返回正数,0,负数

int atoi(char *s1)
// 将字符串转换成int型

double atof(char *s1)
// 将字符串转换成double型

long atol(char *s1)
// 将字符串转换成long型

void itoa(int value,char *s1,int radix)
// 将value转化为一个字符串(radix进制形式)





第八章 递归
主要理解递归函数的调用和求解问题,调用栈啊,重点是用重载的辅助函数导出递归函数,和理解递归和迭代之间的关系
还是一些旧东西,写写递归辅助函数吧:声明一个重载的函数,以接受额外的参数
用递归函数写的排序:
#include <iostream>
#include <cstring>
using namespace std;
void sort(char list[],int high)
{
    if(high>0)
    {
        int indexOfMax=0;
        char max=list[0];
        for(int i=1;i<=high;i++)
        {
            if(list[i]>max)
            {
                max=list[i];
                indexOfMax=i;
            }
        }
        list[indexOfMax]=list[high];
        list[high]=max;
        sort(list,high-1);
    }
}
void sort(char list[])
{
    sort(list,strlen(list)-1);
}
int main()
{
    cout<<"Enter a string : ";
    char s[80];
    cin.getline(s,80);
    sort(s);
    cout<<"The sorted string is "<<s<<endl;
    return 0;
}




Part 2 .面向对象设计


第九章 对象和类
主要写如何使用类对事物建模,构造函数初始化等等
第十章 对象和类的更多内容
各种麻烦的函数与对象,不想看了
第十一章 继承与多态
构造函数与析构函数,多态与虚函数,抽象类与纯虚函数,不想看了
从理解看来,没什么有意思的东西,类这个东西和结构体还有有点像的,目前没有用代码实现过,因为书上的代码敲出来运行不了 ==  好吧 类就像是在结构体里面嵌套函数似的,貌似也只有这些区别了,然后前天看华哥写链表的时候初始化函数貌似写的时候用了类,膜拜,唉,看不下去了,所以等C++这门课完了再回来看吧,先看看后面稍微容易懂的
稍微重要一点的东西: Loan类 ,Course类, StackOfIntergers类 ,C++向量类 ,etc 各种无聊

第十二章 


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值