关闭

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

标签: Primer Plusc++练习题记录
70人阅读 评论(1) 收藏 举报
分类:

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



1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2793次
    • 积分:199
    • 等级:
    • 排名:千里之外
    • 原创:18篇
    • 转载:0篇
    • 译文:0篇
    • 评论:1条
    文章分类
    文章存档