C++ primer plus 第六版编程练习答案

第四章

1、

#include <iostream> 
#include <string>
using namespace std;
int main()
{
	string f_name,l_name,grade;
	int age;
	cout<<"What is your first name?         \b\b\b\b\b\b";
	cin>>f_name;
	cout<<"What is your last name?         \b\b\b\b\b\b";
	cin>> l_name;
	cout<<"What letter grade do you deserve?    \b\b\b";
	cin>>grade;
	cout<<"What is your age?   \b\b\b";
	cin>>age;
	cout<<"Name: "<<f_name<<" "<<l_name<<endl;
	cout<<"Grade: "<<grade<<endl;
	cout<<"Age: "<<age<<endl;
	return 0;
	
}

2、

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string  name,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;
}

3、

#include <iostream>
#include <cstring> 
using namespace std;
int main()
{
	char f_name[20];
	char l_name[20];
	char interrupt[20] = ",";
	char name[40];
	cout << "Enter your first name:      \b\b\b";
	cin >> f_name;
	cin.get();
	cout << "Enter your last name:    \b\b\b";
	cin >> l_name;
	strcat(f_name, interrupt);
	strcat(f_name, l_name);
	cout << "Here 's the information in a  single string:" << f_name << endl;
	return 0;
}

 这里在Vs中运行会出现报错:E4996.

解决方法如下

 

改为否即可。 

 4、

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string f_name;
    string l_name;
    string name;
    cout << "Enter your first name: ";
    getline(cin, f_name);
    cout << "Enter your last name: ";
    getline(cin, l_name);
    name = l_name + ", " + f_name;
    cout << "Here's the information in a single string: " << name << endl;
    return 0;
}

5、

#include <iostream>
#include <string>
using namespace std;
struct CandyBar
{
	string name;
	double weight;
	int calorie;
};
int main()
{
	CandyBar snack =
	{
		"Mocha Munch",
		2.3,
		350
	};
	cout <<"The brand of the sugar is " <<snack.name  << endl;
	cout << "The weight of the sugar is " << snack.weight  << endl;
	cout << "The Calorie content of the sugar is " << snack.calorie  << endl;
	return 0;
}

6、 

#include <iostream>
#include <string>
using namespace std;
struct CandyBar
{
	string name;
	double weight;
	int calorie;
};
int main()
{
	CandyBar snack[3] =
	{
		{"Mocha Munch",2.3,350},
		{"Wowo",2.6,350},
		{"Big rabit",2.5,350}
	};
	for (int i = 0; i < 3; i++)
	{
		std::cout << "The brand of the sugar is " << snack[i].name << endl;
		std::cout << "The weight of the sugar is " << snack[i].weight  << endl;
		std::cout << "The Calorie content of the sugar is " << snack[i] .calorie << endl;
	}
	return 0;
}

7、

#include <iostream>
#include <string>
using namespace std;
struct pizza {
    string brand;
    double diamer;  
    double weight;
};

int main()
{
    pizza p;
    cout << "请输入比萨饼的公司名:";
    cin >> p.brand;
    cout << "请输入比萨饼的直径:";
    cin >> p.diamer;
    cout << "请输入比萨饼的重量:";
    cin >> p.weight;
    cout << "您的比萨饼品牌:" << p.brand << endl;
    cout << "您的比萨饼直径:" << p.diamer << endl;
    cout << "您的比萨饼重量:" << p.weight << endl;
    return 0;
}

8、

#include <iostream>
#include <string>
using namespace std;
struct pizza {
    string brand;
    double diamer;  
    double weight;
};

int main()
{
    pizza* p = new pizza;
    cout << "请输入比萨饼的公司名:";
    cin >>(*p).brand;
    cout << "请输入比萨饼的直径:";
    cin >> (*p).diamer;
    cout << "请输入比萨饼的重量:";
    cin >> (*p).weight;
    cout << "您的比萨饼品牌:" << (*p).brand << endl;
    cout << "您的比萨饼直径:" << (*p).diamer << endl;
    cout << "您的比萨饼重量:" << (*p).weight << endl;
    delete p;
    return 0;
}

9、

#include <iostream>
#include <string>
using namespace std;
struct CandyBar
{
	string name;
	double weight=0;
	int calorie=0;
};
int main()
{
	CandyBar* p = new CandyBar[3];
	p[0] = { "Moncha Munch",2.3,350 };
	p[1] = { "WoWo",2.6,350 };
	p[2] = { "Big rabit",6.3,350 };
	for (int i = 0; i < 3; i++)
	{
		std::cout << "The brand of the sugar is " << p[i].name << endl;
		std::cout << "The weight of the sugar is " << p[i].weight << endl;
		std::cout << "The Calorie content of the sugar is " << p[i].calorie << endl;
	}
	return 0;
	delete[] p;
}

 10、

