当前位置: 首页 > news >正文

哪个网站做正品女装企业网站模板下载尽在

哪个网站做正品女装,企业网站模板下载尽在,报考建设八大员官方网站,华为公司网站建设案例分析文章目录练习3.1练习3.2一次读入一行一次读入一个词练习3.3练习3.4大的字符串长度大的字符串练习3.5未隔开的隔开的练习3.6练习3.7练习3.8练习3.9练习3.10练习3.1 使用恰当的using 声明重做 1.4.1节和2.6.2节的练习。 // 1.4.1 #include <iostream>using std::cin; using…

文章目录

      • 练习3.1
      • 练习3.2
        • 一次读入一行
        • 一次读入一个词
      • 练习3.3
      • 练习3.4
        • 大的字符串
        • 长度大的字符串
      • 练习3.5
        • 未隔开的
        • 隔开的
      • 练习3.6
      • 练习3.7
      • 练习3.8
      • 练习3.9
      • 练习3.10

练习3.1

使用恰当的using 声明重做 1.4.1节和2.6.2节的练习。

// 1.4.1
#include <iostream>using std::cin;
using std::cout;
using std::endl;int main()
{int sum = 0;for (int val = 1; val <= 10; ++val) sum += val;cout << "Sum of 1 to 10 inclusive is " << sum << endl;return 0;
}// 2.6.2
#include <iostream>
#include <string>
#include "exercise2_42.h"using std::cin;
using std::cout;
using std::endl;
using std::cerr;int main()
{Sales_data data1, data2;double price = 0;  cin >> data1.bookNo >> data1.units_sold >> price;data1.revenue = data1.units_sold * price;cin >> data2.bookNo >> data2.units_sold >> price;data2.revenue = data2.units_sold * price;if (data1.bookNo == data2.bookNo){unsigned totalCnt = data1.units_sold + data2.units_sold;double totalRevenue = data1.revenue + data2.revenue;cout << data1.bookNo << " " << totalCnt<< " " << totalRevenue << " ";if (totalCnt != 0)cout << totalRevenue / totalCnt << endl;elsecout << "(no sales)" << endl;return 0;  }else{  cerr << "Data must refer to the same ISBN" << endl;return -1; }
}

练习3.2

编写一段程序从标准输入中一次读入一行,然后修改该程序使其一次读入一个词。

一次读入一行

#include <iostream>
#include <string>using std::string;
using std::cin;
using std::cout;
using std::endl;
using std::getline;int main()
{string s;while (getline(cin,s)){cout << s << endl;}return 0;
}

一次读入一个词

#include <iostream>
#include <string>using std::string;
using std::cin;
using std::cout;
using std::endl;int main()
{string s;while (cin >> s){cout << s << endl;}return 0;
}

练习3.3

请说明string类的输入运算符和getline函数分别是如何处理空白字符的。

  • 类似 is >> s 的读取,string对象会忽略开头的空白并从第一个真正的字符开始,直到遇见下一空白为止。
  • 类似 getline(is, s) 的读取,string对象会从输入流中读取字符,直到遇见换行符为止。

练习3.4

编写一段程序读取两个字符串,比较其是否相等并输出结果。如果不相等,输出比较大的那个字符串。改写上述程序,比较输入的两个字符串是否等长,如果不等长,输出长度较大的那个字符串。

大的字符串

#include <iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;int main()
{string str1, str2;while (cin >> str1 >> str2){if (str1 == str2)cout << "The two strings are equal." << endl;elsecout << "The larger string is " << ((str1 > str2) ? str1 : str2);}return 0;
}

长度大的字符串

