CPP_Basic_Code_P5.1-PP5.9.10

CPP_Basic_Code_P5.1-PP5.9.10

//  The Notes Created by Z-Tech on 2017/2/17.
//  All Codes Boot on 《C++ Primer Plus》V6.0
//  OS:MacOS 10.12.4
//  Translater:clang/llvm8.0.0 &g++4.2.1
//  Editer:iTerm 2&Sublime text 3
//  IDE: Xcode8.2.1&Clion2017.1

//P5.1
#include 
int main()
{
    using namespace std;
    int i;
    for (i=0;i<5;i++)
    //注意循环初始化、循环测试和循环更新括号内全部是分号!不是逗号!
        cout<<"C++ know loops.\n";
    cout<<"C++ Know when to stop.\n";
    return 0;
}

//P5.2
#include 
int main()
{
    using namespace std;
    cout<<"Enter the starting countdown value: ";
    int limit;
    cin>>limit;
    int i;
    for (i=limit;i;i--)
    //自减计算,到0为止,直接输入0则会立即退出,不执行循环
        cout<<"i = "<
int main()
{
    using namespace std;
    int x;
    cout<<"The expression x=100 has the value ";
    cout<<(x=100)<3 has the value ";
    cout<<(x>3)<3 has the value ";
    cout<<(x>3)<
const int ArSize=16;//使用常量便之后修改
int main()
{
    long long factorials[ArSize];//创建long long类型的数组
    factorials[1]=factorials[0]=1LL;
    //初始化数组的第1和第2个元素为long long型的1
    //原因是0!和1!都等于1,而每个元素存储对应的i!
    for (int i=2;i>by;
    cout<<"Counting by "<>
        cout<<"i= "<
#include 
int main()
{
    using namespace std;
    cout<<"Enter a word: ";
    string word;
    cin>>word;

    for (int i=word.size()-1;i>=0;i--)
    //此处i的初始化并未考虑空值字符
        cout<
int main()
{
    using std::cout;
    int a=20,b=20;
    cout<<"a= "<
int main()
{
    using namespace std;
    cout<<"The Amazing accounts will sum and average ";
    cout<<"Five numbers for you.\n";
    cout<<"Please enter five values:\n";
    double number;
    double sum=0.0;
    //此处定义了求和sum和存放输入的number
    for (int i=1;i<=5;i++)//精确控制了五次循环
    {
        cout<<"Value "<>number;
        sum+=number;
    }//{}内为代码块,被C++视为一条语句
    cout<<"Five exquisite choices indeed! ";
    cout<<"They sum to "<
#include 
int main()
{
    using namespace std;
    cout<<"Enter a word: ";
    string word;
    cin>>word;
    char temp;
    int i,j;
    for (j=0,i=word.size()-1;j
int main()
{
    using namespace std;
    int quizscores[10]={20,20,20,20,20,19,20,18,20,20};
    cout<<"Doing it right:\n";
    int i;
    for (i=1;quizscores[i]==20;i++)
    //使用==比较是否相等,相等为true继续,不等为false跳出
        cout<<"quiz "<
#include 
int main()
{
    using namespace std;
    char word[5]="?ate";//定义了数组,将未知首字符设置为?
    for (char ch='a';strcmp(word,"mate");ch++)
    //使用strcmp()函数比较,字符串一样时跳出循环
    //需要注意的是循环检测这部分的句子实际是strcmp()!=0的简写,含义一样
    //两个字符串一致为false,否则为true,true则继续执行
    {
        cout<
#include 
int main()
{
    using namespace std;
    string word="?ate";
    for (char ch='a';word!="mate";ch++)
    //不等于则为true,继续执行,使用C++ string类
    {
        cout<
const int ArSize=20;//可使用#include 
int main()
{
    using namespace std;
    char name[ArSize];
    cout<<"Your first name,please: ";
    cin>>name;
    cout<<"Here is your name,verticalized and ASCIIized:\n";
    int i=0;
    while (name[i]!='\0')
    //可直接用name[i]即可,非0为true,遇空字符为0,false
    //while循环无需初始化和循环更新参数
    // !=空字符就继续,也就是到末尾前一直循环
    //字符串自带结尾空字符标记,所以无需知道字符串长度
    {
        cout<
#include 
int main()
{
    using namespace std;
    cout<<"Enter the delay time,in seconds: ";
    float secs;
    cin>>secs;
    clock_t delay=secs*CLOCKS_PER_SEC;
    //该常量描述每秒系统时间单位数,乘以秒数就是所需延迟的系统时间
    cout<<"Starting\a\n";//\a是转义的振铃符
    clock_t start=clock();//设置时间计时起始点
    while (clock()-start
int main()
{
    using namespace std;
    int n;
    cout<<"Enter number in the range 1-10 to find ";
    cout<<"My favorite number\n";
    do//这种循环是出口条件循环,至少执行一次循环体
    {
        cin>>n;
    }while(n!=7);//表达式成立则继续,否则退出
    cout<<"Yes,7 is My favorite number!\n";
    return 0;
}

//P5.16
#include 
int main()
{
    using namespace std;
    char ch;
    int count=0;//将字符数初始化为0
    cout<<"Enter characters;Enter # to quit:\n";
    cin>>ch;//读入第一个字符
    while (ch!='#')
    {
        cout<>ch;
    }
    cout<
int main()
{
    using namespace std;
    char ch;
    int count=0;//将字符数初始化为0
    cout<<"Enter characters;Enter # to quit:\n";
    cin.get(ch);//使用cin.get()将不会忽略空格等字符
    while (ch!='#')
    {
        cout<//Clion可能不支持键盘模拟EOF
int main()
{
    using namespace std;
    int ch;
    int count=0;
    cin.get(ch);
    while (cin.fail()==false)
    {
        cout<//Clion可能不支持键盘模拟EOF
int main(void)
{
    using namespace std;
    int ch;
    int count=0;
    while ((ch=cin.get())!=EOF)
    //测试ch的值是否为EOF,true则结束,否则继续
    {
        cout.put(char(ch));//强制类型转换为char类型
        ++count;
    }
    cout<
const int Cities=5;
const int Years=4;
int main()
{
    using namespace std;
    const char* cities[Cities]//创建指针数组分别指向五个字符串
    {
         "Gribble City",
         "Gribbletown",
         "New Gribble",
         "San Gribble",
         "Gribble Vista"
    };
    int maxtemps[Years][Cities]//创建2d数组,有Years行,Cities列
    {
        {96,100,87,101,105},//注意里面全部是逗号!!
        {96,98,91,107,104},//且每个子数组都用花括号{}包围
        {97,101,93,108,107},
        {98,103,95,109,109}//最后一行的子数组后面没有逗号

    };
    cout<<"Maximun tempratures for 2008-2011\n\n";
    for (int city=0;city
int main()
{
    using namespace std;
    cout<<"Enter the first intger: ";
    int n1;//这里默认n1<=n2
    cin>>n1;
    cout<<"\nEnter the last integer: ";
    int n2;
    cin>>n2;
    int sum=0;//sum需在外部定义,才不会每次循环都被初始化为0
    for (n1;n1<=n2;n1++)//这里n1++或者++n1都无所谓
        sum+=n1;
    cout<<"\nSum: "<
#include 
int main()
{
    using namespace std;
    cout.setf(ios_base::fixed,ios_base::floatfield);//修复小数点形式
    const int ArSize=16;
    array factrials;//array中的ArSize不能是变量!!
    factrials[1]=factrials[0]=1;//设置初始第一个和第二个元素
    int i=2;
    long double Result=0;
    for (i;i<=ArSize;i++)
    {
        factrials[i]=i*factrials[i-1];
        Result=factrials[i];
    }
    cout<
int main()
{
    using namespace std;
    cout<<"Please enter a number and enter '0' to quit: ";
    long double number=0;
    int count=0;//设定计数器
    int sum=0;//设定求和器
    cin>>number;
    while (number!=0)
    {
        ++count;
        sum+=number;//求和的关键
        cout<<"Sum of all your input is "<>number;
    }
    cout<
int main()
{
    using namespace std;
    int count=1;
    int D_interests;
    double C_interests=100;
    do
    {
        D_interests=100+(10*count);//Daphne是单利计算,每年固定10$
        C_interests*=1.05;//Cleo是复利计算,每年都是实际金额的1.05倍
        // 实际上应该使用次方计算,但是巧妙利用C++的*=和循环成功实现计算
        count++;//年份计数器
    }
    while (D_interests>C_interests);
    cout<<"Need "<
#include 
int main()
{
    using namespace std;
    int m_books[12];
    string Month[12]
            {"January","February","March","April",
            "May","June","July","August", "September",
             "October","November","December"};
    int m_all=0;//求和工具设定初始化
    for (int i=0;i<12;i++)
    {
        cout << "Please enter sales of "<>m_books[i];
        cout<<"The sales of "<
const int years=3;//只需修改此处即可调整不同的年份
const int Monthx=12;
int main()
{
    using namespace std;
    const char* Month[Monthx]//定义月份字符串,使用指针数组
            {   "January", "February","March","April",
                "May","June","July","August", "September",
                "October","November","December"
            };
    int Books_sales [years][Monthx]//定义N年各个月销售数据的2D数组
            {};//为空则全部初始化为0
    int y_all[years] {0};//定义每年销售额总数计数数组
    int m_all=0;//定义全部年份的销售额总数计数器
    for (int i=1;i<(years+1);i++)
    {
        cout<<"Please enter sales of no."<>Books_sales[i][j];//一个个分别存储入2D数组
            cout<<"The sales of no."<
struct car//结构声明
{
    char Product_make[15];
    int Product_year;
};
int main()
{
    using namespace std;
    cout<<"How many cars do you wish to catalog?";
    int car_number;
    cin>>car_number;
    car* xv=new car[car_number];//创建动态指针数组
    for (int i=0;i>xv[i].Product_year;
    }
    cout<<"\nHere is your collections: \n";
    for (int i=0;i
#include 
struct car//结构声明
{
    std::string Product_make;
    int Product_year;
};
int main()
{
    using namespace std;
    cout<<"How many cars do you wish to catalog? ";
    int car_number;
    cin>>car_number;
    car* xv=new car[car_number];//创建动态指针数组
    for (int i=0;i>xv[i].Product_year;
    }
    cout<<"\nHere is your collections: \n";
    for (int i=0;i
#include 
int main()
{
    using namespace std;
    char words[15];
    int count=0;//单词循环计数器
    cout<<"Enter the words(to stop,type the word done): \n";
    while (strcmp(words,"done"))//使用strcmp函数执行比较,不同继续直到相同时跳出
    {
        count++;
        cin>>words;
    }
    cout<<"You entered a total of "<<(count-1)<<" words.";
    return 0;
}

//PP5.9.9
#include 
#include 
int main()
{
    using namespace std;
    string words;
    int count=0;//单词循环计数器
    cout<<"Enter the words(to stop,type the word done): \n";
    while (words!="done")//使用string关系运算符比较,不同继续为true,直到相同时false跳出
    {
        count++;
        cin>>words;
    }
    cout<<"You entered a total of "<<(count-1)<<" words.";
    return 0;
}

//PP5.9.10
#include 
int main()
{
    using namespace std;
    int i,j,n;
    cout<<"Please enter the number of lines: ";
    cin>>n;
    for (i=1;i<=n;i++)//循环嵌套的关键:子循环的判定条件必须是主循环核心变量的关系式!!!
    {
        for (j=1;j<=n-i;j++)//第一次循环4次,然后3、2、1、0
            cout<<".";
        for (j=1;j

你可能感兴趣的:(CPP_Basic_Code_P5.1-PP5.9.10)