本文主要是介绍C++Primer Plus6编程题(第四章),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
4.13.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
注意,该程序应该接受的名字包括多个单词.另外,程序将向下调整程序,用户请求A\B\C,所以不必担心D和F之间的空挡
*/
#include<iostream>
#include<cstring>
#include<sstream>//getline
using namespace std;
int main() {
string first_Name;
string lase_Name;
char grade;
int age;
cout << "What is your first name? " << endl;
getline(cin,first_Name);
cout << "What is your last name?" << endl;
getline(cin, lase_Name);
while (1) {
cout << "What letter grade do you deserve?(A,B,C)" << endl;
cin >> grade;
if (grade == 'A') {
grade = 'B';
break;
}
else if (grade == 'B') {
grade = 'C';
break;
}
else if (grade == 'C') {
grade = 'D';
break;
}
else {
cout << "输入错误,请重新输入" << endl;
continue; //
}
}
cout << "What is your age?" << endl;
cin >> age;
cout << "Name:" <<lase_Name<< ',' << first_Name << endl;
cout << "Grade:" << grade << endl;
cout << "Age:" <<age<< endl;
system("pause");
return 0;
}
/*
4.13.2 修改程序清单4.4 使用c++string类 而不是char数组
*/
#include<iostream>
#include<sstream>
int main() {
using namespace std;
const int ArSize = 20;
char name[ArSize];
char dessert[ArSize];
string x1, x2;
cout << "Enter your name:\n";
cin.getline(name, ArSize);
cout << "Enter your favorite dessert:\n";
cin.getline(dessert, ArSize);
cout << "I have some delicious " << dessert;
cout << "for you" << name << ".\n";
cout << "Enter your name:\n";
getline(cin, x1);
cout << "Enter your favorite dessert:\n";
getline(cin, x2);
cout << "I have some delicious " << x2;
cout << "for you" << x1 << ".\n";
return 0;
}
/*4.13.3
编写一个程序,他要求用户首先输入其名,然后输入其姓;然后程序使用一个逗号和空格将姓和名组合起来,
并且存储和显示其结果.请使用 char数组和cstring中的函数
Enter your first name :Flip;
Enter your lase name :Fleming
Here's the information in a single string:Fleming,Flip
*/
#define _CRT_SECURE_NO_WARNINGS //避免报错
#include<iostream>
#include<cstring>
using namespace std;
const int ArSize = 20;
int main() {
char fi_name[ArSize];
char la_name[ArSize];
char name[41];
cout << "Enter your first name " << endl;
cin.getline(fi_name, 20);
cout << "Enter your lase name :Fleming" << endl;
cin.getline(la_name, 20);
char* name = new char[strlen(fi_name) + strlen(la_name) + 5]; 动态分配
strcpy(name, la_name);
cout << name;
strcat(name, ",");
cout << name;
strcat(name, fi_name);
cout << name<<strlen(name);
delete[] name;
return 0;
}
/*4.13.4
编写一个程序 和上题一样这次使用string和string头文件
*/
#include<iostream>
#include<string>
using namespace std;
int main() {
string f_name, l_name, name;
getline(cin, f_name);
getline(cin, l_name);
name = f_name + "," + l_name;
cout << name;
return 0;
}
/*
5结构CandyBar包含3个成员
第一个是品牌,第二个质量 第三个卡路里
编写程序,声明结构
结构题声明的名字叫snack 初始化"Mocha Munch" 2.3 350
程序显示内容
*/
#include<iostream>
using namespace std;
struct CandyBar {
string Logo;
float quality;
int hot;
};
int main() {
CandyBar snack = {
"Mocha Munch",
2.3,
350
};
cout << snack.Logo << " " << snack.quality << " " << snack.hot;
return 0;
}
/* 6 - 8 将创建5写的candyBar的数组,将他们初始化为所选择的值, 然后显示每个结构的内容*/
#include<iostream>
using namespace std;
struct CandyBar {
string Logo;
float quality;
int hot;
};
int main() {
CandyBar snack[3] = {
{"Mocha Munch", 2.3, 350},
{"Mooncake", 3.5, 369},
{"Birthdaycake", 6.8, 460},
};
cout << "The first candy:" << endl;
cout << "Brand: " << snack[0].Logo << endl;
cout << "Candy_weight: " << snack[0].quality << endl;
cout << "Candy_calorie: " << snack[0].hot << endl;
CandyBar* p = new CandyBar[3];
p[0].Logo= "";
delete[] p;
return 0;
}
/*
William Wingate 从事比萨饼为分析服务,对于每个比萨饼,他都要记录以下信息
*比萨饼公司的名称,可以由多个单词组成
* 比萨饼的直径
* 比萨饼的重量
设计一个结构记录这些信息,cin输入,cout输出显示完整信息
*/
#include<iostream>
#include<sstream>
using namespace std;
struct bisai {
string name;
double length;
double weight;
};
int main() {
bisai x;
cout << "请输入比萨饼公司名称:" << endl;
getline(cin, x.name);
cout << "请输入比萨饼直径"<<endl;
cin >> x.length;
cout << "请输入比萨饼重量" << endl;
cin >> x.weight;
cout << x.name << ":" << x.length << ";" << x.weight;
8.使用new分配内存
bisai* x = new bisai;
cout << "请输入比萨饼直径"<<endl;
cin >> x->length;
return 0 ;
}
/*10.编写有一个程序让用户输入三次40码跑的成绩,显示次数和平均成绩 请使用一个array对象来存储数据*/
#include<iostream>
#include<array>
const int aSize = 3;
using namespace std;
int main() {
double sum = 0 , sve;
array<double, aSize> x;
for (int i = 0; i < aSize; i++) {
cout << "请输入第" << i + 1 << "次40码成绩" << endl;
cin >> x[i];
sum = x[i] + sum;
}
sve = sum / 3;
cout << sve;
return 0;
}
这篇关于C++Primer Plus6编程题(第四章)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!