摘要:本文主要向大家介绍了C/C++知识点之C++Primer Plus 编程练习,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。
本文主要向大家介绍了C/C++知识点之C++Primer Plus 编程练习,通过具体的内容向大家展示,希望对大家学习C/C++知识点有所帮助。
#pragma once
#include "stdafx.h"
#ifndef CHAPTER5_H_
#define CHAPTER5_H_
#include <iostream>
#include <string>
#include <array>
using namespace std;
//1.
class Exercise1
{
private:
int num1_;
int num2_;
public:
int result(int num1, int num2);
};
//2.
class Exercise2
{
private:
public:
void factorial();
};
//3.
class Exercise3
{
private:
float result_ =0;
public:
void count();
};
//4.
struct client
{
string name;
float assets;
float investment;
float value;
};
class Exercise4
{
private:
float percent1_ = 0.1f;
float percent2_ = 0.05f;
public:
void investmentA(client *c);
void investmentB(client *c);
};
//5.
class Exercise5
{
private:
int amount_ = 0;
int salee[12] = {};
public:
void setSale(int month,int sale);
void setNumber(int num);
int getNumber();
};
//6.
class Exercise6
{
private:
unsigned int amount_ = 0;
int storage[3][12];
public:
void setStorage(int year, int month, int sale);
int getStorage(int year ,int month);
int getAmount();
};
//7.
struct car
{
string brand;
int year;
};
class Exercise7
{
private:
public:
};
//8.
//9.
//10.
class Exercise10
{
private:
public:
void showSymbol(int rows);
};
#endif // !CHAPTER5_H_
复制代码
复制代码
#include "stdafx.h"
#include "chapter5.h"
#include <math.h>
#include <iostream>
//1.
int Exercise1::result(int num1, int num2)
{
num1_ = num1;
num2_ = num2;
int res;
res = num1;
while (num1 < num2)
{
++num1;
res += num1;
}
return res;
}
//2.
void Exercise2::factorial()
{
const int size = 100;
array<long double, size + 1> arr = {}; //定义数组的长度后,该数组的最后一位应该是从arr[0]至arr[100]
arr[1] = arr[0] = 1;
for (int i = 2; i < size+1; ++i)
{
arr[i] = i * arr[i - 1];
}
cout << "100! = " << arr[size] << endl;
}
//3.
void Exercise3::count()
{
float temp;
cout << "请输入一个数:" << endl;
cin >> temp;
while (temp != 0)
{
result_ += temp;
cout << "总数为:" << result_ << endl;
cout << "请输入一个数:" << endl;
cin >> temp;
}
}
//4.
void Exercise4::investmentA(client *c) //单利
{
c->value +=((c->investment)*percent1_);
c->assets += c->value;
}
void Exercise4::investmentB(client *c) //复利
{
c->value = (((c->investment)+(c->value))*(1+percent2_))-(c->investment);
c->assets += c->value;
}
void Exercise5::setSale(int month,int sale)
{
salee[month] = sale;
}
//5.
void Exercise5::setNumber(int num)
{
amount_ += num;
}
int Exercise5::getNumber()
{
return amount_;
}
//6.
void Exercise6::setStorage(int year, int month ,int sale)
{
storage[year][month] = sale;
amount_ += sale;
}
int Exercise6::getStorage(int year, int month)
{
return storage[year][month];
}
int Exercise6::getAmount()
{
return amount_;
}
//7.
//8.
//9.
//10.
void Exercise10::showSymbol( int rows)
{
for (int i = 1; i < rows + 1; i++)
{
cout << "\n";
for (int j = rows; j > i; j--)
{
cout << ".";
}
for (int k = 1; k <= i; k++)
{
cout << "*";
}
}
}
复制代码
复制代码
// C++Primer Plus 习题_第五章.cpp: 定义控制台应用程序的入口点。
//-------------------------------复习题-----------------------------------------
//1.入口条件循环和出口条件循环之间的区别是什么?各种C++循环分别属于其中哪一种?
//答案:输入条件循环在进入输入循环体之前将评估测试表达式。如果条件最初为false,则循环不会执行
//其循环体。退出条件循环在处理循环体之后评估测试表达式。因此,即使测试表达式最初为false,循环也
//将执行一次。for和while循环都是输入条件循环,而do while循环是退出条件循环。
//2.如果下面的代码片段是有效程序的组成部分,它将打印什么内容?
// int i;
// for(int i = 0; i<5; i++)
// cout << i;
// cout << endl;
//答案:01234
//3.如果下面的代码片段是有效程序的组成部分,它将打印什么内容?
// int j;
// for (j=0; j < 11; j += 3)
// cout << j;
// cout << endl << j << endl;
//答案:0369
//4.如果下面的代码片段是有效程序的组成部分,它将打印什么内容?
// int j = 5;
// while( ++j < 9)
// cout << j++ <<endl;
//答案:68
//5.如果下面的代码片段是有效的组成部分,它将打印什么内容?
// int k = 8;
// do
// cout <<" k = " << k << endl;
// while (k++ < 5);
//答案:k = 8
//6.编写一个打印1、2、4、8、16、32、64 的for循环,每轮循环都将计数变量的值乘以2。
//答案:
// for (int i =1; i <= 64; i *= 2)
// {
// cout << i;
// }
//7.如何在循环中包括多条语句?
//答案:将语句放在一对大括号中将形成一个符合语句或代码块。
//8.下面的语句是否有效?如果无效,原因是什么?如果有效,它将完成什么工作?
// int x = (1,024);
//下面的语句又如何呢?
// int y;
// y = 1,024;
//答案:都有效。第一条语句,表达式1,024由两个表达式组成——1和024,用逗号运算符链接。
//值为右侧表达式的值。这是024,八进制为20,因此该表达式将20赋值给x。
//第二条语句,运算符优先级将导致它被判定成这样:
// (y = 1),024;
//也就是说,左侧表达式将y设置成1,整个表达式的值(没有使用)为024或20(八进制)。
//9.在查看输入方面,cin>>ch同cin.get(ch)和ch=cin.get()有什么不同?
//答案:cin >> ch 将跳过空格、换行符和制表符,其他两种格式将读取这些字符。
//-------------------------------编程练习-----------------------------------------
//1.编写一个要求用户输入两个整数的程序。该程序计算并输出这两个整数之间(包括这两个整数)所有数的和。
//这里假设先输入较小的整数。例如,如果用户输入的是2和9,则程序将指出2~9之间所有整数的和为44.
//2.使用array对象(而不是数组)和long double(而不是 long long)重新编写程序清单5.4,并计算100!的值。
//3.编写一个要求用户输入数字的程序。每次输入后,程序都将报告到目前为止,所有输入的累积和。当用户输入0时,
//程序结束。
//4.Daphne以10%的单利投资了100美元。也就是说。每一年的利润是投资额的10%,即每年10美元,而Cleo以5%的复利投资
//了100美元。也就是说利息是当前存款(包括获得的利息)的5%,Cleo在第一年投资100美元的盈利5%——得到了105美元。
//下一年的盈利是105美元的5%——即5.12美元,以此类推。请编写一个程序,计算多少年后,Cleo的投资值才能超过Daphne
//的投资价值,并显示此时两个人的投资价值。
//5.假设要销售《C++ For Fools》一书,请编写一个程序,输入全年中每个月的销售量(图书数量,而不是销售额)。程序
//通过循环,使用初始化为月份字符串的char*数组(或sring对象数组)逐月进行提示,并将输入的数据存储在一个int数组中。
//然后,程序计算数组中各元素的总数,并报告这一年的销售情况。
//6.完成编程练习5,但这一次使用一个二维数组来存储输入——3年中每个月的销售量。程序将报告每年销售量以及三年的总销售量。
//7.设计一个名为car的机构,用它存储下述有关汽车的信息:生产商(存储字符数组或string对象中的字符串)、生产年份(整数)。
//编写一个程序,向用户询问有多少量汽车。随后,程序使用new来创建一个由相应数量的car结构组成的动态数组。接下来,程序提示
//用户输入每辆车商(可能由多个单词组成)和年份信息。请注意,这需要特别小心,因为它将交替读取数值和字符串(参见第4章)。
//最后,程序将显示每个结构的内容。该程序的运行情况如下:
// How many cars do you wish to catalog? 2
// Car #1:
// Please enter the make: Hudson Hornet
// Please enter the year made:1952
// Car #2:
// Please enter the make: Kaiser
// Please enter the year made:1951
// Here is your collection:
// 1952 Hudson Hornet
// 1951 Kaiser
//8.编写一个程序,它使用一个char数组和循环来每次读取一个单词,直到用户输入done为止。随后,该程序指出用户输入了多少个单词
//(不包括done在内)。下面是该程序的运行情况:
// Enter words (to stop, type the word done):
// anteater birthday category dumpster
// envy finagle geometry done foe sure
// You entered a total of 7 words.
// 您应在程序中包含头文件cstring,并使用函数strcmp()来进行比较测试。
//9.编写一个满足前一个练习中描述的程序,但使用string对象而不是字符数组。请在程序中包含头文件string,并使用关系运算符来进行
//比较测试。
//10.编写一个使用嵌套循环的程序,要求用户输入一个值,指出要显示多少行。然后,程序将显示相应行数的星号,其中第一行包括一个星号,
//第二行包括两个星号,以此类推。每一行包含的字符数等于用户指定的行数,在星号不够的情况下,在星号前面加上句号。该程序的运行情况如下:
// Enter number of rows: 5
// ....*
// ...**
// ..***
// .****
// *****
#include "stdafx.h"
#include "chapter5.h"
int main()
{
////1.
//cout << "《第一题》" << endl;
//int num1, num2;
//Exercise1 ex1;
//cout << "请输入第一个数:";
//cin >> num1;
//cout << "请输入第二个数:";
//cin >> num2;
//cout << "两个数之间的所有整数(包括两个整数)为:" << ex1.result(num1, num2) << endl;
////2.
//cout << "《第二题》" << endl;
//Exercise2 ex2;
//ex2.factorial();
////3.
//cout << "《第三题》" << endl;
//Exercise3 ex3;
//ex3.count();
////4.
//cout << "《第四题》" << endl;
//Exercise4 ex4;
//const int size = 2;
//int year = 0;
//client c[size] = {};
//cout << "创建" << size << "个客户:" << endl;
//for (int i = 0; i < size; i++)
//{
// string name;
// float assets;
// float investment;
// float value =0;
// cout << "客户" << i + 1 << "_姓名:" << endl;
// cin >> name;
// cout << "客户" << i + 1 << "_资产:" << endl;
// cin >> assets;
// cout << "客户" << i + 1 << "_投资额度:" << endl;
// cin >> investment;
// c[i] = { name ,assets ,investment,value };
//}
//cout << "投资价值分析:" << endl;
//ex4.investmentA(&(c[0]));
//ex4.investmentB(&(c[1]));
//while ( c[0].value >= c[1].value )
//{
// ex4.investmentA(&(c[0]));
// ex4.investmentB(&(c[1]));
// ++year;
//}
//cout << year << "年后客户2的投资价值超过客户1" << endl;
//cout << "客户1的投资价值为:" << c[0].value;
//cout << "客户2的投资价值为:" << c[1].value;
////5.
//cout << "《第五题》" << endl;
//string monthh[12]{ "1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月" };
//Exercise5 ex5;
//cout << "请输入全年中每月的销售量:" << endl;
//for (int i = 0; i < 12; i++)
//{
// int sale;
// cout << monthh[i] + " :";
// cin >> sale;
// ex5.setSale(i, sale);
// ex5.setNumber(sale);
//}
//cout << "一年的销售总量为:" << ex5.getNumber();
////6.
//cout << "《第六题》" << endl;
//Exercise6 ex6;
//for (int i = 0; i < 3; i++)
//{
// for (int j = 0; j < 12; j ++)
// {
// int sale;
// cout << "请输入第" << i + 1 << "年" << j + 1 << "月的销售量: ";
// cin >> sale;
// ex6.setStorage(i, j, sale);
// }
//}
//cout << "报告每年的销售量:" << endl;
//for (int i = 0; i < 3; i++)
//{
// int saleYear =0;
// for (int j = 0; j < 12; j++)
// {
// saleYear += ex6.getStorage(i, j);
// }
// cout << "第" << i + 1 << "年的销售量为: " << saleYear << endl;
//}
//cout << "总销售量为:" << ex6.getAmount();
////7.
//cout << "《第七题》" << endl;
//Exercise7 ex7;
//int num3 = 0;
//cout << "希望收藏多少量车?";
//cin >> num3;
//car *pCar = new car[num3]; // 动态数组。
//for (int i = 0; i < num3; i++)
//{
// string brand;
// int year;
// cout << "请输入品牌"<<i+1<<":";
// cin >> pCar[i].brand;
// cout << "请输入年份" <<i+1<<":";
// cin >> pCar[i].year;
//}
//cout << "这里是你收藏的车辆:" << endl;
//for (int i = 0; i < num3; i++)
//{
// cout << pCar[i].year << " " << pCar[i].brand <<endl;
//}
//delete [] pCar;
//
////8.
//cout << "《第八题》" << endl;
//int num4 = 0;
//cout << "请输入单词:";
//char words1[20];
//while (cin >> words1 && strcmp(words1, "done")) //cin >> 忽略空格 换行符 回车
//{
// num4++;
//}
//cout << "你一共输入了" << num4 << "次单词。";
////9.
//cout << "《第九题》" << endl;
//int num5 = 0;
//cout << "请输入单词:";
//string words2;
//while ((cin >> words2) && (words2 != "done")) //cin >> 忽略空格 换行符 回车
//{
// num5++;
//}
//cout << "你一共输入了" << num5 << "次单词。";
//10.
cout << "《第十题》" << endl;
cout << "请输入行:";
int rows;
cin >> rows;
Exercise10 ex10;
ex10.showSymbol(rows);
return 0;
}
本文由职坐标整理并发布,了解更多内容,请关注职坐标编程语言C/C+频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号