2019独角兽企业重金招聘Python工程师标准>>>

最近要复习一下C和C++的基础知识,于是计划把之前学过的谭浩强的《C程序设计》和《C++程序设计》习题重新做一遍。

编译环境为:操作系统32位Win7,编译工具VC++6.0

第十三章:输入输出流

10.1)输入三角形的三边边长a、b、c,通过海伦公式计算三角形面积,用cerr输出有关出错的信息

#include<iostream>
#include<cmath>using namespace std;int main()
{double a, b, c, s, area;cin >> a >> b >> c;if(a <= 0 || b <= 0 || c <= 0){cerr << "三角形每个边长应大于0" << endl;}else if(a + b <= c || a + c <= b || b + c <= a){cerr << "三角形两边之和须大于第三边" << endl;}else{s = (a + b + c) / 2.0;area = sqrt(s * (s - a) * (s - b) * (s - c));cout << "Area: " << area << endl;}return 0;
}

10.2)从键盘输入一批数值,要求保留3位小数,在输出时上下行小数点对齐

#include<iostream>using namespace std;int main()
{cout.setf(ios :: right);   //右对齐cout.setf(ios :: dec);     //十进制cout.setf(ios :: showpos); //对正数显示+号cout.setf(ios :: fixed);   //浮点数以定点格式输出double a;while(true){cout.precision(3); //实数精度3位cout.width(20);    //字段宽度10位cout.fill(' ');    //填充字符为' 'cin >> a;cout << a << endl;}cout.unsetf(ios :: right);   //取消右对齐cout.unsetf(ios :: dec);     //取消十进制cout.unsetf(ios :: showpos); //取消对正数显示+号cout.unsetf(ios :: fixed);   //取消浮点数以定点格式输出return 0;
}

10.3)编程序,在显示屏上显示一个字母B组成的三角形

#include<iostream>using namespace std;int main()
{int i, j;for(i = 0; i < 8; i++){//cout << i << ":";for(j = 8 - i - 1; j > 0; j--){cout.put(' ');}for(j = 0; j < i * 2 + 1; j++){cout.put('B');}cout << endl;}return 0;
}

10.4)建立两个磁盘文件f1.dat和f2.dat

1:从键盘键入20个整数,分别存在两个磁盘文件中,每个文件存10个

2:从f1.dat中读取10个数放在f2.dat的后面

3:对f2.dat中的20个整数进行排序

#include<iostream>
#include<fstream>
#include<cstdlib>using namespace std;int main()
{int a[20], i, j, temp;//样例输入 1 3 5 7 9 11 13 15 17 19 18 16 14 12 10 8 6 4 2 0cout << "请输入20个整数:";//输入10个数存入f1.datofstream outfile1("f1.dat", ios :: out);if(!outfile1){cerr << "f1.dat 打开错误" << endl;exit(1);}for(i = 0; i < 10; i++){cin >> a[i];outfile1 << a[i] << " ";}outfile1.close();//输入10个数存入f2.datofstream outfile2("f2.dat", ios :: out);if(!outfile2){cerr << "f2.dat 打开错误" << endl;exit(1);}for(i = 10; i < 20; i++){cin >> a[i];outfile2 << a[i] << " ";}outfile2.close();//从f1.dat中读入10个数存到f2.dat后ifstream infile1("f1.dat", ios :: in);if(!infile1){cerr << "f1.dat 打开错误" << endl;exit(1);}ofstream outfile3("f2.dat", ios :: out | ios :: app);if(!outfile3){cerr << "f2.dat 打开错误" << endl;exit(1);}for(i = 0; i < 10; i++){infile1 >> temp;outfile3 << temp << " ";}infile1.close();outfile3.close();//将f2.dat中的20个数排序后写回//读取20个数ifstream infile2("f1.dat", ios :: in);if(!infile2){cerr << "f1.dat 打开错误" << endl;exit(1);}for(i = 0; i < 20; i++){infile2 >> a[i];}infile2.close();//排序for(i = 0; i < 20; i++){for(j = i + 1; j < 20; j++){if(a[i] > a[j]){temp = a[i];a[i] = a[j];a[j] = temp;}}}//将排序后的数字写回ofstream outfile4("f2.dat", ios :: out);if(!outfile4){cerr << "f2.dat 打开错误" << endl;exit(1);}for(i = 0; i < 20; i++){outfile4 << a[i] << " ";}outfile4.close();return 0;
}

