以下程序由 Teddy van Jerry (我自己)编写并运行,基本保证正确性。(有时可能会为优化程序超前使用某些内容)
Contents
Before we comb through the codes
没有C++?参见我的博客 下载 C++ (Visual Studio 2019) 的步骤。
这一章比较基础,重点要养成好的习惯。
(Tip:使用英文版VS时源文件加到Source Files中(不是Resource Files),位置和中文版的不太一样。)
TIP:标有(C++/11)者为C++/11标准下可以使用的题,若为老版本应加以修改。
Exercise 1.3
#include <iostream>
int main() // If using void main(), don't write 'return 0' (Line 6).
{
std::cout << "Hello World" << std::endl;
return 0;
}
Exercise 1.4
#include <iostream>
using namespace std; // namespace using declaration (now we can omit std::)
int main()
{
int a = 0;
int b = 0;
cout << "Please enter two numbers." << endl;
cin >> a >> b;
cout << "The product of " << a << " and " << b << " is " << a * b << "." <<endl;
return 0;
}
Exercise 1.8
#include <iostream>
using namespace std;
int main()
{
cout << "/*";
cout << "*/";
return 0;
}
Exercise 1.9
#include <iostream>
using namespace std;
int main()
{
int s = 0;
int i = 50;
while (i <= 100) {
s += i;
++i;
}
cout << "Sum of 50 to 100 inclusive is " << s << "." << endl;
return 0;
}
Exercise 1.10
#include <iostream>
int main()
{
int s = 0;
int i = 10;
while (i >= 1) {
s += i;
--i;
}
std::cout << s << std::endl;
return 0;
}
Exercise 1.11
#include <iostream>
using namespace std;
int main()
{
int a = 0; int b = 0; int i = 0; int t = 0;
cout << "Please enter two numbers." << endl;
cin >> a >> b;
if ((a == b) || (a==b+1) || (a==b-1))
cout << "Sorry, there's none." << endl;
else {
if(a>b)
{
t = a; a = b; b = t;
}
i = a+1;
while (i < b - 1) {
cout << i << " ";
++i;
}
cout << b-1 << endl;
}
return 0;
}
Exercise 1.12
#include <iostream>
using namespace std;
int main()
{
int s = 0;
for (int i = -100; i <= 100; i++)
s += i;
cout << s << endl;
return 0;
}
Exercise 1.13
#include <iostream>
using namespace std;
int main()
{
int a = 0; int b = 0; int t = 0;
cout << "Please enter two numbers." << endl;
cin >> a >> b;
if (a == b || a == b + 1 || a == b - 1)
cout << "Sorry, there's none." << endl;
else {
if (a > b)
{
t = a; a = b; b = t;
}
for (int i = a + 1; i < b - 1; i++)
cout << i << " ";
cout << b - 1 << endl;
}
return 0;
}
Exercise 1.16
#include <iostream>
using namespace std;
int main()
{
int i = 0;
int s = 0;
for (; cin >> i;)
// while (cin >> i)
s += i;
cout << s << endl;
return 0;
}
Exercise 1.18
#include <iostream>
using namespace std;
int main()
{
int a = 0;
int b = 0;
int s = 1;
int t = 0;
cout << "Make sure that the same numbers aren't seperated." << endl;
cout << "When stopping inputting numbers, please enter Control-z before pressing the Enter button." << endl;
if (cin >> b)
{
while (cin >> a) {
if (a == b)
s++;
else
{
if (s != 1)
cout << b << " occurs " << s << " times" << endl;
else cout << b << " occurs 1 time" << endl;
b = a;
s = 1;
t++;
}
}
if(s!=1)
cout << b << " occurs " << s << " times" << endl;
else cout << b << " occurs 1 time" << endl;
}
else cout << "no number" << endl;
return 0;
}
Exercise 1.20
提示:头文件下载在书上提供的网址 http://informit.com/title/0321714113 ,实际进入 https://www.informit.com/store/c-plus-plus-primer-9780321714114,注意向下拉才能看见下载选项。
#include <iostream>
#include "Sales_item.h"
using namespace std;
int main()
{
Sales_item book;
cin >> book;
cout << book << endl;
return 0;
}
Exercise 1.21
#include <iostream>
#include "Sales_item.h"
using namespace std;
int main()
{
Sales_item item1, item2;
cin >> item1 >> item2;
cout << item1 + item2 << endl;
return 0;
}
Exercise 1.22
#include <iostream>
#include "Sales_item.h"
using namespace std;
int main()
{
Sales_item item;
Sales_item sum;
cout << "Please enter control-z when finishing inputting numbers." << endl;
cin >> item;
sum = item;
while (cin >> item)
sum += item;
cout << sum << endl;
return 0;
}
Exercise 1.23
#include <iostream>
#include "Sales_item.h"
using namespace std;
int main()
{
Sales_item item, init; //'init' refers to 'initial'
int s = 1;
int t = 0;
int sum[1000] ;
string name[1000];
//This means there should be no more than 1000 ISBN numbers.
for (int i = 0; i <= 999; i++)
sum[i] = 0;
cout << "Please enter control-z when finishing inputting information." << endl;
cin >> init;
while (cin >> item)
{
if (item.isbn() == init.isbn())
s++;
else
{
sum[t] = s; //store occurring times in sum[1000]
name[t] = init.isbn(); //store ISBN in name[1000]
s = 1;
t++;
init = item;
}
}
for (int j = 0; j < t; j++)
{
if (sum[j] != 1)
cout << name[j] << " occurs " << sum[j] << " times" << endl;
else cout << name[j] << " occurs 1 time" << endl;
}
if (s != 1)
cout << init.isbn() << " occurs " << s << " times" << endl;
else cout << init.isbn() << " occurs 1 time" << endl;
cout << "@Teddy van Jerry" << endl;
return 0;
}
评析见我的博客【思考】,其中也提出了其他操作方法(使用vector)。
Exercise 1.25
#include <iostream>
#include "Sales_item.h"
using namespace std;
int main()
{
Sales_item total, trans;
if (cin >> total)
{
while(cin >> trans)
if (total.isbn() == trans.isbn())
total += trans;
else
{
cout << total << endl;
total = trans;
}
}
else
{
cerr << "No data?!" << endl;
return -1;
}
return 0;
}
Next Chapter
【C++ Primer(5th Edition) Exercise】练习程序 - Chapter 2(第二章)
ALL RIGHTS RESERVED © 2020 Teddy van Jerry
欢迎转载,转载请注明出处。