c++primer plus 第4章编程练习之路

原创 2017年01月03日 20:01:27

c++ Primer Plus(习题4.1)

#include<iostream>
const int Lengh = 10;
int main()
{
	using namespace std;
	cout << "what is your frist name? ";
	char fristname[Lengh];
	cin.getline(fristname, Lengh);
	cout << "what is your last name?  ";
	char lastname[Lengh];
	cin.get(lastname, Lengh);
	cout << "what letter grade do you deserve?  ";
	char grade;
	cin >> grade;
	grade += 1;
	cout << "what is you age?  ";
	int years;                
	cin >> years;                          //要注意不能再cout那里改变字符的值,会按照编码值输出的
	cout << "Name: " << lastname << "," << fristname << endl;   
	cout << "Grade: " << grade << endl
		<< "Age: " << years<<endl;
	return 0;


}

c++ Primer Plus(习题4.2)

//修改程序清单4.4的,用string类不是数组
#include<iostream>
#include<string>
int main()
{
	std::string name, dessert;
	std::cout << "Enter you name: \n";
	std::cin>>name;
	std::cout << "Enter your favorite dessert: \n";
	std::cin >> dessert;
	std::cout << "I have some delicious " << dessert
		<< "for you," << name << ".\n";
	return 0;
}


c++ Primer Plus(习题4.3)

/*用数组拼接字符串*/
#include<iostream>
#include<cstring>
const int Lengh=10;
int main()
{
	using namespace std;
	cout << "Enter your frist name: ";
	char fristname[Lengh];
	cin >> fristname;
	cout << "Enter your last name: ";
	char lastname[Lengh];
	cin >> lastname;
	char result[Lengh * 2];
	strcpy(result, lastname);
	strcat(result, ", ");
	strcat(result, fristname);        //这里曾异想天开的用strcpy
	cout << "Here's the information in a single string :"
		<< result << endl;
	return 0;

}

c++ Primer Plus(习题4.4)

/*用string类拼接字符串*/
#include<iostream>
#include<cstring>                     //可省略,利用string类的特性
#include<string>
int main()
{
	using namespace std;
	cout << "Enter your frist name: ";
	string fristname;
	cin >> fristname;
	cout << "Enter your last name: ";
	string lastname;
	cin >> lastname;
	string result;
	result = lastname;								//string类特性,=为字符串strcpy
	result += ", ";									//,+= 为加在末尾,等同于strcat
	result += fristname;
	cout << "Here's the information in a single string :"
		<< result << endl;
	return 0;

}


c++ Primer Plus(习题4.5)

/*声明一个结构的文件,主要是学会利用结构*/
#include<iostream>
struct CandyBar
{
	char name[20];
	double weight;
	int calorie;
};
int main()
{
	using namespace std;
	CandyBar snack =                 //c++中不用带struct关键字了
	{
		"Mocha Munch",
		2.3,
		350					 //最后一个成员后面不用加逗号了,加了也没感觉!遗憾!
	};
	cout << "Name: " << snack.name << endl
		<< "Weight: " << snack.weight << endl
	<< "Calorie: " << snack.calorie<<endl;
	return 0;
}

c++ Primer Plus(习题4.6)

/*声明一个结构的文件,主要是学会利用结构*/
/*这次要用结构数组*/
#include<iostream>
struct CandyBar
{
	char name[20];
	double weight;
	int calorie;
};
int main()
{
	using namespace std;
	CandyBar snack[3] =                 //结构数组初始化的方式,要试才知道
	{
		{"Mocha Munch",2.3,350},
		{ "Aca Match",4.6,750 },
		{ "Joa Touch",1.1,320 },
	
						
	};                               //没学到循环,将就着用吧
	cout << "#1\n";                      
	cout << "Name: " << snack[0].name << endl
		<< "Weight: " << snack[0].weight << endl
		<< "Calorie: " << snack[0].calorie << endl;
	cout << "#2\n";
	cout << "Name: " << snack[1].name << endl
		<< "Weight: " << snack[1].weight << endl
		<< "Calorie: " << snack[1].calorie << endl;
	cout << "#3\n";
	cout << "Name: " << snack[2].name << endl
		<< "Weight: " << snack[2].weight << endl
		<< "Calorie: " << snack[2].calorie << endl;
	return 0;
}
c++ Primer Plus(习题4.7)