10.5)编程实现下列功能

1:按职工号大小由小到大顺序将5个员工的数据(包括号码、姓名、年龄、工资)输出到磁盘文件中保存

2:从键盘输入两个员工数据,增加到文件末尾

3:从键盘键入一个号码,从文件中查找数据,如果有则输出职工数据,没有则输出“查无此人”

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>using namespace std;//职工
struct Person
{int m_Id;string m_Name;int m_Age;int m_Wage;Person(int i, string n, int a, int w): m_Id(i), m_Name(n), m_Age(a), m_Wage(w) { }void PrintInfo(){cout << "===================" << endl;cout << "Id:\t" << this -> m_Id << endl;cout << "Name:\t" << this -> m_Name << endl;cout << "Age:\t" << this -> m_Age << endl;cout << "Wage:\t" << this -> m_Wage << endl;cout << "===================" << endl;}
};Person *p[5] =
{new Person(101, "Tsybius", 23, 7100),new Person(102, "Galatea", 21, 6600),new Person(105, "Quintus", 15, 5000),new Person(104, "Aurelia", 20, 8000),new Person(103, "Julius", 30, 5050)
};int main()
{//对5组数据按ID进行排序int i, j;Person *temp;for(i = 0; i < 5; i++){for(j = i + 1; j < 5; j++){if(p[i] -> m_Id > p[j] -> m_Id){temp = p[i];p[i] = p[j];p[j] = temp;}}}//将排好序的数据存入到文件f.datofstream outfile1("f.dat", ios :: out);if(!outfile1){cerr << "f.dat 打开错误" << endl;exit(1);}for(i = 0; i < 5; i++){outfile1 << p[i] -> m_Id << " " << p[i] -> m_Name << " "<< p[i] -> m_Age << " "<< p[i] -> m_Wage << endl;}outfile1.close();//再写入两个职工信息//示例输入 108 Titus 40 4000 109 Tulius 35 5000cout << "请输入2个职工信息:";ofstream outfile2("f.dat", ios :: out | ios :: app);if(!outfile2){cerr << "f.dat 打开错误" << endl;exit(1);}temp = new Person(0, "", 0, 0);cin >> temp -> m_Id >> temp -> m_Name >> temp -> m_Age >> temp -> m_Wage;outfile2 << temp -> m_Id << " "<< temp -> m_Name << " "<< temp -> m_Age << " "<< temp -> m_Wage << endl;cin >> temp -> m_Id >> temp -> m_Name >> temp -> m_Age >> temp -> m_Wage;outfile2 << temp -> m_Id << " "<< temp -> m_Name << " "<< temp -> m_Age << " "<< temp -> m_Wage << endl;outfile2.close();//输出文件中的全部职工数据ifstream infile1("f.dat", ios :: in);if(!infile1){cerr << "f.dat 打开错误" << endl;exit(1);}for(i = 0; i < 7; i++){infile1 >> temp -> m_Id >> temp -> m_Name >> temp -> m_Age >> temp -> m_Wage;temp -> PrintInfo();}infile1.close();//输入一个ID检测是否存在int id;cout << "请输入一个ID:";cin >> id;bool isFound = false;ifstream infile2("f.dat", ios :: in);if(!infile2){cerr << "f.dat 打开错误" << endl;exit(1);}for(i = 0; i < 7; i++){infile2 >> temp -> m_Id >> temp -> m_Name >> temp -> m_Age >> temp -> m_Wage;if(temp -> m_Id == id){temp -> PrintInfo();isFound = true;break;}}if(!isFound){cerr << "没有找到这个人" << endl;}infile2.close();return 0;
}

10.6)在例13.17的基础上,修改程序,将存在在c数组中的数据读入并显示出来

