当前位置: 首页 > news >正文

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;
}


http://www.mrgr.cn/news/30449.html

相关文章:

  • 【读书笔记-《30天自制操作系统》-22】Day23
  • 枚举(not二分)
  • 【移动端开发】“明日头条APP”
  • 【Java】网络编程-地址管理-IP协议后序-NAT机制-以太网MAC机制
  • [2025]基于微信小程序慢性呼吸系统疾病的健康管理(源码+文档+解答)
  • 预处理、makefile、静动态库编写、nfs挂载、快捷命令
  • 白酒与朋友聚餐:如何活跃气氛,增进感情?
  • 每日OJ题_牛客_除2!(贪心+堆)
  • 2024.9.19
  • 【C++算法】模拟算法
  • 【MySQL】表的相关操作
  • linux网络编程3
  • 项目实现:云备份服务端①(文件操作、Json等工具类实现)
  • 建设工程联合体的性质如何认定?
  • 【机器学习】经典数据集鸢尾花的分类识别
  • Yocto - 使用Yocto开发嵌入式Linux系统_01 前言
  • 模型训练时CPU和GPU大幅度波动——可能是数据的读入拖后腿
  • OJ在线评测系统 思考主流OJ的实现方案 常用概念 自己的思考
  • Win32 Wmi获取设备信息
  • 总结拓展十:SAP开发计划(下)