P15习题
-
- #include <iostream>
-
- int main()
- {
- std::cout << "Enter two numbers:" << std::endl;
- int v1,v2;
- std::cin >> v1 >> v2;
-
- int lower,upper;
- if (v1 <= v2)
- {
- lower = v1;
- upper = v2;
- }
- else
- {
- lower = v2;
- upper = v1;
- }
-
- int sum = 0;
- for (int i = lower; i <= upper; ++i)
- {
- sum += i;
- }
-
- std::cout << "Sum of " << lower
- << " to " << upper
- << " inclusive is "
- << sum << std::endl;
-
- return 0;
- }
//题1.14: 试分析假设v1 == v2的情况下,该程序的输出结果
#include <iostream>
int main()
{
std::cout << "Enter two numbers:" << std::endl;
int v1,v2;
std::cin >> v1 >> v2;
int lower,upper;
if (v1 <= v2)
{
lower = v1;
upper = v2;
}
else
{
lower = v2;
upper = v1;
}
int sum = 0;
for (int i = lower; i <= upper; ++i)
{
sum += i;
}
std::cout << "Sum of " << lower
<< " to " << upper
<< " inclusive is "
<< sum << std::endl;
return 0;
}
-
- #include <iostream>
-
- int main()
- {
- std::cout << "Please input a sequence of numbers:" << std::endl;
- int val;
- int count = 0;
-
- while (std::cin >> val)
- {
- if (val < 0)
- {
- ++ count;
- }
- }
-
- std::cout << "There is " << count << " negatives" << std::endl;
- return 0;
- }
//1.16
#include <iostream>
int main()
{
std::cout << "Please input a sequence of numbers:" << std::endl;
int val;
int count = 0;
//为推断条件。先运行输入操作
while (std::cin >> val)
{
if (val < 0)
{
++ count;
}
}
std::cout << "There is " << count << " negatives" << std::endl;
return 0;
}
-
- #include <iostream>
-
- int main()
- {
- std::cout << "Please input two numbers:" << std::endl;
- int v1,v2;
- int count = 1;
- std::cin >> v1 >> v2;
- for (int i = v1 ; i <= v2; ++i)
- {
- std::cout << i << ‘ ‘;
- if (count % 10 == 0)
- {
- std::cout << std::endl;
- }
- ++ count;
- }
-
- return 0;
- }
//题1.19
#include <iostream>
int main()
{
std::cout << "Please input two numbers:" << std::endl;
int v1,v2;
int count = 1;
std::cin >> v1 >> v2;
for (int i = v1 ; i <= v2; ++i)
{
std::cout << i << ' ';
if (count % 10 == 0)
{
std::cout << std::endl;
}
++ count;
}
return 0;
}
五、类的简单介绍
1、C++中,我们通过定义类来定义自己的数据结构
实际上,C++设计的主要焦点就是使所定义的类类型的行为能够像内置类型一样自然!!。
2、使用类的时候,我们不须要指定哦啊这个类是如何实现的。相反。我们须要知道的是:这个类可以提供什么样的操作!
3、对于自己定义的类,必须使得编译器能够訪问和类相关的定义。
4、通常文件名称和定义在头文件里的类名是一样的。
通常后缀名是.h,可是有些IDE会挑剔头文件的后缀名!
5、当使用自己定义头文件时。我们採用双引號(””)把头文件包括进来。
6、当调用成员函数的时候,(通常)指定函数要操作的对象。语法是使用点操作符(”.”),左操作符必须是类类型,右操作符必须指定该类型的成员。
P19
-
- #include <iostream>
- #include <fstream>
- #include "Sales_item.h"
-
- int main()
- {
- freopen("book_sales","r",stdin);
- Sales_item item;
- while (std::cin >> item)
- {
- std::cout << item << std::endl;
- }
-
- return 0;
- }
//习题1.21
#include <iostream>
#include <fstream>
#include "Sales_item.h"
int main()
{
freopen("book_sales","r",stdin);
Sales_item item;
while (std::cin >> item)
{
std::cout << item << std::endl;
}
return 0;
}
-
- #include <iostream>
- #include <fstream>
- #include "Sales_item.h"
-
- int main()
- {
- freopen("book_sales","r",stdin);
- Sales_item item1;
- Sales_item item2;
- while (std::cin >> item1 >> item2)
- {
- if (item1.same_isbn(item2))
- {
- std::cout << item1 + item2 << std::endl;
- }
- }
-
- return 0;
- }
//习题1.22
#include <iostream>
#include <fstream>
#include "Sales_item.h"
int main()
{
freopen("book_sales","r",stdin);
Sales_item item1;
Sales_item item2;
while (std::cin >> item1 >> item2)
{
if (item1.same_isbn(item2))
{
std::cout << item1 + item2 << std::endl;
}
}
return 0;
}
六、C++程序
- #include <iostream>
- #include <fstream>
- #include "Sales_item.h"
-
- int main()
- {
-
-
- Sales_item total,trans;
-
- if (std::cin >> total)
- {
- while (std::cin >> trans)
- {
- if (total.same_isbn(trans))
- {
- total += trans;
- }
- else
- {
- std::cout << total << std::endl;
- total = trans;
- }
- }
- std::cout << total << std::endl;
- }
- else
- {
- std::cout << "No Data?
!"
<< std::endl; - return -1;
- }
- return 0;
- }
#include <iostream>
#include <fstream>
#include "Sales_item.h"
int main()
{
//freopen("book_sales.","r",stdin);
Sales_item total,trans;
if (std::cin >> total)
{
while (std::cin >> trans)
{
if (total.same_isbn(trans))
{
total += trans;
}
else
{
std::cout << total << std::endl;
total = trans;
}
}
std::cout << total << std::endl;
}
else
{
std::cout << "No Data?!" << std::endl;
return -1;
}
return 0;
}
【说明:Sales_item类需要图灵网站下载及参考:
http://www.ituring.com.cn/book/656】