C++ Primer第四章课后编程题
#include<iostream> #include<string> int main() { using namespace std; string name; string lname; char grade; int age; cout << "What is your first name?"; getline(cin, name); cout << "What is your last name?"; cin >> lname; cout << "What letter grade do you deserve?"; cin >> grade; grade = grade + 1; cout << "what is your age?"; cin >> age; cout << "Name:" << lname << "," << name <<endl; cout << "Grade:" <<grade <<endl; cout << "Age" << age << endl; return 0; }运行结果
#include<iostream> #include<string> int main() { using namespace std; 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"; return 0; }运行结果
#include<iostream> #include<cstring> int main() { using namespace std; const int SIZE = 20; char fname[SIZE]; char lname[SIZE]; char fullname[2*SIZE+1]; cout << "Enter your first name:"; cin >> fname; cout << "Enter your last name:"; cin >> lname; strncpy(fullname,lname,SIZE); strcat(fullname,", "); strncat(fullname,fname,SIZE); cout << "Here's yhe information in a single string:" << fullname <<endl; return 0; }运行结果
#include<iostream> #include<string> int main() { using namespace std; string fname; string lname; string fullname; cout << "Enter your first name:"; cin >> fname; cout << "Enter your last name:"; cin >> lname; fullname = lname + ", " + fname; cout << "Here's yhe information in a single string:" << fullname <<endl; return 0; }运行结果
#include<iostream> struct CandyBar { char brand[20]; double weight; int energy; }; int main() { using namespace std; CandyBar snack = { "Mocha Munch", 2.3, 350 }; cout << "品牌名为:" << snack.brand <<endl; cout << "重量为:" << snack.weight <<endl; cout << "能量为:" << snack.energy <<endl; return 0; }运行结果
#include<iostream> struct CandyBar { char brand[20]; double weight; int energy; }; int main() { using namespace std; CandyBar snack[3] = { {"Mocha Munch", 2.3,350}, {"Coca-cola", 3.1, 500}, {"Nestle", 3.8, 200} }; cout << "品牌名为:" << snack[0].brand <<endl; cout << "重量为:" << snack[0].weight <<endl; cout << "能量为:" << snack[0].energy <<endl; cout << "品牌名为:" << snack[1].brand <<endl; cout << "重量为:" << snack[1].weight <<endl; cout << "能量为:" << snack[1].energy <<endl; cout << "品牌名为:" << snack[2].brand <<endl; cout << "重量为:" << snack[2].weight <<endl; cout << "能量为:" << snack[2].energy <<endl; return 0; }运行结果
#include<iostream> const int LEN = 70; struct inflatable { char name[LEN]; double diameter; double weight; }; int main() { using namespace std; inflatable pizza; cout << "请输入公司名称:"; cin.getline(pizza.name, LEN); cout << "请输入比萨饼的直径:"; cin >> pizza.diameter; cout << "请输入比萨的重量:"; cin >> pizza.weight; cout << "公司:" << pizza.name <<endl; cout << "直径:" << pizza.diameter <<endl; cout << "重量:" << pizza.weight <<endl; return 0; }运行结果
#include<iostream> const int LEN = 70; struct inflatable { char name[LEN]; double diameter; double weight; }; int main() { using namespace std; inflatable *ps = new inflatable; cout << "请输入公司名称:"; cin.getline(ps->name, LEN); cout << "请输入比萨饼的直径:"; cin >> (*ps).diameter; cout << "请输入比萨的重量:"; cin >> ps->weight; cout << "公司:" << (*ps).name <<endl; cout << "直径:" << ps->diameter <<endl; cout << "重量:" << ps->weight <<endl; delete ps; return 0; }运行结果
9、//暂时有问题,先发布
#include<iostream> const int LEN=60; struct CandyBar { char brand[LEN]; double weight; int energy; }; int main() { using namespace std; CandyBar *snack = new CandyBar[3]; cout << "输入品牌名称:"; cin.getline(snack->brand, LEN); cout << "请输入重量:"; cin >> snack->weight; cout << "请输入能力:"; cin >> snack->energy; cout << "输入品牌名称:"; cin.getline((snack+1)->brand, LEN); cout << "请输入重量:"; cin >> (snack+1)->weight; cout << "请输入能力:"; cin >> (snack+1)->energy; cout << "第一个数组数据:" << snack->brand << " " << snack->weight << " " < < snack->energy <<endl; cout << "第二个数组数据:" << (snack+1)->brand << " " << (snack+1)->weight << " " << (snack+1)->energy <<endl; return 0; }运行结果
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。