// 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
//P6.1
#include <iostream>
int main()
{
using std::cin;
using std::cout;
char ch;
int spaces=0;//外部全局變量
int total=0;//外部全局變量
cin.get(ch);//讀取第一個(gè)字符
while (ch!='.')//如果不是句號(hào)則繼續(xù)循環(huán)
{
if (ch==' ')
++spaces;//遇到空格計(jì)數(shù)
++total;//無(wú)論是否空格均計(jì)數(shù),且字符總數(shù)包含最后的回車(chē)生成的換行符
cin.get(ch);
}
cout<<spaces<<" spaces, "<<total
<<" characters total in sentence\n";
return 0;
}
//P6.2
#include <iostream>
int main()
{
char ch;
std::cout<<"Type,and I shall repeat.\n";
std::cin.get(ch);
while (ch!='.')
{
if (ch=='\n')
std::cout<<ch;
else
std::cout<<++ch;//此處++ch被cout理解為字符輸出,若改為ch+1則會(huì)被認(rèn)為int輸出
std::cin.get(ch);
}
std::cout<<"\nPlease excuse the slight confusion.\n";
return 0;
}
//P6.3
#include <iostream>
const int fave=9;
int main()
{
using namespace std;
int n;
cout<<"Enter a number in the range 1-100 to find"
<<"my favorite number: ";
do{
cin>>n;
if (n<fave)//小了就說(shuō)
cout<<"Too low! Then guess again: ";
else if (n>fave)//這種寫(xiě)法實(shí)際上是一個(gè)if else被包含在另一個(gè)if else中
cout<<"Too high! Then guess again: ";
else//這樣調(diào)整后的寫(xiě)法實(shí)際上更清晰更好
cout<<fave<<" is right!\n";
}while (n!=fave);//沒(méi)猜對(duì)就繼續(xù)猜
return 0;
}
//P6.4
#include <iostream>
int main()
{
using namespace std;
cout<<"This program may reformat your hard disk\n"http://引號(hào)具有輸出流連續(xù)性
"and destroy all your data.\n"
"Do you wish to continue? <y/n>";
char ch;
cin>>ch;
if (ch=='y'||ch=='Y')//或運(yùn)算符,或真則真,滿足其一即可
cout<<"You were warned!\a\a\n";
else if (ch=='n'||ch=='N')//再次使用
cout<<"A wise choice...Bye!\n";
else
cout<<"That's wasn't a 'Y/y' or 'N/n'!Apparently you "
"can't follow\ninstructions,so "
"I'll trash your disk anyway.\a\a\a\n";
return 0;
}
//P6.5
#include <iostream>
const int ArSize=10;//控制數(shù)組大小,即報(bào)價(jià)個(gè)數(shù)
int main()
{
using namespace std;
float naaq[ArSize];
cout<<"Enter the NAAQs (New Age Awareness Quotients) "
<<"Of\nyour neighbors. Program terminates "
<<"when you make\n"<<ArSize<<" entries "
<<"or enter a negative value.\n";
int i=0;
float temp;
cout<<"First value: ";
cin>>temp;
while (i<ArSize&&temp>=0)//第一個(gè)條件保證數(shù)組不溢出,第二個(gè)條件確保輸入報(bào)價(jià)有效
{
naaq[i]=temp;//有效則逐個(gè)存入數(shù)組
++i;//數(shù)組角標(biāo)自增
if (i<ArSize)//然后如果i<ArSize說(shuō)明數(shù)組還有空間,繼續(xù)請(qǐng)求輸入
{
cout<<"Next value: ";
cin>>temp;//再次賦值給temp后繼續(xù)循環(huán)直到存滿數(shù)組
}
}
if (i==0)//意味著輸入了一個(gè)不滿足第二個(gè)條件的數(shù),于是未進(jìn)入過(guò)while循環(huán)
cout<<"No data--bye\n";//如果中途輸入了負(fù)數(shù)則會(huì)跳過(guò)這里直接到下一模塊請(qǐng)求自己報(bào)價(jià),因?yàn)橹型緄!=0
else
{
cout<<"Enter your NAAQ: ";//輸入自己的報(bào)價(jià)
float you;
cin>>you;
int count=0;//設(shè)定比較計(jì)數(shù)器
for (int j=0;j<i;j++)//循環(huán)次數(shù)等于ArSize,也即是逐個(gè)比較
if (naaq[j]>you)//如果之前報(bào)價(jià)你自己輸入的高,則計(jì)數(shù)器+1
++count;//逐個(gè)比較直到循環(huán)結(jié)束
cout<<count;//開(kāi)始輸出結(jié)果
cout<<" of your naghbors have greater awareness of\n"
<<"the New Age than you do.\n";
}
return 0;
}
//P6.6
#include <iostream>
const char* qualify[4]//創(chuàng)建指向字符串的常量指針數(shù)組
{
"10,000-meter race.\n",
"mud tug-of-war.\n",
"masters canoe jousting.\n",
"pie-throwing festival.\n"
};
int main()
{
using namespace std;
int age;
cout<<"Enter your age in years: ";
cin>>age;
int index;
if (age>17&&age<35)//18-34
index=0;
else if (age>=35&&age<50)//35-49
index=1;
else if (age>=50&age<65)//50-64,且可見(jiàn)else if可以連續(xù)使用
index=2;
else //包括負(fù)數(shù),0-17,65以上;但是年齡默認(rèn)不會(huì)輸入負(fù)數(shù)
index=3;
cout<<"Your qualify for the "<<qualify[index];//輸出分級(jí)頁(yè)結(jié)果
return 0;
}
//P6.7
#include <iostream>
#include <climits>
bool is_int(double);//函數(shù)聲明
int main()
{
using namespace std;
double num;
cout<<"Yo,dude!Enter an intger value: ";
cin>>num;
while (!is_int(num))//while條件函數(shù)調(diào)用取反
{
cout<<"Out if range __ Please try again: ";
cin>>num;
}
int val=int (num);
cout<<"You've entered the integer "<<val<<"\nBye\n";
return 0;
}
bool is_int(double x)
{
if (x<=INT_MAX&&x>=INT_MIN)
return true;//輸入范圍成功則返回true,取反為false,則while被跳過(guò)
else
return false;//超出范圍,返回false,取反為true,則while被激活,提示重新輸入
}
//P6.8
#include <iostream>
#include <cctype>
int main()
{
using namespace std;
cout<<"Enter text for analysis and type @"
" to terminate input.\n";
char ch;
int whitespace=0;
int digits=0;
int chars=0;
int punct=0;
int others=0;
cin.get(ch);
while (ch!='@')
{
if (isalpha(ch))//字母檢查
chars++;
else if (isspace(ch))//空白符檢查
whitespace++;
else if (isdigit(ch))//數(shù)字檢查
digits++;
else if (ispunct(ch))//標(biāo)點(diǎn)檢查
punct++;
else
others++;//剩下的歸于其它,比如漢字等特殊符號(hào)
cin.get(ch);
}
cout<<chars<<" letters, "
<<whitespace<<" whitespace, "
<<digits<<" digits, "
<<punct<<" punctuations, "
<<others<<" others.\n";
return 0;
}
//P6.9
#include <iostream>//求出兩個(gè)數(shù)最大值
int main()
{
using namespace std;
int a,b;
cout<<"Enter two integers: ";
cin>>a>>b;
cout<<"The larger of "<<a<<" and "<<b;
int c=a>b?a:b;//使用了?:條件運(yùn)算符
cout<<" is "<<c<<endl;
return 0;
}
//P6.10.V1
#include <iostream>
using namespace std;
void showmenu();//無(wú)參無(wú)返回值函數(shù)聲明
void report();
void comfort();
int main()
{
showmenu();//函數(shù)調(diào)用顯示菜單
int choice;
cin>>choice;
while (choice!=5)
{
switch (choice)
{
case 1: cout<<"\a\n";
break;//跳出循環(huán)
case 2: report();
break;
case 3: cout<<"The boss was in all day.\n";
break;
case 4: comfort();
break;
default: cout<<"That's not a choice.\n";
//缺省分支,后面不需要加break;
}
showmenu();
cin>>choice;
}
cout<<"Bye!\n";
return 0;
}
void showmenu()//菜單函數(shù)模塊
{
cout<<"Please enter 1,2,3,4,or 5:\n"
"1) alarm 2) report\n"
"3) alibi 4) comfort\n"
"5) quit\n";
}
void report()//報(bào)告函數(shù)模塊
{
cout<<"It's been an excellent week for business.\n"
"Sales are up 120$. Expense are down 35%.\n";
}
void comfort()//安撫模塊
{
cout<<"Your employees think you are the finest CEO\n"
"in the industry .The board of directors thinks\n"
"you are the finest CEO in the industry.\n";
}
//P6.10.V2
#include <iostream>
using namespace std;
void showmenu();//無(wú)參無(wú)返回值函數(shù)聲明
void report();
void comfort();
int main()
{
showmenu();//函數(shù)調(diào)用顯示菜單
char choice;//使用英文字符控制
cin>>choice;
while (choice!='Q'&&choice!='q')
{
switch (choice)
{
case 'a'://利用連續(xù)執(zhí)行的特性解決大小寫(xiě)的問(wèn)題
case 'A': cout<<"\a\n";
break;//跳出循環(huán)
case 'b':
case 'B': report();
break;
case 'c':
case 'C': cout<<"The boss was in all day.\n";
break;
case 'd':
case 'D': comfort();
break;
default: cout<<"That's not a choice.\n";//缺省分支,后面不需要加break;
}
showmenu();
cin>>choice;
}
cout<<"Bye!\n";
return 0;
}
void showmenu()//菜單函數(shù)模塊
{
cout<<"Please enter 1,2,3,4,or 5:\n"
"a) alarm b) report\n"
"c) alibi d) comfort\n"
"q) quit\n";
}
void report()//報(bào)告函數(shù)模塊
{
cout<<"It's been an excellent week for business.\n"
"Sales are up 120$. Expense are down 35%.\n";
}
void comfort()//安撫模塊
{
cout<<"Your employees think you are the finest CEO\n"
"in the industry .The board of directors thinks\n"
"you are the finest CEO in the industry.\n";
}
//P6.11
#include <iostream>
enum {red,orange,yellow,green,blue,violet,indigo};
//int類型 0,1,2,3,4,5,6
//枚舉常量聲明,沒(méi)用變量名,注意逗號(hào),結(jié)尾分號(hào),和結(jié)構(gòu)聲明類似
int main()
{
using namespace std;
cout<<"Enter color code (0-6): ";
int code;
cin>>code;
while (code>=red&&code<=indigo)//范圍限定在0-6之間
{
switch (code)//標(biāo)簽
{
case red: cout<<"Her lips were red.\n";break;//注意此處有兩個(gè)分號(hào)
case orange: cout<<"Her hair was orange.\n";break;
case yellow: cout<<"Her shoes were yellow.\n";break;
case green: cout<<"Her nails were green.\n";break;
case blue: cout<<"Her sweatsuit was blue.\n";break;
case violet: cout<<"Her eyes were violet.\n";break;
case indigo: cout<<"Her mood was indigo.\n";break;
}
cout<<"Enter color code (1-6)";
cin>>code;
}
cout<<"Bye.\n";
return 0;
}
//P6.12
#include <iostream>
const int ArSize=80;
int main()
{
using namespace std;
char line[ArSize];//設(shè)定字符數(shù)組
int spaces=0;//初始化空格計(jì)數(shù)器
cout<<"Enter a line of text:\n";
cin.getline(line,ArSize);//字符讀入line數(shù)組中
cout<<"Complete line:\n"<<line<<endl;
cout<<"Line through first period:\n";
for (int i=0;line[i]!='\0';i++)//沒(méi)碰到數(shù)組結(jié)尾\0將繼續(xù)循環(huán),逐個(gè)輸出數(shù)組中的字符
{
cout<<line[i];
if (line[i]=='.')//檢測(cè)到句點(diǎn)則離開(kāi)循環(huán)
break;
if (line[i]!=' ')//檢測(cè)不到空格則回到for的循環(huán)更新處,即i++后繼續(xù)循環(huán)
continue;
spaces++;//有空格將會(huì)到達(dá)此處,空格計(jì)數(shù)器
}
cout<<"\n"<<spaces<<" sapces\n";
cout<<"Done.\n";
return 0;
}
//P6.13
#include <iostream>
const int Max=5;
int main()
{
using namespace std;
double fish[Max];//初始化存重量的數(shù)組
cout<<"Please enter the weights of your fish.\n";
cout<<"You may enter up to "<<Max<<"fish <q to terminate>,\n";
cout<<"fish #1: ";
int i=0;
while (i<Max&&cin>>fish[i])//且假則假,因此當(dāng)左側(cè)為false,右側(cè)不會(huì)被判斷,防止數(shù)組溢出
{
if (++i<Max)
cout<<"fish #"<<i+1<<": ";//輸出魚(yú)的編號(hào)
}
double total =0.0;//設(shè)定重量計(jì)數(shù)器
for (int j=0;j<i;j++)
total+=fish[j];//統(tǒng)計(jì)整個(gè)數(shù)組的魚(yú)總量
if (i==0)
cout<<"No fish\n";
else cout<<total/i<<" = average weight of "<<i<<" fish\n";//計(jì)算平均值
cout<<"Done.\n";
return 0;
}
//P6.14
#include <iostream>//可阻止非法輸入并給出正確的提示
const int Max=5;
int main()
{
using namespace std;
int golf[Max];
cout<<"Please enter your golf scores.\n";
cout<<"You must enter "<<Max<<" rounds.\n";
int i;
for (i=0;i<Max;i++)
{
cout<<"round #"<<i+1<<": ";
while (!(cin>>golf[i]))//輸入失敗返回false,取反為true即啟用錯(cuò)誤處理
{
cin.clear();//重置cin
while (cin.get()!='\n')//讀取【行尾】之前所有的錯(cuò)誤輸入,從而刪除這一行的錯(cuò)誤輸入
continue;//循環(huán)直到cin.get()=='\n',也就是到達(dá)行尾
cout<<"Please enter a number: ";
}
}
double total=0.0;
for (i=0;i<Max;i++)//求和模塊
total+=golf[i];
cout<<total/Max<<" =average score "<<Max<<" rounds\n";
return 0;
}
//P6.15
#include <iostream>
#include <fstream>//重要文件輸入輸出頭文件
int main()
{
using namespace std;
char automobile[50];
int year;
double a_price;
double d_price;
ofstream outFile;//創(chuàng)建ofstream對(duì)象——outFile
outFile.open("car_info.txt");//關(guān)聯(lián)對(duì)象到文件
//以下是輸入讀取模塊
cout<<"Enter the make and model of automobile: ";
cin.getline(automobile,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;//輸出格式修復(fù)
cout.precision(2);//精度為小數(shù)點(diǎn)后2位有效數(shù)字
cout.setf(ios_base::showpoint);//顯示小數(shù)點(diǎn)
cout<<"Make and model: "<<automobile<<endl;
cout<<"Year: "<<year<<endl;
cout<<"Was asking $"<<a_price<<endl;
cout<<"Now asking $"<<d_price<<endl;
//以下是文件寫(xiě)入模塊
outFile<<fixed;//輸出格式修復(fù)
outFile.precision(4);//精度為小數(shù)點(diǎn)后4位有效數(shù)字
outFile.setf(ios_base::showpoint);//顯示小數(shù)點(diǎn)
outFile<<"Make and model: "<<automobile<<endl;
outFile<<"Year: "<<year<<endl;
outFile<<"Was asking $"<<a_price<<endl;
outFile<<"Now asking $"<<d_price<<endl;
outFile.close();//關(guān)閉文件寫(xiě)入
return 0;
}
//P6.16
#include <iostream>
#include <fstream>
//#include <cstdlib> //為支持exit()
const int SIZE=60;
int main()
{
using namespace std;
char filename[SIZE];
ifstream inFile;//聲明輸入文件變量inFile
cout<<"Enter name of data file: ";
cin.getline(filename,SIZE);//讀取文件名
inFile.open(filename);//將inFile與filename關(guān)聯(lián)
if (!inFile.is_open())//輸入成功則跳過(guò),失敗則激活
{
cout<<"Could not open the file "<<filename<<endl;
cout<<"Program terminating.\n";
exit(EXIT_FAILURE);
}
double value;
double sum=0.0;
int count=0;
inFile>>value;//讀取文件中第一個(gè)值
while (inFile.good())//輸入正常且未遇到EOF文件尾則繼續(xù)循環(huán),該方法沒(méi)有出錯(cuò)時(shí)且不遇到EOF時(shí)返回均為true
{
++count;
sum+=value;
inFile>>value;//繼續(xù)讀取文件下一個(gè)值
}
if (inFile.eof())//到此處說(shuō)明循環(huán)結(jié)束到達(dá)文件尾,最后一次讀取遇到EOF則返回true
cout<<"End of file reached.\n";
else if (inFile.fail())//檢測(cè)到數(shù)據(jù)類型不匹配或遇到EOF時(shí)均會(huì)被激活返回true
cout<<"Input terminated by data mismatch.\n";
else//如果仍然不滿足判斷條件則說(shuō)明失敗原因未知
cout<<"Input terminated by unknown reason.\n";
if (count==0)//為true則說(shuō)明未曾進(jìn)入過(guò)讀取循環(huán)體
cout<<"No data processed.\n";
else//一切正常則進(jìn)入最終的讀取狀態(tài)統(tǒng)計(jì)報(bào)告
{
cout<<"Item read: "<<count<<endl;
cout<<"Sum: "<<sum<<endl;
cout<<"Average: "<<sum/count<<endl;
}
inFile.close();
return 0;
}
//PP6.11.1
#include <iostream>
#include <cctype>
int main()
{
using namespace std;
cout<<"Please enter something: ('@' to quit)";
char words;
cin.get(words);//這比cin>>words更精細(xì)
while (words!='@')
{
if (isalpha(words))
{
if (words>='a'&&words<='z')
cout.put(toupper(words));//這里使用字符輸出格式,cout默認(rèn)十進(jìn)制
else
cout<<char(tolower(words));//這是第二種方法,采用強(qiáng)制類型轉(zhuǎn)換
}
//else if (words=='\n')//如果啟用,檢測(cè)到換行符即退出
// break;
else
cin.clear();//如果輸入非字母字符,比如數(shù)字等,則重置cin
cin.get(words);
}
return 0;
}
//PP6.11.2
#include <iostream>
#include <cctype>
int main()
{
using namespace std;
const int ArSize=5;
int i=0,over_avrage=0;
double tmp=0,sum=0,average=0;
double donation[ArSize];
cout<<"Enter the number: ";
while (cin>>tmp&&i<ArSize)//此行代碼不需要&&!isdigit(tmp),此法無(wú)效
{
donation[i]=tmp;
sum+=donation[i];
i++;
}
if (i!=0)//做商需要防止分母為0
average=sum/i;//巧妙利用i
for (int j=0;j<i;j++)
{
if (donation[j]>average)//找出大于平均值的數(shù)并計(jì)數(shù)
++over_avrage;
}
cout<<" sum: "<<sum<<" over_avrage: "<<over_avrage
<<" i: "<<i<<" Average: "<<average;
return 0;
}
//PP6.11.3
#include <iostream>
#include <cstdlib>//調(diào)用exit()函數(shù)必備
void show_menu();
void Select_c();
void Select_p();
void Select_t();
void Select_g();
void Select_q();
using namespace std;
int main()
{
show_menu();
char choice;
cin.get(choice);
while (choice)//任何選擇都繼續(xù),意味著只能通過(guò)break;跳出循環(huán)結(jié)束任務(wù)
{
switch (choice)
{
case 'c':
case 'C':Select_c();
break;
case 'p':
case 'P':Select_p();
break;
case 't':
case 'T':Select_t();
break;
case 'g':
case 'G':Select_g();
break;
case 'q':
case 'Q':Select_q();
break;
default:cout<<"That's not a choice.\n";//應(yīng)對(duì)隨意輸入設(shè)計(jì)
}
show_menu();//此處是防止死循環(huán)的關(guān)鍵
cin>>choice;
}
return 0;
}
void show_menu()//子函數(shù)括號(hào)右邊不需要";"!
{
cout<<"Please enter the following choice: (Use 'q' to quit.)\n"
"c)Carnivo p)Pianist\n"
"t)Tree g)Game\n";
}
void Select_c()
{
cout<<"這個(gè)單詞的意思是食蟲(chóng)植物。\n";
}
void Select_p()
{
cout<<"鋼琴家還是理查德厲害啊!\n";
}
void Select_t()
{
cout<<"鏈表和二叉樹(shù)又來(lái)了~\n";
}
void Select_g()
{
cout<<"想玩游戲,你想多了吧\n";
}
void Select_q()
{
exit(EXIT_FAILURE);//嘗試調(diào)用失敗返回值
}
//PP6.11.4
#include <iostream>
#include <cstdlib>//調(diào)用exit()函數(shù)必備
void show_menu();
void Select_a();
void Select_b();
void Select_c();
void Select_d();
void Select_q();
using namespace std;
struct bop
{
string fullname;
string title;
string bopname;
int preference;
};
int main()
{
show_menu();
char choice;
cin.get(choice);
bop* VIP=new bop[5];//指針指向結(jié)構(gòu)數(shù)組
VIP[0].fullname="HsuChou Yang";
VIP[0].title="VVVIP";
VIP[0].bopname="Z";
VIP[0].preference=1;
VIP[1].fullname="周杰倫";
VIP[1].title="IP";
VIP[1].bopname="Jay";
VIP[1].preference=2;
VIP[2].fullname="臉紅的思春期";
VIP[2].title="RSP";
VIP[2].bopname="RFF";
VIP[2].preference=0;
VIP[3].fullname="金莎";
VIP[3].title="Kim susu";
VIP[3].bopname="Love";
VIP[3].preference=1;
VIP[4].fullname="沢井美空";
VIP[4].title="SwaiMiku";
VIP[4].bopname="美空";
VIP[4].preference=2;
while (choice)//
{
switch (choice)
{
case 'a':
case 'A':
{
for (int i=0;i<5;i++)
cout<<VIP[i].fullname<<endl;
};
break;
case 'b':
case 'B':
{
for (int i=0;i<5;i++)
cout<<VIP[i].title<<endl;
};
break;
case 'c':
case 'C':
{
for (int i=0;i<5;i++)
cout<<VIP[i].bopname<<endl;
};
break;
case 'd':
case 'D':
{
for (int i=0;i<5;i++)
//這一段是分解偏好的核心,使用if else順利解決之
{
if (VIP[i].preference==0)
cout<<VIP[i].fullname<<endl;
else if (VIP[i].preference==1)
cout<<VIP[i].title<<endl;
else
cout<<VIP[i].bopname<<endl;
}
};
break;
case 'q':
case 'Q':
{
delete [] VIP;//注意釋放指針
exit(EXIT_FAILURE);
}
}
show_menu();//此處是防止死循環(huán)的關(guān)鍵
cin>>choice;
}
delete [] VIP;
return 0;
}
void show_menu()//菜單函數(shù),子函數(shù)括號(hào)右邊不需要";"
{
cout << "Please enter the following choice: (Use 'q' to quit.)\n"
"a)display by name b)display by title\n"
"c)display by bopname d)display by preference\n";
}
//PP6.11.5
#include <iostream>
int main()
{
using namespace std;
double salary,tax;
cout<<"Please enter your salary first: \n";
cin>>salary;
while (salary>=0)
{
if (salary>=35000)
{
tax=(salary-35000)*0.2+20000*0.15+10000*0.1+0;
break;
}
else if (salary>=15001)
{
tax=(salary-15000)*0.15+10000*0.1+0;
break;
}
else if (salary>5000)
{
tax=(salary-5000)*0.1+0;
break;
}
else if (salary<=5000)
{
tax=0;
break;
}
else
{
tax=0;
break;
}
}
while (!cin)//輸入不是數(shù)字將報(bào)錯(cuò)
{
cout<<"That's may not a number.\n";
exit(EXIT_FAILURE);
}
cout<<"Tax that you need to pay: "<<tax<<"$"<<endl;
return 0;
}
//PP6.11.6
#include <iostream>
const int ArSize=20;
const int num=5;
struct fund
{
char name[ArSize];
double money;
};
int main()
{
using namespace std;
int count=0,poor=0;//解決無(wú)人時(shí)none的顯示
fund* person=new fund[num];
//以下是數(shù)據(jù)采集模塊
for (int i=0;i<num;i++)
{
cout<<"Please enter the name: ";
cin.get(person[i].name,ArSize);
cout<<"Please enter the money: ";
cin>>person[i].money;
if (!cin)
{
cout<<"Wrong number! Please run again. ";
exit(EXIT_FAILURE);
}
cin.get();//至關(guān)重要,使用cin輸入double后需要讀取回車(chē)
}
cout << "Grand Patrons: \n";//開(kāi)始輸出重要人物
for (int i=0;i<num;i++)
{
if (person[i].money > 10000)
{
cout << person[i].name << " : " << person[i].money <<"$"<<endl;
count++;
}
}
if (count == 0)
cout << "none.\n";
cout<<"Patrons: \n";//開(kāi)始輸出一般捐獻(xiàn)者
for (int i=0;i<num;i++)
{
if (person[i].money<=10000)
{
cout<<person[i].name<<endl;
poor++;
}
}
if (poor==0)
cout<<"none.\n";
delete [] person;
return 0;
}
//PP6.11.7
#include <iostream>
#include <cctype>
#include <cstring>
const int ArSize=20;
int main()
{
using namespace std;
char word[ArSize];
int vowel=0,consonants=0,others=0;
cout<<"Please enter the words: \n";
cin>>word;
cout<<word;
while (strcmp(word,"q"))//警告!不能用賦值號(hào)比較字符串!
{
if (isalpha(word[0]))//檢測(cè)是否字母
{
if (word[0]=='a'||word[0]=='e'||word[0]=='i'||word[0]=='o'||word[0]=='u')
//是否元音字母
vowel++;
else
consonants++;//否則輔音字母
}
else
others++;//其余雜項(xiàng)統(tǒng)計(jì)
cin>>word;
}
cout<<"Vowel: "<<vowel<<"\nConsonants: "<<consonants<<"\nOthers: "<<others<<endl;
return 0;
}
//PP6.11.8
#include <iostream>
#include <fstream>
int main()
{
using namespace std;
char filename[20];
int count=0;
ifstream inFile;
cout<<"Enter name of file: ";
cin.getline(filename,20);
inFile.open(filename);
if (!inFile.is_open())
{
exit(EXIT_FAILURE);
}
char ch;
inFile.get(ch);//使用cin將忽略空格,使用get()將不會(huì)忽略空格
while (inFile.good())//使用good檢查文件讀取是否正常,是否抵達(dá)文件尾
{
count++;
inFile.get(ch);
}
cout<<count<<" characters reads.\n";
inFile.close();
return 0;
}
//PP6.11.9
#include <iostream>
#include <fstream>
const int ArSize=20;
int num=0;//創(chuàng)建的動(dòng)態(tài)結(jié)構(gòu)指針數(shù)組成員數(shù)
struct fund
{
char name[ArSize];
double money;
};
int main()
{
using namespace std;
//以下是數(shù)據(jù)初始化模塊
int count=0,poor=0;//解決無(wú)人時(shí)none的顯示
fund* person=new fund[num];
char filename[ArSize];
cout<<"Please enter the file name: ";
cin.getline(filename,ArSize);
fstream inFile;
inFile.open(filename);
//以下是文件打開(kāi)成功檢測(cè)
if (!inFile.is_open())
{
cout<<"Fail to open the file.\n";
exit(EXIT_FAILURE);
}
//以下是數(shù)據(jù)讀取模塊
inFile>>num;//讀取應(yīng)該創(chuàng)建的結(jié)構(gòu)數(shù)
inFile.get();//讀取數(shù)組后再讀取換行符
for (int i=0;i<num;i++)
{
inFile.get(person[i].name,ArSize).get();//讀取名字后讀取換行符
inFile>>person[i].money;//讀取double,注意讀取方式
inFile.get();//讀取換行符
if (inFile.eof())
{
cout<<"End of file reached:OK!\n";
}
else if (inFile.fail())
{
cout<<"Input terminated by data mismatch.\n";
}
else
continue;
}
cout<< "Grand Patrons: \n";//開(kāi)始輸出重要人物
for (int i=0;i<num;i++)
{
if (person[i].money > 10000)
{
cout << person[i].name << " : " << person[i].money <<"$"<<endl;
count++;
}
}
if (count == 0)
cout << "none.\n";
cout<<"Patrons: \n";//開(kāi)始輸出一般捐獻(xiàn)者
for (int i=0;i<num;i++)
{
if (person[i].money<=10000)
{
cout<<person[i].name<<endl;
poor++;
}
}
if (poor==0)
cout<<"none.\n";
delete [] person;
return 0;
}