C++ Primer Plus (第六版) 编程练习4.13

#1:

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

#2:

#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
string dessert;
cout << "Enter your name:\n";
getline(cin, name);
cout << "Enter your favorite dessert:\n";
getline(cin, dessert);
cout << "I have some delicious " << dessert;
cout << " for you, " << name << ".\n";
system("pause");
return 0;
}

#3:

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

#4:

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

#5:

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

#6:

#include <iostream>
#include <string>
using namespace std;
struct CandyBar
{
string brand;
double weight;
double  calorie;
};
int main()
{
CandyBar candyArr[3];
for (int i = 0; i < 3;i++)
{
cout << "Please Enter #" << i + 1 << " Brand: ";
getline(cin, candyArr[i].brand);
cout << "Please Enter #" << i + 1 << " Weight: ";
cin>>candyArr[i].weight;
cout << "Please Enter #" << i + 1 << " Calorie: ";
cin>>candyArr[i].calorie;
cin.get();
}
for (int i = 0; i < 3; i++)
{
cout << "#" << i + 1 << ":\n";
cout << "Brand: " << candyArr[i].brand;
cout << ", Weight: " << candyArr[i].weight;
cout << ", Calorie: " << candyArr[i].calorie;
}
system("pause");
return 0;
}

#7:

#include <iostream>
#include <string>
using namespace std;
struct Pizza
{
string companyName;
double Dia;
double weight;
};
int main()
{
Pizza WillPizza;
cout << "Please Enter The Pizza Company Name: ";
getline(cin, WillPizza.companyName);
cout << "Please Enter The Pizza Dia: ";
cin >> WillPizza.Dia;
cout << "Please Enter The Pizza Weight: ";
cin >> WillPizza.weight;
cout << "Here's the information of your Pizza:\n";
cout << "Company Name: " << WillPizza.companyName << endl;
cout << "Dia: " << WillPizza.Dia << endl;
cout << "Weight: " << WillPizza.weight << endl;
system("pause");
return 0;
}

#8:

#include <iostream>
#include <string>
using namespace std;
struct Pizza
{
string companyName;
double Dia;
double weight;
};
int main()
{
auto WillPizza = new Pizza;
cout << "Please Enter The Pizza Dia: ";
cin >> WillPizza->Dia;
cin.get();
cout << "Please Enter The Pizza Company Name: ";
getline(cin, WillPizza->companyName);
cout << "Please Enter The Pizza Weight: ";
cin >> WillPizza->weight;
cout << "Here's the information of your Pizza:\n";
cout << "Company Name: " << WillPizza->companyName << endl;
cout << "Dia: " << WillPizza->Dia << endl;
cout << "Weight: " << WillPizza->weight << endl;
system("pause");
delete WillPizza;
return 0;
}

#9:

#include <iostream>
#include <string>
using namespace std;
struct CandyBar
{
string brand;
double weight;
double  calorie;
};
int main()
{
auto candyArr = new CandyBar[3];
for (int i = 0; i < 3; i++)
{
cout << "Please Enter #" << i + 1 << " Brand: ";
getline(cin, candyArr[i].brand);
cout << "Please Enter #" << i + 1 << " Weight: ";
cin >> candyArr[i].weight;
cout << "Please Enter #" << i + 1 << " Calorie: ";
cin >> candyArr[i].calorie;
cin.get();


}
for (int i = 0; i < 3; i++)
{
cout << "#" << i + 1 << ":\n";
cout << "Brand: " << candyArr[i].brand;
cout << ", Weight: " << candyArr[i].weight;
cout << ", Calorie: " << candyArr[i].calorie;
}
system("pause");
delete[]candyArr;
return 0;
}

#10:

#include <iostream>
#include <array>
using namespace std;
int main()
{
array<int, 3> runningGrade;
int total = 0;
for (int i =0; i <3;i++)
{
cout << "Please Enter The Grade: ";
cin >> runningGrade[i];
total += runningGrade[i];
}
cout << "The Average = " << total / 3 << endl;
system("pause");
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值