#include<iostream>
#include<strstream>using namespace std;struct Student
{int m_Num;char m_Name[20];float m_Score;
};int main()
{Student stud[3] = { 1001, "Li", 78, 1002, "Wang", 89.5f, 1004, "Fun", 90 };//将信息存入到字符串中char c[50];ostrstream strout(c, sizeof(c));int i;for(i = 0; i < 3; i++){strout << stud[i].m_Num << " "<< stud[i].m_Name << " " << stud[i].m_Score << " ";}strout << ends;cout << "array c:" << c << endl;//从字符串中读取信息Student stud2[3];istrstream strin(c, sizeof(c));for(i = 0; i < 3; i++){strin >> stud2[i].m_Num >> stud2[i].m_Name >> stud2[i].m_Score;cout << stud2[i].m_Num << " " << stud2[i].m_Name << " " << stud2[i].m_Score << endl;}return 0;
}

第十四章:输入输出流

14.1)求一元二次方程ax^2+bx+c=0的实根,如果方程没有实根,则输出有关警告信息

#include<iostream>
#include<cmath>using namespace std;int main()
{double a, b, c, delta;cin >> a >> b >> c;try{//计算Δdelta = b * b - 4 * a * c;if(delta < 0){throw delta;}//求根double x1 = (-1 * b + sqrt(delta)) / (2 * a);double x2 = (-1 * b - sqrt(delta)) / (2 * a);cout << "x1=" << x1 << ";x2=" << x2 << endl;}catch(double){//Δ<0的情况cout << "b^2-4ac=" << delta << "<0" << endl;cout << "方程无实根" << endl;}cout << "程序运行完毕" << endl;return 0;
}

14.3)在两个命名空间中实现Student类