/*pizza结构,一样的味道,主要是学会利用结构*/
#include<iostream>
struct Pizza
{
	char name[20];
	double weight;
	int calorie;
};
int main()
{
	using namespace std;
	Pizza snack;               //c++中不用带struct关键字了
	cout << "Input company name: ";
	cin.get(snack.name,20);                     //这里注意不能用cin>>snack,name来获取输入行,读取单词还差不多,和c的scanf差不多,遇到空白停止读入
	cout << "Pizza weight: ";
	cin >> snack.weight;
	cout << "Input pizza calorie: ";
	cin >> snack.calorie;

	cout << "Hear is you input:\n";
	cout << "Name: " << snack.name << endl
		<< "Weight: " << snack.weight << endl
		<< "Calorie: " << snack.calorie << endl;
	return 0;
}
c++ Primer Plus(习题4.8)

/*pizza结构,一样的味道,主要是学会利用结构*/
/*用new重写版*/
#include<iostream>
struct Pizza
{
	char name[20];
	double weight;
	int calorie;
};
int main()
{
	using namespace std;
	Pizza *snack=new Pizza;               //new是深蓝色色,差评,改成这种颜色了
	cout << "Input company name: ";
	cin.get(snack->name, 20);                
	cout << "Pizza weight: ";
	cin >> snack->weight;
	cout << "Input pizza calorie: ";
	cin >> snack->calorie;

	cout << "Hear is you input:\n";
	cout << "Name: " << snack->name << endl
		<< "Weight: " << snack->weight << endl
		<< "Calorie: " << snack->calorie << endl;
	delete(snack);
	return 0;
}

c++ Primer Plus(习题4.9)

/*声明一个结构的文件,主要是学会利用结构*/
/*这次要用new来构建数组结构*/
#include<iostream>
struct CandyBar
{
	char name[20];
	double weight;
	int calorie;
};
int main()
{
	using namespace std;               //用new感觉无法初始化结构数组啊
	CandyBar *snack1 = new CandyBar;
	*snack1 = {"Mocha Munch",2.3,350};        //只能这样玩了,刚才犯了错误,用了snck1={"Mocha Munch",2.3,350}
	CandyBar *snack2 = new CandyBar;          //没理清楚指针的含义
	*snack2 = { "Aca Match", 4.6, 750 };
	CandyBar *snack3 = new CandyBar;
	*snack3 = {"Joa Touch",1.1,320};

                             //没学到循环,将就着用吧
	cout << "#1\n";
	cout << "Name: " << snack1->name << endl
		<< "Weight: " << snack1->weight << endl
		<< "Calorie: " << snack1->calorie << endl;
	delete(snack1);
	cout << "#2\n";
	cout << "Name: " << snack2->name << endl
		<< "Weight: " << snack2->weight << endl
		<< "Calorie: " << snack2->calorie << endl;
	delete(snack2);
	cout << "#3\n";
	cout << "Name: " << snack3->name << endl
		<< "Weight: " << snack3->weight << endl
		<< "Calorie: " << snack3->calorie << endl;
	delete(snack3);
	return 0;
}

c++ Primer Plus(习题4.10)

