// 编写程序在标准输出上打印 Hello World。#include<iostream>//引用标准输入输出流库intmain(){
std::cout <<"Hello, World !"<< std::endl;return0;}
习题1.4 乘法操作,直接输出。
//编写程序,使用乘法操作符“*”来产生两个数的积。#include<iostream>intmain(){
std::cout <<"Enter the two numbers : "<< std::endl;int v1, v2;
std::cin >> v1 >> v2;
std::cout <<"The product of "<< v1 <<" and "<< v2 <<" is "<< v1*v2 << std::endl;return0;}
习题1.5
// 重写习题1.4的程序,用单独的语句打印每一个操作数。#include<iostream>intmain(){
std::cout <<"Enter the two numbers : "<< std::endl;int v1, v2;
std::cin >> v1;
std::cin >> v2;
std::cout <<"The sum of "<< v1 <<" and "<< v2 <<" is "<< v1*v2 << std::endl;return0;}
习题1.9
// 解释下列代码,for循环。#include<iostream>intmain(){int sum =0;for(int i=-100; i<=100;++i)// 不要忘记分号。{
sum += i;}
std::cout << sum << std::endl;return0;}
习题1.10
// 用for循环编程,求从50到100的所有自然数的和.#include<iostream>intmain(){int sum =0;for(int i=50; i<=100;++i){
sum += i;}
std::cout << sum << std::endl;return0;}
// 用while循环编程,求从50到100的所有自然数的和.#include<iostream>intmain(){int sum =0;int i=50;while( i<=100){
sum += i;++i;}
std::cout << sum << std::endl;return0;}
习题1.11
// 用while循环变成,输出10-0递减的自然数#include<iostream>intmain(){int sum =0;int i=10;while( i>=0){
std::cout << i << std::endl;--i;}return0;}
习题1.16
// 编写程序,输出用户输入的两个数中的较大者。#include<iostream>intmain(){
std::cout <<"Enter two numbers, I will give the bigger back"<< std::endl;int a,b;
std::cin >> a >> b ;if(a>=b){
std::cout <<"the bigger one is "<< a << std::endl;}else{
std::cout <<"the bigger one is "<< b << std::endl;}return0;}
习题1.17
// 编写程序,要求用户输入一组数,输出信息说明其中有多少个负数。#include<iostream>intmain(){int amount =0, value;while(std::cin >> value){if(value <=0){++amount;}}
std::cout <<"Amount of all negative values read is "<< amount << std::endl;return0;}
习题1.21
// 编写程序,循环遍历一组书的销售交易,读入每笔交易并将交易写入标准输出。#include<iostream>#include"Sales_item.h"intmain(){
Sales_item book;
std::cout <<"Enter transactions: "<< std::endl;// 提醒输入数据while(std::cin >> book)// 循环持续读入书的信息,直到碰到文件终止符,Ubuntu中是Ctrl+D{
std::cout <<"ISBN, number of copies sold, total revenue, and average price are"<< std::endl;
std::cout << book << std::endl;}return0;}
习题1.22
// 编写程序,读入两个具有相同ISBN的Sales_item对象并产生他们的和。#include<iostream>#include"Sales_item.h"intmain(){
Sales_item book1, book2, book;
std::cout <<"Enter the transaction: "<< std::endl;
std::cin >> book1;
std::cout <<"Enter another transaction: "<< std::endl;
std::cin >> book2;
book = book1 + book2;// 求和
std::cout <<"ISBN, number of copies sold, total revenue, and average price are"<< std::endl;
std::cout << book << std::endl;return0;}
习题1.23
// 编写程序,读入几个具有相同ISBN的交易,输出所有读入交易的和。#include<iostream>#include"Sales_item.h"intmain(){
Sales_item book1,book;
std::cout <<"Enter the transaction: "<< std::endl;while(std::cin >> book1){
book = book + book1;}
std::cout <<"ISBN, number of copies sold, total revenue, and average price are"<< std::endl;
std::cout << book << std::endl;return0;}