#include<iostream>
#include<string>using namespace std;namespace ns1
{struct Student{int m_ID;string m_Name;int m_Age;string m_Addr;//构造函数Student(int id, string na, int ag, string ad): m_ID(id), m_Name(na), m_Age(ag), m_Addr(ad) { }}stud[3] = {Student(101, "Tsybius", 23, "XXX"),Student(102, "Galatea", 21, "YYY"),Student(103, "Aurelia", 22, "ZZZ")};
}namespace ns2
{struct Student{int m_ID;string m_Name;char m_Sex;string m_Addr;//构造函数Student(int id, string na, char sx, string ad): m_ID(id), m_Name(na), m_Sex(sx), m_Addr(ad) { }}stud[3] = {Student(101, "Tsybius", 'm', "XXX"),Student(102, "Galatea", 'f', "YYY"),Student(103, "Aurelia", 'f', "ZZZ")};
}int main()
{int id;cout << "请输入一个ID:";cin >> id;//搜索内容ns1 :: Student *s1;ns2 :: Student *s2;for(int i = 0; i < 3; i++){if(ns1 :: stud[i].m_ID == id){s1 = &(ns1 :: stud[i]);}if(ns2 :: stud[i].m_ID == id){s2 = &(ns2 :: stud[i]);}}cout << "ID:\t" << s1 -> m_ID << endl;cout << "Name:\t" << s1 -> m_Name << endl;cout << "Age:\t" << s1 -> m_Age << endl;cout << "Sex:\t" << s2 -> m_Sex << endl;cout << "Addr:\t" << s1 -> m_Addr << endl;return 0;
}

END

转载于:https://my.oschina.net/Tsybius2014/blog/314958

谭浩强《C++程序设计》书后习题 第十三章-第十四章相关推荐

  1. 谭浩强C程序设计(第五版)P82 习题六(求圆周长、圆面积、圆球表面积、圆球体积、圆柱体积)——中职

    谭浩强C程序设计(第五版)P82 习题六 设圆半径r = 1.5,圆柱高h = 3,求圆周长.圆面积.圆球表面积.圆球体积.圆柱体积.用scanf输入数据,输出计算结果,输出时要求文字说明,取 小数点 ...

  2. 谭浩强C程序设计第五版课后答案视频+代码讲解完整版(合集)持续跟新中~~~

    这里是一个谭浩强C程序设计第五版课后答案的合集 请看下面: 工欲善其事必先利其器 写C语言代码,首先得有安装一个趁手的工具,那么VS系列是最合适不过的了 这里是安装教程: VS2013安装教程和使用 ...

  3. 在c语言中,字符串topt65的长度是,谭浩强c__程序设计第13章.ppt

    谭浩强c__程序设计第13章 第13章 输入输出流 13.1 C++的输入和输出 13.2 标准输出流 13.3 标准输入流 13.4 文件操作与文件流 13.5 字符串流 13.1 C++的输入和输 ...

  4. C语言的学习第一天——谭浩强C程序设计第五版

    学习了谭浩强C程序设计第一章 编译器:VS2019 鹏哥C语言:c语言编程 C语言入门 c语言(C语言程序设计教程 c语言视频教程 c语言零基础入门教程 学习c语言 c语言视频教程 c语音 C语言教程 ...

  5. 【C++习题笔记】谭浩强C++程序设计(第三版)第七章

    本文是谭浩强老师c++程序设计(第三版)第七章的习题总结,主要涉及结构体的内容. 1. 定义一个结构体变量(包括年.月.日),编写程序,要求输入年月日,程序能够计算并输出改日是本年中的第几天.注意闰年 ...

  6. 谭浩强C程序设计快速复习笔记

    README 该笔记主要根据岭南学院专插本的考纲进行编写.用到的教材是谭浩强的<C程序设计>第三版 适用人群:对C有一定基础,针对考点快速复习的考生.但不限于岭南学院专插本考生 亮点:①结 ...

  7. 谭浩强c语言课后习题笔记[1-4章]

    c语言程序设计(第五版)谭浩强课后习题笔记 文章目录 c语言程序设计(第五版)谭浩强课后习题笔记 第一章 程序设计和c语言 1.4 打印 Hello World 1.6 输入abc求最大值 第二章 算 ...

  8. 谭浩强C程序设计第四版答案

    只给出一些需要编程的答案,用的是谭浩强老师的书: 本文持续更新中- C程序设计课本中的一些笔记见:C程序设计笔记 我的目录 只给出一些需要编程的答案,用的是谭浩强老师的书: 本文持续更新中... C程 ...

  9. c语言程序灵魂编译后,谭浩强C程序设计第4版精讲视频课程

    谭浩强<C程序设计>(第4版)网授精讲班[教材精讲+考研真题串讲] 本课程由资深辅导教师马丽梅老师讲授,全面讲解教材的重点.难点.考点,教会学员理解并掌握该教材中的基本概念.基本原理和基本 ...

最新文章

  1. c语言相邻字符串字面量,C语言预处理#运算符的细节
  2. Python介绍以及Python 优缺点
  3. 一个word文档中,多个表格的批量调整(根据窗口调整表格和添加表格水平线)...
  4. 中国顶级互联网公司的技术组织架构调整预示着什么?【强烈推荐行业人士阅读】
  5. C语言解释器的实现--语法解析(五)
  6. Java并发教程–原子性和竞争条件
  7. JavaScript websocket 实例
  8. 前端学习(2006)vue之电商管理系统电商系统之绘制商品参数的复选框
  9. 【TensorFlow】TensorFlow从浅入深系列之九 -- 教你认识图像识别中经典数据集
  10. 一文搞懂隐马尔可夫模型(HMM)
  11. DataSet 更新 心得(转)
  12. webpack-md5-hash问题记录
  13. oracle建表默认now,oracle建表脚本当中使用默认值_oracle
  14. java 导出word文件,支持导出表格和图片
  15. 齐齐哈尔那个职校学电子计算机好,齐齐哈尔职业学校有什么专业
  16. ubuntu背景色改为淡绿色
  17. 再携手,齐并进!菊风助力宁波银行坐席PUSH外呼项目
  18. EVE模拟器如何设置预配
  19. 关于直播电商平台的一些数据
  20. 举个栗子!Tableau 技巧(90):让你的图表背景变透明

热门文章

  1. pap和chap交叉认证
  2. ssqlit3.0数据库使用方法
  3. 医院六级电子病历建设思路及要点
  4. CSS控制字体在一行内显示不换行
  5. 买得嗨更要聊得嗨 阿里通免费电话惠战双11
  6. 记一次 MySQL 的慢查优化
  7. 4、jQuery实现的全选、反选和不选功能
  8. 微软,您的.net为中国程序员带来了什么?
  9. 超越对手pdf_如何创建一个超越竞争对手的移动应用
  10. hic染色体构想_了解微服务:从构想到起点