上机编程题

  • 字符串到整数
  1. #include <string>
  2. #include <cstdlib>
  3. using namespace std;
  4. int StringToInt(string str) {
  5.         return atoi(str.c_str());
  6. }

复制代码



 

  • 整数到字符串
  1. #include <string>
  2. #include <cstdlib>
  3. using namespace std;
  4. string IntToString(int num) {
  5.         char str[100];
  6.         return itoa(num, str, 10);
  7. }

复制代码

进制转换

  • 十进制转其他进制
  1. #include <string>
  2. #include <cstdlib>
  3. using namespace std;
  4. string DecToOther(int dec) {
  5.         char str[100];
  6.         return itoa(dec, str, 2);//把2改为其他数字即可转换为其他进制
  7. }

复制代码

  • 其他进制转换为十进制
  1. #include <string>
  2. #include <cstdlib>
  3. using namespace std;
  4. int OtherToDec(string str) {
  5.         const char *s = str.c_str();
  6.         char *stop;
  7.         return        strtol(s, &stop, 2);//把2改为其他数字即可由其他进制改为十进制
  8. }

复制代码

排序

  1. #include <algorithm>
  2. #include <vector>
  3. using namespace std;
  4. struct Node {
  5.         int x;
  6.         int y;
  7. };
  8. //如果x不相等则按x降序排列,x相等则按y升序排列
  9. bool com(Node &a, Node &b) {
  10.         if (a.x != b.x) return a.x > b.x;
  11.         else return a.y < b.y;
  12. }
  13. int main() {
  14.         vector<Node> vec;
  15.         //...给vec添加数据,略
  16.         sort(vec.begin(), vec.end(), com);
  17.         return 0;
  18. }

复制代码

string容器实用函数

  1. #include <iostream>
  2. #include <string>
  3. #include <iterator>
  4. using namespace std;
  5. int main() {
  6.         //迭代器访问等基础不总结了,0基础的同学可以参考《算法笔记》
  7.         string a = "xxxyyy";
  8.         string b = "zzz";
  9.         string s2 = a.substr(3, 2);//取子串。s2为"yy"
  10.         string::size_type n = a.find("yy");//n为3
  11.         a.find("yyx");//返回string::npos
  12.         string s3 = a.replace(0, 3, "fff");//s3为"fffyyy"
  13.         string::iterator p = a.insert(a.begin(), '_');//返回成功插入后指向'_'的迭代器
  14.         a.insert(a.find('y'), "_str_");//在第一个找到'y'的下标前面插入一个指定字符串"_str_"
  15. }

复制代码

set容器的使用

  1. #include <set>
  2. using namespace std;
  3. int main() {
  4.         set<int> s;
  5.         for (int i = 0; i < 5; ++i) {
  6.                 s.insert(i*i);//添加元素
  7.         }
  8.         s.find(4);//查找返回迭代器,找不到则返回end()
  9.         s.erase(4);//删除
  10.         return 0;
  11. }

复制代码

map容器的使用

  1. #include <iostream>
  2. #include <map>
  3. #include <iterator>
  4. using namespace std;
  5. int main() {
  6.         map<char, int> mp;
  7.         mp['c'] = 1;//添加元素
  8.         mp['b'] = 2;
  9.         mp['a'] = 3;
  10.         map<char, int>::iterator p = mp.begin();//使用迭代器遍历
  11.         for (; p != mp.end(); ++p) {
  12.                 //输出顺序是a,b,c,因为map使用红黑树实现,自动排序的
  13.                 cout << p->first << " " << p->second << endl;
  14.         }
  15.         cout << mp['b'] << endl;//使用下标访问,输出2
  16.         map<char, int>::iterator it = mp.find('b');//it指向<'b',1>
  17.         cout << it->second << endl;//输出2
  18.         mp.erase(it);//删除it指向的<'b',1>
  19.         mp.erase('a');//删除<'a',3>,只剩下<'c',1>
  20.         cout << mp.size() << endl;
  21.         return 0;
  22. }

复制代码

队列的使用

  1. #include <iostream>
  2. #include <queue>
  3. #include <iterator>
  4. using namespace std;
  5. int main() {
  6.         queue<int> q;
  7.         for (int i = 1; i <= 5; ++i) {
  8.                 q.push(i);//入队
  9.         }
  10.         if (q.empty()) {
  11.                 cout << q.front() << " ";
  12.         }
  13.         cout << q.back() << endl;//输出队首1和队尾5
  14.         if (q.empty()) {
  15.                 q.pop();//队首1出队
  16.         }
  17.         return 0;
  18. }

复制代码

控制小数点后精度位数及补齐整数位数

  1. #include <cstdio>
  2. #include <cstdlib>
  3. int main() {
  4.         int n = 9;
  5.         double pi = 3.1415926;
  6.         printf("%02d\n", n);//输出09
  7.         printf("0.3f\n", pi);//输出3.142
  8.         return 0;
  9. }

复制代码

cctype头文件中处理字符的函数

文件操作(19年考了,20基本不会考)

  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <vector>
  5. using namespace std;
  6. int main() {
  7.         //打开文件,清空内容,重新写入。ofstream::out指示以写模式打开
  8.         ofstream out1("D\\abc.txt", ofstream::out);
  9.         string s;
  10.         while (getline(cin, s)) {
  11.                 out1 << s << endl;
  12.         }
  13.         out1.close();
  14.         //打开文件,向文件追加内容。
  15.         //ofstream::out指示以写模式打开,ofstream::app指示写操作前定位到文件末尾
  16.         ofstream out2("D\\abc.txt", ofstream::out | ofstream::app);
  17.         while (getline(cin, s)) {
  18.                 out2 << s << endl;
  19.         }
  20.         out2.close();
  21.         //读取文件
  22.         ifstream in("D\\abc.txt");
  23.         vector<string> vecs;
  24.         while (getline(in, s)) {
  25.                 vecs.push_back(s);//保存到vector方便后续处理。若只需打印则无需保存。
  26.         }
  27.         in.close();
  28.         return 0;
  29. }

复制代码

求最大公约数

  1. int gcd(int a, int b) {
  2.         if (b == 0)return a;
  3.         else return gcd(b, a%b);
  4. }

复制代码

求最小公倍数
求得A和B的最大公约数是C,则最小公倍数是A*B / C(防止溢出可以写为A/C * B)
函数指针

  1. //声明一个函数指针类型ptask,该型指针指向一个函数,该函数返回void,输入参数为空。
  2. typedef void(*ptask)();
  3. void task() {
  4.         //...
  5. }
  6. int main() {
  7.         ptask p = task;
  8.         p();//通过函数指针调用函数
  9.         return 0;
  10. }

复制代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wangchuang2017

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值