C++ Primer Plus 编程练习ch4

来源:互联网 发布:什么是数据库的安全性 编辑:程序博客网 时间:2024/06/08 19:41

4-1

#include<iostream>#include<string>#include<cstring>using namespace std;int main() {    cout << "What is your first name? ";    char fname[20];    cin.getline(fname, 20);    cout << "What is your last name? ";    char lname[20];    cin.getline(lname, 20);    cout << "What letter grade do you deserve? ";    char grade;    cin >> grade;    cout << "What is your age? ";    int age;    cin >> age;    cout << "Name: " << lname << "," << fname << endl;    cout << "Grade: " << ++grade << endl;    cout << "Age: " << age << endl;    system("pause");    return 0;}

4-2


#include<iostream>#include<string>#include<cstring>using namespace std;int main() {    cout << "What is your first name? ";    string fname;    getline(cin, fname);    cout << "What is your last name? ";    string lname;    getline(cin, lname);    cout << "What letter grade do you deserve? ";    char grade;    cin >> grade;    cout << "What is your age? ";    int age;    cin >> age;    cout << "Name: " << lname << "," << fname << endl;    cout << "Grade: " << ++grade << endl;    cout << "Age: " << age << endl;    system("pause");    return 0;}


4-3


#include<iostream>#include<string>#include<cstring>using namespace std;int main() {cout << "Enter your first name:";char fname[20];cin >> fname;cout << "Enter your last name:";char lname[20];cin >> lname;cout << "Here's the information in a single string:";strcat_s(lname, ", ");strcat_s(lname, fname);cout << lname << endl;system("pause");return 0;}

4-4


#include<iostream>#include<string>#include<cstring>using namespace std;int main() {cout << "Enter your first name:";string fname;cin >> fname;cout << "Enter your last name:";string lname;cin >> lname;cout << "Here's the information in a single string:";lname += ", ";lname += fname;cout << lname << endl;system("pause");return 0;}


4-5


#include<iostream>#include<string>#include<cstring>using namespace std;int main() {struct CandyBar {string brand;double weight;int calorie;};CandyBar snack = { "Mocha Munch", 2.3, 350 };cout << "snack.brand = " << snack.brand << endl;cout << "snack.weight = " << snack.weight << endl;cout << "snack.calorie = " << snack.calorie << endl;system("pause");return 0;}


4-6


#include<iostream>#include<string>#include<cstring>using namespace std;int main() {struct CandyBar {string brand;double weight;    //poundint calorie;};CandyBar renko[3];renko[0] = { "Mocha Munch", 2.3, 350 };renko[1] = { "Orio Strawberry Cake", 1.1,1390 };renko[2] = { "Egg Pudding", 0.2, 174 };for (int i = 0; i < 3; i++) {cout << "renko[" << i << "].brand = " << renko[i].brand << endl;cout << "renko[" << i << "].weight = " << renko[i].weight << endl;cout << "renko[" << i << "].calorie = " << renko[i].calorie << endl;}system("pause");return 0;}


4-9


#include<iostream>#include<string>#include<cstring>using namespace std;int main() {struct CandyBar {string brand;double weight;    //poundint calorie;};CandyBar * renko = new CandyBar[3];renko[0] = { "Mocha Munch", 2.3, 350 };renko[1] = { "Orio Strawberry Cake", 1.1,1390 };renko[2] = { "Egg Pudding", 0.2, 174 };for (int i = 0; i < 3; i++) {cout << "renko[" << i << "].brand = " << renko[i].brand << endl;cout << "renko[" << i << "].weight = " << renko[i].weight << endl;cout << "renko[" << i << "].calorie = " << renko[i].calorie << endl;}delete[] renko;system("pause");return 0;}

4-10


#include<iostream>#include<string>#include<cstring>#include<array>using namespace std;int main() {array<double, 3>run;cout << "The first test: ";cin >> run[0];cout << "The seond test: ";cin >> run[1];cout << "The third test: ";cin >> run[2];double ave = (run[0] + run[1] + run[2]) / 3.0;cout << "The average is " << ave << endl;system("pause");return 0;}


1 0
原创粉丝点击