C++ Primer Plus第六版编程练习---第4章 复合类型
1、
#include <iostream>
int main(int argc, char* argv[])
{
char firstName[50] = {0};
char lastName[50] = {0};
char grade;
int age;
std::cout << "What is your first name? ";
std::cin.getline(firstName, 49);
std::cout << "What is your last name? ";
std::cin.getline(lastName, 49);
std::cout << "What letter grade do you deserve? ";
std::cin >> grade;
std::cout << "What is your age? ";
std::cin >> age;
std::cout << "Name: "<< lastName << ", "<< firstName <<std::endl;
std::cout << "Grade: "<< (char)(grade + 1) <<std::endl;
std::cout << "Age: "<< age <<std::endl;
return 0;
}
2、
//instr2.cpp --- reading more than one word with getline
#include <iostream>
#include <string>
int main()
{
std::string name;
std::string dessert;
std::cout << "Enter your name:"<< std::endl;
getline(std::cin, name);//reads through newline
std::cout << "Enter your favorite dessert:";
getline(std::cin, dessert);
std::cout << "I have some delicious " << dessert;
std::cout << " for you, " << name << std::endl;
return 0;
}
3、
#ifndef _AFXDLL
#define _AFXDLL
#endif
#include <iostream>
#include <afx.h>
#include <string>
int main()
{
char first[100] = {0};
char last[100] = {0};
std::cout << "Enter your first name: ";
std::cin.getline(first, 100);
std::cout << "Enter your last name: ";
std::cin.getline(last, 100);
CString firstName(first);
CString lastName(last);
CString name(firstName + ", " + lastName);
std::cout << "Here's the information in a single string: ";
std::wcout << (LPCTSTR)name << std::endl;
return 0;
}
4、
#include <iostream>
#include <string>
int main()
{
std::string firstName;
std::string latstName;
std::cout << "Enter your first name: ";
getline(std::cin, firstName);
std::cout << "Enter your last name: ";
getline(std::cin, latstName);
std::cout << "Here's the information in a single string: " << firstName << ", " << latstName << std::endl;
return 0;
}
5、
#include <iostream>
#include <string>
struct CandyBar{
std::string brand;
double weight;
int calorie;
};
int main()
{
CandyBar snack = { "Mocha Munch", 2.3, 350 };
std::cout << "Here's the information in snack: " << std::endl << "brand is : " << snack.brand << std::endl
<< "weight : " << snack.weight << std::endl << "calorie : "<< snack.calorie << std::endl;
return 0;
}
6、
#include <iostream>
#include <string>
struct CandyBar{
std::string brand;
double weight;
int calorie;
};
int main()
{
CandyBar snack[3] = { {"Mocha Munch", 2.3, 350}, {"www", 3.7, 345}, {"vdf", 0.3, 250}};
for(int i =0 ; i < 3; i++)
{
std::cout << "Here's the information in snack[ " << i << "] : "<< std::endl << "brand is : " << snack[i].brand << std::endl
<< "weight : " << snack[i].weight << std::endl << "calorie : "<< snack[i].calorie << std::endl;
}
return 0;
}
7、
#include <iostream>
#include <string>
struct Pizza{
std::string company;
double diameter;
double weight;
};
int main()
{
Pizza pizza;
std::cout << "Please enter compay of this pizza: ";
getline(std::cin, pizza.company);
std::cout << "Please enter diameter of this pizza: ";
std::cin >> pizza.diameter;
std::cout << "Please enter weight of this pizza: ";
std::cin >> pizza.weight;
std::cout << "Here's the information of pizza: " << std::endl << "company is : " << pizza.company << std::endl
<< "diameter : " << pizza.diameter << std::endl << "weight : "<< pizza.weight << std::endl;
return 0;
}
8、
#include <iostream>
#include <string>
struct Pizza{
std::string company;
double diameter;
double weight;
};
int main()
{
Pizza *pizza = new Pizza;
std::cout << "Please enter diameter of this pizza: ";
std::cin >> pizza->diameter;
std::cin.get(); //如果不加std::cin.get()将无法输入公司名
std::cout << "Please enter compay of this pizza: ";
getline(std::cin, pizza->company);
std::cout << "Please enter weight of this pizza: ";
std::cin >> pizza->weight;
std::cout << "Here's the information of pizza: " << std::endl << "company is : " << pizza->company << std::endl
<< "diameter : " << pizza->diameter << std::endl << "weight : "<< pizza->weight << std::endl;
delete pizza;
pizza = NULL;
return 0;
}
9、感觉前3个for循环输入是大量的重复代码,想将其做成两层循环或者写成接口,但是貌似除了函数模板又没有更简单易行的方法,暂且搁置吧,等二刷再优化。
1 #include <iostream> 2 #include <string> 3 4 struct CandyBar{ 5 std::string brand; 6 double weight; 7 int calorie; 8 }; 9 10 int main() 11 { 12 int CandyNum = 3; 13 CandyBar * pCandyBar = new CandyBar[3]; 14 for(int i = 0;i < CandyNum;i++) 15 { 16 std::cout << "please enter the brand of CandyBar[" << i << "]" << std::endl; 17 getline(std::cin, pCandyBar[i].brand); 18 } 19 for(int i = 0;i < CandyNum;i++) 20 { 21 std::cout << "please enter the weight of CandyBar[" << i << "]" << std::endl; 22 std::cin >> pCandyBar[i].weight; 23 } 24 for(int i = 0;i < CandyNum;i++) 25 { 26 std::cout << "please enter the calorie of CandyBar[" << i << "]" << std::endl; 27 std::cin >> pCandyBar[i].calorie; 28 }
29 for(int i =0 ; i < 3; i++) 30 { 31 std::cout << "Here's the information in pCandyBar[ " << i << "] : "<< std::endl << "brand is : " << pCandyBar[i].brand << std::endl 32 << "weight : " << pCandyBar[i].weight << std::endl << "calorie : "<< pCandyBar[i].calorie << std::endl; 33 } 34 delete [] pCandyBar; 35 return 0; 36 }
10、
1 #include <iostream> 2 #include <string> 3 #include <array> 4 5 6 int main() 7 { 8 std::array<double, 3> arr; 9 double average = 0; 10 for(int i=0; i<3; i++) 11 { 12 std::cout<<"please enter the time consum of 40 yd." << std::endl; 13 std::cin >> arr[i]; 14 average += arr[i]; 15 } 16 average = average/3; 17 std::cout << "the " << arr.size() << " times averge time consum is : " << average << std::endl; 18 19 return 0; 20 }
浙公网安备 33010602011771号