c++primer plus 第六章习题

来源:互联网 发布:兄弟网络技术论坛 编辑:程序博客网 时间:2024/05/16 15:44
#include<iostream>#include<stdlib.h>//第六章习题using namespace std;int main(){char ch;cin.get(ch);while (ch != '@'){if (isalpha(ch)){if (islower(ch))cout << char(ch - 32);else if (isupper(ch))cout << char(ch + 32);}else if(!(isdight(ch)))cout << ch;cin.get(ch);}system("pause");return 0;}#include<iostream>#include<stdlib.h>//第六章习题using namespace std;int main(){double donation[10];double average=0,sum=0;int i=0,num=0;cout << "please enter your number:";while (i<10&&cin>>donation[i]){cout << "please enter your number:";sum += donation[i];i++;}average = sum / i;cout << "平均值:" <<average << endl;for (int j = 0; j < i; j++){if (donation[j]>average){num++;}}cout << "超过平均值的数字个数:" << num << endl;system("pause");return 0;}#include<iostream>#include<stdlib.h>//第六章习题using namespace std;void menu();int main(){menu();char choice;while (cin>>choice&&choice!= 'c' && choice != 'p' && choice != 't' && choice != 'g'){cout << "Please enter c p t g :";}switch (choice){case 'c': cout << "I love you!\n"; break;case 'p':cout << "I hate you!\n"; break;case't':cout << "I miss you!\n"; break;case'g':cout << "oh my gald!\n"; break;}system("pause");return 0;}void menu(){cout << "Please enter c p t g :\n""c) carnivore          p) pianist\n""t) tree               g) game\n";}#include<iostream>#include<stdlib.h>//第六章习题using namespace std;void menu();int main(){menu();char choice;cin >> choice;while (choice != 'q'){switch (choice){case 'a': cout << "I love you!\n"; break;case 'b':cout << "I hate you!\n"; break;case'c':cout << "I miss you!\n"; break;case'd':cout << "oh my gald!\n"; break;}cout << "Enter your choice:";cin >> choice;}system("pause");return 0;}void menu(){cout << "Please enter a b c d q :\n""a) carnivore          b) pianist\n""c) tree               c) game\n""q) quit\n""Enter your choice:";}#include<iostream>#include<stdlib.h>//第六章习题using namespace std;void menu();int main(){int choice;double tax;cout << "Enter your salary:";while (cin >> choice){if (choice < 0)break;else if (choice <= 5000){tax = 0;cout << "Tax:" << tax << endl;}else if (choice>5000&&choice<=15000){tax = (choice-5000)*0.1;cout << "Tax:" << tax << endl;}else if (choice>15000 && choice <= 35000){tax = (choice-15000)*0.15 + 10000 * 0.1;cout << "Tax:" << tax << endl;}else{tax = (choice-35000)*0.2 + 20000 * 0.15 + 10000 * 0.1;cout << "Tax:" << tax << endl;}cout << "Enter your salary:";}system("pause");return 0;}#include<iostream>#include<stdlib.h>#include<string>#include<cstring>//第六章习题using namespace std;struct donate{string  name;double  money;};int main(){int num;cout << "Please enter donate numbers:";cin >> num;cin.get();donate *d1 = new donate[num];for (int i = 0; i < num; i++){cout << "Please enter name:";getline(cin,d1[i].name);cout << "Please enter money:";cin >> d1[i].money;cin.get();}cout << "Grand Patrons:";int counter=0;for (int i = 0; i < num; i++){if (d1[i].money>10000){cout << d1[i].name << '\t'; counter++;}}if (counter == 0){cout << "None !" << endl;}cout << '\n';counter = 0;cout << "Patrons:";for (int i = 0; i < num; i++){if (d1[i].money < 10000){cout << d1[i].name << '\t'; counter++;}}cout << "\n";if (counter == 0){cout << "None !" << endl; }  delete[]d1;system("pause");return 0;}#include<iostream>#include<stdlib.h>#include<string>#include<cstring>//第六章习题using namespace std;int main(){cout << "Enter words(q to quit):";string s;int counter=0,i=0,j=0;while (cin >> s){if (s[0] == 'q' || s[0] == 'Q')break;else if (s[0] == 'a' || s[0] == 'A' || s[0] == 'e' || s[0] == 'E' ||s[0] == 'i' || s[0] == 'I')counter++;else if (s[0] == 'b' || s[0] == 'c')i++;elsej++;}cout << counter << "words begin with vowels" << endl;cout << i << "words begin with constants" << endl;cout << j << "words begin with others" << endl;system("pause");return 0;}#include<iostream>#include<stdlib.h>#include<fstream>//第六章习题using namespace std;int main(){ifstream infile;int count=0;infile.open("1.txt");char s;infile >> s;while (infile.good()){++count;infile >> s;}cout << count << " Items!\n";infile.close();system("pause");return 0;}#include<iostream>#include<stdlib.h>#include<string>#include<cstring>//第六章习题using namespace std;struct donate{string  name;double  money;};int main(){int num;cout << "Please enter donate numbers:";cin >> num;cin.get();donate *d1 = new donate[num];for (int i = 0; i < num; i++){cout << "Please enter name:";getline(cin,d1[i].name);cout << "Please enter money:";cin >> d1[i].money;cin.get();}for (int i = 0; i < num; i++){cout <<"Name:"<< d1[i].name <<endl;cout <<"Money:"<< d1[i].money << endl;}    delete[]d1;system("pause");return 0;}

1 0
原创粉丝点击