#include <iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;int main()
{string str1, str2;while (cin >> str1 >> str2){if (str1.size() == str2.size())cout << "The two strings have the same length." << endl;elsecout << "The longer string is " << ((str1.size() > str2.size()) ? str1 : str2) << endl;}return 0;

练习3.5

编写一段程序从标准输入中读入多个字符串并将他们连接起来,输出连接成的大字符串。然后修改上述程序,用空格把输入的多个字符串分割开来。

未隔开的

#include <iostream>
#include <string>using std::string;
using std::cin;
using std::cout;
using std::endl;int main()
{string result, s;while (cin >> s){result += s;}cout << result << endl;return 0;
}

隔开的

#include <iostream>
#include <string>using std::string;
using std::cin;
using std::cout;
using std::endl;int main()
{string result, s;while (cin >> s){result += s + " ";}cout << result << endl;return 0;
}

练习3.6

编写一段程序,使用范围for语句将字符串内所有字符用X代替。

#include <iostream>
#include <string>
#include <cctype>using std::string;
using std::cin;
using std::cout;
using std::endl;int main()
{string s = "this is a string";for (auto &x : s){x = 'X';}cout << s << endl;return 0;
}

练习3.7

就上一题完成的程序而言,如果将循环控制的变量设置为char将发生什么?先估计一下结果,然后实际编程进行验证。

如果设置为char,那么原来的字符串不会发生改变。

#include <iostream>
#include <string>
#include <cctype>using std::string;
using std::cin;
using std::cout;
using std::endl;int main()
{string s = "this is a string";for (char x : s){x = 'X';}cout << s << endl;return 0;
}

练习3.8

分别用while循环和传统for循环重写第一题的程序,你觉得哪种形式更好呢?为什么?

范围for语句更好,不直接操作索引,更简洁。

#include <iostream>
#include <string>
#include <cctype>using std::string;
using std::cin;
using std::cout;
using std::endl;int main()
{string s = "this is a string";decltype(s.size()) i = 0;while (i != s.size()){s[i] = 'X';++i;}cout << s << endl;for (i = 0; i != s.size(); ++i){s[i] = 'Y';}cout << s << endl;return 0;
}

练习3.9

下面的程序有何作用?它合法吗?如果不合法?为什么?

string s;
cout << s[0] << endl;

不合法。使用下标访问空字符串是非法的行为。

练习3.10

编写一段程序,读入一个包含标点符号的字符串,将标点符号去除后输出字符串剩余的部分。

#include <iostream>
#include <string>
#include <cctype>using std::string;
using std::cin;
using std::cout;
using std::endl;int main()
{string s = "this, is. a :string!";string result;for (auto x : s){if (!ispunct(x)){result += x;}}cout << result << endl;return 0;
}
http://www.yayakq.cn/news/229652/

相关文章:

  • 网站制作+app+公众号做网站公司属于什么行业
  • 建设一个什么网站赚钱用xampp搭建wordpress
  • 销售型网站怎么做做商业网站
  • 网站建设要花多少钱怎么做关键词优化排名
  • 网站排名数据南宁手机建站公司
  • 网站怎么做数据接口江西省住房和城乡建设厅
  • 淘宝内部卷网站建设网站开发算互联网公司吗
  • 做sohu最好的推广网站类型: 营销型网站建设
  • 做网站哪些技术云南热搜科技做网站不给源码
  • 网站都有什么费用网站开发先学前端还是后端
  • 手机网站模版单页设计是什么
  • 科学数据分析网站html52014中文网站seo排名名单
  • 百度推广自己做网站厦门百度推广开户
  • 专做畜牧招聘网站的wordpress中文站cn
  • 站长之家99网站建设上海诏业
  • 网站评论回复如何做郑州seo排名第一
  • 多个域名指向同一个网站 备案百度推广账户登录首页
  • 徐州做网站需要多少钱乐陵属于山东哪个市
  • 计算机有网站建设专业吗四川住房和城乡建设局网站首页
  • 怎么在公众号做影视网站网页设计首页制作
  • 怎么给网站设置搜索关键词 wordpressvi标识设计公司
  • 广州网站建设案例网站建设知识平台
  • 网站建设的公司晋城 网站建设
  • 换域名影响网站不如何在网站上推广自己的产品
  • 怎样用自己的空间做网站网站建设中面包屑导航的特点
  • 经营网站备案信息天津做网站优化
  • 国外建站 网站 推荐解决方案网站
  • 新房网站建设公司让wordpress的页面有具体的地址
  • 昆山高端网站建设公司简单个人网站制作流程
  • 杭州淘策网站开发网站名超链接怎么做