赞
踩
第四章编程练习(1-5)
4.13.1.cpp
#include<iostream>
using namespace std;
struct infatable {
char first_name[20];
char last_name[20];
int age;
char grade;
};
int main() {
infatable a;
cout << "What's your first name? ";
cin >> a.first_name;
cout << "What's your last name? ";
cin >> a.last_name;
cout << "What letter grade do you deserve? ";
cin >> a.grade;
cout << "What's your age? ";
cin >> a.age;
cout << "Name: " << a.last_name << "," << a.first_name << endl
<< "Grade: " << ++(a.grade) << endl
<< "Age: " << a.age << endl;
return 0;
}
4.13.2.cpp
#include<iostream>
#include<string>
using namespace std;
int main() {
string name, dessert;
cout << "Enter your name: ";
getline(cin, name);
cout << "Enter your favorite dessert: ";
getline(cin, dessert);
cout << "I have some delicious " << dessert << " for you,"
<< name << "." << endl;
return 0;
}
4.13.3.cpp
#include<iostream>
#include<cstring>
using namespace std;
int main() {
const int num = 20;
char first_name[num];
char last_name[num];
char name[num * 2];
cout << "Enter your first name: ";
cin.getline(first_name,num);
cout << "Enter your last name: ";
cin.getline(last_name,num);
strcpy_s(name, last_name);
strcat_s(name, ",");
strcat_s(name, first_name);
cout << "Here's the information in a single string : ";
cout << name << endl;
return 0;
}
4.13.4.cpp
#include<iostream>
#include<string>
using namespace std;
string name(string a, string b) {
string c;
c = b + "," + a;
return c;
}
int main() {
string first_name;
string last_name;
cout << "Enter your first name: ";
getline(cin,first_name);
cout << "Enter your last name: ";
getline(cin,last_name);
cout << "Here's the information in a single string : ";
cout << name(first_name, last_name);
return 0;
}
4.13.5
#include<iostream>
using namespace std;
struct CandyBar {
string brand;
float weight;
int cal;
};
int main() {
CandyBar snack{
"Mocha Munch",
2.3,
350
};
cout << "The brand of sugar cubes: " << snack.brand << endl;
cout << "The weight of sugar cubes: " << snack.weight << endl;
cout << "The calorie of sugar cubes: " << snack.cal << endl;
return 0;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。