《2022年C语言笔记 .pdf》由会员分享,可在线阅读,更多相关《2022年C语言笔记 .pdf(17页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、对于计算上的的一些想法希望能和大家分享,也希望能得到大家指教。(1)C+开多次整数算法方/在 VC+6.0上调试通过/只支持开整数方/#include#include double SQRT_xx(double x,int y);void main()double z;char result35=;z=SQRT_xx(4097,4);gcvt(z,16,result);/double 转成 char puts(result);double SQRT_xx(double x,int y)double up;double down=0;double result=0;double temp=0;d
2、ouble mul=1;up=x;while(1)result=(up+down)/2;temp=y;mul=1;while(temp)mul*=result;temp-;名师资料总结-精品资料欢迎下载-名师精心整理-第 1 页,共 17 页 -if(mul=x)|(mul=x-0.00000000001)&(mul=x+0.00000000001)break;if(mulx)down=result;else up=result;return result;名师资料总结-精品资料欢迎下载-名师精心整理-第 2 页,共 17 页 -(2)gcvt 浮点数转char/在 VC+6.0上调试通过/#
3、include#include void gcvt_xx(double change_num,int precision,char change_array);void main()double a=-1234.56734535;char bb20=;/必须为空gcvt_xx(a,7,bb);puts(bb);void gcvt_xx(double change_num,int precision,char change_array)char p50=,p150=,k=0;char i=0,b=0;double z;unsigned long y;if(change_num0)/判断是否为负数
4、 pk=-;change_num=0-change_num;k+;y=(unsigned long)change_num;z=change_num-y;if(y)/反序整数部分p1 while(y)p1b=y%10+0 x30;y/=10;b+;名师资料总结-精品资料欢迎下载-名师精心整理-第 3 页,共 17 页 -else p10=0+0 x30;b=strlen(p1);/正序整数部分到p for(i=0;istrlen(p1);i+,k+,b-)pk=p1b-1;for(i=0;i50;i+)p1i=0;k=0;i=0;p1k=.;/加小数点k+;while(i!=precision+
5、1)/正序小数部分p1 z=z*10;y=(int)(z);z-=y;p1k=y+0 x30;k+;i+;p1strlen(p1)-1=0;strcat(p,p1);strcat(change_array,p);return;名师资料总结-精品资料欢迎下载-名师精心整理-第 4 页,共 17 页 -(3)100 以内的质数/在 VC+6.0上调试通过/#include#include void main()int x,z;double y;x=2;while(x=100)y=sqrt(x);z=(int)y;loop:if(z=1)printf(%dt,x);if(x%z!=0)z-;goto
6、 loop;else x+;printf(n);名师资料总结-精品资料欢迎下载-名师精心整理-第 5 页,共 17 页 -(4)生成 1-80 随机数/在 VC+6.0上调试通过/#include#include#include void main(void)int a=0;int b=0;srand(unsigned)time(NULL);for(b=0;b100;b+)a=rand()%80;printf(%dn,a);名师资料总结-精品资料欢迎下载-名师精心整理-第 6 页,共 17 页 -(5)用*打印 sinx 和 cosx 曲线/在 VC+6.0上调试通过/#include#inc
7、lude void main()double y;int x,m,n,b;for(b=0;b=20;b+)y=0.1*b;m=acos(1-y)*10;n=asin(1-y)*10;for(x=0;x=62;x+)if(x=m&x=n)printf(+);else if(x=n|x=31-n|x=62+n)printf(+);else if(x=m|x=62-m)printf(*);else printf();printf(n);名师资料总结-精品资料欢迎下载-名师精心整理-第 7 页,共 17 页 -(6)我自己写的一个小游戏/在 TC2.0上调试通过/#include#include#in
8、clude#include#include 名师资料总结-精品资料欢迎下载-名师精心整理-第 8 页,共 17 页 -void right1();void up1();void left1();void down1();void right_down();void right_up();void up_down();void left_down();void right_left();void left_up();void socrt();void init();void clkb();double fen=0;int del;int i;char ch18*20*2,ch28*20*2,ch
9、38*20*2,ch48*20*2,sco=scort:,sm1=up:w down:s left:a right:d,sm2=start:p exit:t;char*right=*,*left=*,*up=*,*down=*;void main()int a;char b,*path100;window(1,1,80,35);textbackground(5);textcolor(7);clrscr();window(22,12,80,12);cputs(sm1);window(31,20,80,20);cputs(sm2);LOOP:b=getch();名师资料总结-精品资料欢迎下载-名师
10、精心整理-第 9 页,共 17 页 -if(b=p)goto KAISHI;else if(b=t)exit(1);else goto LOOP;KAISHI:init();fen=0;srand(unsigned)time(NULL);while(1)a=rand()%10;if(fen100)del=2500;else if(fen75)del=5000;else if(fen60)del=7500;else if(fen45)del=10000;else if(fen30)del=15000;else if(fen10)del=25000;else del=35000;switch(a)
11、case 0:right1();clkb();break;case 1:up1();clkb();break;case 2:down1();clkb();break;case 3:left1();clkb();break;case 4:right_down();clkb();break;case 5:right_up();clkb();break;case 6:up_down();clkb();break;case 7:left_down();clkb();break;case 8:left_up();clkb();break;case 9:right_left();clkb();break;
12、default:break;TUICHU:;void clkb()char c;while(kbhit()c=getch();void init()名师资料总结-精品资料欢迎下载-名师精心整理-第 10 页,共 17 页 -window(1,1,20,25);/*定义文本窗口*/textbackground(1);textcolor(7);clrscr();gotoxy(1,2);cputs(right);window(21,1,40,25);/*定义文本窗口*/textbackground(2);textcolor(7);clrscr();cputs(up);/*定义窗口背景色*/windo
13、w(41,1,60,25);/*定义文本窗口*/textbackground(3);textcolor(7);clrscr();cputs(down);window(61,2,80,25);/*定义文本窗口*/textbackground(0);textcolor(7);clrscr();cputs(left);window(61,1,80,1);textbackground(5);textcolor(8);clrscr();cputs(sco);gettext(4,1,18,8,ch1);gettext(26,1,34,8,ch2);gettext(46,1,54,8,ch3);gettex
14、t(63,2,77,8,ch4);void right1()char c,k=0;for(i=24;i0;i-)gotoxy(4,i);puttext(4,i,18,i+6,ch1);if(k=0)if(!kbhit();else c=getch();k+;if(c=d)fen+;名师资料总结-精品资料欢迎下载-名师精心整理-第 11 页,共 17 页 -socrt();else if(c=p);else if(c=t)exit(1);delay(del);delay(del);void left1()char c,k=0;for(i=24;i0;i-)gotoxy(63,i);puttext
15、(63,i+1,77,i+7,ch4);if(k=0)if(!kbhit();else c=getch();k+;if(c=a)fen+;socrt();else if(c=p);else if(c=t)exit(1);delay(del);delay(del);void up1()char c,k=0;for(i=24;i0;i-)gotoxy(26,i);puttext(26,i,34,i+7,ch2);if(k=0)if(!kbhit();else c=getch();k+;if(c=w)fen+;socrt();名师资料总结-精品资料欢迎下载-名师精心整理-第 12 页,共 17 页
16、-else if(c=p);else if(c=t)exit(1);delay(del);delay(del);void down1()char c,k=0;for(i=24;i0;i-)gotoxy(46,i);puttext(46,i,54,i+7,ch3);if(k=0)if(!kbhit();else c=getch();k+;if(c=s)fen+;socrt();else if(c=p);else if(c=t)exit(1);delay(del);delay(del);void up_down()char c,k=0;for(i=24;i0;i-)gotoxy(46,i);put
17、text(46,i,54,i+7,ch3);gotoxy(26,i);puttext(26,i,34,i+7,ch2);if(k0;i-)gotoxy(46,i);puttext(46,i,54,i+7,ch3);gotoxy(4,i);puttext(4,i,18,i+6,ch1);if(k0;i-)gotoxy(46,i);puttext(46,i,54,i+7,ch3);gotoxy(63,i);puttext(63,i+1,77,i+7,ch4);if(k0;i-)gotoxy(4,i);puttext(4,i,18,i+6,ch1);gotoxy(26,i);puttext(26,i
18、,34,i+7,ch2);if(k0;i-)gotoxy(63,i);puttext(63,i+1,77,i+7,ch4);gotoxy(26,i);puttext(26,i,34,i+7,ch2);if(k0;i-)gotoxy(63,i);puttext(63,i+1,77,i+7,ch4);gotoxy(4,i);puttext(4,i,18,i+6,ch1);if(k2)if(!kbhit();else c=getch();k+;if(c=a|c=d)fen+;socrt();else if(c=p);else if(c=t)exit(1);delay(del);delay(del);void socrt()char ffff20;gcvt(fen,5,ffff);window(67,1,80,1);cputs(ffff);名师资料总结-精品资料欢迎下载-名师精心整理-第 16 页,共 17 页 -名师资料总结-精品资料欢迎下载-名师精心整理-第 17 页,共 17 页 -