1.编写一个C++程序,如下述输出示例所示的那样请求并显示信息。
What is your first name? Betty Sue
What is your last name? Yewe
What letter grade do you deserve? B
What is your age? 22
Name: Yewe, Betty Sue
Grade: C
Age: 22
#include<iostream>
using namespace std;
int main()
{
const int a = 50 ;
char name[a];
char lname[a];
char grade[a];
char age[a];
cout << "What is your first name?";
cin.get(name, a).get();
cout << "What is your last name?";
cin.get(lname, a).get();
cout << "What letter grade do you deserve?";
cin.get(grade, a).get();
cout << "What is your age?";
cin.get(age, a).get();
cout << "Name:" << lname << "," << name <<endl;
cout << "Grade:" << grade << endl;
cout << "Age:" << age << endl;
return 0;
}
2.修改程序清单4.4,使用C++string类而不是char数组。
//程序清单4.4
#include<iostream>
using namespace std;
int main()
{
const int size = 100;
char name[size];
char dessert[size];
cout << "Enter your name:" << endl;
cin.getline(name, size);
cout << "Enter your favorite dessert " << endl;
cin.getline(dessert, size);
cout << "I have some delicious " << dessert;
cout << "for you, " << name << endl;
return 0;
}
修改后:
#include<iostream>
using namespace std;
int main()
{
string name;
string dessert;
cout << "Enter your name:" << endl;
getline(cin,name);
cout << "Enter your favorite dessert " << endl;
getline(cin,dessert);
cout << "I have some delicious " << dessert;
cout << " for you, " << name << endl;
return 0;
}
3.编写一个程序,它要求用户首先输入其名,然后输入其姓;然后程序使用一个逗号和空格将姓和名组合起来,并存储和显示组合结果。请使用char数组和头文件cstring中的函数。下面是该程序运行时的情形。
Enter your first name: Flip
Enter your last name: Fleming
Here's the information in a single string: Fleming Flip
#include<cstring>
#inlcude<iostream>
using namespace std;
int main()
{
const int size = 100;
char fname[size];
char lname[size];
cout << "Enter your first name: ";
cin.getline(fname, size);
cout << "Enter your last name: ";
cin.getline(lname, size);
cout << "Here's the information in a single string: ";
strcat_s(lname, ", ");
strcat_s(lname, fname);
cout << lname;
return 0;
}
4.
编写一个程序,它要求用户首先输入其名,然后输入其姓;然后程序使用一个逗号和空格将姓和名组合起来,并存储和显示组合结果。请使用string对象和头文件string中的函数。下面是该程序运行时的情形。
Enter your first name: Flip
Enter your last name: Fleming
Here's the information in a single string: Fleming Flip
#include<iostream>
#include<string>
using namespace std;
int main()
{
string fname;
string lname;
cout << "Enter your first name: ";
getline(cin,fname);
cout << "Enter your last name: ";
getline(cin,lname);
cout << "Here's the information in a single string: ";
fname = lname + ", " + fname;
cout << fname;
return 0;
}
5. 结构CandyBar包含3个成员。第一个成员存储了糖块的品牌;第二个成员存储糖块的重量(可以有小数);第三个成员存储了糖块的卡路里含量(整数)。请编写一个程序,声明这个结构,创建一个名为snack的CandyBar变量并将其成员分别初始化为“Mocha Munch”、2.3和350.初始化应在声明snack时进行。最后,程序显示snack变量的内容。
#include<iostream>
using namespace std;
struct CandyBar
{
char brand[1000];
double weight;
int colorie;
};
int main()
{
CandyBar snack =
{
"Mocha Munch",
2.3,
350
};
cout << "牌子:" << snack.brand << endl;
cout << "重量:" << snack.weight << endl;
cout << "卡路里:" << snack.colorie << endl;
return 0;
}
6.结构CandyBar包含3个成员,如编程练习5所示。请编写一个程序,创建一个包含3个元素的CandyBar数组,并将它们初始化为选择的值,然后显示每个结构的内容。
#include<iostream>
using namespace std;
struct CandyBar
{
char brand[1000];
double weight;
int colorie;
};
int main()
{
CandyBar snack[3] =
{
{"Mocha Munch",2.3,350},
{"special ll",2.5,450},
{"lily whow",3.5,756},
};
cout << "牌子:" << snack[0].brand << endl;
cout << "重量:" << snack[0].weight << endl;
cout << "卡路里:" << snack[0].colorie << endl;
return 0;
}