/*提示用户输入3次100米跑成绩,输出次数和平均成绩,当然用户输入也要算进去*/
/*分别用了vector模板类和array模板类*/
#include<iostream>
#include<array>
#include<vector>
int main()
{
	using namespace std;
	double vaverage = 0,aaverage=0;
	array<double, 3> agrade;          //array模板类
	vector<double> vgrade(3);        //和数组一样的用法
	cout << "Input your 1st 100m grade: ";
	cin >> vgrade[0];
	vaverage += vgrade[0];                //处理平均值你懂
	cout << "Input your 2st 100m grade: ";
	cin >> vgrade[1];
	vaverage += vgrade[1];
	cout << "Input your 3st 100m grade: ";
	cin >> vgrade[2];
	vaverage += vgrade[2];
	vaverage/=3;
	cout << "Using vector stream:\n";
	cout << "1st grade:" << vgrade[0] << endl
		<< "2st grade:" << vgrade[1] << endl
		<< "2st grade:" << vgrade[2] << endl
		<< "Average grade:" << vaverage << endl;
	agrade[0] = vgrade[0];
	agrade[1] = vgrade[1];
	agrade[2] = vgrade[2];
	aaverage = vaverage;
	cout << "Using array stream:\n";
	cout << "1st grade:" << agrade[0] << endl
		<< "2st grade:" << agrade[1] << endl
		<< "2st grade:" << agrade[2] << endl
		<< "Average grade:" << aaverage << endl;
	cout << "Good Bye!";
	return 0;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

c++ Primer Plus(第六版)第十一章,编程练习之路

c++ Primer Plus(第六版)第十一章,个人学习记录,成长中
  • Robot_x
  • Robot_x
  • 2017年01月14日 12:55
  • 163

c++primer plus 第六版 第2章 编程练习之路

//c++ Primer Plus(习题2.2)//input 以 long为单位的距离,将它转换成码.(1 long =220码) #include const float Yard = 220...
  • Robot_x
  • Robot_x
  • 2017年01月03日 19:54
  • 122

【C++】学习笔记十四——C++ primer plus第六版第4章编程练习

1. //4.13编程练习 1 #include #include int main() { using namespace std; enum grade {A, B, C, D}...
  • budf01
  • budf01
  • 2016年12月02日 21:15
  • 4565

c++ Primer Plus(第六版)第八章习题,写代码之路

c++ Primer Plus(习题8.1) /*题意看了好久才明白意思,即不依赖形参n,来输出字符串*/ /*个人见解,无*/ #include const int TIMES = 10; int...
  • Robot_x
  • Robot_x
  • 2017年01月20日 11:56
  • 120

c++ Primer Plus(第六版)第十四章习题,写代码之路

c++ Primer Plus(习题14.1) //书上的测试文件,相当于客户的使用说明书 //对于书上那个Pair对象,不认真的人都怀疑是否学过 //竟然是一个模板,这道题目想了很久很久,还是没思...
  • Robot_x
  • Robot_x
  • 2017年02月08日 19:57
  • 163

c++ Primer Plus(第六版)第十二章习题,写代码之路

c++ Primer Plus(习题12.1) //12.1头文件 #pragma once #ifndef COW_H #define COW_H #include #include class ...
  • Robot_x
  • Robot_x
  • 2017年01月20日 13:01
  • 327

c++ Primer Plus(第六版)第十章习题,写代码之路

c++ Primer Plus(第六版)第十章习题,写代码之路
  • Robot_x
  • Robot_x
  • 2017年01月20日 12:05
  • 463

c++ Primer Plus(第六版)第十八章习题,写代码之路

c++ Primer Plus(习题18.1) //与c类似的初始化列表功能 //简单的函数题目,这本书的最后一张其实可以不用做了 //不太好做 #include #include //C++1...
  • Robot_x
  • Robot_x
  • 2017年02月08日 20:29
  • 99

c++ Primer Plus(第六版)第十六章习题,写代码之路

c++ Primer Plus(习题16.1) //让用户输入回文的单词,就是那种倒着读和顺着读一样意思的 //实现这个功能有很多方法,用了一种简单的方法 #include #include usi...
  • Robot_x
  • Robot_x
  • 2017年02月08日 20:19
  • 207

c++ Primer Plus(第六版)第七章习题,写代码之路

c++ Primer Plus(习题7.1) //用户输入啷个数,返回调和平均数 //其中一个为0结束程序 #include using namespace std; double t_averag...
  • Robot_x
  • Robot_x
  • 2017年01月20日 11:53
  • 239
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:c++primer plus 第4章编程练习之路
举报原因:
原因补充:

(最多只允许输入30个字)