【算法学习笔记】04.C++中结构体定义练习(bign初步)
来源:互联网 发布:南昌淘宝大学课程表 编辑:程序博客网 时间:2024/06/05 00:17
练习基本上是照着源码打的,,主要是各种const想不清楚原因和来由。
以下是自己的代码,已存为bign.h
#include <stdio.h>#include <iostream>#include <string> //此处不用.h using namespace std;//使用命名空间std struct bign//C++中取消了tpyedef {//this 关键字表示的意思是 &x 所以用*this才能操作 不过为什么不能*this.len呢? int len,s[3000];// 定义成员变量 //定义构造函数 C++专属 bign(){len=1;memset(s,0,sizeof(s));}//定义对于数组的=运算法则 bign operator = (const char* num)//此处的const何用? {len = strlen(num);for(int i=0;i<len;i++)s[i]=num[len-1-i]-'0'; //s[len-1-i]=num[i]-'0';return *this;}//定义对于int变量的=运算法则 bign operator = (const int num){char t[3000];sprintf(t,"%d",num);*this=t;//此处已经运用了对于数组的赋值运算法则 len,s已经在内部实现 return *this;}//定义初始化方法bign(int num) {*this = num;}bign(const char* num) {*this = num;}//此处要求必须为常量数组// 定义成员函数 string str()const//此处的const表示不会在x.str()函数内部改变x的值 { string res="";//为string变量初始化for(int i=0;i<len;i++) res = (char)(s[i]+'0')+res;//res累加顺序if(res=="") res="0";return res; } //定义加法 bign operator + (const bign& b) const //返回一个bign的运算结果 { bign res;res.len=0;for(int i=0,g=0;g||i<max(len,b.len);i++){int x = g; //从上一次运算中取出累加余量if(i<len) x += s[i];if(i<b.len) x += b.s[i];res.s[res.len++]=x%10;g=x/10;}return res;}//定义乘法 bign operator * (const bign& b) const //返回一个bign的运算结果 { bign res;res.len=0;for(int i=0;i<b.len;i++){int g=0;bign tem; tem.len=0;for(int k=0;k<i;k++){//tem.s[tem.len++]=0;tem.len++;}for(int j=0;j<len;j++) {int x = g + s[j]*b.s[i]; //从上一次运算中取出累乘余量 tem.s[tem.len++]=x%10;g=x/10;} res = res + tem; }return res;} //定义比较符号bool operator < (const bign &b )const{if(len!=b.len)return len<b.len; else{for(int i=len-1;i>0;i--){if(s[i]!=b.s[i])return s[i]<b.s[i];}} }bool operator > (const bign& b)const { return b<*this;}bool operator >= (const bign& b)const { return !(b>*this);}bool operator <= (const bign& b)const { return !(*this>b);}bool operator != (const bign& b)const{ return (*this<b||*this>b);} bool operator == (const bign& b)const{ return !(*this != b);} } ;//";" 太重要了 //为bign定义<<和>>运算符 必须在外部istream& operator >>(istream &in,bign& x)//&的位置有关系么?{string s;in>>s;//in表示输入的流 x=s.c_str();//把string 转换为char*return in; } ostream& operator <<(ostream &out,const bign& x)//此处要求x为const的{out<<x.str();return out;}
提出了几个疑问,
1.为什么不能*this.len
2.bign operator = (const char* num)//此处的const何用?
3.istream& operator >>(istream &in,bign& x)//&的位置有关系么?
0 0
- 【算法学习笔记】04.C++中结构体定义练习(bign初步)
- 【算法学习笔记】07.数据结构基础 链表 初步练习
- C语言中结构体学习笔记
- 【算法学习笔记】10.数据结构基础 二叉树初步练习3(遍历与递归复习)
- c c++中结构体定义
- 从头开始学算法:考研机试题练习(C/C++)–算法初步
- 【算法学习笔记】08.数据结构基础 二叉树初步练习1
- 【算法学习笔记】09.数据结构基础 二叉树初步练习2
- 【算法学习笔记】11.数据结构基础 二叉树初步练习4
- C语言初步学习笔记
- 黑马程序员—C学习笔记—结构体变量五种定义方式与指针结构体数组探究
- C语言中结构定义
- 数据库学习笔记和小练习(1)数据定义
- linux c 结构中定义结构
- objective-c中c结构体的定义
- 数据结构与算法(c语言) 学习笔记——第三章练习
- C语言优化学习——结构体定义优化
- C语言学习-struct结构体定义你自己的数据类型
- Raspberry - MP4视频合并
- wubi硬盘安装Ubuntu 10.04教程
- hdoj.1056 HangOver 20140728
- Spark官方链接
- linux下的串口操作
- 【算法学习笔记】04.C++中结构体定义练习(bign初步)
- ActionScript3游戏中的图像编程(连载二)
- hdoj.2106 decimal system 20140728
- JDBC高级编程——PreparedStatement
- 创建你的第一个安卓应用2 - 第二课 - 运行你的应用
- Python--执行系统命令
- 剑指offer-15:链表中倒数第k个结点
- hdoj.2028 Lowest Common Multiple Plus 20140728
- 答读者问(10):有关对博客的评价及个人技术发展路线等问题