#include
int main()
{
std::cout << "Hello, World" << std::endl;
return 0;
}
#include
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;
}
#include
int main()
{
std::cout << "Enter two number:" << 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;
}
不合法。输出运算符(<<)左侧的运算对象要是一个iostream对象,这里第一行最后有分号结束了这一句,第二行的(<<)左边没有iostream对象。
改正:去掉第一行最后的分号。
输入编译器后,编译器的提示,这里编译器提示的第四行的std下有红线,是因为上一句错误,导致编译器认为上一行语句还没结束
第三行不合法语句应该修改成
std::cout << /* "*/" */"; //在最后加一个引号
修改后,四条语句输出的结果是
std::cout << "/*"; // 输出 '/*'
std::cout << "*/"; // 输出 '*/'
std::cout << /* "*/" */"; // 输出 ' */'
std::cout << /* "*/" /* "/*" */; // 输出 ' /* '
#include
int main()
{
int sum = 0, val = 50;
while (val <= 100)
{
sum += val;
++val;
}
std::cout << "Sum of 50 to 100 inclusive is " << sum << std::endl;
return 0;
}
#include
int main()
{
int val = 10;
while (val >=0)
{
std::cout << val << std::endl;
--val;
}
return 0;
}
在打印这两个数所指定的范围内的所有整数之前,先比较两个数的大小,并把小的数赋给small,大的赋给big。
#include
int main()
{
int small = 0, big = 0;
std::cout << "Please enter two integers:";
std::cin >> small >> big;
if (small > big) {
int tmp = small;
small = big;
big = tmp;
}
while (small <= big) {
std::cout << small << " ";
++small;
}
std::cout << std::endl;
return 0;
}
这个for循环的作用是求-100到100的和,sum的终值是0。
练习1.9的for循环版
#include
int main()
{
int sum = 0;
for (int i = 50; i <= 100; ++i)
sum += i;
std::cout << "the sum is: " << sum << std::endl;
return 0;
}
练习1.10的for循环版
#include
int main()
{
for (int i = 10; i >= 0; --i)
std::cout << i << " ";
std::cout << std::endl;
return 0;
}
练习1.11的for循环版
#include
int main()
{
int small = 0, big = 0;
std::cout << "Please enter two integers:";
std::cin >> small >> big;
if (small > big) {
int tmp = small;
small = big;
big = tmp;
}
for (int i = small; i <= big; ++i)
std::cout << i << " ";
std::cout << std::endl;
return 0;
}
while的优点和缺点:
for的优点和缺点:
#include
int main()
{
int num,sum = 0;
while ( std::cin >> num )
{
sum += num;
}
std::cout << sum << std::endl;
return 0;
}
如果输出的所有值全都是相等的,会打印出输入值的次数。
如果没有重复的值,在回车单击时打印上一个数出现一次。
先把Sales_item.h头文件拷贝进你自己的工作目录中,使用时在代码中包含该头文件即可。
#include
#include "../include/Sales_item.h"
int main()
{
Sales_item item;
while (std::cin >> item)
{
std::cout << item << std::endl;
}
return 0;
}
#include
#include "../include/Sales_item.h"
int main()
{
Sales_item item1, item2;
std::cin >> item1 >> item2;
if (item1.isbn() == item2.isbn()) {
std::cout << item1 + item2 << std::endl;
return 0;
}
else {
std::cerr << "Data must refer to same ISBN." << std::endl;
return -1;
}
}
#include
#include "../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;
}
#include
#include "../include/Sales_item.h"
int main()
{
Sales_item currItem, valItem;
if (std::cin >> currItem) {
int cnt = 1;
while (std::cin >> valItem) {
if (valItem.isbn() == currItem.isbn())
++cnt;
else {
std::cout << currItem << " occurs " << cnt << " times " << std::endl;
currItem = valItem;
cnt = 1;
}
}
std::cout << currItem << " occurs " << cnt << " times " << std::endl;
}
return 0;
}