NewInt终极版本(不考虑用int赋值NewInt,因为弄不粗来啊妹妹的)
来源:互联网 发布:js聚合物水泥基 编辑:程序博客网 时间:2024/04/29 19:55
终于把这个代码码完了,抹泪T_T,虽然还是有一些比较苦逼的地方,比如说:0/-1=-0但是0/1=0,然后恩那吧,就这样了
然后上代码
头文件清单:
NewInt.h
#include "iostream.h"#include <string>class NewInt{private:int *array;int length;public:NewInt();NewInt(const char*s);~NewInt(){/*if (array!=NULL){delete[]array;}*/}void clear(){if (array!=NULL){delete []array;}}void changeToLonger(int len);void addToLast(int len);NewInt operator +(NewInt &ni);NewInt operator -(NewInt &ni);NewInt operator *(NewInt &ni);NewInt operator /(NewInt &ni);NewInt& operator =(const NewInt &ni);friend ostream & operator<<(ostream&out,NewInt &ni);void setLength(int len);bool compare(int s1[],int s2[],int len);void quFan();//取反,起名无能表示压力很大NewInt operator +(int i);NewInt operator -(int i);NewInt operator *(int i);NewInt operator /(int i);//NewInt& operator =(const int &i);char * intToChar(int i); };
源文件清单:
NewInt.cpp
#include "iostream.h"#include "NewInt.h"NewInt::NewInt(){array =NULL;length = 0;}NewInt::NewInt(const char* s){if (s[0]=='-'){int len = strlen(s)-1;array = new int[len+1];array[0] = -1;for(int i = 1;i<=len;i++){array[i] = s[i]-'0';}length = len+1;}else {int len = strlen(s);array = new int[len+1];array[0] = 0;for (int i = 1;i<=len;i++){array[i] = s[i-1]-'0';}length = len+1;}}void NewInt::setLength(int len){array = new int[len];length = len;}//比较绝对值大小。这两个数字长度相同bool NewInt::compare(int s1[],int s2[],int len){int flag = 0;for (int i = 1;i<len;i++){if (s1[i]-s2[1]<0){flag = 1;}}if (flag ==0){return true;}else{return false;}}void NewInt::changeToLonger(int len){ int *temp = array;if (array[0]==0){array = new int[len];for (int i = len-length+1;i<len;i++){array[i]=temp[i-(len-length)];}for (int j = 1;j<=len-length;j++){array[j] = 0;}array[0]=0;length = len;}else if (array[0]==-1){array = new int[len];array[0]=-1;for (int i = len-length+1;i<len;i++){array[i]=temp[i-(len-length)];}for (int j = 1;j<=len-length;j++){array[j] = 0;}length = len;}}void NewInt::quFan(){if (array[0]==0){array[0]=-1;}else{array[0]=0;}}void NewInt::addToLast(int len){int *temp = array;array = new int[length+len];for (int i = length;i<length+len;i++){array[i]=0;}for (int j = 0;j<length;j++){array[j] = temp[j];}length = len+length;}NewInt NewInt::operator +(NewInt &ni){NewInt temp;if (length>=ni.length){ni.changeToLonger(length);temp.setLength(length);}else{changeToLonger(ni.length);temp.setLength(ni.length);}if (array[0]==0&&ni.array[0]==0){for (int i1 = 0;i1<temp.length;i1++){temp.array[i1]=0;}for(int i = temp.length -1;i>=1;i--){if (temp.array[i]+array[i]+ni.array[i]<=9){temp.array[i]+= array[i]+ni.array[i];}else {temp.array[i] += array[i]+ni.array[i]-10;temp.array[i-1]+=1;}}if (temp.array[0]>0){temp.length++;int *tempArray = temp.array;temp.array = new int[temp.length];for (int i = temp.length;i>0;i--){temp.array[i] = tempArray[i-1];}temp.array[0] = 0;}}else if (array[0]==0&&ni.array[0]==-1)//正数加上负数,你妹妹啊{for (int i1 =0;i1<temp.length;i1++){temp.array[i1]=0;}if (compare(array,ni.array,temp.length)){for (int i = temp.length-1;i>=1;i--){if (temp.array[i]+array[i]-ni.array[i]>=0){temp.array[i]+=array[i]-ni.array[i];}else{temp.array[i]+=array[i]-ni.array[i]+10;temp.array[i-1]-=1;}}}else {temp.array[0] = -1;for (int i = temp.length-1;i>=1;i--){if (temp.array[i]-array[i]+ni.array[i]>=0){temp.array[i]+=ni.array[i]-array[i];}else{temp.array[i]+=ni.array[i]-array[i]+10;temp.array[i-1]-=1;}}}}else if (array[0]==-1&&ni.array[0]==0)//负数加正数,好玩吗吗{for (int i1 =0;i1<temp.length;i1++){temp.array[i1]=0;}if (compare(array,ni.array,temp.length)){temp.array[0]=-1;for (int i = temp.length-1;i>=1;i--){if (temp.array[i]+array[i]-ni.array[i]>=0){temp.array[i]+=array[i]-ni.array[i];}else{temp.array[i]+=array[i]-ni.array[i]+10;temp.array[i-1]-=1;}}}else {for (int i = temp.length-1;i>=1;i--){if (temp.array[i]-array[i]+ni.array[i]>=0){temp.array[i]+=ni.array[i]-array[i];}else{temp.array[i]+=ni.array[i]-array[i]+10;temp.array[i-1]-=1;}}}}else {temp.array[0]=-1;for (int i1 =1;i1<temp.length;i1++){temp.array[i1] = 0;}for(int i = length -1;i>=1;i--){if (temp.array[i]+array[i]+ni.array[i]<=9){temp.array[i]+= array[i]+ni.array[i];}else {temp.array[i] += array[i]+ni.array[i]-10;temp.array[i-1]+=1;}}if (temp.array[0]>=0){temp.length++;int *tempArray = temp.array;temp.array = new int[temp.length];for (int i = temp.length;i>0;i--){temp.array[i] = tempArray[i-1];}temp.array[1]+=1;temp.array[0] = -1;}}return temp;}NewInt NewInt::operator -(NewInt &ni){NewInt temp;NewInt middleValue;middleValue.setLength(ni.length);for (int i = 0;i<ni.length;i++){middleValue.array[i]=ni.array[i];}middleValue.quFan();temp=*this + middleValue;return temp;}NewInt NewInt::operator *( NewInt &ni){//先初始化temp使得temp与this相同长度,但是均为0NewInt temp;temp.setLength(length);for (int i =0;i<temp.length;i++){temp.array[i]=0;}if ((array[0]==0&&ni.array[0]==0)||(array[0]==-1&&ni.array[0]==-1))//正数乘以正数{for (int i = 1;i<ni.length;i++){int j = ni.array[i];NewInt middleValue1 ;middleValue1.setLength(length);for (int z = 0;z<length;z++){middleValue1.array[z] = array[z];}NewInt middleValue2 ;middleValue2.setLength(length);for (int z1 = 0;z1<length;z1++){middleValue2.array[z1] = 0;}while (j!=0){middleValue2=middleValue2+middleValue1;j--;}middleValue2.addToLast(ni.length-i-1);temp =temp+middleValue2;}temp.array[0]=0;}else if ((array[0]==0&&ni.array[0]==-1)||(array[0]==-1&&ni.array[0]==0)){for (int i = 1;i<ni.length;i++){int j = ni.array[i];NewInt middleValue1 ;middleValue1.setLength(length);for (int z = 0;z<length;z++){middleValue1.array[z] = array[z];}NewInt middleValue2 ;middleValue2.setLength(length);for (int z1 = 0;z1<length;z1++){middleValue2.array[z1] = 0;}while (j!=0){middleValue2=middleValue2+middleValue1;j--;}middleValue2.addToLast(ni.length-i-1);temp =temp+middleValue2;}temp.array[0]=-1;}return temp;}NewInt NewInt::operator /(NewInt &ni){NewInt temp;temp.setLength(2);temp.array[0]=0;temp.array[1]=0;NewInt middleValue;middleValue.setLength(2);middleValue.array[0]=0;middleValue.array[1]=1;NewInt middleValue1 ;middleValue1.setLength(length);for (int z = 0;z<length;z++){middleValue1.array[z] = array[z];}if (array[0]==0&&ni.array[0]==0){while (middleValue1.array[0]==0){middleValue1 = middleValue1-ni;temp = temp+middleValue;}temp = temp -middleValue;}else if (array[0]==0&&ni.array[0]==-1){NewInt middleValue2;middleValue2.setLength(ni.length);for (int i = 0;i<ni.length;i++){middleValue2.array[i]=ni.array[i];} middleValue2.quFan();while (middleValue1.array[0]==0){middleValue1 = middleValue1-middleValue2;temp = temp+middleValue;}temp = temp -middleValue;temp.array[0]=-1;}else if (array[0]==-1&&ni.array[0]==0){while (middleValue1.array[0]==-1){middleValue1 = middleValue1+ni;temp = temp+middleValue;}temp = temp -middleValue;temp.array[0]=-1;}else{middleValue1.quFan();while (middleValue1.array[0]==0){middleValue1 = middleValue1+ni;temp = temp+middleValue;}temp = temp -middleValue;temp.array[0]=0;}return temp;}//NewInt& operator =(const NewInt &ni);NewInt& NewInt::operator =(const NewInt &ni){length =ni.length;int *temp = new int[ni.length];for (int i =0;i<ni.length;i++){temp[i] =ni.array[i];}delete []array;array = temp;return *this;}ostream & operator<<(ostream&out,NewInt &ni){if (ni.array[0]==-1){out<<"-";}for (int i = 1;i<ni.length;i++){out<<ni.array[i];}return out;}char* NewInt::intToChar(int i){int count =1;char *s;int m = 1;if (i ==0){s = new char[count+1];s[0]='0';s[1]=NULL;}else if (i>0){int j =i;while(j>=10){count++;j = j/10;m = m*10;}s = new char[count+1];int k =i;m=m*10;for (int i1 =0;i1<count;i1++){m = m/10;s[i1]= k/m-0+'0';k = k%m;}s[count]=NULL;}else {int j =-i;while(j>=10){count++;j = j/10;m = m*10;}s = new char[count+2];int k =-i;s[0]='-'; m=m*10;for (int i1 =1;i1<count+1;i1++){m = m/10;s[i1]= k/m-0+'0';k = k%m;}s[count+1]=NULL;}return s;}NewInt NewInt::operator +(int i){char *s = intToChar(i);NewInt value = NewInt(s);NewInt temp = *this+value;return temp;}NewInt NewInt::operator -(int i){char *s = intToChar(i);NewInt value = NewInt(s);NewInt temp = *this-value;return temp;}NewInt NewInt::operator *(int i){char *s = intToChar(i);NewInt value = NewInt(s);NewInt temp = (*this)*value;return temp;}NewInt NewInt::operator /(int i){char *s = intToChar(i);NewInt value = NewInt(s);NewInt temp = (*this)/value;return temp;}/*NewInt& NewInt::operator =(const int &i){char *s = intToChar(i);NewInt ni = NewInt(s);int *temp = new int[ni.length];for (int i1 =0;i1<ni.length;i1++){temp[i1] =ni.array[i1];}delete []array;array = temp;return *this;}*/
main.cpp
#include "NewInt.h"#include <cstring>#include "iostream.h"void main(){char s[]= {'-','5',NULL};char s1[] = {'5',NULL};NewInt new1(s);cout<<new1<<endl;NewInt new2(s1);int i =22;NewInt new3 = new1/i;cout<<new2<<endl;cout<<new3<<endl;int i =-123;new1.clear();new2.clear();new3.clear();}
然后赋值的时候也不用传长度的参数咯,然后就是通过自己写代码发现了码代码是一件很好玩的事情= =、可以帮助解决很多问题。。
然后最近由于刘花花学长的事情,我认为能力越强的人越应该承受更多的责任。。恩
- NewInt终极版本(不考虑用int赋值NewInt,因为弄不粗来啊妹妹的)
- NewInt 2
- NewInt 3
- NewInt 4
- 数字版本比较(不考虑带字母的版本号)
- int 的灵活使用可以当作char short和3字节使用(这里不考虑占用内存)
- Gradle不匹配报错的终极大招(gradle插件版本和gradle版本对应关系)
- JS判断浏览器,不考虑版本
- 为什么不考虑考虑我的心情
- 数值的整数次方double pow(double base,int exponent)求base的exponent次方 不考虑大数 不准用库函数
- 考虑异常安全性的赋值语句
- 不考虑优先级的加减乘除
- 用for循环对int数组的赋值和用System.arraycopy()对int数组赋值的速度比较
- 大数加法的实现(不考虑负数)
- 数值的整数次方(不考虑大数问题)
- 树结构的子树合并(不考虑子树顺序)
- bochs在window环境下的调试(有些命令如果不能用可能是因为版本比较新的原因)
- 黑马程序员 C#统计一行字符串中各个字母出现的频率(不考虑大小写,不考虑其他字符)
- Visual Studio插件及小技巧
- pager-taglib标签的相关属性
- Palindromic Subsequence UVA11404
- vb代码:获取网卡实际MAC
- workqueue
- NewInt终极版本(不考虑用int赋值NewInt,因为弄不粗来啊妹妹的)
- 又变老了一岁
- iOS 5两大隐藏特性
- 2012总结
- 2012-12-27 11gR2 "Performance Tuning Guide" page 83 - 113
- Android System SoftwareWatchdog必要代码粘贴--总是忘--狗咬人
- 2012年总结
- CNET盘点2012年IT人物:进退得意还是没落迷茫
- 微软IE漏洞曝光 黑客可跟踪光标轨迹