##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 <iostream>
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 <iostream>
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 = "<<i<<"\n";
cout<<"Done now that i = "<<i<<"\n";
return 0;
}
//P5.3
#include <iostream>
int main()
{
using namespace std;
int x;
cout<<"The expression x=100 has the value ";
cout<<(x=100)<<endl;
cout<<"Now x="<<x<<endl;
cout<<"The expression x<3 has the value ";
cout<<(x<3)<<endl;
cout<<"The expression x>3 has the value ";
cout<<(x>3)<<endl;
cout.setf(ios_base::boolalpha);//C++11新特性,bool输出
cout<<"The expression x<3 has the value ";
cout<<(x<3)<<endl;
cout<<"The expression x>3 has the value ";
cout<<(x>3)<<endl;
return 0;
}
//P5.4
#include <iostream>
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<ArSize;i++)//
factorials[i]=i*factorials[i-1];
//i=2开始,因此从2!开始计算,即第三个元素开始计算,一直计算到15!
for (int i=0;i<ArSize;i++)
std::cout<<i<<"!="<<factorials[i]<<std::endl;
return 0;
}
//P5.5
#include <iostream>
int main()
{
using std::cout;//使用using声明,而不是using编译指令
using std::cin;
using std::endl;
cout<<"Enter an integer: ";
int by;
cin>>by;
cout<<"Counting by "<<by<<"s:\n";
for (int i=0;i<100;i=i+by)
//注意内部条件的大于小于号不要错误输入成<<和>>
cout<<"i= "<<i<<endl;
return 0;
}
//P5.6
#include <iostream>
#include <string>
int main()
{
using namespace std;
cout<<"Enter a word: ";
string word;
cin>>word;
for (int i=word.size()-1;i>=0;i--)
//此处i的初始化并未考虑空值字符
cout<<word[i];//数组法访问string并反向打印
cout<<"\nBye.\n";
return 0;
}
//P5.7
#include <iostream>
int main()
{
using std::cout;
int a=20,b=20;
cout<<"a= "<<a<<" "<<" b= "<<b<<"\n";
cout<<"a++= "<<a++<<" "<<" ++b= "<<++b<<"\n";
//a++是后加,当时不变事后变,++b是先加,值立刻即改变
cout<<"a= "<<a<<" "<<" b= "<<b<<"\n";
return 0;
}
//P5.8
#include <iostream>
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 "<<i<<": ";
cin>>number;
sum+=number;
}//{}内为代码块,被C++视为一条语句
cout<<"Five exquisite choices indeed! ";
cout<<"They sum to "<<sum<<endl;
cout<<"And average to "<<sum/5<<".\n";//求出平均值
cout<<"The Amazing Account bid you adieu!\n";
return 0;
}
//P5.9
#include <iostream>
#include <string>
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<i;--i,++j)//此处不应该有分号!!!
{
temp=word[i];//将最后一个字符赋值给temp
word[i]=word[j];//将string word第一个字符赋值给最后一个元素
word[j]=temp;
//将temp中的最后一个字符赋值给string word的第一个元素
}
cout<<word<<"\nDone\n";
cout<<word[0];//数组式访问string第一个元素(字符)
return 0;
}
//P5.10
#include <iostream>
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 "<<i<<" is a 20\n";
//cout<<"Doing it dangerously wrong:\n";
//for (i=1;quizscores=20;i++)
//此处并未使用==将十分危险,因为表达式值被赋值锁定为20
//始终为true将陷入死循环
//cout<<"quiz "<<i<<" is a 20\n";
return 0;
}
//P5.11
#include <iostream>
#include <cstring>
int main()
{
using namespace std;
char word[5]="?ate";//定义了数组,将未知首字符设置为?
for (char ch='a';strcmp(word,"mate");ch++)
//使用strcmp()函数比较,字符串一样时跳出循环
//需要注意的是循环检测这部分的句子实际是strcmp()!=0的简写,含义一样
//两个字符串一致为false,否则为true,true则继续执行
{
cout<<word<<endl;
word[0]=ch;//将自增后的字符赋值给word首字母
}
cout<<"After loop ends.Word is "<<word<<endl;
return 0;
}
//P5.12
#include <iostream>
#include <string>
int main()
{
using namespace std;
string word="?ate";
for (char ch='a';word!="mate";ch++)
//不等于则为true,继续执行,使用C++ string类
{
cout<<word<<endl;
word[0]=ch;
}
cout<<"After loop ends.Word is "<<word<<endl;
return 0;
}
//P5.13
#include <iostream>
const int ArSize=20;//可使用#include <string>
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<<name[i]<<": "<<int(name[i])<<endl;
i++;//while循环应当在循环体内调整测试条件参数
}
return 0;
}
//P5.14
#include<iostream>
#include <ctime>
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<delay)
//测试当前时间减去起始时间的间隔是否等于所需延迟的系统时间
;//循环体为空
cout<<"Done!\a\n";
return 0;
//此程序以系统时间单位为单位,避免在每轮循环中将系统时间转换为秒
}
//P5.15
#include <iostream>
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 <iostream>
int main()
{
using namespace std;
char ch;
int count=0;//将字符数初始化为0
cout<<"Enter characters;Enter # to quit:\n";
cin>>ch;//读入第一个字符
while (ch!='#')
{
cout<<ch;//cin使得输入的空格将会被忽略,也不会被计数
++count;//立刻增加
cin>>ch;
}
cout<<endl<<count<<" characters read\n";
return 0;
}
//P5.17
#include <iostream>
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<<ch;
++count;//立刻增加
cin.get(ch);
}
cout<<endl<<count<<" characters read\n";
return 0;
}
//P5.18
#include <iostream>//Clion可能不支持键盘模拟EOF
int main()
{
using namespace std;
int ch;
int count=0;
cin.get(ch);
while (cin.fail()==false)
{
cout<<ch;//强制类型转换为char类型
++count;
cin.get(ch);
}
cout<<endl<<count<<" Characters read.\n";
return 0;
}
//P5.19
#include <iostream>//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<<endl<<count<<" Characters read.\n";
return 0;
}
//P5.20
#include <iostream>
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<Cities;++city)//城市数是5大于年份数4,故此先for大的,即城市数(列)
{
cout<<cities[city]<<":\t";//\t是制表符,这里实际上输出城市名称
for (int year=0;year<Years;++year)//循环年份数
cout<<maxtemps[year][city]<<"\t";
//这里输出同一城市四年最高温度,也就是列输出
cout<<endl;
}
return 0;
}
//PP5.9.1
#include <iostream>
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: "<<sum<<endl;
return 0;
}
//PP5.9.2
#include <iostream>
#include <array>
int main()
{
using namespace std;
cout.setf(ios_base::fixed,ios_base::floatfield);//修复小数点形式
const int ArSize=16;
array<long double,ArSize> 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<<i-1<<"!= "<<Result<<endl;
return 0;
}
//PP5.9.3
#include <iostream>
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 "<<sum<<endl;
cin>>number;
}
cout<<endl<<count<<" times read.";//输入的次数也就是循环体执行的次数
return 0;
}
//PP5.9.4
#include<iostream>
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 "<<count-1<<" Years.";
//因为count初值为1,且循环结束时多+1,故此处应该-1
return 0;
}
//PP5.9.5
#include <iostream>
#include <string>
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 "<<Month[i]<<": ";
cin>>m_books[i];
cout<<"The sales of "<<Month[i]<<" is: "<<m_books[i]<<endl;
m_all+=m_books[i];
}
cout<<"This year the books you have saled in total: "<<m_all<<endl;
return 0;
}
//PP5.9.6
#include <iostream>
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."<<i<<" year ";
for (int j=0;j<Monthx;j++)
{
cout<<Month[j]<<": ";
cin>>Books_sales[i][j];//一个个分别存储入2D数组
cout<<"The sales of no."<<i<<" year "
<<Month[j]<<" is: "<<Books_sales[i][j]<<endl;//回显输入
y_all[i-1]+=Books_sales[i][j];//求出当年总额后存入年份数组
m_all+=Books_sales[i][j];//持续累计销售总额
}
}
for (int i=0;i<years;i++)//年度销售数据报告
cout<<"The no."<<i+1<<" year you sales: "<<y_all[i]<<endl;
cout<<years<<" year that books you have saled in total: "<<m_all<<endl;//最终总数
return 0;
}
//PP5.9.7_V1
#include <iostream>
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<car_number;i++)
{
cout<<"Car #"<<i+1<<": "<<endl;
cout<<"Please enter the make: ";
cin.get();//十分关键!!!此时输入队列中有回车
cin.getline(xv[i].Product_make,15);//注意动态指针数组的读取方式
cout<<"Please enter the year made: ";
cin>>xv[i].Product_year;
}
cout<<"\nHere is your collections: \n";
for (int i=0;i<car_number;i++)
{
cout<<xv[i].Product_year<<" "<<xv[i].Product_make<<endl;
}
delete [] xv;
return 0;
}
//PP5.9.7_V2
#include <iostream>
#include <string>
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<car_number;i++)
{
cout<<"Car #"<<i+1<<": "<<endl;
cout<<"Please enter the make: ";
cin.get();//十分关键!!!此时输入队列中有回车
getline(cin,xv[i].Product_make);//注意动态指针数组里string的读取方式
cout<<"Please enter the year made: ";
cin>>xv[i].Product_year;
}
cout<<"\nHere is your collections: \n";
for (int i=0;i<car_number;i++)
{
cout<<xv[i].Product_year<<" "<<xv[i].Product_make<<endl;
}
delete [] xv;
return 0;
}
//PP5.9.8
#include <iostream>
#include <cstring>
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 <iostream>
#include <string>
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 <iostream>
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<i+1;j++)//第一次循环1次,然后2,3,4,5
cout<<"*";
cout<<endl;//每一行需要分隔
}
return 0;
}