P15习题
[cpp] view plaincopyprint?
- //题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;
- }
//题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; }
[cpp] view plaincopyprint?
- //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;
- }
//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; }
[cpp] view plaincopyprint?
- //题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.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
[cpp] view plaincopyprint?
- //习题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;
- }
//习题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; }
[cpp] view plaincopyprint?
- //习题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;
- }
//习题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++程序
[cpp] view plaincopyprint?
- #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;
- }
#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】
时间: 2024-10-09 04:21:26