第四章
4.13.1编写一个c++程序,如下述输出请求并显示信息。
#include <iostream>
int main()
{
using namespace std;
cout << "What is your first name? ";
char firstName[20];
cin.getline(firstName, 20);
cout << "What is your last name? ";
char lastName[20];
cin >> lastName;
cin.sync();
cout << "what letter grade do you deserve? ";
char Grade;
cin.get(Grade);
cout << "What is your age?";
int Age;
cin >> Age;
cout << "Name: " << lastName << ", " << firstName << endl;
cout << "Grade: " << Grade;
cout << "Age: " << Age;
cin.sync();
cin.get();
return 0;
}
4.13.2修改4.13.1,使用c++ string类而不是char数组。
#include <iostream>
#include <string>
int main()
{
using namespace std;
cout << "What is your first name? ";
string firstName;
cin >> firstName;
cin.sync();
cout << "What is your last name? ";
string lastName;
cin >> lastName;
cin.sync();
cout << "what letter grade do you deserve? ";
string Grade;
cin>>Grade;
cout << "What is your age? " ;
int Age;
cin >> Age;
cout << "Name: " << lastName << ", " << firstName << endl;
cout << "Grade: " << Grade;
cout << "Age: " << Age;
cin.sync();
cin.get();
return 0;
}
4.13.3编写一个程序,它首先要求用户输入其名,然后输入姓;然后程序使用一个逗号和空格将姓名结合起来,并储存和显示结果,请使用char和头函数cstring中的函数。
#include <iostream>
#include <cstring>
int main()
{
using namespace std;
char firstName[20];
cout << "Enter your first name: ";
cin >> firstName;
char lastName[20];
cout << "Enter your last name: ";
cin >> lastName;
strcat_s(lastName, ", ");
strcat_s(lastName, firstName);
cout << "Here 's the information in a signal string: " << lastName << endl;
cin.sync();
cin.get();
return 0;
}
运行结果
4.13.4同上,改用string对象。
#include <iostream>
#include <string>
#include <cstring>
int main()
{
using namespace std;
string firstName;
cout << "Enter your first name: ";
cin >> firstName;
string lastName;
cout << "Enter your last name: ";
cin >> lastName;
lastName = lastName + ", ";
lastName = lastName + firstName;
cout << "Here 's the information in a signal string: " << lastName << endl;
cin.sync();
cin.get();
return 0;
}
4.13.5结构CandyBar包含3个成员。第一个成员存储了糖块的品牌;第二个成员存储了糖块的重量,第三个成员存储了糖块的卡路里数(整数)。编写一个程序,声明这个结构,创建一个名为snack的CandyBar变量,并将其成员初始化。
#include <iostream>
#include <string>
using namespace std;
struct CandyBar
{
string Name;
double Weight;
int number;
};
int main()
{
CandyBar snack = { "Mocha Munch", 2.3, 350 };
cout << snack.Name << endl;
cout << snack.Weight << endl;
cout << snack.number << endl;
cin.get();
return 0;
}
4.13.6结果CandyBar的三个成员,编写一个程序,创建一个包含3个元素的CandyBar数组,并初始化为所选择的值。
#include <iostream>
#include <string>
using namespace std;
struct CandyBar
{
string Name;
double Weight;
int number;
};
int main()
{
CandyBar snack[3] = {
{"Mocha Munch", 2.3, 350 },
{ "Avalon_Y", 2.3, 350 },
{"Arthur Yong", 2.3, 350 }
};
for (int i = 0; i <3; i++)
{
cout << snack[i].Name << endl;
cout << snack[i].Weight << endl;
cout << snack[i].number << endl;
cout << endl;
}
cin.get();
return 0;
}
4.13.7William Wingate从事披萨饼分析服务,对于每个披萨记录以下信息
披萨公司的名称,可以由多个单词组成
披萨的直径
披萨的重量
设计一个能存储这些信息的结构,并编写一个使用这种结构的程序。程序将请求用户输入上述信息,然后显示这些信息。
#include <iostream>
#include <string>
struct William
{
std::string Name;
double dia;
double weight;
};
int main()
{
William temp;
using namespace std;
cout << "Enter the name: ";
cin >> temp.Name;
cout << "Enter the dia: ";
cin >> temp.dia;
cout << "Enter the weight: ";
cin >> temp.weight;
cout << endl;
cout << "Name: " << temp.Name << endl;
cout << "Dia: " << temp.dia << endl;
cout << "Weight" << temp.weight << endl;
cin.sync();
cin.get();
return 0;
}
4.13.8完成4.13.7,但使用new来动态分配内存,而不是声明一个结构体
#include <iostream>
#include <string>
struct William
{
std::string Name;
double dia;
double weight;
};
int main()
{
William *temp=new William;
using namespace std;
cout << "Enter the dia: ";
cin >> (*temp).dia;
cout << "Enter the name: ";
cin >> (*temp).Name;
cout << "Enter the weight: ";
cin.sync();
cin >> (*temp).weight;
cout << endl;
cout << "Name: " << temp->Name << endl;
cout << "Dia: " << temp->dia << endl;
cout << "Weight: " << temp->weight << endl;
cin.sync();
cin.get();
delete temp;
return 0;
}
4.13.9完成4.13.6,但使用new来动态分配内存。
#include <iostream>
#include <string>
using namespace std;
struct CandyBar
{
string Name;
double Weight;
int number;
};
int main()
{
CandyBar* snack = new CandyBar[3];
snack[0] = { "Mocha Munch", 2.3, 350 };
snack[1] = { "Avalon_Y", 2.3, 350 };
snack[2] = { "Arthur Yong", 2.3, 350 };
for (int i = 0; i <3; i++)
{
cout << snack[i].Name << endl;
cout << snack[i].Weight << endl;
cout << snack[i].number << endl;
cout << endl;
}
delete [] snack;
cin.get();
return 0;
}
4.13.10编写一个程序,让用户输入三次跑40米所用时间,并显示其平均成绩。使用array完成。
#include <iostream>
#include <array>
int main()
{
using namespace std;
array<double,3> runTime;
for (int i = 0; i < 3; i++)
{
cout << "Enter the RunTime: ";
cin >> runTime[i];
}
cout << "Average Time: " << (runTime[0] + runTime[1] + runTime[2]) / 3 << endl;
cin.sync();
cin.get();
return 0;
}