#include <iostream>
#include <array>
using namespace std;
int main()
{
	array <double, 3> grade;
	cout << "请输入第一次40米跑成绩:" << endl;
	cin >> grade[0];
	cout << "请输入第一次40米跑成绩:" << endl;
	cin >> grade[1];
	cout << "请输入第一次40米跑成绩:" << endl;
	cin >> grade[2];
	cout << "进行了" << sizeof(grade) / sizeof(double) << "次" << "平均成绩为:" << (grade[0] + grade[1] + grade[2]) / (sizeof(grade) / sizeof(double)) << endl;
	return 0;
}

第五章:

1、

#include <iostream>
using namespace std;
int main()
{
	int n,m;
	cout<<"Please input an integer:"<<endl;
	cin>>n;
	cout<<"Please input the large integer:"<<endl;
	cin>>m;
	int num=0;
	for(;n<=m;n++)
	{
		num+=n;
	}
	cout<<num;
	return 0;
}

2、

#include <iostream>
#include <array>
using namespace std;
const int ArSize = 100;
int main()
{
    array <long double, ArSize> factorials;
    factorials[1] = factorials[0] = 1;
    for (int i = 2; i < ArSize; i++)
        factorials[i] = i * factorials[i-1];
    for (int i = 0; i < ArSize; i++)
        cout << i << "! = " << factorials[i] << endl;
    return 0;
}

3、

#include <iostream> 
using namespace std;
int main()
{
	int n,sum=0;
	cout<<"Please input number:"<<endl;
	while((cin>>n)!=0)
	{
		sum+=n;
		cout<<sum<<endl;
	}
	return 0;
}

 4、

#include <iostream>
using namespace std;
int main()
{
	int daphne=100,n=0;
	double cleo=100;
	while (daphne >= cleo)
	{
		daphne +=10;
		cleo *= 1.05;
		n++;
	}
	cout << n << endl;
	cout << daphne << "\n" << cleo << endl;
	return 0;
	

}

5、

#include <iostream>
using namespace std;
int main()
{
	int numbers[12];
	int sum=0;
	const char* month[12] =
	{
		"January",
		"February",
		"March",
		"April",
		"May",
		"June",
		"July",
		"August",
		"September",
		"October",
		"November",
		"December"
	};
	for (int i = 0; i < 12; i++)
	{
		cout << "Please input the sales volume in " << month[i] <<":" << endl;
		cin >> numbers[i];
		cout << "The sales volume in " << month[i] << " is " << numbers[i] << endl;
		cout << "************************************************************" << endl;
	}
	for (int i = 0; i < 12; i++)
	{
		sum += numbers[i];
	}
	cout << "The whole sales volume is " << sum << endl;
	return 0;
}

6、

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string month[12] =
    {"一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"};
    int num_books[3][12];
    int sum[4] = {0};
    for (int i = 0; i < 3; i++) 
    {
        for (int j = 0; j < 12; j++) 
        {
            cout << "请输入第" << i+1 << "年的" << month[j] << "月的销售量:";
            cin >> num_books[i][j];
            sum[i] += num_books[i][j];
        }
        sum[3] += sum[i];
    }
    for (int i = 0; i < 3; i++) {
        cout << "第" << i+1 << "年的销量为:" << sum[i] << endl;
    }
    cout << "三年总销量为:" << sum[3] << endl;
    return 0;
}

7、

#include <iostream>
#include <string>
using namespace std;
int main()
{
	int n;
	struct car 
	{
		string name;
		int year;
	};
	cout << "How many cars do you wish to catalog?" << endl;
	cin >> n;
	car *p = new car[n];
	for (int i = 0; i < n; i++)
	{
		cout << "Car #" << i + 1 << endl;
		cout << "Please enter the make:         \b\b\b\b\b";
		cin >> p[i].name;
		cout << "Please enter the year made:       \b\b\b\b\b";
		cin >> p[i].year;
	}
	cout << "Here is your collection:" << endl;
	for (int i = 0; i < n; i++)
	{
		cout << p[i].year << "  " << p[i].name << endl;
	}
	return 0;
}

8、

#include <iostream>
#include <string>
using namespace std;
int main()
{
	int n=0;
	cout << "Enter words (to stop,typr the word done):" << endl;
	char word[20];
	cin >> word;
	while (strcmp(word, "done"))
	{
		n++;
		cin >> word;
	}
	cout << "You entered a total of " << n << " words." << endl;
	return 0;
}

9、

#include <iostream>
#include <string>
using namespace std;
int main()
{
	int n=0;
	cout << "Enter words (to stop,typr the word done):" << endl;
	string word;
	cin >> word;
	while (word!="done")
	{
		n++;
		cin >> word;
	}
	cout << "You entered a total of " << n << " words." << endl;
	return 0;
}

10.

#include <iostream>
using namespace std;
int main()
{
	int n;
	cout << "Please enter number of rows:     \b\b\b";
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			if (j < n - 1 - i)
			{
				cout << ".";
			}
			else
				cout << "*";
		}
		cout << endl;
	}
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值