1.C++中,可以使用getline函数输入一行字符(含空白符);getline遇到换行符、文件结束符(EOF)或读取错误时停止。
输入一个字符串。-----注意输入形式 判断是否是回文串
2.当使用cin
读取键盘输入时,遇到空格、制表符或回车键时停止读取。这意味着如果输入的字符串中包含空格,cin
只会读取并输出空格之前的字符。
#include <iostream>
#include <string>
#include <string.h>
#include <istream>
using namespace std;
class Thesis
{
public:
void input();
void output();
int n,y,r;
string sheng,lun,xue;
char wei;
};
void Thesis::input()
{
getline(cin,sheng);
getline(cin,lun);
getline(cin,xue);
cin>>n>>y>>r;
cin>>wei;
}
void Thesis::output()
{
char s[100];
if(wei=='b')
strcpy(s,"Bachelor");
else if(wei=='m')
strcpy(s,"Master");
else
strcpy(s,"Doctor");
cout<<sheng<<';'<<lun<<';'<<xue<<';'<<n<<'-'<<y<<'-'<<r<<';'<<s<<endl;
}
int main()
{
Thesis thesis;
thesis.input();
thesis.output();
return 0;
}
//输入:
Zhang San
Design and Implementation of Management system
Shandong University of Science and Technology
2024 9 8
b
//输出:
Zhang San;Design and Implementation of Management system;Shandong University of Science and Technology;2024-9-8;Bachelo
****当输入的数据类型与程序所需要的数据类型不匹配时,看起来好像是给程序中的这个数据赋值****0,然后结束输入(老师说不会结束输入,有待商榷)-------->只需要知道出现一些莫名其妙的数的时候有可能是因为类型传错了
3.判断是否是奇数(偶数)别忘了考虑0
bool Integer::isOdd()
{
if(m%2!=0)
return true;
return false;
}
4.判断是否是素数别忘了单独考虑0、1
bool Integer::isPrime()
{
if(m==1)
return false;
for(int i=2;i<m;i++)
{
if(m%i==0)
return false;
}
return true;
}
5.
输出格式用头文件<iomanip>中流操作算子:
setw(w) :设置数据的输出宽度为w个字符
setfill(c):设置用字符c作为填充字符
#include <cmath> //sqrt()
#include <iomanip>fixed<<setprecision(2) //只作用于后面的一个数
6.当输出’+‘时,这个东西要由她后面的那一项是否为0来控制;
7.无论输出什么(eg:高阶多项式),一定要考虑只输出0的情况
void Equation::print()
{
int cnt=0;
int tmp=0;
for(int i=w;i>=0;i--)
{
if(b[i]!=0&&cnt==0)
{
tmp=1;
cnt=1;
if(i==0)
{
cout<<b[i];
break;
}
//注意这个if和else里面都要看看是否输出x^1的1
if(b[i]!=1)
{
if(i!=1)
cout<<b[i]<<"x^"<<i;
else
cout<<b[i]<<"x";
}
else if(b[i]==1)
{
if(i!=1)
cout<<"x^"<<i;
else
cout<<"x";
}
}
else if(b[i]!=0&&cnt==1)
{
tmp=1;
if(i==0)
{
cout<<"+"<<b[i];
break;
}
if(b[i]!=1)
{
if(i!=1)
cout<<"+"<<b[i]<<"x^"<<i;
else
cout<<"+"<<b[i]<<"x";
}
else if(b[i]==1)
{
if(i!=1)
cout<<"+x^"<<i;
else
cout<<"+x";
}
}
}
if(tmp==0)
cout<<"0";
cout<<endl;
}
8.要打印“”,要用转义字符
cout<<"A person whose name is \""<<name<<"\" and age is "<<age<<" is created!"<<endl;
9.
/*1. 一个字符数组或字符指针,用于存储字符串内容。
2. void input():读取一个不含空白符的字符串。
3. void output():输出字符串。如果字符串不是回文串,则输出原串。如果是回文串,则仅输出前半部分。如:
原串:abccba,则输出abc
原串:abcdcba,则输出abcd
原串:abcd,则输出abcd*/
#include <iostream>
#include <string>
using namespace std;
class MyString
{
private:
string a;
public:
void input();
void output();
};
void MyString::input()
{
cin>>a;
int i=0,j=(a.size()-1);
for(;i<j;i++)
{
if(a[i]==a[j])
{
a.erase(j);
}
else
{
break;
}
j--;
}
}
void MyString::output()
{
cout<<a<<endl;
a.erase(0);//删除下标0及之后的
}
int main()
{
MyString str;
int n, i;
cin>>n;
for (i = 0; i < n; i++)
{
str.input();
str.output();
}
return 0;
}
10.com2.setName(str).print();像这种链式访问,那么setName的返回值必须是类
Computer& setName(char* a)
{
name=a;
}
11.int Computer::cnt=0;//静态成员变量必须在类外初始化
12.传引用,就相当于把地址传过去了,可以改变这个数本身的值
//传引用
#include <iostream>
using namespace std;
void get_num(int &a,int &b)
{
cin>>a>>b;
}
void put_sum(int &a,int &b)
{
cout<<(a+b)<<endl;
}
int main()
{
int a, b;
get_num(a, b);
put_sum(a, b);
}
13.注意输出数据的正负,判断是否需要只输出正值
14.判断回文素数
static bool judge(int i)
{
int m=i;
int n=1;
int tmp=0;
int cnt=1;
while(m)
{
n=m%10;
arr[tmp]=n;
tmp++;
m/=10;
}
for(int j=0,k=tmp-1;j<=k;j++)
{
if(arr[j]!=arr[k])
cnt=0;
k--;
}
if(cnt==0)
return false;
for(int j=2;j<=sqrt(i);j++)
{
if(i%j==0)
return false;
}
if(cnt==1)
return true;
}
15.字符强转整型转成的是acs码值
题目中没有说字母可以大写,但是我们必须考虑进去
千万不要忘了考虑负数
char arr[100000];
Integer(char *p,int b)
{
int n=0;
int fu=0;
int tmp=0;
if(p[0]=='-')
fu=1;
else
fu=0;
for(int i=0; p[i]!='\0'; i++)
{
arr[i]=p[i];
tmp=i;//代表最后一位数所在下表
}
int k=0;
for(int i=0; i<tmp; i++)
{
if(arr[i]>='0'&&arr[i]<='9')
{
k=(int)(arr[i])-48;
// cout<<k<<endl;
}
else if(arr[i]>='a'&&arr[i]<='z')
{
k=((int)(arr[i])-97)+10;
// cout<<k<<endl;
}
else if(arr[i]>='A'&&arr[i]<='Z')
{
k=((int)(arr[i]-65)+10);
}
n=(n+k)*b;//这个地方很关键,循环加乘
}
//把最后一位单独拿出来
if(arr[tmp]>='0'&&arr[tmp]<='9')
{
k=(int)(arr[tmp])-48;
// cout<<k<<endl;
}
else if(arr[tmp]>='a'&&arr[tmp]<='z')
{
k=((int)(arr[tmp])-97)+10;
// cout<<k<<endl;
}
else if(arr[tmp]>='A'&&arr[tmp]<='Z')
{
k=((int)(arr[tmp]-65)+10);
}
n+=k;
if(fu==1)
n=(-1)*n;
}
16.
//c++语言不能判断哪个函数会对数据成员进行修改,所以一旦定义const对象,所有成员函数都无法访问问。但是可以作为返回值返回。
//若主函数定义了const对象,那么这个对象直接调用的和间接调用的都必须是const函数const passing 'const xxxx' as 'this' argument discards qualifiers否则就出现这一行报错
//为了能够使const对象能够访问成员函数,可以在不改变数据成员的成员函数后面加上const,这并不代表将函数变成const型,只是const对象能够使用这些成员函数
//另外,即使给改变数据成员的成员函数加上const,const对象也不能访问这些函数
//对象作为参数时最好都加上const
17.
//no matching function to call to注意函数重载
18.
当发现调用同一个构造函数时,一个传了一个参数,另一个传了两个参数,就要意识到该使用缺省值了。缺省值要从后往前给
19.传一个参两个数都得到怎么写------即想办法确定一个参考值,用这个参考值为多少来确定每个参数的值到底是多少
#include <iostream>
#include <iomanip>
using namespace std;
class Point
{
private:
double x,y;
public:
Point(double a=0.0,double b=0.0)
{
x=a;
y=b;
if(y==0.0)
y=x;
cout<<"Point : ("<<setprecision(16)<<x<<", "<<y<<") is created."<<endl;
}
};
int main()
{
double a=0.2, b=3.5;
Point q;
Point p(a, b);
Point q1(q), q2(1);
}
20.先调用父类的组合的构造函数,再调用父类的构造函数,之后调用子类的组合的构造函数,最后调用子类自身的构造函数;组合对象要在成员初始化列表初始化,不在这初始化可能会出问题。
21.先观察main函数,再确定类中的成员是什么数据类型
22.一定要设计的合理一些,要学会包装,不要一个类中创建的变量,再在另一个类中创建同样作用的变量,此时可以利用组合或者继承
23.输出很多行数据的时候,要记得检查空格(输出格式)
24.传参时不加引用,产生拷贝,产生的这份拷贝在运行完这个拷贝构造函数就会析构,不会等到最后。
25.
缺省构造函数,将属性初始化为0,并输出"Data's default constructor."。
Data()
{
da=0;
cout<<"Data's default constructor."<<endl;
}
带参构造函数,将属性初始化为指定参数,并输出"Data # is created.",其中“#”即参数值。
Data(double _da)
{
da=_da;
cout<<"Data "<<da<<" is created."<<endl;
}
26.类中定义指针时一定要在析构函数中释放指针
Data* arr;--------->>arr=new Data[10];----------->>delete []arr;
//一定要开辟空间!!!!!!!(容器要注意这个位置只有放进去数据了才能进行下标操作)
//不开辟空间的话程序只能输进去一个数据就自动结束了或者一直停留在输入
指针只能用箭头访问其中的成员!!!
27.数组作为参数的写法
DataArray(int _n,int _tmp[])
{
dd=_n;
arr=new Data[dd];
for(int i=0;i<dd;i++)
{
arr[i].da=_tmp[i];
}
cout<<"DataArray's constructor."<<endl;
}
28.有一个类A,A中有元素a。有一个类B,B中有A* arr;,如果要在类B中通过arr访问a,那么需要在类A的public中声明友元类B:friend class B;,这样就可以访问了。
29.
Rectangle& aa()//能链式访问
{
return *this(对象名也可以);
}
Rectangle* aa()//不能链式访问
{
return this;
}
30.//题目中说多组输入就用while不然不对
31.//注意容器清空
32.vector
*****(区间操作一般是左闭右开)
操作1:Build a b (产生一个大小为a的线性表,其值全部赋为b,每组样例仅出现一次,在起始行)
vector之assign()函数
一、assign()函数的两种函数原型
1.第一种函数原型
void assign(const_iterator first,const_iterator last);
这个函数原型相当于拷贝函数,把first到last的值赋值给调用者,注意区间的闭合;
将区间 [first,last) 的元素赋值到当前的 vector 容器中;[)这个是左闭右开区间,表示左边的元素包括,右边的元素不包括;
2.第二种函数原型
void assign(size_type n,const T& x = T());
这个函数原型是把n个x赋值给调用者;也就是赋值n个x的元素到vector容器中,并且清除掉vector容器中以前的内容;
操作2:Modify a b (将线性表的第a个元素的值设为b)
下标直接访问
操作3:Insert a b c (在线性表的第a个位置插入第b到第c个位置的所有元素)
//iterator insert(pos,elem) 在迭代器 pos 指定的位置之前插入一个新元素elem,并返回表示新插入元素位置的迭代器。
//iterator insert(pos,n,elem) 在迭代器 pos 指定的位置之前插入 n 个元素 elem,并返回表示第一个新插入元素位置的迭代器。
//iterator insert(pos,first,last) 在迭代器 pos 指定的位置之前,插入其他容器(不仅限于vector)中位于 [first,last) 区域的所有元素,并返回表示第一个新插入元素位置的迭代器。
//iterator insert(pos,initlist) 在迭代器 pos 指定的位置之前,插入初始化列表(用大括号{}括起来的多个元素,中间有逗号隔开)中所有的元素,并返回表示第一个新插入元素位置的迭代器。
操作4:Erase a b(删除线性表第a到第b个位置的所有元素)
iterator erase(iterator position);//删除单个位置
iterator erase(iterator first, iterator last);//删除一段注意:erase操作传入迭代器,迭代器所指位置在删除前后不发生改变,改变的只是容器中元素值。删除该元素后,被删元素后面的所有元素复制到被删元素位置上,尾部迭代器也移动到新的尾部位置。
//用迭代器去除某个元素
for (vector<int>::iterator iter = tmp.begin(); iter != tmp.end();)
{
if (*iter == 1)
iter = tmp.erase(iter);
else
iter++;
}
操作5:Print a b (输出线性表的第a到第b个元素)
if(s=="Build")
{
cin>>m>>n;
a.assign(m,n);
}
else if(s=="Modify")
{
cin>>m>>n;
a[m-1]=n;
}
else if(s=="Insert")
{
cin>>m>>n>>k;
a.insert(a.begin()+m-1,a.begin()+n-1,a.begin()+k);
}
else if(s=="Erase")
{
cin>>m>>n;
a.erase(a.begin()+m-1,a.begin()+n);
}
else if(s=="Print")
{
cin>>m>>n;
for(int i=m-1;i<n;i++)
{
if(i==m-1)
cout<<"["<<i+1<<"]:"<<a[i];
else
cout<<" ["<<i+1<<"]:"<<a[i];
}
cout<<endl;
}
操作六:排序(按照字母序)
string s;
vector<string> str;
for(int i=0;i<n;i++)
{
cin>>s;
str.push_back(s);
}
sort(str.begin(),str.end());
vector<string>::iterator it;
for(it=str.begin();it!=str.end();it++)
{
cout<<(*it)<<endl;
}
33.set
总结:
set是按照一定次序存储元素的容器
在set中,元素的value也标识它(value就是key,类型为T),并且每个value必须是唯一的。set中的元素不能在容器中修改(元素总是const),但是可以从容器中插入或删除它们。!!!!!!!!!!!!!
在内部,set中的元素总是按照其内部比较对象(类型比较)所指示的特定严格弱排序准则进行排序。(前面的比后面的严格小)
set容器通过key访问单个元素的速度通常比unordered_set容器慢,但它们允许根据顺序对子集进行直接迭代。
set在底层是用二叉搜索树(红黑树)实现的。
注意事项:
与map/multimap不同,map/multimap中存储的是真正的键值对<key, value>,set中只放value,但在底层实际存放的是由<value, value>构成的键值对。
set中插入元素时,只需要插入value即可,不需要构造键值对。
set中的元素不可以重复(因此可以使用set进行去重)。!!!!!!!!!!!
使用set的迭代器遍历set中的元素,可以得到有序序列!!!!!!!!!!!!
set中的元素默认按照小于来比较
set中查找某个元素,时间复杂度为:log2N
set中的元素不支持修改 底层是一个const类型的迭代器;!!!!!!!!!!!!
set中的底层使用二叉搜索树(红黑树)来实现。
upper_bound确定范围比key大的,不包括key,相当于数学里的开区间
lower_bound确定的范围是大于key的,相当于数学里的闭区间
#include <iostream>
#include <set>
#include <iterator>
#include <algorithm>//是那一堆很复杂的set.......的头文件
using namespace std;
void print(const set<int>& a)
{
set<int>::iterator it;
cout<<"{";
for(it=a.begin();it!=a.end();it++)
{
if(it==a.begin())
cout<<*it;
else
cout<<", "<<*it;
}
cout<<"}"<<endl;
}
int main()
{
set<int> a;
set<int> b;
set<int> c,d,e,f,g;
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Case# "<<i+1<<":"<<endl;
int f1,f2,e1,e2;
cin>>f1;
for(int j=0;j<f1;j++)
{
cin>>e1;
a.insert(e1);
}
cin>>f2;
for(int j=0;j<f2;j++)
{
cin>>e2;
b.insert(e2);
}
cout<<"A = ";
print(a);
cout<<"B = ";
print(b);
cout<<"A u B = ";
set_union(a.begin(),a.end(),b.begin(),b.end(),inserter(c,c.begin()));
print(c);
cout<<"A n B = ";
set_intersection(a.begin(),a.end(),b.begin(),b.end(),inserter(d,d.begin()));
print(d);
cout<<"A - B = ";
set_difference(a.begin(),a.end(),b.begin(),b.end(),inserter(e,e.begin()));
print(e);
cout<<"SA = ";
set_difference(c.begin(),c.end(),a.begin(),a.end(),inserter(f,f.begin()));
print(f);
cout<<"SB = ";
set_difference(c.begin(),c.end(),b.begin(),b.end(),inserter(g,g.begin()));
print(g);
a.clear();
b.clear();
c.clear();
d.clear();
e.clear();
f.clear();
g.clear();
}
}
#include <iostream>
#include <set>
#include <algorithm>
//#include <stdio.h>//EOF的头文件
#include <iterator>
using namespace std;
int main()
{
set<unsigned long long> a,b,c,d;
unsigned long long n;
while(cin>>n)//注意循环输入的写法
{
a.clear();
b.clear();
c.clear();
d.clear();
if(n!=0)
{
a.insert(n);
while(1)
{
cin>>n;
if(n==0)
break;
a.insert(n);
}
}
//当第一行只有一个0,第二行还有数据的时候
while(1)
{
cin>>n;
if(n==0)
break;
b.insert(n);
}
set_difference(a.begin(),a.end(),b.begin(),b.end(),inserter(c,c.begin()));
set_difference(b.begin(),b.end(),a.begin(),a.end(),inserter(d,d.begin()));
if(c.empty()&&d.empty())
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
}
(ps:一定要注意即使清空栈!!!!!!!!!!!!!!!)
//清空栈
#include <iostream>
#include <stack>
#include <string.h>
using namespace std;
int main()
{
stack<char> str;
char a[105];
while(cin>>a)//注意这个可以直接读取一行字符(不含空格的写法)//每次可以自己自动覆盖上一次的
{
int s;
s=strlen(a);
int m=s;
for(int i=0; i<s; i++)
{
if(str.empty())
{
str.push(a[i]);
}
else
{
if(str.top()=='('&&a[i]==')')
{
str.pop();
m-=2;//一下子两个括号没进栈
}
else
{
str.push(a[i]);
}
}
}
if(str.empty())
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
// while(1)
// {
// if(str.empty())
// break;
// else
// str.pop();
// }
//栈一定要及时pop,stack库中没有直接的clear函数
while(m>0)
{
str.pop();
m--;
}
}
}
34.map
注意map的用法
#include <iostream>
#include <map>
//map自动按照字母序排序
using namespace std;
int main()
{
int m,n;
string s;
map<string,int> str;
//注意输入多组数据----------------------
while(cin>>m)
{
str.clear();
while(m--)
{
cin>>s>>n;
str[s]+=n;//相当于往map中放入名字和数目
}
map<string,int>::iterator it;
{
for(it=str.begin(); it!=str.end(); it++)
{
cout<<it->first<<":"<<it->second<<endl;
}
}
}
}
//map中的map,使用方法----------------------------------------------------------
//#include<iostream>
//#include<map>
//#include<string>
//using namespace std;
//map<string,map<string,int> > mp; //第二个值表示水果和数量
//string s1,s2;
//int d;
//int main(){
// int n;
// cin >> n;
// for(int i = 0;i < n; i++){
// cin >> s1 >> s2 >> d;
// mp[s2][s1] += d;
// }
// for( map<string,map<string,int> >::iterator it1=mp.begin(); it1!=mp.end(); it1++){
// cout<< (it1->first) <<endl;
// for( map<string,int>::iterator it2=(it1->second).begin(); it2!=(it1->second).end(); it2++){
// cout<< " |----"<<(it2->first)<<"("<<(it2->second)<<")" <<endl;
// }
// }
// return 0;
//}
35.queue
STL中的queue容器。
关于队列的知识;
使用queue之前,要先利用构造函数一个队列对象,才可以进行元素的入队,出队,取队首和队尾等操作;
(1).queue() queue<int> q; 或者 queue<int>Q[10000];
(2).queue(const queue&) 复制构造函数
例如:用一行代码利用queue对象q1,创建一个以双向链表为底层容器的queue对象q2
queue<int,list<int>>q1;
queue<int,list<int>>q2(q1);
(3).元素入队 函数为:push()例如:q.push(3),意思是将3入队 ,注意队列的大小不预设
(4).元素出队 函数为:pop()例如:q.pop()
(5)。取对头元素 函数为:front()
(6),取队尾元素 函数为:back()
(7)。判断对空 函数为:empty()
(8)。队列的大小 函数为:size()返回队列的当前元素的个数
例题:
#include <iostream>
#include <queue>
using namespace std;
queue<int> p[10001];
int main()
{
int k,n;
while(cin>>k)
{
cin>>n;
for(int i=0;i<n;i++)
{
string s;
cin>>s;
if(s=="INIT")
{
for(int j=0;j<n;j++)
{
while(!p[j].empty())
p[j].pop();
}
}
else if(s=="PUSH")
{
int m,g;
cin>>m>>g;
p[m].push(g);
}
else if(s=="POP")
{
int m;
cin>>m;
if(!p[m].empty())
{
cout<<p[m].front()<<endl;
p[m].pop();
}
else
cout<<"NULL"<<endl;
}
}
}
}
#include <iostream>
#include <queue>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
queue<int> p[10002];
string s;
int i,v,t,n,m;
while(cin>>n)//一定要注意多组循环输入
{
cin>>m;
for(int g=0; g<m; g++)
{
cin>>s;
if(s=="INIT")
{
for(int j=1; j<=n; j++)
{
while(!p[j].empty())
{
p[j].pop();
}
}
}
else if(s=="PUSH")
{
cin>>i>>v>>t;
while(t--)
{
p[i].push(v);
}
}
else if(s=="POP")
{
cin>>i>>t;
if(p[i].empty())
cout<<"NULL"<<endl;
else
{
while(t--)
{
cout<<p[i].front()<<endl;
p[i].pop();
}
}
}
}
}
}
36.虚继承时,D的基类是BC,BC的基类是A,在D中也要给A显式初始化:A(_x)。
多继承问题(虚继承) <----> 多态问题(虚函数)
37.返回值一般都写成&,传的参都写成const &
38.#include <algorithm>//find在里面
39.创建一个tmp记录需要输入输出多少个
40.严谨考虑谁大谁小;不能仅考虑十以内的情况
41.排序一定要学会用sort
T* str;
sort(str,str+cnt,greater<T>());//从大到小;
sort(str,str+cnt);//从小到大
42.
for(int it=str.size()-2; it>=0; it--)
{
sum+=sqrt(x,ant)*str[it];//sqrt(x,ant)表示x的ant次方
//sum=sum*x+str[n-i]//i=n~0//chq强烈推荐
ant++;
}
43.打印多项式
void print()
{
bool r1=false;
for(int i=0; i<=m; i++)
{
if(a[i]==0)
continue;
if(a[i]<0)
cout<<'-';
else if(r1)
cout<<'+';
r1=true;
if(a[i]!=1&&a[i]!=-1)
cout<<(a[i]>0?a[i]:-a[i]);
if((a[i]==1||a[i]==-1)&&i==m)
cout<<1;
if(m-i>=1)
cout<<'x';
if(m-i>1)
cout<<'^'<<m-i;
}
if(r1==false)//!!!!!!!!!!!!!!!!!!!!!!!!!
cout<<"0";
cout<<endl;
}
44.不要用vector的reverse
45.
46.可以正向反向找规律,能正确输出就行
47.多组输入输出:用变量来控制输出(指针)/直接清空(容器)
48.pow函数在#include <cmath>
49.输出表时,表头的输出格式要和内容一样,因为是一体的,不能图省事
50.矩阵运算
//在新定义的对象cc中也要给他赋值m和n,不然输出的时候没有m和n
#include <iostream>
#include <vector>
using namespace std;
class Matrix
{
private:
vector<vector<int> > str;
int m,n;
int tmp;
public:
Matrix operator+(Matrix& ano)
{
Matrix cc;//AB还要用,必须新定义对象
cc.m=m;
cc.n=n;
cc.str.resize(m,vector<int>(n));
// cout<<"*";
if((m==ano.m)&&(n==ano.n))
{
cc.tmp=1;
for(int l=0; l<m; l++)
{
for(int j=0; j<n; j++)
{
cc.str[l][j]=str[l][j]+ano.str[l][j];
}
}
return cc;
}
else
{
cc.tmp=0;
return cc;
}
}
Matrix operator*(Matrix& ano)
{
Matrix cc;
cc.m=m;
cc.n=ano.n;
cc.str.resize(m,vector<int>(ano.n));
if(n==ano.m)
{
cc.tmp=1;
int sum=0;
for(int l=0; l<m; l++)
{
for(int j=0; j<ano.n; j++)
{
sum=0;
for(int k=0;k<n;k++)
sum+=str[l][k]*ano.str[k][j];
cc.str[l][j]=sum;
}
}
return cc;
}
else
{
cc.tmp=0;
return cc;
}
}
friend istream& operator>>(istream& is,Matrix& mm);
friend ostream& operator<<(ostream& os,const Matrix& mm);
};
istream& operator>>(istream& is,Matrix& mm)
{
int data;
is>>mm.m>>mm.n;
mm.str.resize(mm.m);
for(int l=0; l<mm.m; l++)
{
for(int j=0; j<mm.n; j++)
{
cin>>data;
mm.str[l].push_back(data);
}
}
return is;
}
ostream& operator<<(ostream& os,const Matrix& mm)
{
if(mm.tmp==0)
os<<"Error"<<endl;
else
{
for(int l=0; l<mm.m; l++)
{
for(int j=0; j<mm.n; j++)
{
if(j==0)
os<<mm.str[l][j];
else
os<<" "<<mm.str[l][j];
}
os<<endl;
}
}
return os;
}
int main()
{
int cases, i;
cin>>cases;
for (i = 0; i < cases; i++)
{
Matrix A, B, C, D;
cin>>A>>B;
C = A + B;
// cout<<"+";
D = A * B;
// cout<<"*";
cout<<"Case "<<i + 1<<":"<<endl;
cout<<C<<endl;
cout<<D;
}
return 0;
}
51.把str中等于a的数字全部删掉(可能不止一个)
class Array
{
private:
int *str;
int sum;
int cnt;
public:
Array operator-(int a)
{
for(int i=0; i<sum; i++)
{
if(str[i]==a)
{
cnt--;
for(int j=i; j<sum; j++)
{
str[j]=str[j+1];
}
sum--;
i=-1;//!!!!!!!!!!!!!!!!!!!!!!!!!很牛
}
}
return *this;
}
friend istream& operator>>(istream& is,Array& mm);
friend ostream& operator<<(ostream& os,const Array& mm);
};
52.默认构造函数很重要(否则输出很可能是乱码)
53.注意if判断条件(判断开头的放前面,判断结束条件时放最后面)
54. m=getchar();//cin不能读取换行,getchar可以
55. s.push_back((int)(m-'0'));//int强制类型转换:1->49
56.
cout << " Annual Salary is " << person->pay() << "." << endl;
//这个pay函数得有返回值才行
57.typeid
//Problem G: 驾驶员与汽车
#include <iostream>
#include <iomanip>
#include <typeinfo>
using namespace std;
class Automobile
{
protected:
double speed;
public:
virtual void run() const = 0;
virtual~Automobile()//只有这个是虚析构才能调用子类中的析构函数
{
cout<<"An automobile is erased!"<<endl;
}
};
class Benz:public Automobile
{
private:
double speed;
public:
Benz(double s=0.0)
{
speed=s;
}
void run() const
{
cout<<"Benz at speed of "<<fixed<<setprecision(2)<<speed<<"km/h."<<endl;
}
~Benz()
{
cout<<"A Benz is erased!"<<endl;
}
};
class Buick:public Automobile
{
private:
double speed;
public:
Buick(double s=0.0)
{
speed=s;
}
void run() const
{
cout<<"Buick at speed of "<<fixed<<setprecision(2)<<speed<<"km/h."<<endl;
}
~Buick()
{
cout<<"A Buick is erased!"<<endl;
}
};
class Zhongba:public Automobile
{
private:
double speed;
public:
Zhongba(double s=0.0)
{
speed=s;
}
void run() const
{
cout<<"Zhongba at speed of "<<fixed<<setprecision(2)<<speed<<"km/h."<<endl;
}
~Zhongba()
{
cout<<"A Zhongba is erased!"<<endl;
}
};
class Beiqi:public Automobile
{
private:
double speed;
public:
Beiqi(double s=0.0)
{
speed=s;
}
void run() const
{
cout<<"Beiqi at speed of "<<fixed<<setprecision(2)<<speed<<"km/h."<<endl;
}
~Beiqi()
{
cout<<"A Beiqi is erased!"<<endl;
}
};
class Dayu:public Automobile
{
private:
double speed;
public:
Dayu(double s=0.0)
{
speed=s;
}
void run() const
{
cout<<"Dayu at speed of "<<fixed<<setprecision(2)<<speed<<"km/h."<<endl;
}
~Dayu()
{
cout<<"A Dayu is erased!"<<endl;
}
};
class Jianghuai:public Automobile
{
private:
double speed;
public:
Jianghuai(double s=0.0)
{
speed=s;
}
void run() const
{
cout<<"Jianghuai at speed of "<<fixed<<setprecision(2)<<speed<<"km/h."<<endl;
}
~Jianghuai()
{
cout<<"A Jianghuai is erased!"<<endl;
}
};
class Driver
{
private:
string name;
char type;
public:
Driver(string n,char t)
{
name=n;
type=t;
}
void Drive(Automobile *automobile);
};
void Driver::Drive(Automobile *automobile)
{
switch (type)
{
case 'A':
cout<<"Driver "<<name<<" can drive ";
automobile->run();
break;
case 'B':
if (typeid(*automobile) == typeid(Dayu) || typeid(*automobile) == typeid(Jianghuai))
cout<<"Driver "<<name<<" cannot drive large bus."<<endl;
else
{
cout<<"Driver "<<name<<" can drive ";
automobile->run();
}
break;
case 'C':
if (typeid(*automobile) != typeid(Benz) && typeid(*automobile) != typeid(Buick))
cout<<"Driver "<<name<<" cannot drive bus."<<endl;
else
{
cout<<"Driver "<<name<<" can drive ";
automobile->run();
}
break;
}
}
int main()
{
string name;
char type;
double speed;
char automobileType;
int cases;
Automobile *automobile;
cin>>cases;
for (int i = 0; i < cases; i++)
{
cin>>name>>type>>automobileType>>speed;
Driver driver(name, type);
switch (automobileType)
{
case 'a':
automobile = new Benz(speed);
break;
case 'b':
automobile = new Buick(speed);
break;
case 'c':
automobile = new Zhongba(speed);
break;
case 'd':
automobile = new Beiqi(speed);
break;
case 'e':
automobile = new Dayu(speed);
break;
case 'f':
automobile = new Jianghuai(speed);
break;
}
driver.Drive(automobile);
delete automobile;
}
return 0;
}
//Problem H: 让动物们叫起来吧!
#include <iostream>
using namespace std;
class Animal
{
protected:
string name;
public:
virtual void sound()=0;
};
class Cock:public Animal
{
public:
Cock(string s)
{
name=s;
}
void sound()
{
cout<<name<<" is a cock, and it can crow."<<endl;
}
};
class Turkey:public Animal
{
public:
Turkey(string s)
{
name=s;
}
void sound()
{
cout<<name<<" is a turkey, and it can gobble."<<endl;
}
};
class Duck:public Animal
{
public:
Duck(string s)
{
name=s;
}
void sound()
{
cout<<name<<" is a duck, and it can quack."<<endl;
}
};
//这个写法只弄了一个指针,不断改变指针指向的东西
int main()
{
int cases;
string name;
char type;
Animal *animal;
cin>>cases;
for (int i = 0; i < cases; i++)
{
cin>>name>>type;
switch(type)
{
case 'A':
animal = new Duck(name);
break;
case 'B':
animal = new Turkey(name);
break;
case 'C':
animal = new Cock(name);
break;
}
animal->sound();
}
return 0;
}
//Problem I: 来开个书店吧
#include <iostream>
#include <iomanip>
#include <typeinfo>//typeid的头文件
using namespace std;
class Publication
{
protected:
double price;
int length;
public:
Publication(double p=0.0,int l=0)
{
price=p;
length=l;
cout<<"Call Publication's constructor!"<<endl;
}
virtual double getTotalPrice()=0;
double getPrice()const
{
return price;
}
int getLength()
{
return length;
}
virtual~Publication()
{
cout<<"Call Publication's de-constructor!"<<endl;
}
};
class Book:public Publication
{
public:
Book(double p=0.0,int l=0)
{
price=p;
length=l;
cout<<"Call Book's constructor!"<<endl;
}
double getTotalPrice()
{
return price*(1.0*length);
}
~Book()
{
cout<<"Call Book's de-constructor!"<<endl;
}
};
class Tape:public Publication
{
public:
Tape(double p=0.0,int l=0)
{
price=p;
length=l;
cout<<"Call Tape's constructor!"<<endl;
}
double getTotalPrice()
{
if(length%10==0)
length/=10;
else
length=length/10+1;
return price*(1.0*length);
}
~Tape()
{
cout<<"Call Tape's de-constructor!"<<endl;
}
};
// 使用typeid判断对象指针指向的实际对象的类型。
//涉及到指针必须用箭头,不能用点
class BookStore
{
private:
Publication **pubs;
int num;
public:
BookStore(Publication **p, int n)
{
pubs = new Publication*[n];
num = n;
for (int i = 0; i < n; i++)
{
if (typeid(*(p[i])) == typeid(Book))
{
pubs[i] = new Book(p[i]->getPrice(), p[i]->getLength());
}
else
{
pubs[i] = new Tape(p[i]->getPrice(), p[i]->getLength());
}
}
}
int getNumOfBook()
{
int c = 0;
for (int i = 0; i < num; i++)
{
if (typeid(*(pubs[i])) == typeid(Book))
c++;
}
return c;
}
int getNumOfTape()
{
int c = 0;
for (int i = 0; i < num; i++)
{
if (typeid(*(pubs[i])) == typeid(Tape))
c++;
}
return c;
}
~BookStore()
{
for (int i = 0; i < num; i++)
{
delete pubs[i];
}
delete[] pubs;
cout<<"Call BookStore's de-constructor!\n";
}
};
int main()
{
int cases, date;
char type;
double total,price;
Publication **pub;//相当于二维数组
cin>>cases;
pub = new Publication*[cases];
for (int i = 0; i < cases; i++)
{
cin>>type>>price>>date;
switch(type)
{
case 'B':
pub[i] = new Book(price,date);
break;
case 'T':
pub[i] = new Tape(price,date);
break;
}
}
BookStore bookStore(pub, cases);
cout<<"There are "<<bookStore.getNumOfBook()<<" books and "<<bookStore.getNumOfTape()<<" tapes.";
total = 0;
for (int i = 0; i < cases; i++)
{
total += pub[i] -> getTotalPrice();
}
cout<<" Their total price is "<<setprecision(2)<<fixed<<total<<"."<<endl;
for (int i = 0; i < cases; i++)
{
delete pub[i];
}
delete[] pub;
return 0;
}
58.setw(4)<<setfill('0')只针对一个变量,不针对一行#include <iomanip>
59.
60.
const DateTime cnt(date_time);//常对象只能调用常成员函数//所以下一行这些函数只能是常成员函数
cout << "DateTime : " << cnt.year() << " " << cnt.month() << " " << cnt.day();
61.一定要注意重名问题:函数名、变量名、参数名!!!!!!!!!!!!!!
62.
63.只有容器非空才能把数据从容器中拿出来或者pop
64.
65.用<cstdlib>下的abs()函数对整型进行操作,用<cmath>下的fabs()函数对整型进行操作。这样既保险又不容易混淆!
66.
67.菱形继承中,每一个子类必须要在初始化列表把父类、爷爷类、太爷爷类......全部初始化
68.
类模板不可以定义对象
重载输入输出必须放在类内,因为T只作用于类中
比较大小的时候初始化一个参照数
注意每个地方Data都要先创造对象才行
69.两个重载的静态成员函数getMax,分别用于求2个数的最大值和3个数的最大值。
70.
71.一定要注意题目条件,闭区间!!!!!!!!!
72.整数范围内一定不要用把一个数字反过来相乘,eg:2147483647---->7463847412--越界了;还有要注意-2147483648只会输出一个negative,剩下的数字越界了,不会输出
73.判断一个字符串是否是整型常量判断一个字符串是否是合法的用户自定义标识符
74.万能头文件 #include <bits/stdc++.h>
75.输出最小整数-2147483648,不要用他乘-1转化为正数,乘出来就越界了,用把他转化成字符串的形式
76.穿的参数都加_,防止出现与函数参数重名的情况
77.强制类型转换运算符重载、下标运算符重载、输入输出运算符重载
78.一定要把所有逻辑走一遍,很容易出现漏洞
79.时间类加减-------转化为秒很简单
80.重载输入输出必须按模板来写
81.一定要手写默认构造函数,不然可能输出乱码
82. error: cannot bind non-const lvalue reference of type 'Fract&' to an rvalue of type 'Fract'| ------------->Fract operator+=(Fract ano){}这个函数的传参不能是引用
83.指针字符串最后千万别忘记加'\0'。指针一定要开辟空间,多开一位存\0。计算string有多少位:这段代码主函数:STR h(
"Hello World!"
);
84. #include <string> (name.compare(ss.name)<0)------name<ss.name///也可以直接用<、>比较
85.用映射关系统计次数
86.
87.operator+=接收的必须是非const的左值引用 Fract& operator+=(Fract& ano);第一个引用是为了让它可以作为左值 ,不加引用就只能作右值 Fract operator[](int n)
{
return str[n];
} frsum += fr[i];
88.严格分析是否需要虚继承,不需要虚继承的千万别用,用了就报错。然后当需要在不同的子类中调用不同的函数时,就要用虚函数。有继承就要用虚析构。默认构造函数要给缺省值(根据主函数分析缺省值给什么)。虚继承千万注意把所有直接间接继承的都要在初始化列表初始化,不然那些间接继承的类中的值不对,不是我们想要的。
89.冒泡排序
90.
91.注意链式访问,前面的函数返回值得是类名加&。
92.有时候需要忽略一下输入中的某个空白字符:cin.ignore();
93.存数据的时候,千万要注意是否引用,不然你把数据存在新创建的东西中了,就不对了
94. #include <algorithm>//find在里面
95.