第1章 开始
练习1.1
int main()
{
return 0;
}
练习1.2
int main()
{
return -1;
}
练习1.3
#include <iostream>
int main()
{
std::cout << "Hello, World";
return 0;
}
练习1.4
#include <iostream>
int main()
{
std::cout << "Enter two numbers:" << std::endl;
int v1 = 0, v2 = 0;
std::cin >> v1 >> v2;
std::cout << "The product of " << v1 << " and " << v2 << " is " << v1 * v2 << std::endl;
return 0;
}
练习1.5
#include <iostream>
int main()
{
std::cout << "Enter two numbers:" << std::endl;
int v1 = 0, v2 = 0;
std::cin >> v1 >> v2;
std::cout << "The product of ";
std::cout << v1;
std::cout << " and ";
std::cout << v2;
std::cout << " is ";
std::cout << v1 * v2;
std::cout << std::endl;
return 0;
}
练习1.6
不合法。v1和v2后面的分号多余,除掉这两个分号即可。
练习1.7
/*
* /*不能嵌套*/
*/
int main()
{
return 0;
}
编译器错误信息:
练习1.8
第三行编译出错,改正方法是增加一个引号。如下。
std::cout << /* "*/" */";
练习1.9
#include <iostream>
int main()
{
int sum = 0, val = 50;
while (val <= 100)
{
sum += val++;
}
std::cout << "Sum is " << sum << std::endl;
return 0;
}
练习1.10
#include <iostream>
int main()
{
int val = 10;
while (val >= 0)
{
std::cout << val-- << std::endl;
}
return 0;
}
练习1.11
#include <iostream>
int main()
{
int v1 = 0, v2 = 0;
std::cout << "Enter to numbers:" << std::endl;
std::cin >> v1 >> v2;
while (v1 >= v2)
{
std::cout << v1-- << std::endl;
}
while (v1 <= v2)
{
std::cout << v1++ << std::endl;
}
return 0;
}
练习1.12
从-100一直加到100,sum的终值为0。
练习1.13
练习1.9
#include <iostream>
int main()
{
int sum = 0;
for (int val = 50; val <= 100; ++val)
sum += val;
std::cout << "Sum is: " <<sum << std::endl;
return 0;
}
练习1.10
#include <iostream>
int main()
{
for (int val = 10; val >= 0; --val)
std::cout << val << std::endl;
return 0;
}
练习1.11
#include <iostream>
int main()
{
std::cout << "Enter two numbers:" << std::endl;
int v1 = 0, v2 = 0;
std::cin >> v1 >> v2;
for (int i = v1; i >= v2; --i)
std::cout << i << std::endl;
for (int i = v1; i <= v2; ++i)
std::cout << i << std::endl;
return 0;
}
练习1.14
for循环适合循环次数已知的情况,而while循坏更适合循环次数不易预知的情况。
练习1.15
略
练习1.16
#include <iostream>
int main()
{
int sum = 0, val = 0;
while (std::cin >> val)
sum += val;
std::cout << "Sum is: " << sum << std::endl;
return 0;
}
练习1.17
输入值都相等,则输出这个值出现的次数;输入值没有重复,则输出各个值都出现1次。
练习1.18
#include <iostream>
int main()
{
int currVal = 0, val = 0;
if (std::cin >> currVal)
{
int cnt = 1;
while (std::cin >> val)
{
if (val == currVal)
++cnt;
else
{
std::cout << currVal << " occurs " << cnt << " times" << std::endl;
currVal = val;
cnt = 1;
}
}
std::cout<<currVal<<" occurs "<<cnt<<" times"<<std::endl;
}
return 0;
}
练习1.19
#include <iostream>
int main()
{
std::cout << "Enter two numbers:" << std::endl;
int v1 = 0, v2 = 0;
std::cin >> v1 >> v2;
if (v1 >= v2)
{
while (v1 >= v2)
std::cout << v1-- << std::endl;
}
else
{
while (v2 >= v1)
std::cout << v2--<<std::endl;
}
return 0;
}
练习1.20
#include <iostream>
#include "Sales_item.h"
int main()
{
Sales_item item;
while (std::cin >> item)
std::cout << item << std::endl;
return 0;
}
练习1.21
#include <iostream>
#include "Sales_item.h"
int main()
{
Sales_item item1, item2;
while (std::cin >> item1 >> item2)
std::cout << item1 + item2 << std::endl;
return 0;
}
练习1.22
#include <iostream>
#include "Sales_item.h"
int main()
{
Sales_item item0, item;
if (std::cin >> item)
{
while (std::cin >> item0)
item += item0;
}
std::cout << item << std::endl;
return 0;
}
练习1.23
#include <iostream>
#include "Sales_item.h"
int main()
{
Sales_item item0, item;
if (std::cin >> item0)
{
int cnt = 1;
while (std::cin >> item)
{
if (item.isbn() == item0.isbn())
++cnt;
else
{
std::cout << item0.isbn() << " occurs " << cnt <<" times"<< std::endl;
item0 = item;
cnt = 1;
}
}
std::cout << item0.isbn() << " occurs " << cnt << " times" << std::endl;
}
return 0;
}
练习1.24
略
练习1.25
#include <iostream>
#include "Sales_item.h"
int main()
{
Sales_item total;
if (std::cin >> total)
{
Sales_item trans;
while (std::cin >> trans)
{
if (total.isbn() == trans.isbn())
total += trans;
else
{
std::cout << total << std::endl;
total = trans;
}
}
std::cout << total << std::endl;
}
else
{
std::cerr << "No data?!" << std::endl;
return -1;
}
return 0;
}