C语言培训_011
2016-07-24 09:54
423 查看
斐波那契前100项(借助数组法)
#include"stdio.h" typedef struct shu { int x[100]; }shu; shu plus(shu a,shu b) { shu c; for(int i=0;i<100;i++) { c.x[i]=0; } int temp=99; while(a.x[temp]==0&&temp>0) { temp--; } temp++; for(int i=0;i<temp;i++) { c.x[i]=a.x[i]+b.x[i]; } for(int i=0;i<100;i++) { while(c.x[i]>=10) { c.x[i]=c.x[i]-10; c.x[i+1]=c.x[i+1]+1; } } return c; } void output(shu a) { int i=99; while(a.x[i]==0&&i>0) { i--; } for(int j=i;j>-1;j--) { printf("%d",a.x[j]); } } int main() { shu n[100]={{{1}},{{1}}}; for(int i=2;i<100;i++) { n[i]=plus(n[i-1],n[i-2]); } for(int i=0;i<1000;i++) { output(n[i]); printf("\n\n"); } return 0; }
长正整数的加法乘法(接上一题)
#include"stdio.h" //定义无符号结构体。最大可储存100位数。 typedef struct shu { int x[100]; }shu; shu plus(shu a,shu b)//加法函数 { shu c;// C 为最后返回的结果。 for(int i=0;i<100;i++) { c.x[i]=0;//一般系统会把各个位置重置为 0 。但保险起见我们再手动设置一下。 } int a1=99,b1=99;// a1,b1,为输入两个数的最大位数。 while(a.x[a1]==0&&a1>0)//从99位起,只要是 0 ,则不是最高位.最后算出的 a1 为 shu a 的最高位。 { a1--; } while(b.x[b1]==0&&b1>0)// b1 为 shu b 的最高位。 { b1--; } int max=a1>b1?a1:b1;// max 为两个最高位的最大值。 for(int i=0;i<=max;i++)// C 每一位都是 a,b 对应位置的和。 { c.x[i]=a.x[i]+b.x[i]; } for(int i=0;i<max+1;i++)// 化简 c . { while(c.x[i]>=10)//找到一个位置比 10 大,则进位。 { int k=c.x[i]; c.x[i]=c.x[i]%10; c.x[i+1]=c.x[i+1]+k/10; } } return c; } shu multi(shu a,shu b)// 乘法函数 { shu c;// C 为最后返回的结果 for(int i=0;i<100;i++) { c.x[i]=0;//初始化 C 的每一位。 } int a1=99,b1=99;//求出输入两个数的位数。 while(a.x[a1]==0) { a1--; } while(b.x[b1]==0) { b1--; } int max=a1>b1?a1:b1;//两个输入数的最大位数。 int min=a1>b1?b1:a1;//两个输入数较小的位数。 int sum=max+min;//两个输入数的总位数。 for(int i=0;i<=b1;i++) { for(int j=0;j<=a1;j++) { c.x[i+j]=c.x[i+j]+b.x[i]*a.x[j];//画出乘法公式找规律,累加c。 } } for(int i=0;i<100&&i<sum;i++)//想想一个数乘 99 就知道为什么 i 要小于 sum 了。 { while(c.x[i]>=10)//大于 10 的那个位置 进位。 { int k=c.x[i]; c.x[i]=c.x[i]%10; c.x[i+1]=c.x[i+1]+k/10; } } return c; } shu min(shu a,shu b) { int a1=99,b1=99; while(a.x[a1]==0) { a1--; } while(b.x[b1]==0) { b1--; } int max=a1>b1?a1:b1; shu c; for(int i=0;i<100;i++) { c.x[i]=0; } for(int i=0;i<=max;i++) { c.x[i]=a.x[i]-b.x[i]; } return c; } void output(shu a) { int i=99; while(a.x[i]==0&&i>0) { i--; } int temp=1; for(int j=i;j>-1;j--) { printf("%d",a.x[j]); if(temp++==3) { temp=1; printf(","); } } printf("\n"); } int main() { shu a={8,8,8,8,8,8,8,8};//10的意思。高位到低位算的。 shu b={9,9,9,9,9,9,9,9,9};//321的意思。写函数的时候没考虑到这个问题。现在不想改了。 shu c=multi(b,a); printf("88 888 888 * 999 999 999 ="); output(c); printf("88 888 888 + 999 999 999 ="); shu d=plus(a,b); output(d); return 0; }
有符号大数的四则运算(接上一题)
相关文章推荐
- c语言版大数计算器(实现大数的加减乘除运算)
- 李洪强漫谈iOS开发[C语言-019]-断点调试
- VS2013(Ctrl+F5)运行程序后闪退问题
- c++设计模式之适配器模式
- 使用QProxyStyle定制QMenu (三)
- Cpp Concurrency In Action 读书笔记 第一章
- 【Effective C++读书笔记】篇八(条款18~条款21)
- C语言常量
- C++中的句柄
- c++
- C++ exception类
- C++ throw:抛出自己的异常
- C++ char[] 与 string 转换
- c语言声明
- C++之char*,const char*和string的相互转换
- C++类和new、delete操作符和堆和栈内存的分配
- C++中的友元函数、static函数、常函数小结(理论篇)
- c++学习笔记
- register,static,const,extern,typedef,volatile的详讲
- C语言中浮点数的运算