1.1节练习
练习1.1
编译器GCC 4.9.2 文档
练习1.2
在CentOS、GCC下,输入echo $?
获得返回值为255
在Win7、GCC下,输入
echo %ERRORLEVEL%
获得返回值为-1
1.2节练习
练习1.3
#include<iostream>
int main()
{
std::cout << "Hello, World" << std::endl;
return 0;
}
练习1.4
#include<iostream>
int main()
{
int v1=0, v2=0;
std::cin >> v1 >> v2;
std::cout << "v1*v2=" << 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 sum 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
#include<iostream>
int main()
{
std::cout << "Enter two numbers:" << std::endl;
int v1=0, v2=0;
std::cin >> v1 >> v2;
std::cout << "The sum of " << v1;
std::cout << " and " << v2;
std::cout << " is " << v1+v2 << std::endl;
return 0;
}
1.3节练习
练习1.8
#include<iostream>
int main()
{
//std::cout << "/*";
//std::cout << "*/";
//报错
//std::cout << /* "*/" */";
std::cout << /* "*/" /* "/*" */;
return 0;
}
1.4节练习
练习1.9
#include<iostream>
int main()
{
int sum=0, i=50;
while(i<=100)
{
sum += i;
++i;
}
std::cout << "sum is " << sum << std::endl;
return 0;
}
练习1.10
#include<iostream>
int main()
{
int i=10;
while(i>=0)
{
std::cout << i << std::endl;
--i;
}
return 0;
}
练习1.11
#include<iostream>
int main()
{
int i, m, n;
std::cout << "Input number m and n(m<n): " << std::endl;
std::cin >> m >> n;
i=m;
while(i<=n)
{
std::cout << i << std::endl;
++i;
}
return 0;
}
练习1.13
#include<iostream>
int main()
{
int i, sum=0, m, n;
//1.9
for(i=50;i<=100;++i)
{
sum += i;
}
std::cout << "sum of 50 to 100 inclusive is " << sum << std::endl;
//1.10
for(i=10;i>=0;--i)
{
std::cout << i << std::endl;
}
//1.11
std::cout << "Input number m, n(m<n) " << std::endl;
std::cin >> m >> n;
for(i=m;i<=n;++i)
{
std::cout << i << std::endl;
}
return 0;
}
练习1.16
#include<iostream>
int main()
{
int sum=0, n;
std::cout << "Input numbers " << std::endl;
while(std::cin>>n)
{
sum += n;
}
std::cout << "Sum is " << sum << std::endl;
return 0;
}
练习1.19
#include<iostream>
int main()
{
int i, m, n, temp;
std::cout << "Input number m and n: " << std::endl;
std::cin >> m >> n;
if(m>n)
{
temp = m;
m=n;
n = temp;
}
i=m;
while(i<=n)
{
std::cout << i << std::endl;
++i;
}
return 0;
}
1.5节练习
练习1.20
int main()
{
Sales_item book;
while(std::cin >> book) {
std::cout << book << std::endl;
}
return 0;
}
练习1.21
int main()
{
Sales_item book1, book2;
std::cin >> book1 >> book2;
std::cout << book1 + book2 << std::endl;
return 0;
}
练习1.22
int main()
{
Sales_item total, book;
std::cin >> total;
while(std::cin >> book) {
total += book;
}
std::cout << total << std::endl;
return 0;
}
练习1.23
int main()
{
Sales_item book, tmp;
int cnt = 1;
std::cin >> book;
while(std::cin >> tmp) {
if(tmp.isbn()==book.isbn()) {
cnt++;
}else {
std::cout << book.isbn() << " : " << cnt << std::endl;
cnt=1;
book = tmp;
}
}
std::cout << book.isbn() << " : " << cnt << std::endl;
return 0;
}