C语言常用算法大全

  • 格式:pdf
  • 大小:210.45 KB
  • 文档页数:48

case'2': do{ system("cls"); if(password1!=password) //如果在case1中密码输入不正确将无法进行后面操作 { printf("please logging in,press any key to continue..."); getch(); break; } else { printf("******************************\n"); printf(" Please select:\n"); printf("* 1.$100 *\n"); printf("* 2.$200 *\n"); printf("* 3.$300 *\n"); printf("* 4.Return *\n"); printf("******************************\n"); CMoney=getch(); } }while(CMoney!='1'&&CMoney!='2'&&CMoney!='3'&&CMoney!='4'); //当输入值不是1,2,3,4中任意数将继续执行do循环体中语句 switch(CMoney) { case'1': system("cls"); a=a-100; printf("**********************************************\n"); printf("* Your Credit money is $100,Thank you! *\n"); printf("* The balance is $%d. *\n",a); printf("* Press any key to return... *\n"); getch(); break; case'2': system("cls");
printf("please select the max number(<10000):\n"); scanf("%d",&max); srand((unsigned long)time(0)); a=rand()%max; b=rand()%max; while((a<b)&&(sign==1)) { a=rand()%max; b=rand()%max; } sign1=(sign==1?'-':'+'); printf("\n%d%c%d=",a,sign1,b); scanf("%d",&c); if((sign==1)&&(a-b==c)||(sign!=1)&&(a+b==c)) printf("OK!\n"); else printf("The result is wrong\n"); // getch(); } 9.模拟ATM机界面程序 #include<stdio.h> #include<stdlib.h> #include<conio.h> int system(const char *string); main() { char Key,CMoney; int password,password1=123,i=0,a=1000; while(1) { do{ system("cls"); printf("******************************\n");
int a,b,c,t; printf("please input a,b,c,:\n"); scanf("%d%d%d",&a,&b,&c); if(a>b) {t=a;a=b;b=t;} if(a>c) {t=a;a=c;c=t;} if(b>c) {t=b;b=c;c=t;} printf("the order of the number is:\n"); printf("%d,%d,%d",a,b,c); } 7.判断闰年 #include<stdio.h> main() { int year; printf("please input the year:\n"); scanf("%d",&year); if((year%4==0&&year%100!=0)||year%400==0) printf("%d is a leap year",year); else printf("%d is not a leap year",year); } 8.随机函数进行加减运算 #include<stdio.h> #include<stdlib.h> #include<time.h> main() { int a,b,c,sign,max; char sign1; printf("please select sign(1 or other,1:-,other:+):\n"); scanf("%d",&sign);
a=a-200; printf("**********************************************\n"); printf("* Your Credit money is $200,Thank you! *\n"); printf("* The balance is $%d. *\n",a); printf("* Press any key to return... *\n"); getch(); break; case'3': system("cls"); a=a-300; printf("**********************************************\n"); printf("* Your Credit money is $300,Thank you! *\n"); printf("* The balance is $%d. *\n",a); printf("* Press any key to return... *\n"); getch(); break; case'4': break; } break; case'3': printf("*****************************************\n"); printf("* Thank you for using! *\n"); printf("* Goodbye! *\n"); printf("*****************************************\n"); getch(); exit(0); } continue; } } 10.输出三角形图案 #include<stdio.h> main() {
for(m=0;m<8;m++) { i=n%8; n=n/8; a[m]=i; } for(m=7;m>=0;m--) { printf("%d",a[m]); } printf("\n"); } 4.十进制转任意进制数 #include<stdio.h> main() { int i,n,m,j; int a[8]={0}; printf("please input the decimalism number(0~32767):\n"); scanf("%d",&n); printf("please input 基数:"); scanf("%d",&j); for(m=0;m<8;m++) { i=n%j; n=n/j; a[m]=i; } for(m=7;m>=0;m--) { printf("%d",a[m]); } printf("\n"); }
1.十进制数转二进制数 #include<stdio.h> main() { int i,n,m; int a[16]={0}; printf("please input the decimalism number(0~32767):\n"); scanf("%d",&n); for(m=0;m<15;m++) { i=n%2; n=n/2; a[m]=i; } for(m=15;m>=0;m--) { printf("%d",a[m]); if(m%4==0) printf(""); } printf("\n"); } 2.十进制转十六进制数 1)用格式控制的方式 #include<stdio.h> main() { int i; printf("please input decimalism number:\n"); scanf("%d",&i); printf("the hex number is %x\n",i); } 2)除以基数取余法 #include<stdio.h> main()