![](https://img-blog.csdnimg.cn/20201014180756754.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
c++
C++基础知识和部分题解
CyberTorch
Good morning, and in case I don't see ya, good afternoon, good evening, and good night!
展开
-
【解决】windows配置vscode中c++环境(CLang)
大佬文章地址:传送门https://blog.csdn.net/qq_45707925/article/details/115601111?spm=1001.2014.3001.5501转载 2021-10-28 22:14:01 · 1032 阅读 · 0 评论 -
Write a function that counts the occurences of each digit in a string using the following header:
题:Write a function that counts the occurences of each digit in a string using the following header:int* count(const string& s)返回字符串里各个数字出现的次数。#include<iostream>#include<string>using namespace std; int* count(const string& s){原创 2020-08-19 15:07:27 · 273 阅读 · 0 评论 -
用指针找最大值
#include<iostream>using namespace std; int* findmax(int* p, int n){ int i, * max; max = p; for (i = 0; i < n; i++) { if (*(p + i) > * max) max = p + i; } return max;}int main(){ i原创 2020-08-19 15:01:57 · 1034 阅读 · 0 评论 -
编写具有排序功能的类模板SortableArray,该类模板是实现对整型数组,浮点型数组,字符型数组元素的升序排序
题:编写具有排序功能的类模板SortableArray,该类模板是实现对整型数组,浮点型数组,字符型数组元素的升序排序#include<iostream>#include<string>using namespace std;template<typename T>//模板类class SortableArray{public: T element[10]; int index; SortableArray(T a[], int);原创 2020-08-19 14:51:57 · 1084 阅读 · 0 评论 -
定义一个日期类Date,包括年、月、日等私有数据成员。要求为所定义的Date类设计如下重载运算符函数:
题:定义一个日期类Date,包括年、月、日等私有数据成员。要求为所定义的Date类设计如下重载运算符函数:Date operator+(int days);//返回一个日期加天数days后得到的日期Date operator-(int days);//返回一个日期减去天数days后得到的日期Int operator-(Date& b);//返回两个日期相差的天数#include<iostream>using namespace std;class Date{priva原创 2020-08-19 14:48:27 · 14393 阅读 · 0 评论 -
定义一个复数类Complex,重载运算符“+”、“-”、“*”、“/”,使之能用于复数的加、减、乘、除运算,运算符重载函数作为Complex类的成员函数。编写程序,分别求两个复数之和、差、积、商。
题:定义一个复数类Complex,重载运算符“+”、“-”、“*”、“/”,使之能用于复数的加、减、乘、除运算,运算符重载函数作为Complex类的成员函数。编写程序,分别求两个复数之和、差、积、商。#include<iostream>using namespace std;class Complex{public: double real; double imag; Complex(double, double); Complex operator+(const Comp原创 2020-08-19 14:46:37 · 934 阅读 · 0 评论 -
定义一个Student类,在该类定义中包括:两个数据成员name(学生姓名)和score(分数);两个静态数据成员total(总分)和count(学生人数)。
题:定义一个Student类,在该类定义中包括:两个数据成员name(学生姓名)和score(分数);两个静态数据成员total(总分)和count(学生人数)。成员函数scoretotalcount(float s)用于设置每个学生的分数;静态成员函数sum()用于返回总分;静态成员函数average()用于求分数平均值。在main函数中,输入某班同学的成绩(班级学生人数随个数录入情况自动增加),并调用上述函数求全班学生的总分和平均分。``#include<iostream>using原创 2020-08-19 14:44:16 · 26498 阅读 · 10 评论 -
创建一个二维空间的MyPoint类。 MyPoint类具有x和y分别代表xy坐标,两个x和y的get函数,以及用于返回两个点之间的距离的函数。创建一个三维空间名为ThreeDPoint的类。
题:创建一个二维空间的MyPoint类。 MyPoint类具有x和y分别代表xy坐标,两个x和y的get函数,以及用于返回两个点之间的距离的函数。创建一个三维空间名为ThreeDPoint的类。让ThreeDPoint从MyPoint派生并能够获取Z坐标值,设置一个坐标为(0,0,0)的无参构造函数点和具有三个指定坐标的点的构造函数。设计以(const ThreeDPoint&)形式的函数求距离,编写测试程序求两个点的距离。通过继承从基类中派生获取基类的函数等,在派生类中可以设计属于自己的函数可来区别于原创 2020-08-19 14:39:16 · 2104 阅读 · 0 评论 -
设计一个名为Person的类,并设计两个Person的派生的类Student和Employee。Employee的Faculty和Staff的派生类。
题:设计一个名为Person的类,并设计两个Person的派生的类Student和Employee。Employee的Faculty和Staff的派生类。Person类具有name,address,phone number和e-mail。Student具有freshmen, sophonore, junior, or senior四个状态。Employee有office,salary和datehired。定义一个名为MyDate的类,其中包含year,month和day字段。Faculty有officeh原创 2020-08-19 14:36:16 · 6693 阅读 · 0 评论 -
先提示用户输入输入文件名,再从文件中读取数据,然后对每个字节加5来加密文件,将加密后的数字写回文件,先提示用户输入输入文件名,再从文件中读取数据,然后对每个字节减5来解密文件,将解密后的数字写回文件。
//加密#include<iostream>#include<fstream>#include<string>#include<string.h>using namespace std;int main(){ fstream binaryio;//使用fstream来定义一个可输入和可输出流 cout << "Please enter the input file name: "; string in_filename; c原创 2020-08-19 14:32:24 · 1965 阅读 · 0 评论 -
假定名为Exercise13_4.txt的文本文件包含100个整数,先从文件中读取数据,然后对数据排序,将排序后的数字写回文件。整数在文件中以空格分开。
#include<iostream>#include<algorithm>#include<fstream>using namespace std;int main(){ ifstream input;//用ifstream用作输入 ofstream output;//用ofstream用作输出 output.open("Exercise13_4.txt"); if (output.fail())//判断是否存在此文件 cout <<原创 2020-08-19 14:28:08 · 426 阅读 · 0 评论 -
设计一个函数,将中序表达式转换为后序表达式,函数头为 String infixToPostfix(constantstring& expression)如:将中序表达式“(1+2)*3”转换为“12+
用vector来实现可增长的数组出现过输出有小括号问题。解决方案:出现小括号是因为代码中由于加减乘除的等级不同导致在遇到“)”时有的输出会多出括号,最后解决方法是在有括号的情况下再分两种情况一种是在小括号中出现(先乘除后加减)另一种(先加减后乘除)#include <iostream>#include<string>#include<vector>using namespace std;int s2i(string ch){ if (ch == "*" |原创 2020-08-18 19:42:56 · 233 阅读 · 0 评论 -
编写函数和测试程序,返回两个字符串的公共字符
#include<iostream>#include<string>using namespace std;string commonChars(const string& s1, const string& s2);//string simplify(string& c);int main(){ string s1; string s2; cout << "Enter a string s1: "; cin >>原创 2020-08-18 19:34:48 · 663 阅读 · 0 评论 -
编写一个检查字符串是否为有效密码的方法
题:编写一个检查字符串是否为有效密码的方法。要求如下:密码必须至少有八个字符、密码仅由字母和数字组成、密码必须至少包含两位数字。如果遵循规则,则显示“有效密码”,否则显示“无效密码”。//Password.h#ifndef PASSWORD_H#define PASSWORD_H#include<string>class Password{public: Password(std::string); bool is8char(std::string t) const;原创 2020-08-18 19:32:13 · 2818 阅读 · 0 评论 -
写一个c++类 time对象
题目:1、设计一个名为Time的类2、定义hour minute second的数据字段3、为当前时间创建一个无参对象 Time()4、创建一个有参数的构造函数,自1970年1月1日午夜以来的时间5、创建一个构造函数,用指定的小时 分钟 秒来构造一个对象6、设计能获取hour minute second的get函数7、设置一个名为settime(int elapsetime)函数,为对象设置一个新的函数8、编写代码实现这个类,创建两个time对象,一个使用无参构造函数,另一个使用time(5原创 2020-08-18 17:26:46 · 3863 阅读 · 0 评论 -
试设计一个算法,求(p^q)v(p^-q)的真值表
设计三个函数,因为c++中“与”、“或”、“非”刚好对应离散数学的三个符号,则分别用“&&”、“||”、“!” 来实现合取、析取、非的三个功能,当有两个符号时,则用上述的函数嵌套使用。#include<iostream>using namespace std;int hequ(int a, int b){ return a && b;}//合取符号的作用int xiqu(int a, int b){ return a || b;}//析取符号的原创 2020-08-18 17:15:34 · 1492 阅读 · 0 评论 -
转换运算符、优先级、生成随机数或字母(笔记)
转换运算符:static_cast(value)例:static_cast(1.7) 结果为1 static_cast(1)/2 结果为0.5bool:!(a&&b)等同于!a||b!(a||b)等同于!a&&b优先级:a++和a–±(一元加减),++a和–a,static_cast(V),(type)(Casting)!(逻辑非)乘 、/、%(乘 除 模)±(二元加减)< <= > >=(关系)== !=&&a原创 2020-08-18 17:02:23 · 138 阅读 · 0 评论 -
cmath库、ctime库、cctype库、随机生成数rand()
#include:sizeof() 来查看一个类型或者是变量在所使用的机器上所占的大小。可展示int long double 和变量age和area在当前机器上所占的大小。#include:pow(a,b) 用来计算a的b次sprt(x) 返回x的平方根exp(x) 返回e的x次log10(x)返回以10为底的对数的值log(x) 返回自然对数的值即以e为底ceil(x) x被向上取整到一个最接近它的整数。该整数为double类型的值floor(x) x被向下取整到一个最接近它的整数原创 2020-08-18 16:46:50 · 475 阅读 · 0 评论 -
ASCII码字符转换,浮点数转换为字符(笔记)
ASCII码:‘0’~‘9’ 48~57‘A’~‘Z’ 65~90‘a’~‘z’ 97~122当一个浮点数转换为一个字符类型时,浮点数先转换为int类型,然后再转换成char类型char c=65.25cout<<c //c为Aint i=‘2’+‘3’;cout<<"i is"<<i<<endl;//i is now 101int j=2+'a';cout<<"j is"<<j<<endl;原创 2020-08-18 16:29:56 · 4151 阅读 · 0 评论