C++ prime plus-4-编程练习
#include <iostream>
#include <string>
int main() {
// 声明变量用于存储用户信息
std::string first_name;
std::string last_name;
char letter_grade;
int age;
// 请求用户输入名字
std::cout << "What is your first name? ";
std::getline(std::cin, first_name);
// 请求用户输入姓氏
std::cout << "What is your last name? ";
std::getline(std::cin, last_name);
// 请求用户输入成绩
std::cout << "What letter grade do you deserve? ";
std::cin >> letter_grade;
std::cin.ignore(); // 忽略输入流中的换行字符
// 请求用户输入年龄
std::cout << "What is your age? ";
std::cin >> age;
std::cin.ignore(); // 忽略输入流中的换行字符
// 向上调整成绩
switch (letter_grade) {
case 'A':
case 'a':
letter_grade = 'A';
break;
case 'B':
case 'b':
letter_grade = 'A';
break;
case 'C':
case 'c':
letter_grade = 'B';
break;
default:
// 对于D和F,我们不调整,因为您提到不必担心D和F之间的空档
break;
}
// 显示用户信息
std::cout << "Name: " << last_name << ", " << first_name << "\n";
std::cout << "Grade: " << letter_grade << "\n";
std::cout << "Age: " << age << std::endl;
return 0;
}
2,
#include <iostream>
#include <string>
int main() {
// 使用 std::string 来存储名字和最爱的甜点
std::string name;
std::string dessert;
// 请求用户输入名字
std::cout << "Enter your name: ";
std::getline(std::cin, name); // 读取一行,包括空格
// 请求用户输入最爱的甜点
std::cout << "Enter your favorite dessert: ";
std::getline(std::cin, dessert); // 读取一行,包括空格
// 显示用户信息
std::cout << "I have some delicious " << dessert;
std::cout << " for you, " << name << ".\n";
return 0;
}
3,
#include <iostream>
#include <cstring>
int main() {
// 定义字符数组的大小
const int ArSize = 20;
char first_name[ArSize];
char last_name[ArSize];
char combined[ArSize * 2]; // 假设组合后的字符串不会超过两个数组大小的总和
// 请求用户输入名字
std::cout << "Enter your first name: ";
std::cin.getline(first_name, ArSize);
// 请求用户输入姓氏
std::cout << "Enter your last name: ";
std::cin.getline(last_name, ArSize);
// 使用strcat函数将姓氏和名字组合起来,并在中间加入逗号和空格
strcpy(combined, last_name); // 首先复制姓氏
strcat(combined, ", "); // 然后添加逗号和空格
strcat(combined, first_name); // 最后添加名字
// 显示组合结果
std::cout << "Here's the information in a single string: " << combined << std::endl;
return 0;
}
//程序使用 std::cin.getline 函数来读取用户的输入,然后使用 cstring 头文件中的 strcpy 和 strcat 函数来组合姓氏和名字。strcpy 函数用于复制字符串,而 strcat 函数用于将一个字符串追加到另一个字符串的末尾。
4,
#include <iostream>
#include <string>
int main() {
// 声明std::string对象来存储名字和姓氏
std::string first_name;
std::string last_name;
// 请求用户输入名字
std::cout << "Enter your first name: ";
std::getline(std::cin, first_name);
// 请求用户输入姓氏
std::cout << "Enter your last name: ";
std::getline(std::cin, last_name);
// 使用逗号和空格将姓氏和名字组合起来
std::string combined = last_name + ", " + first_name;
// 显示组合结果
std::cout << "Here's the information in a single string: " << combined << std::endl;
return 0;
}
5,
#include <iostream>
#include <string>
// 声明结构体 CandyBar
struct CandyBar {
std::string brand; // 糖块品牌
double weight; // 糖块重量
int calories; // 糖块卡路里含量
};
int main() {
// 创建并初始化名为snack的CandyBar变量
CandyBar snack{"Mocha Munch", 2.3, 350};
// 显示snack变量的内容
std::cout << "Brand: " << snack.brand << std::endl;
std::cout << "Weight: " << snack.weight << std::endl;
std::cout << "Calories: " << snack.calories << std::endl;
return 0;
}
6,
#include <iostream>
#include <string>
// 声明结构体 CandyBar
struct CandyBar {
std::string brand; // 糖块品牌
double weight; // 糖块重量
int calories; // 糖块卡路里含量
};
int main() {
// 创建并初始化CandyBar数组
CandyBar candyBars[] = {
{"Mocha Munch", 2.3, 350},
{"Choco Chunky", 1.5, 450},
{"Caramel Craze", 3.0, 300}
};
// 计算数组中的元素数量
int size = sizeof(candyBars) / sizeof(CandyBar);
// 显示每个结构的内容
for (int i = 0; i < size; i++) {
std::cout << "CandyBar " << i + 1 << ":\n";
std::cout << "Brand: " << candyBars[i].brand << std::endl;
std::cout << "Weight: " << candyBars[i].weight << " oz\n"; // 假设重量单位是盎司
std::cout << "Calories: " << candyBars[i].calories << std::endl;
std::cout << std::endl; // 添加空行以便于阅读
}
return 0;
}
7,
#include <iostream>
#include <string>
// 声明结构体 Pizza
struct Pizza {
std::string company; // 比萨饼公司名称
double diameter; // 比萨饼直径
double weight; // 比萨饼重量
};
int main() {
Pizza myPizza;
// 请求用户输入比萨饼公司名称
std::cout << "Enter the name of the pizza company: ";
std::getline(std::cin, myPizza.company);
// 请求用户输入比萨饼直径
std::cout << "Enter the diameter of the pizza: ";
std::cin >> myPizza.diameter;
std::cin.ignore(); // 忽略输入流中的换行字符
// 请求用户输入比萨饼重量
std::cout << "Enter the weight of the pizza: ";
std::cin >> myPizza.weight;
std::cin.ignore(); // 忽略输入流中的换行字符
// 显示比萨饼信息
std::cout << "You entered the following pizza information:\n";
std::cout << "Company: " << myPizza.company << std::endl;
std::cout << "Diameter: " << myPizza.diameter << " inches" << std::endl;
std::cout << "Weight: " << myPizza.weight << " ounces" << std::endl;
return 0;
}
8,
#include <iostream>
#include <string>
// 声明结构体 Pizza
struct Pizza {
std::string company; // 比萨饼公司名称
double diameter; // 比萨饼直径
double weight; // 比萨饼重量
};
int main() {
// 使用 new 为结构分配内存
Pizza* myPizza = new Pizza;
// 请求用户输入比萨饼直径
std::cout << "Enter the diameter of the pizza: ";
std::cin >> myPizza->diameter;
std::cin.ignore(); // 忽略输入流中的换行字符
// 请求用户输入比萨饼重量
std::cout << "Enter the weight of the pizza: ";
std::cin >> myPizza->weight;
std::cin.ignore(); // 忽略输入流中的换行字符
// 请求用户输入比萨饼公司名称
std::cout << "Enter the name of the pizza company: ";
std::getline(std::cin, myPizza->company);
// 显示比萨饼信息
std::cout << "You entered the following pizza information:\n";
std::cout << "Company: " << myPizza->company << std::endl;
std::cout << "Diameter: " << myPizza->diameter << " inches" << std::endl;
std::cout << "Weight: " << myPizza->weight << " ounces" << std::endl;
// 释放分配的内存
delete myPizza;
return 0;
}
9,
#include <iostream>
#include <string>
// 声明结构体 CandyBar
struct CandyBar {
std::string brand; // 糖块品牌
double weight; // 糖块重量
int calories; // 糖块卡路里含量
};
int main() {
// 使用 new 动态分配包含3个元素的CandyBar数组
CandyBar* candyBars = new CandyBar[3];
// 初始化数组元素
candyBars[0] = {"Mocha Munch", 2.3, 350};
candyBars[1] = {"Choco Chunky", 1.5, 450};
candyBars[2] = {"Caramel Craze", 3.0, 300};
// 显示每个结构的内容
for (int i = 0; i < 3; ++i) {
std::cout << "CandyBar " << (i + 1) << ":\n";
std::cout << "Brand: " << candyBars[i].brand << std::endl;
std::cout << "Weight: " << candyBars[i].weight << " oz\n"; // 假设重量单位是盎司
std::cout << "Calories: " << candyBars[i].calories << std::endl;
std::cout << std::endl; // 添加空行以便于阅读
}
// 释放分配的内存
delete[] candyBars;
return 0;
}
10,
#include <iostream>
int main() {
// 声明一个数组来存储成绩
double times[3];
double sum = 0.0; // 用于计算总和
int count = 0; // 用于计算次数
// 请求用户输入三次40码跑的成绩
for (int i = 0; i < 3; ++i) {
std::cout << "Enter your 40-yard dash time (in seconds) attempt " << (i + 1) << ": ";
std::cin >> times[i];
sum += times[i];
count++;
}
// 计算平均成绩
double average = sum / count;
// 显示次数和平均成绩
std::cout << "You have made " << count << " attempts." << std::endl;
std::cout << "Your average time is " << average << " seconds." << std::endl;
return 0;
}