c++
作业0章节
0-0编译 并运行Hello,world!程序。
#include
int main()
{
std::cout << “Holle,worde” << std::endl;
return 0;
}
0-1下面的表达式是做什么的?
3+4
求 整数3+4的和
0-2编写一个程序,使它在运行是输出:
This (")is a quote, and this() is a backlash.
#include
int main()
{
std::cout << “This (“) is a quote, and this () is a backlash.”
<< std::endl;
return 0;
}
#include
int main()
{
std::cout << “Holle\tworde” << std::endl;
return 0;
}
#include <stdio.h>
#include <windows.h>
int main()
{
printf(“hello world!”);
system("pause");
return 0;
}
无效!!!没有花括号 还缺少返回值
程序 有效 花括号把出现在他们之间的所有内容当作一个单元来处理。左花括号是函数执行的开始,右花括号是结束。花括号表明他们之间的所有语句属于同一个函数。
程序错误
#include
int main()
{
//这是一个注释,它占据了几行的范围
//在这里,我们使用了//而不是/*
//和*/来为注释定界
std::cout << “Does this work?” << std::endl;
return 0;
}
int main() {}
函数 花括号
#include
#include<conio.h>
int main()
{
std::cout
<<
“hello, world!”
<<
std::endl;
return 0;
}
1-0 编译、允许并测试本章中的程序。
#include
#include
int main()
{
std::cout << “Please enter your first name ;”;
std::string name;
std::cin >> name;
std::cout << "Hello," << name << "!" << std::endl;
return 0;
}
1-1 以下的定义有效吗?理由是什么?
const std::string hello = “Hello”;
const std::string message = hello + “, world” + “!”;
1-2 以下的定义有效吗?理由是什么?
const std::string exclam = “!”;
const std::string message = “Hello” + “, world” + exclam;
1-3 以下的定义有效吗?如果有效的话,它是做什么的?如果无效,为什么呢?
#include
#include
int main()
{
{const std::string s = “a string”;
std::cout << s << std::endl;}
{const std::string s = "another string";
std::cout << s << std::endl;}
return 0;
}
1-4 下面的这个程序又怎样呢?如果我们把倒数第三行的 }} 改成 };} 的话,会出现什么情况呢?
#include
#include
int main()
{
{const std::string s = “a string”;
std::cout << s << std::endl;
{const std::string s = "another string";
//std::cout << s << std::endl;}}
std::cout << s << std::endl;} ;}
return 0;
}
#include
#include
int main()
{
{const std::string s = “a string”;
std::cout << s << std::endl;
{const std::string s = "another string";
//std::cout << s << std::endl;};}
std::cout << s << std::endl; }; }
return 0;
}
1-5 下面这个程序呢?如果有效,它是做什么的?如果无效,说出理由,然后把它改写成有效的程序。
#include
#include
int main()
{
{std::string s = “a string”;
{std::string x = s+ “, really”;
std::cout << s << std::endl;}
std::cout << x << std::endl; // x is out of its scope here
}
return 0;
}
#include
#include
int main()
{
std::string s = “a string”;
std::string x = s + “, really”;
std::cout << s << std::endl;
std::cout << x << std::endl; // x is out of its scope here;
return 0;
}
1-6 在下面的程序向你发出输入请求的时候,如果你输入了两个名字(例如,Samuel Beckett),它会怎么样处理呢?在运行程序之前先预测一下结果,然后上机测一下。
#include
#include
int main()
{
std::cout << "What is your name? ";
std::string name;
std::cin >> name;
std::cout << "Hello, " << name
<< std::endl << "And what is yours? ";
std::cin >> name;
std::cout << "Hello, " << name
<< “;nice to meet you roo!” << std::endl;
return 0;
#include
#include
int main()
{
std::cout << "Please enter your first name: ";
std::string name;
std::cin >> name;
const std::string greeting = “Hello,” + name + “!”;
return 0;
}
2-0 编译并运行我们在本章中介绍的程序。
#include
#include
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
cout <<“please enter your first name:”;
string name;
cin >> name;
const string greeting ="hello," + name +"!";
const int pad = 1;
const int rows = pad*2 + 3;
const string::size_type cols = greeting.size() + pad*2 + 2;
cout << endl;
for (int r = 0; r !=rows; r++) {
string::size_type c = 0;
while (c != cols) {
if (r == pad+1 && c == pad+1) {
cout << greeting;
c += greeting.size();
}else {
if (r == 0 || r == rows-1 || c == 0 || c == cols-1)
cout<< "*";
else
cout<< " ";
++c;
}
}
cout << endl;
}
return 0;
}
2-1 改写框架程序,输出跟框架没有间隔的问候语。
const int pad = 1; 改为 const int pad = 0;
#include
#include
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
cout << “please enter your first name:”;
string name;
cin >> name;
const string greeting = "hello," + name + "!";
const int pad = 0;
const int rows = pad * 2 + 3;
const string::size_type cols = greeting.size() + pad * 2 + 2;
cout << endl;
for (int r = 0; r != rows; r++) {
string::size_type c = 0;
while (c != cols) {
if (r == pad + 1 && c == pad + 1) {
cout << greeting;
c += greeting.size();
}
else {
if (r == 0 || r == rows - 1 || c == 0 || c == cols - 1)
cout << "*";
else
cout << " ";
++c;
}
}
cout << endl;
}
return 0;
}
2-2 在我们的框架程序中,我们使用了一定数目的空格来把问候语和顶部以及底部边界分隔开来。现在,重新编写这个程序,在重写的程序中使用数量跟源程序不同的空格来把各边界和问候语分隔开。
const int pad = 1;
替换成
cout <<“please enter the pad:”;
int pad;
cin >> pad;
#include
#include
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
cout << “please enter your first name:”;
string name;
cin >> name;
const string greeting = "hello," + name + "!";
cout << "please enter the pad:";
int pad;
cin >> pad;
const int rows = pad * 2 + 3;
const string::size_type cols = greeting.size() + pad * 2 + 2;
cout << endl;
for (int r = 0; r != rows; r++) {
string::size_type c = 0;
while (c != cols) {
if (r == pad + 1 && c == pad + 1) {
cout << greeting;
c += greeting.size();
}
else {
if (r == 0 || r == rows - 1 || c == 0 || c == cols - 1)
cout << "*";
else
cout << " ";
++c;
}
}
cout << endl;
}
return 0;
2-4 在框架程序中的空白行是用来把边界和问候语分隔开的,程序每次一个字符地输出了大部分的空白行。改写这个程序,让它在单独的一条输出表达式中输出所有的空白行。
#include
#include
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
cout <<“please enter your first name:”;
string name;
cin >> name;
const string greeting ="hello," + name +"!";
const int pad = 1;
const int rows = pad*2 + 3;
const string::size_type cols = greeting.size() + pad*2 + 2;
const string spaces = string(greeting.size() + pad * 2, ' ');
cout << endl;
for (int r = 0; r !=rows; r++) {
string::size_type c = 0;
while (c != cols) {
if (r == pad+1 && c == pad+1) {
cout << greeting;
c += greeting.size();
}else {
if (r == 0 || r == rows-1 || c == 0 || c == cols-1)
{
cout<< "*";
++c;
}
else if (r == pad + 1) {
cout<< " ";
++c;
}else {
cout<< spaces;
c +=spaces.size();
}
}
}
cout << endl;
}
return 0;
}
2-5 编写一个程序,让它输出一系列的“*”字符,程序输出的这些字符将构成一个正方形,一个长方形和一个三角形
#include
#include
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
// this constant is the height of each figure
const int height = 5;
// these variables are used to track the row and
// column count while drawing each figure
int row;
int col;
// draw square
row = 0;
col = 0;
// draw top line
while (col < height) {
cout <<’*’;
++col;
}
cout << endl;
++row;
// draw middle lines
while (row < height - 1) {
col = 0;
while (col < height) {
++col;
if (col == 1)
cout <<’’;
else {
if (col == height)
cout<<’’;
else
cout<<’ ';
}
}
cout <<endl;
++row;
}
// draw bottom line
col = 0;
while (col < height) {
cout <<’*’;
++col;
}
// new line after figure
cout << endl;
// blank line between figures
cout << endl;
// draw rectangle
row = 0;
col = 0;
// draw top line
while (col < height * 2) {
cout <<’*’;
++col;
}
cout << endl;
++row;
// draw middle lines
while (row < height - 1) {
col = 0;
while (col <= height * 2) {
++col;
if (col == 1)
cout<<’’;
else {
if (col == height * 2)
cout<<’’;
else
cout<<’ ';
}
}
cout <<endl;
++row;
}
// draw bottom line
col = 0;
while (col < height * 2) {
cout <<’*’;
++col;
}
// new line after figure
cout << endl;
// blank line between figures
cout << endl;
// draw triangle
row = 0;
// draw rows above base
while (row < height - 1)
{
col = 0;
while (col < height + row) {
++col;
if (col == height - row)
cout<<’*’;
else {
if (col == height + row)
cout<<'*';
else
cout<<' ';
}
}
cout <<endl;
++row;
}
// draw the base
col = 0;
while (col < height * 2 - 1) {
cout <<’*’;
++col;
}
// new line after figure
cout << endl;
return 0;
}
2-6 下面的代码是做什么的?
int i=0;
while(i<10)
{
i+=1;
std::cout << i << std::endl;
}
#include<iostream>
#include
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
int i = 0;
while (i < 10)
{
i += 1;
std::cout << i << std::endl;
}
return 0;
}
2-7 编写一个程序来一次输出从10~-5的整数
#include
#include
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
int i = 10;
while (i>-5)
{
i -= 1;
std::cout << i << std::endl;
}
return 0;
}
2-8 编写一个程序来计算区间[1,10)中的所有数值的乘积。
#include
#include
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
int i = 1;
while (i<9)
{
i++;
int j = i;
while (j < i)
j++;
std::cout << i*j << std::endl;
}
return 0;
}
2-9 编写一个程序,让用户输入两个数值并告知用户在这两个数值中哪一个较大。
#include
#include
int main()
{
int i = 90;
int j = 100;
std::cout <<" i<j "<< std::endl;
return 0;
}
2-10 在下面的程序中,对std::的每一次使用进行解释。
int main()
{
int k =0;
while (k!=n){
using std::cout;
cout << “*”;
}
std::cout << std::endl;
return 0;
}
第三个表示输出
3-1
#include “stdafx.h”
#include
#include
#include
#include
#include
#include
using namespace std;
int main()
{
//请求输入并读入学生的姓名
cout << "please enter your first name: ";
string name;
cin >> name;
cout << "Hello, " << name << “!” << endl;
//请求输入并读入期中和期末成绩
cout << "please enter your midterm and final exam grades: ";
double midterm, final;
cin >> midterm >> final;
cout <<"enter all your homework grades, followed by end-of-file: ";
vector<double> homework;
double x;
//不变式 home包含了所有的家庭作业成绩
while (cin >> x)
{
homework.push_back(x);
}
//检查homework是否为空
typedef vector<double>::size_type vec_sz;
vec_sz size =homework.size();
if (size == 0) {
cout << endl<< "you must enter your grades."
"please try again." << endl;
return 1;
}
//对成绩进行排序
sort(homework.begin(), homework.end());
//计算家庭作业成绩的中值
vec_sz mid = size/2;
double median;
median = (size % 2 ==0) ? (homework[mid] + homework[mid-1]/2) : homework[mid];
//计算并输出总成绩
streamsize prec = cout.precision();
cout << "Your final grade is " << setprecision(3)
<< 0.2 * midterm + 0.4 * final + 0.4 * median
<< setprecision(prec) << endl;
return 0;
}
3-2.1
#include
#include
#include
#include
using std::cin; using std::setprecision;
using std::cout; using std::string;
using std::endl; using std::streamsize;
using std::setprecision;
int main()
{
cout << “Pleas enter your firt name:”;
string name;
cout << “Hello,” << name << “!” << endl;
cout << "Please enter your midterm and final exam grades:";
double midterm, final;
cin >> midterm >> final;
cout << "Enter all your homework grades,"
"followed by end-of-file:";
double x;
vector<double>homework;
while (cin >> x)
homework.pish_back(x);
}
streamsize prec = cout.precision();
cout << "Your final grade is" << setprecision(3)
<< 0.2 * midterm + 0.4 * final + 0.4 * sum / count
<< setprecision(prec) << endl;
return 0;
}
3.22
#include
#include
#include
#include
#include
#include
using std::cin; using std::sort;
using std::cout; using std::streamsize;
using std::endl; using std::string;
using std::precision; using std::vectot;
using std::setprecision;
int main()
{
cout << “Please enter your first name:”;
string name;
cin >> name;
cout << “Hello,” << name << “!” << endl;
cout << "Please enter your midterm and final exam grades:";
double midterm, final;
cin >> midterm >> final;
cout << "Enter all your homework grades,"
"followed by end-df-file:"
vector<double> homework;
double x;
while (cin >> x)
homework.push_back(x);
typedef vector<double>::size_type vec_sz;
vec_sz size = homework.size();
if (size == 0) (
cout << endl << "You mus enter your grades."
<< "Please try again." << endl;
return 1;
}
sort(homework.begin(), homework.endl());
vec_sz mid = size / 2;
double = (size & 2 == 0) ? (homework[mid] + homework[mid - 1]) / 2
streamize prec = cout.precision();
cout << "Your final grade is " << setprecision(3)
<< 0.2 * midterm + 0.4 * final + 0.4 * median
<< setprecision(prec) << endl;
return 0;
}