日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++PrimerPlus学习——第四章编程练习

發(fā)布時間:2025/3/11 c/c++ 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++PrimerPlus学习——第四章编程练习 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

**疫情期間學(xué)習(xí)C++
4-1
需要使用cin.get()設(shè)置讀取位數(shù),避免空格導(dǎo)致無法讀取多個詞

#include <iostream> struct info_people //define structure {char first_name[20];char last_name[20];char grade;unsigned int age; }; int main() {using namespace std;info_people* pd = new info_people;cout << "What is your first name?";cin.get(pd->first_name, 20);cout << endl << "What is your last name?";cin >> (*pd).last_name;cout << endl << "What letter grade do you deserve?";cin >> (*pd).grade;cout << endl << "What is your age?";cin >> pd->age;cout << endl << "Name: " << (*pd).first_name << ", " << (*pd).last_name << endl;cout << "Grade: " << char((*pd).grade + 1) << endl;cout << "Age: " << (*pd).age << endl;delete pd;return 0; }

4-2
getline(cin,name)用于讀取一行的string

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

4-3
char設(shè)置長度,沒空格可以直接cin

#include <iostream> #include <cstring> int main() {using namespace std;const int size = 15;char first_name[size];char last_name[size];string dessert;cout << "Enter your fisrt name: ";cin >> first_name;cout << "\nEnter your last name: ";cin >> last_name;cout << "\nHere's the information in a single string: " << first_name << ", " << last_name << endl;return 0; }

4-4
不含空格同樣可以直接cin

#include <iostream> #include <string> int main() {using namespace std;string first_name;string last_name;string dessert;cout << "Enter your fisrt name: ";cin >> first_name;cout << "\nEnter your last name: ";cin >> last_name;cout << "\nHere's the information in a single string: " << first_name << ", " << last_name << endl;return 0; }

4-5

#include <iostream> struct CandyBar {char brand[20];float weight;unsigned int cal; };int main() {using namespace std;CandyBar snack ={"Mocha Munch", 2.3, 350};cout << "Brand: " << snack.brand << "\nWeight: " << snack.weight << "\nCal: " << snack.cal << endl;return 0; }

4-6
不知道題目的意思理解錯了沒有。。。

#include <iostream> struct CandyBar {char brand[20];float weight;unsigned int cal; };int main() {using namespace std;CandyBar snack;cout << "Enter the brand: ";cin >> snack.brand;cout << "\nEnter the weight: ";cin >> snack.weight;cout << "\nEnter the cal: ";cin >> snack.cal;cout << "Brand: " << snack.brand << "\nWeight: " << snack.weight << "\nCal: " << snack.cal << endl;return 0; }

4-7

#include <iostream> #include <string> struct Pissa_Info {std::string brand;float diameter;float weight; };int main() {using namespace std;Pissa_Info pissa;cout << "Enter the brand: ";getline(cin, pissa.brand);cout << endl << "Enter the diameter: ";cin >> pissa.diameter;cout << endl << "Enter the weight: ";cin >> pissa.weight;cout << "Brand: " << pissa.brand << "\nDiameter: " << pissa.diameter << "\nWeight: " << pissa.weight<< endl;return 0; }

4-8
cin數(shù)字之后,要使用getline的話需要先用cin.get()跳過換行符

#include <iostream> #include <string> using namespace std;struct Pissa_Info {string brand;float diameter;float weight; };int main() {Pissa_Info* pd = new Pissa_Info;cout << "Enter the diameter: ";cin >> pd->diameter;cin.get();cout << endl << "Enter the brand: ";getline(cin, pd->brand);cout << endl << "Enter the weight: ";cin >> pd->weight;cout << "Brand: " << (*pd).brand << "\nDiameter: " << (*pd).diameter << "\nWeight: " << (*pd).weight<< endl;delete pd;return 0; }

4-9

#include <iostream> #include <string> using namespace std; struct CandyBar {string brand;float weight;unsigned int cal; };int main() {CandyBar* pd = new CandyBar;cout << "Enter the brand: ";getline(cin, pd->brand);cout << "\nEnter the weight: ";cin >> pd->weight;cout << "\nEnter the cal: ";cin >> pd->cal;cout << "Brand: " << (*pd).brand << "\nWeight: " << (*pd).weight << "\nCal: " << (*pd).cal << endl;delete pd;return 0; }

4-10

#include
#include

int main()
{
using namespace std;
array<float, 3> grade;
cout << "The first time finish 40m: ";
cin >> grade[0];
cout << "\nThe second time finish 40m: ";
cin >> grade[1];
cout << "\nThe third time finish 40m: ";
cin >> grade[2];
cout << "Average time is " << (grade[0] + grade[1] + grade[2]) / 3 << endl;
return 0;
}

總結(jié)

以上是生活随笔為你收集整理的C++PrimerPlus学习——第四章编程练习的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。