c实例源代码
单片机c语言实例程序300篇
单片机c语言实例程序300篇单片机C语言【程序1】题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。
组成所有的排列后再去掉不满足条件的排列。
2.程序源代码:main(){inti,j,k;printf("\n");for(i=1;i<5;i++)/某以下为三重循环某/for(j=1;j<5;j++)for(k=1;k<5;k++){if(i!=k&&i!=j&&j!=k)/某确保i、j、k三位互不相同某/printf("%d,%d,%d\n",i,j,k);}}==============================================================【程序2】题目:企业发放的奖金根据利润提成。
利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?1.程序分析:请利用数轴来分界,定位。
注意定义时需把奖金定义成长整型。
2.程序源代码:main()单片机C语言{longinti;intbonu1,bonu2,bonu4,bonu6,bonu10,bonu;canf("%ld",&i);bonu1=100000某0.1;bonu2=bonu1+100000某0.75;bonu4=bonu2+200000某0.5;bonu6=bonu4+200000某0.3;bonu10=bonu6+400000某0.15;if(i<=100000)bonu=i某0.1;eleif(i<=200000)bonu=bonu1+(i-100000)某0.075;eleif(i<=400000)bonu=bonu2+(i-200000)某0.05;eleif(i<=600000)bonu=bonu4+(i-400000)某0.03;eleif(i<=1000000)bonu=bonu6+(i-600000)某0.015;elebonu=bonu10+(i-1000000)某0.01;printf("bonu=%d",bonu);}============================================================ ==【程序3】题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?1.程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上268后再开方,如果开方后的结果满足如下条件,即是结果。
C语言游戏源代码
C语言游戏源代码1、简单的开机密码程序#include "conio.h"#include "string.h"#include "stdio.h"void error(){window(12,10,68,10);textbackground(15);textcolor(132);clrscr();cprintf("file or system error! you can't enter the system!!!");while(1); /*若有错误不能通过程序*/}void look(){FILE *fauto,*fbak;char *pass="c:\\windows\\password.exe"; /*本程序的位置*/char a[25],ch;char *au="autoexec.bat",*bname="hecfback.^^^"; /*bname 是autoexec.bat 的备份*/setdisk(2); /*set currently disk c:*/chdir("\\"); /*set currently directory \*/fauto=fopen(au,"r+");if (fauto==NULL){fauto=fopen(au,"w+");if (fauto==NULL) error();}fread(a,23,1,fauto); /*读取autoexec.bat前23各字符*/a[23]='\0';if (strcmp(a,pass)==0) /*若读取的和pass指针一样就关闭文件,不然就添加*/fclose(fauto);else{fbak=fopen(bname,"w+");if (fbak==NULL) error();fwrite(pass,23,1,fbak);fputc('\n',fbak);rewind(fauto);while(!feof(fauto)){ch=fgetc(fauto);fputc(ch,fbak);}rewind(fauto);rewind(fbak);while(!feof(fbak)){ch=fgetc(fbak);fputc(ch,fauto);}fclose(fauto);fclose(fbak);remove(bname); /*del bname file*/ }}void pass(){char *password="88888888"; char input[60];int n;while(1){window(1,1,80,25); textbackground(0);textcolor(15);clrscr();n=0;window(20,12,60,12); textbackground(1);textcolor(15);clrscr();cprintf("password:");while(1){input[n]=getch();if (n>58) {putchar(7); break;} /*若字符多于58个字符就结束本次输入*/ if (input[n]==13) break;if (input[n]>=32 && input[n]<=122) /*若字符是数字或字母才算数*/ {putchar('*');n++;}if (input[n]==8) /*删除键*/if (n>0){cprintf("\b \b");input[n]='\0';n--;}}input[n]='\0';if (strcmp(password,input)==0)break;else{putchar(7);window(30,14,50,14);textbackground(15);textcolor(132);clrscr();cprintf("password error!");getch();}}}main(){look();pass();}2、彩色贪吃蛇#include <graphics.h>#include <stdlib.h>#define N 200#define up 0x4800#define down 0x5000#define left 0x4b00#define right 0x4d00#define esc 0x011b#define Y 0x1579#define n 0x316eint gamespeed; /* 游戏速度*/int i, key, color;int score = 0; /* 游戏分数*/char cai48H[] ={0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x18, 0x00, 0x00, 0x00, 0x0E, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x1C, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x20, 0x00, 0x38, 0x00, 0x00, 0x00, 0x40, 0x00, 0x78, 0x00, 0x00, 0x01, 0x80, 0x40, 0x70, 0x00, 0x00, 0x03, 0x80, 0xC0, 0xE0, 0x00, 0x00, 0x07, 0x80, 0x80, 0xC0, 0x00, 0x00, 0x0E, 0x11, 0x81, 0xC0, 0x00, 0x00, 0x08, 0x61, 0x01, 0x80, 0x00, 0x00, 0x00, 0x23, 0x03, 0x04, 0x00, 0x00, 0x02, 0x02, 0x00, 0x06, 0x00, 0x00, 0x1E, 0x04, 0x00, 0x0F, 0x00, 0x00, 0x1C, 0x1F, 0x80, 0x1E, 0x00, 0x00, 0x08, 0x3F, 0x80, 0x3C, 0x00, 0x00, 0x00, 0xFF, 0x80, 0x38, 0x00, 0x00, 0x03, 0xFF, 0x80, 0x78, 0x00, 0x00, 0x0F, 0xF8, 0x00, 0xF0, 0x00, 0x00, 0x7F, 0xF0, 0x00, 0xE0, 0x00, 0x03, 0xFF, 0xFC, 0x01, 0x80, 0x00, 0x03, 0xC0, 0xFF, 0x01, 0x03, 0x80, 0x01, 0x01, 0xFF, 0x00, 0x03, 0x80, 0x00, 0x01, 0x3F, 0x00, 0x07, 0x80,0x10, 0x00, 0x07, 0x00, 0x00, 0x00, 0x10, 0x00, 0x0E, 0x00, 0x00, 0x08, 0x10, 0x00, 0x1C, 0x00, 0x00, 0x30, 0x10, 0x00, 0x18, 0x00, 0x00, 0x70, 0x10, 0x00, 0x30, 0x00, 0x01, 0xE0, 0x10, 0x00, 0x70, 0x00, 0x03, 0x80, 0x10, 0x00, 0x60, 0x00, 0x00, 0x00, 0x30, 0x00, 0xE0, 0x00, 0x00, 0x00, 0xF0, 0x01, 0xC0, 0x00, 0x00, 0x00, 0x70, 0x03, 0xC0, 0x00, 0x00, 0x00, 0x10, 0x07, 0x80, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x01, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, };char she48H[] ={0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0E, 0x00, 0x00, 0x00, 0x03, 0x00, 0x07, 0x00, 0x00, 0x00, 0x02, 0x00, 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0xF8, 0x00, 0x00, 0x02, 0x00, 0x07, 0x86, 0x00, 0x00, 0x02, 0x00, 0x18, 0x03, 0x00, 0x00, 0x02, 0x00, 0x00, 0x07, 0x80, 0x00, 0x03, 0xF0, 0x00, 0x07, 0x80, 0x00, 0x0F, 0xFC, 0x00, 0x0C, 0x00, 0x00, 0x7E, 0x3F, 0x80, 0x00, 0x00, 0x01, 0xFE, 0x1F, 0x80, 0x00, 0x00, 0x01, 0xE2, 0x39, 0x8C, 0x00, 0x00, 0x00, 0xC2, 0x30, 0x08, 0x00, 0x00, 0x00, 0xC2, 0x60, 0x08, 0x00, 0x00, 0x00, 0xC3, 0xE0, 0x08, 0x60, 0x00, 0x00, 0x7F, 0xE0, 0x01, 0xE0, 0x00, 0x00, 0x3F, 0x80, 0x1F, 0xE0, 0x00, 0x00, 0x1E, 0x00, 0x1F, 0x80, 0x00, 0x00, 0x1E, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x02, 0x38, 0x1E, 0x00, 0x00, 0x00, 0x07, 0xFC, 0x1C, 0x00, 0x20, 0x00, 0x07, 0xFC, 0x18, 0x00, 0x20, 0x00, 0x1F, 0x0C, 0x10, 0x00, 0x20, 0x00, 0x7C,0x00, 0x60, 0x01, 0xE0, 0x00, 0x08, 0x00, 0xF0, 0x00, 0x80, 0x00, 0x08, 0x03, 0xF0, 0x00, 0x00, 0x00, 0x07, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x07, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x03, 0xFF, 0xE0, 0x00, 0x00, 0x00, 0x01, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, };char tun48H[] ={0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x03, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x1F, 0x00,0x00, 0x01, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x03, 0xF8, 0x00, 0x40, 0x00, 0x00, 0x00, 0x06, 0x07, 0xC0, 0x00, 0x00, 0x00, 0x07, 0xFF, 0xE0, 0x00, 0x00, 0x00, 0x07, 0xFF, 0xE0, 0x00, 0x00, 0x00, 0x0F, 0xFF, 0x80, 0x00, 0x00, 0x00, 0x7F, 0xF8, 0x00, 0x00, 0x00, 0x1F, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x1F, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x1F, 0xFC, 0x3C, 0x00, 0x00, 0x00, 0x0F, 0xF8, 0x0E, 0x00, 0x00, 0x00, 0x04, 0x70, 0x07, 0x00, 0x00, 0x00, 0x00, 0x60, 0x03, 0x80, 0x00, 0x00, 0x00, 0xC0, 0x00, 0xC0, 0x00, 0x00, 0x01, 0x80, 0x00, 0x30, 0x00, 0x00, 0x01, 0x00, 0x3C, 0x18, 0x00, 0x00, 0x02, 0x03, 0xFF, 0x0C, 0x00, 0x00, 0x0C, 0x7F, 0xFF, 0x8E, 0x00, 0x00, 0x18, 0xFF, 0xFF, 0xC7, 0x80, 0x00, 0x78, 0xFE, 0x07, 0x87, 0xE0, 0x01, 0xF0, 0x70, 0x07, 0x03, 0xF8, 0x07, 0xE0, 0x70, 0x0E, 0x03, 0xFE, 0x00, 0x00, 0x38, 0x1E, 0x01, 0xFE, 0x00, 0x00, 0x3F, 0xFE, 0x00, 0x0C, 0x00, 0x00, 0x1F, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x1F, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x0F, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, };char dan48H[] ={0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x07, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x7F, 0xC0, 0x80, 0x00, 0x00, 0x03, 0xFF, 0x80, 0x40, 0x00, 0x00, 0x01, 0xF1, 0x80, 0x40, 0x00, 0x00, 0x01, 0x81, 0x80, 0xE0, 0x00, 0x00, 0x00, 0x01, 0x93, 0xF0, 0x00, 0x00, 0x00, 0x01, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x21, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x21, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x61, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x61, 0x80, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x01, 0xFF, 0xC0, 0x00, 0x00, 0x00, 0x03, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x02, 0x00, 0xFC, 0x00, 0x00, 0x00, 0x04, 0x02, 0x1F, 0x00, 0x00, 0x00, 0x08, 0x03, 0x01, 0xC0, 0x00, 0x00, 0x38, 0x03, 0x00, 0x7C, 0x00, 0x00, 0xF8, 0x07, 0xF8, 0x3F, 0xC0, 0x01, 0xF0, 0x3F, 0xFE, 0x3F, 0xF8, 0x03, 0xC1, 0xFF, 0x0F, 0x1F, 0xF8, 0x00, 0x01, 0xE3, 0x0F, 0x0F, 0xF0, 0x00, 0x01, 0xC3, 0x0E, 0x00, 0x00, 0x00, 0x01, 0x83, 0xFC, 0x00, 0x00, 0x00, 0x00, 0xC7, 0xF8, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x7F, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x3F, 0x03, 0x80, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00, 0x00, 0x00, 0x00, 0x03, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x1F, 0xF8, 0x20, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xE0, 0x00, 0x00, 0x07, 0xFF, 0x81, 0xE0, 0x00, 0x00, 0x07, 0xE0, 0x00, 0xE0, 0x00, 0x00, 0x03, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,};char zuo16H[] ={0x18, 0xC0, 0x18, 0xC0, 0x19, 0x80, 0x31, 0xFE, 0x33, 0xFE, 0x76, 0xC0, 0xF0, 0xFC, 0xB0, 0xFC, 0x30, 0xC0, 0x30, 0xC0, 0x30, 0xFE, 0x30, 0xFE, 0x30, 0xC0, 0x30, 0xC0, 0x30, 0xC0, 0x00, 0x00, };char zhe16H[] ={0x03, 0x00, 0x03, 0x0C, 0x1F, 0xCC, 0x1F, 0xD8, 0x03, 0x30, 0xFF, 0xFE, 0xFF, 0xFE, 0x03, 0x00,0x0F, 0xF8, 0x3F, 0xF8, 0xEC, 0x18, 0xCF, 0xF8, 0x0C, 0x18, 0x0F, 0xF8, 0x0F, 0xF8, 0x00, 0x00, };char tian16H[] ={0x00, 0x00, 0x3F, 0xFC, 0x3F, 0xFC, 0x31, 0x8C,0x31, 0x8C, 0x31, 0x8C, 0x31, 0x8C, 0x3F, 0xFC, 0x3F, 0xFC, 0x30, 0x0C, 0x00, 0x00, 0x00, 0x00, };char xue16H[] ={0x33, 0x18, 0x19, 0x98, 0x08, 0xB0, 0x7F, 0xFC, 0x7F, 0xFC, 0x60, 0x0C, 0x1F, 0xF0, 0x1F, 0xF0, 0x00, 0xC0, 0x7F, 0xFC, 0x7F, 0xFC, 0x01, 0x80, 0x01, 0x80, 0x07, 0x80, 0x03, 0x00, 0x00, 0x00, };char ke16H[] ={0x00, 0x00, 0x0C, 0x18, 0xFD, 0x98, 0xF8, 0xD8, 0x18, 0x58, 0xFE, 0x18, 0xFE, 0x98, 0x18, 0xD8, 0x3C, 0x58, 0x7E, 0x1E, 0xDB, 0xFE, 0x9B, 0xF8, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00, 0x00, };struct Food/*定义结构体存储食物的属性*/{int x; /* 食物的坐标*/int y;int yes; /* 值为0表示屏幕上没有食物,值为1表示屏幕上有食物*/ int color; /* 食物颜色*/} food;struct Snake/*定义结构体存储蛇的属性*/{int x[N]; /* 每一节蛇的坐标*/int y[N];int color[N];/*存储每一节蛇的颜色*/int node; /* 蛇的节数*/int direction; /* 蛇移动的方向*/int life; /* 蛇的生命,如果为1,蛇死,游戏结束*/} snake;void init(void)/*图形驱动*/{int driver = DETECT, mode = 0;registerbgidriver(EGAVGA_driver);initgraph(&driver, &mode, "");}void drawmat(char *mat, int matsize, int x, int y, int color) /*汉字点阵*/ {int i, j, k, m;m = (matsize - 1) / 8 + 1;for(j = 0; j < matsize; j++)for(i = 0; i < m; i++)for(k = 0; k < 8; k++)if(mat[j*m+i]&(0x80 >> k))putpixel(x + i * 8 + k, y + j, color);}void showword(void){/* 调用汉字点阵输出程序,显示标题和作者信息*/drawmat(cai48H, 48, 249, -4, 7);drawmat(she48H, 48, 329, -4, 7);drawmat(tun48H, 48, 409, -4, 7);drawmat(dan48H, 48, 489, -4, 7);drawmat(cai48H, 48, 250, -5, 4);drawmat(she48H, 48, 330, -5, 4);drawmat(tun48H, 48, 410, -5, 4);drawmat(dan48H, 48, 490, -5, 4);/*作者田学科*/drawmat(zuo16H, 16, 515, 465, 7);drawmat(zhe16H, 16, 530, 465, 7);drawmat(tian16H, 16, 550, 465, 7);drawmat(xue16H, 16, 565, 465, 7);drawmat(ke16H, 16, 580, 465, 7); }void draw(void)/*画出四周的墙*/ {if(color == 15)color = 0;setcolor(++color);setlinestyle(SOLID_LINE, 0, 1);for(i = 30; i <= 600; i += 10){rectangle(i, 40, i + 10, 49);rectangle(i, 451, i + 10, 460);}for(i = 40; i < 450; i += 10){rectangle(30, i, 39, i + 10);rectangle(601, i, 610, i + 10);}}void prscore(void){/* 打印游戏分数*/char str[10];setfillstyle(SOLID_FILL, YELLOW);bar(50, 10, 200, 30);setcolor(6);settextstyle(0, 0, 2);sprintf(str, "score:%d", score);outtextxy(55, 15, str);}void gameover(void){cleardevice(); /* 清屏函数*/for(i = 0; i < snake.node; i++) /* 画出蛇死时的位置*/{setcolor(snake.color[i]);rectangle(snake.x[i], snake.y[i], snake.x[i] + 10, snake.y[i] + 10);}prscore(); /* 显示分数*/draw();showword();settextstyle(0, 0, 6);setcolor(7);outtextxy(103, 203, "GAME OVER");setcolor(RED);outtextxy(100, 200, "GAME OVER");}void gameplay(void)/* 玩游戏的具体过程*/{int flag, flag1;randomize();prscore();gamespeed = 50000;food.yes = 0; /* food.yes=0表示屏幕上没有食物*/ snake.life = 1; /* snake.life=1表示蛇是活着的*/ snake.direction = 4; /* 表示蛇的初始方向为向右*/ snake.node = 2; /* 蛇的初始化为两节*/ snake.color[0] = 2; /*两节蛇头初始化为绿色*/ snake.color[1] = 2;snake.x[0] = 100;snake.y[0] = 100;snake.x[1] = 110;snake.y[1] = 100;food.color = random(15) + 1;while(1){while(1){if(food.yes == 0) /* 如果蛇活着*/{while(1){flag = 1;food.yes = 1;food.x = random(56) * 10 + 40;food.y = random(40) * 10 + 50;for(i = 0; i < snake.node; i++){if(food.x == snake.x[i] && food.y == snake.y[i]) flag = 0;}if(flag) break;}}if(food.yes){setcolor(food.color);rectangle(food.x, food.y, food.x + 10, food.y + 10); }for(i = snake.node - 1; i > 0; i--){snake.x[i] = snake.x[i-1];snake.y[i] = snake.y[i-1];}switch(snake.direction)case 1:snake.y[0] -= 10;break;case 2:snake.y[0] += 10;break;case 3:snake.x[0] -= 10;break;case 4:snake.x[0] += 10;break;}for(i = 3; i < snake.node; i++){if(snake.x[i] == snake.x[0] && snake.y[i] == snake.y[0]) {gameover();snake.life = 0;break;}if(snake.x[0] < 40 || snake.x[0] > 590 || snake.y[0] < 50 || snake.y[0] > 440) {gameover();snake.life = 0;}if(snake.life == 0)break;if(snake.x[0] == food.x && snake.y[0] == food.y) /*蛇吃掉食物*/{setcolor(0);rectangle(food.x, food.y, food.x + 10, food.y + 10);snake.x[snake.node] = -20;snake.y[snake.node] = -20;snake.color[snake.node] = food.color;snake.node++;food.yes = 0;food.color = random(15) + 1;prscore();if(score % 100 == 0 && score != 0){for(i = 0; i < snake.node; i++) /* 画出蛇*/{setcolor(snake.color[i]);rectangle(snake.x[i], snake.y[i], snake.x[i] + 10, snake.y[i] + 10);}sound(200);delay(50000);delay(50000);delay(50000);delay(50000);delay(50000);delay(50000);nosound();gamespeed -= 5000;draw();}else{delay(500);nosound();}}for(i = 0; i < snake.node; i++) /* 画出蛇*/{setcolor(snake.color[i]);rectangle(snake.x[i], snake.y[i], snake.x[i] + 10, snake.y[i] + 10); }delay(gamespeed);delay(gamespeed);flag1 = 1;setcolor(0);rectangle(snake.x[snake.node-1], snake.y[snake.node-1],snake.x[snake.node-1] + 10, snake.y[snake.node-1] + 10);if(kbhit() && flag1 == 1) /*如果没按有效键就重新开始循环*/ {flag1 = 0;if(key == esc)exit(0);else if(key == up && snake.direction != 2)snake.direction = 1;else if(key == down && snake.direction != 1)snake.direction = 2;else if(key == left && snake.direction != 4)snake.direction = 3;else if(key == right && snake.direction != 3)snake.direction = 4;}}if(snake.life == 0) /*如果蛇死了就退出循环*/break;}}void main(void){while(1){color = 0;init();cleardevice();showword();draw();gameplay();setcolor(15);settextstyle(0, 0, 2);outtextxy(200, 400, "CONTINUE(Y/N)?");while(1){key = bioskey(0);if(key == Y || key == n || key == esc)break;}if(key == n || key == esc)break;}closegraph();}3、c语言实现移动电话系统#include <stdio.h>#define GRID-SIZE 5#define SELECTED -1 /*低于矩阵中所有元素*/#define TRAFFIC-FILE “traffic.dat”/*关于交通数据的文件*/#define NUM-TRANSMITTERS 10 /*可用的发射器数量*/void get-traffic-data(int commuters[GRID-SIZE][GRID-SIZE],int salesforce[GRID-SIZE][GRID-SIZE],int weekends [GRID-SIZE][GRID-SIZE];voide print-matrix[GRID-SIZE][GRID-SIZE];intmain(void){int commuters[GRID-SIZE][GRID-SIZE];/*上午8:30的交通数据*/ int salesforce[GRID-SIZE][GRID-SIZE]; /*上午11:00的交通数据*/ int weekend[GRID-SIZE][GRID-SIZE];/*周末交通数据*/int commuter-weight, /*通勤人员数据的权重因子*/ sale-weight,/*营销人员数据的权重因子*/weekend-weight;/*周末数据的权重因子*/int location-i,/*每个发射器的位置*/location-j;int current-max;/*和数据中当前的最大值*/int i,j,/*矩阵的循环计数器*/tr;/*发射器的循环计数器*//*填入并显示交通数据*/Get-traffic-data (commuters,salesforce,weekend);Printf(“8:30 A.M.WEEKDAY TRAFFIC DATA 、\n\n”)print-matrix(commuters);printf(“\n\n WEEKEND TRAFFIC DATA\n\n”);print-matrix(salesforce);printf(“\n\n WEEKEND TRAFFIC DATA\n\n”);printf_matrix(weekeng);/*请用户输入权重因子*/printf(“\n\nPlease input the following value:\n”);printf(“Weight (an interger>=0) for the 8:30 muter data>”) scanf(“%d”,&commuter_weight);printf(“weight(an integer>=0) for the weekeng data>”);scanf(“%d”,&weekend_weight);scanf(“%d”,&weekend_weight);/*计算并显示加权后求和的数据*/for (i=0;i<GRID_SIZE;++i)for (j=0;j<GRID_SIZE;++j)summed_data[i][j]=commuter_weight*commuter[i][j]+salesforce_weight*salesforce[i][j]+weekend_weight*weekend[i][j];printf(“\n\nThe weighted,summed data is :\n\n”);printf_matrix(summed_data);/*在summed_data矩阵中找出NUM_TRANSMITTERS个最大值,将坐标临时存储在location_i和location_j中,然后把最后的结果坐标输出*/printf(“\n\nLocations of the %d transmitters:\n\n”,NUM_TRANSMITTERS);for (tr=1;tr<=NUM_TRANSMITTERS;++tr){current_max=SELECTED;/*以一个过低的值为起点开始查找*/for (i=0;i<GRID_SIZE;++i){for(j=0;j<GRID_SIZE;++j){if(current_max<summed_data[i][j]){current_max=summed_data[i][j]){location_i=i;location_j=j;}}}/*将选中的单元赋一较低的值以避免下次再选中这一元素,显示查找结果*/ summed_data[location_i][location_j]=SELECTED;printf(“Tran smitter %3d:at location %3d %3d\n”,tr,location_i,location_j);}return (0);}/**把TRAFFIC_FILE中的交通数据填充到3个GRID_SIZE×GRID_SIZE数组中*/voidget_traffic_data(int commuters[GRID_SIZE],/*输出*/int salesforce[GRID_SIZE][GRID_SIZE],/*输出*/int weekend[GRID_SIZE][GRID_SIZE],/*输出*/{int i,j; /*循环计数器*/FILE *fp; /*文件指针*/fq=fopen(TRAFFIC_FILE,“r”);for(i=0;i<GRID_SIZE;++i)for(j=0;j<GRID_SIZE;++j)fscanf(fp,“%d”,&commnters[i][j];for(i=0;i<GRID_SIZE;++j)for(j=0;j<GRID_SIZE;++j)fscanf(fq,“%d”,&weekend[i][j]);fclose(fq);}/**显示一个GRID_SIZE×GRID_SIZE整数矩阵的内容*/voidprint_matrix(int matrix[GRID_SIZE][GRID_SIZE]){int i,j; /*循环计数器*/for(i=0;i<GRID_SIZE;++j){ for(j=0;j<GRID_SIZE;++J)printf(“%3d”,matrix[i][j]);printf(“\n”);}}4、扑克牌游戏/*************************************Copyright(C) 2004-2005 vision,math,NJU.File Name: guess_card.cppAuthor: vision Version: 1.0 Data: 23-2-2004Description: 给你9张牌,然后让你在心中记住那张牌,然后电脑分组让你猜你记住的牌在第几组,然后猜出你记住的那张牌.Other: 出自儿童时的一个小魔术History:修改历史**************************************/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#include <assert.h>#define CARDSIZE 52 /*牌的总张数*/#define SUITSIZE 13 /*一色牌的张数*//*扑克牌结构*/typedef struct Card{char val;/*扑克牌面上的大小*/int kind :4; /*扑克牌的花色*/}Card;/*************************************************Function: // riffleDescription: // 洗牌,然后随机的得到9张牌,要求九张牌不能有重复.Calls: //Called By: // main()Table Accessed: //被修改的表(此项仅对于牵扯到数据库操作的程序)Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)Input: //Card card[] 牌结构, int size 结构数组的大小Output: //Return: // voidOthers: // 此函数修改card[]的值,希望得到九张随机牌Bug: //此函数有bug,有时会产生两个相同的牌,有待修订*************************************************/void riffle(Card *cards, int size);/*************************************************Function: // showDescription: // 显示数组的内容Calls: //Called By: // main()Table Accessed: //被修改的表(此项仅对于牵扯到数据库操作的程序)Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)Input: //Card *card 牌结构指针, int size 结构数组的大小Output: //Return: // voidOthers: //*************************************************/void show(const Card *cards, int size);/*************************************************Function: // groupingDescription: //把9张牌分别放到3个数组中,每组3张,a.e分组Calls: //Called By: // main()Table Accessed: //被修改的表(此项仅对于牵扯到数据库操作的程序)Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)Input: //Card *card 牌结构指针, int size 结构数组的大小Output: //Return: // voidOthers: // 此函数修改*carr1,*carr2,* carr3的值*************************************************/void grouping(const Card *cards, Card *carr1, Card *carr2, Card *carr3); /*************************************************Function: // result_processDescription: //用递归计算,所选的牌Calls: // rshiftCalled By: // main()Table Accessed: //被修改的表(此项仅对于牵扯到数据库操作的程序)Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)Input: //Card *carr1, Card *carr2, Card *carr3Output: //Return: // voidOthers: // 此函数修改*carr1,*carr2,* carr3的值*************************************************/Card* result_process(Card *carr1, Card *carr2, Card *carr3, int counter); /*************************************************Function: // rshiftDescription: //右移操作Calls: //Called By: // result_processTable Accessed: //被修改的表(此项仅对于牵扯到数据库操作的程序)Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)Input: //Card *carr1, Card *carr2, Card *carr3 ,int counter Output: //Return: // Card*Others: // 此函数修改*carr1,*carr2,* carr3的值*************************************************/void rshift(Card *carr1, Card *carr2, Card *carr3, int counter);void main(){Card cards[9]; /*存放九张牌*/Card carr1[3]; /*第一组牌,cards array 1*/Card carr2[3]; /*第二组牌,cards array 2*/Card carr3[3]; /*第三组牌,cards array 3*/int select = 0; /*玩家的选择*/Card *selected_card;/*存放玩家所记住(选)的牌*/int counter = 0;riffle(cards, 9); /*洗牌,得到九张牌*/puts("请记住一张牌千万别告诉我!最多经过下面三次我与你的对话,我就会知道你所记的那张牌!");puts("如果想继续玩,请准确的回答我问你的问题,根据提示回答!");puts("请放心,我不会问你你选了哪张牌的!");grouping(cards, carr1, carr2, carr3); /*把9张牌分别放到3个数组中,每组3张,a.e 分组*/show(carr1, 3);show(carr2, 3);show(carr3, 3);puts("请告诉我你记住的那张牌所在行数");select = getchar();switch(select)/*分支猜你玩家记住的牌*/{case '1':selected_card = result_process(carr1, carr2, carr3, counter); break;case '2':selected_card = result_process(carr2, carr3, carr1, counter); break;case '3':selected_card = result_process(carr3, carr1, carr2, counter); break;default:puts("你在撒谎!不和你玩了!");fflush(stdin);getchar();exit(0);}if( selected_card ==NULL){fflush(stdin);getchar();exit(0);}puts("你猜的牌为:");show(selected_card, 1);puts("我猜的对吧,哈哈~~~~");fflush(stdin);getchar();}/*riffle的原代码*/void riffle(Card *cards, int size){char deck[CARDSIZE];/*临时数组,用于存储牌*/ unsigned int seed;/*最为产生随机数的种的*/ int deckp = 0; /*在牌的产生中起着指示作用*/seed = (unsigned int)time(NULL);srand(seed);/*洗牌*/while (deckp < CARDSIZE){char num = rand() % CARDSIZE;if ((memchr(deck, num, deckp)) == 0){assert(!memchr(deck,num,deckp));deck[deckp] = num;deckp++;}}/*找9张牌给card*/for (deckp = 0; deckp < size; deckp++){div_t card = div(deck[deckp], SUITSIZE);cards[deckp].val = "A23456789TJQK"[card.rem]; /*把余数给card.val*/ cards[deckp].kind = "3456"[card.quot]; /*把商给card.kind*/}}/*show的原代码,将会自动换行*/void show(const Card *cards, int size){for(int i = 0; i < size; i++){printf("%c%c ",cards[i].kind,cards[i].val);if( (i !=0) && (((i+1 ) % 3) == 0))puts("");}puts(""); /*自动换行*//*grouping 的原代码*/void grouping(const Card *cards, Card *carr1, Card *carr2, Card *carr3) {int i = 0;/*循环参数*//*分给carr1三个数*/while (i < 3){carr1[i].val = cards[i].val;carr1[i].kind = cards[i].kind;i++;}/*分给carr2接下来的三个数*/while (i < 6){carr2[i-3].val = cards[i].val;carr2[i-3].kind = cards[i].kind;i++;}/*分给carr3接下来的三个数*/while (i < 9)carr3[i-6].val = cards[i].val;carr3[i-6].kind = cards[i].kind;i++;}}/*rshift的实现*/void rshift(Card *carr1, Card *carr2, Card *carr3, int counter) {Card temp2;/*用于存放carr2[counter]*/Card temp3;/*用于存放carr3[counter]*//*temp = carr2*/temp2.val = carr2[counter].val;temp2.kind = carr2[counter].kind;/*carr2 = carr1*/carr2[counter].val = carr1[counter].val;carr2[counter].kind = carr1[counter].kind;/*temp3 = carr3*/temp3.val = carr3[counter].val;temp3.kind = carr3[counter].kind;/*carr3 = carr2*/carr3[counter].val = temp2.val;carr3[counter].kind = temp2.kind;/*carr1 = carr3*/carr1[counter].val = temp3.val;carr1[counter].kind = temp3.kind;}Card* result_process(Card *carr1, Card *carr2, Card *carr3, int counter) {rshift(carr1, carr2, carr3, counter); /* 把数组的第一个元素依次右移*/ if(counter == 2){return(&carr2[2]);}show(carr1, 3);show(carr2, 3);show(carr3, 3);puts("请给出你记住的牌所在行数:");fflush(stdin);int input = 1;input = getchar(); /*获取你选的组*/switch(input){case '1':return(result_process(carr1, carr2, carr3, ++counter));break;case '2':return(&carr2[counter]);break;default:puts("你在撒谎!不和你玩了!");return NULL;}}5、C语言实现打字游戏#include "stdio.h"#include "time.h"#include "stdlib.h"#include "conio.h"#include "dos.h"#define xLine 70#define yLine 20#define full 100#define true 1#define false 0/*---------------------------------------------------------------------*/void printScreen(int level,int right,int sum,char p[yLine][xLine])/* 刷新屏幕的输出图像*/{int i,j;clrscr();printf("level:%d Press 0 to exit;1 topause score:%d/%d\n",level,right,sum);/* 输出现在的等级,击中数和现在已下落总数*/printf("----------------------------------------------------------------------\n");for (i=0;i<yLine;i++){for(j=0;j<xLine;j++)printf ("%c",p[i][j]);printf("\n");}/* for (i) */printf ("----------------------------------------------------------------------\n"); }/* printScreen *//*---------------------------------------------------------------------*/void leave()/* 离开程序时,调用该函数结束程序。
编程源代码
《C语言程序设计教程》例题源码第一章【例1.1】打印“九江职业大学”的汉语拼音。
程序源码如下:main(){printf(“jiu jiang zhi ye da xue!”);}【例1.2】求任意两个整数的积。
程序源码如下:#include <stdio.h>int mul(int a,int b);main(){int x,y,z;printf("input two numbers:\n");scanf("%d%d",&x,&y);z=mul(x,y);printf("mul=%d",z);}int mul(int a,int b){int c;c=a*b;return c;}【例1.3】求整数a、b的和。
程序源码如下:main(){int a, b;int sum;scanf(“%d,%d”,&a,&b);sum = a + b;printf(“%d”,sum);}【例2.1】转义字符的使用。
程序源码如下:main(){printf("\x4f\x4b\x21\n");printf("\112 \112\132\104\n");}【例2.2】符号常量的使用。
程序源码如下:#define PI 3.14159265main(){float r,m,s;r=1.2;m=2* PI *r;s= PI *r*r;printf("%f,%f",m,s);}【例2.3】整型变量的定义与使用。
程序源码如下:main(){int x, y, m;unsigned u, n;x=30; y=18; u=32800;m=x+y; n=y+u;printf("x+y=%d, y+u=%d\n",m,n); }【例2.4】整型变量的定义与使用。
经典C源代码300例
题目:八进制转换为十进制
1.程序分析:
2.程序源代码:
main()
{ char *p,s[6];int n;
p=s;
gets(p);
n=0;
while(*(p)!='\0')
{n=n*8+*p-'0';
p++;}
printf("%d",n);
}
==============================================================
【程序15】
题目:时间函数举例4,一个猜数游戏,判断一个人反应快慢。(版主初学时编的)
1.程序分析:
2.程序源代码:
#include "time.h"
n++;}
getch();
}
==============================================================
【程序10】
题目:某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的,加密规则如下:
每位数字都加上5,然后用和除以10的余数代替该数字,再将第一位和第四位交换,第二位和第三位交换。
1.程序分析:
2.程序源代码:
main()
{int i,a,n=1;
while(n<=7)
{ do {
scanf("%d",&a);
}while(a<1¦¦a>50);
纯C语言写的一个小型游戏 源代码
/* A simple game*//*CopyRight: Guanlin*/ #include<stdio.h>#include<stdlib.h>#include<string.h>#include<time.h>#include<conio.h>#include<process.h> ?struct object_fix{char name[20];char id[5];char desc[500];char action[30];char im[5];};struct object_move {char name[20];char id[5];char desc[500];int loc;int pwr;int strg;char im[5];};struct rover{char name[20];char id[5];char desc[500];int pwr;int strg;int location[2];char im[5];};struct map /* this is the map structure*/{char data[20];char add_data[20];int amount;int x; /* this were the successor keeps it's x & y values*/int y;};struct location /*this structure is for the successor lister*/{float height;char obj;};void stats_update(int selected, struct rover *p_rover){switch (selected){case 1:if(p_rover->pwr < 7)printf("\n\nYou do not have enough power to perform this action!\n\n"); else{(p_rover->pwr) -= 7;printf("You have destroyed the object!\n\n");}break;case 2:if(p_rover->pwr < 3)printf("\n\nYou do not have enough power to perform this action!\n\n");else if(p_rover->strg > 90)printf("\n\nYou do not have enough storage space for this object!\n\n");else{(p_rover->pwr) -= 3;(p_rover->strg) += 10;printf("You have collected a sample of the object!\n\n");}break;case 3:p_rover->pwr -= 10; /*Distance around object- value gained from mapper module. 1 square = -1 power*/printf("You have avoided the object!\n\n");break;case 4:p_rover->pwr -= 2;printf("You have driven through the obstacle!\n\n");break;case 5:if(p_rover->pwr == 100)printf("\n\nYou do not need to charge up!\n\n");else{p_rover->pwr = 100;printf("You have charged up your rover!\n\n");}break;default:printf("\n\n*****ERROR*****\nInvalid Selection\n\n");break;}}void action(char object, struct rover *p_rover){int selection;switch(object){case 1:printf("\nYou have encountered: A Sandy Rock\n\n");printf("This object can be:\n1.\tDestroyed\n2.\tCollected\nPlease choose action 1 or 2:\t"); scanf("%d", &selection);stats_update(selection, p_rover);break;case 2:printf("\nYou have encountered: A Solid Rock\n\n");printf("This object can be:\n1.\tAvoided\n2.\tCollected\nPlease choose action 1 or 2:\t"); scanf("%d", &selection);if (selection == 1)selection = 3;stats_update(selection, p_rover);break;case 3:printf("\nYou have encountered: A Mountain\n\n");printf("This object can be:\n1.\tAvoided\nPlease enter 1:\t");scanf("%d", &selection);selection = 3;stats_update(selection, p_rover);break;case 4:printf("\nYou have encountered: Dust\n\n");printf("This object can be:\n1.\tDriven through\n2.\tCollected\nPlease choose action 1 or 2:\t"); scanf("%d", &selection);if (selection == 1)selection = 4;stats_update(selection, p_rover);break;case 5:printf("\nYou have encountered: A Sheer Valley\n\n");printf("This object can be:\n1.\tAvoided\nPlease enter 1:\t");scanf("%d", &selection);selection = 3;stats_update(selection, p_rover);break;case 6:printf("\nYou have encountered: A Gentle Valley\n\n");printf("This object can be:\n1.\tDriven through\n2.\tAvoided\nPlease choose action 1 or 2:\t"); scanf("%d", &selection);if (selection == 1)selection = 4;if (selection == 2)selection = 3;stats_update(selection, p_rover);break;case 7:printf("\nYou have encountered: A 'Martian' Tree\n\n");printf("This object can be:\n1.\tDestroyed\n2.\tCollected\n3.\tAvoided\nPlease choose action 1, 2 or 3:\t");scanf("%d", &selection);stats_update(selection, p_rover);break;case 8:printf("\nYou have encountered: Shallow Water\n\n");printf("This object can be:\n1.\tDriven through\n2.\tCollected\n3.\tAvoided\nPlease choose action 1, 2 or 3:\t");scanf("%d", &selection);if (selection == 1)selection = 4;stats_update(selection, p_rover);break;case 9:printf("\nYou have encountered: Deep Water\n\n");printf("This object can be:\n1.\tAvoided\n2.\tCollected\nPlease choose action 1 or 2:\t");scanf("%d", &selection);if (selection == 1)selection = 3;stats_update(selection, p_rover);break;case 10:printf("\nYou have encountered: An Aggressive Alien\n\n");printf("This object can be:\n1.\tDestroyed\nPlease enter 1:\t");scanf("%d", &selection);selection = 1;stats_update(selection, p_rover);break;case 11:printf("\nYou have encountered: A Non-Aggressive Alien\n\n");printf("This object can be:\n1.\tAvoided\nPlease enter 1:\t");scanf("%d", &selection);selection = 3;stats_update(selection, p_rover);break;case 12:printf("\nYou have encountered: Another Rover\n\n");printf("This object can be:\n1.\tAvoided\nPlease enter 1:\t");scanf("%d", &selection);selection = 3;stats_update(selection, p_rover);break;case 13:printf("\nYou have encountered: A Power Station\n\n");printf("You can:\n1.\tCharge up\n2.\tAvoid\nPlease choose action 1 or 2:\t");scanf("%d", &selection);if (selection == 1)selection = 5;if (selection == 2)selection = 3;stats_update(selection, p_rover);break;default:printf("\n\n*****ERROR*****\n\n");break;}}void show_map(struct map *number, struct map *number_2, struct object_fix *rsny, struct object_fix *rsld, struct object_fix *mnt, struct object_fix *dst,struct object_fix *vshr, struct object_fix *vgnt, struct object_fix *mtre,struct object_fix *wshl, struct object_fix *wdp, struct object_move *aagr,struct object_move *anon, struct object_move *rvr, struct object_move *pstn, struct rover*p_rover) /*the show map function calling number and number_2 from the map structure to see if moveable objects are needed*/{struct map f_map[8][8]; /*8 by 8 map*/int i,j, rx, ry, object; /*this is your x and y value in your map (f_map)*/system("cls"); /* this is your x and y value in your map (f_map)*///srand(time(NULL)); /*calling the time from include to gather random variables*/for(i=0;i<8;i++) /*for loop to copy all your fixed object lists into f_map so they can be displayed*/ {for(j=0;j<8;j++)strcpy(f_map[i][j].data,"");}if (number->amount>0) /*this is looking at the add function to see weather or not there are aliens in f_map*/{for(i=0;i<number->amount;i++) /*allocating the number of aliens in f_map*/strcpy(f_map[rand()%8][rand()%8].data, anon->im); /*randomizing their position*/}if(number_2->amount>0) /*repeat of above just for rovers instead*/{for(i=0;i<number_2->amount;i++)strcpy(f_map[rand()%8][rand()%8].data, rvr->im);}rx= p_rover->location[0];ry= p_rover->location[1];strcpy(f_map[1][0].data, wdp->im);strcpy(f_map[4][0].data, mnt->im);strcpy(f_map[5][0].data, mnt->im);strcpy(f_map[3][1].data, dst->im);strcpy(f_map[4][1].data, dst->im);strcpy(f_map[3][2].data, dst->im);strcpy(f_map[4][2].data, dst->im);strcpy(f_map[0][3].data, mnt->im);strcpy(f_map[1][3].data, mnt->im);strcpy(f_map[3][3].data, vshr->im);strcpy(f_map[4][3].data, dst->im);strcpy(f_map[5][3].data, vgnt->im);strcpy(f_map[3][4].data, vshr->im);strcpy(f_map[4][4].data, dst->im);strcpy(f_map[5][4].data, vgnt->im);strcpy(f_map[2][5].data, wshl->im);strcpy(f_map[3][5].data, wshl->im);strcpy(f_map[4][5].data, wshl->im);strcpy(f_map[1][6].data, pstn->im);strcpy(f_map[2][6].data, wdp->im);strcpy(f_map[3][6].data, wdp->im);strcpy(f_map[4][6].data, wshl->im);strcpy(f_map[7][6].data, mnt->im);strcpy(f_map[0][7].data, mnt->im);strcpy(f_map[1][7].data, wdp->im);strcpy(f_map[2][7].data, wshl->im);strcpy(f_map[3][7].data, wshl->im);strcpy(f_map[6][7].data, mnt->im);strcpy(f_map[rx][ry].data, p_rover->im);if((rx == 1 && ry == 0) || (rx == 2 && ry == 6) ||(rx == 3 && ry == 6) ||(rx == 1 && ry == 7)) {object = 9;action(object, p_rover);}else if((rx == 4 && ry == 0) || (rx == 5 && ry == 0) || (rx == 0 && ry == 3) || (rx == 1 && ry == 3) || (rx == 7 && ry == 6) || (rx == 0 && ry == 7) || (rx == 6 && ry == 7)){object = 3;action(object, p_rover);}else if((rx == 3 && ry== 1) || (rx == 4 && ry == 1) || (rx == 3 && ry== 2) || (rx == 4 && ry == 2) || (rx == 4 && ry == 3) || (rx == 4 && ry == 4)){object = 4;action(object, p_rover);}else if((rx == 3 && ry == 3) || (rx == 3 && ry == 4)){object = 5;action(object, p_rover);}else if((rx == 5 && ry == 3) || (rx == 5 && ry == 4)){object = 6;action(object, p_rover);}else if((rx == 2 && ry == 5) || (rx == 3 && ry == 5 ) || (rx == 4 && ry == 5) || (rx == 4 && ry== 6) ||(rx == 2 && ry == 7) || (rx == 3 && ry == 7)){object = 8;action(object, p_rover);}else if(rx == 1 && ry == 6){object = 13;action(object, p_rover);}i=0; /*re-allocate i to 0 so map is printed from start*/for(i=0;i<8;i++) /*8 by 8 map*/{printf("+----+----+----+----+----+----+----+----+\n");for(j=0;j<8;j++) /*8 by 8 map*/{if(strlen(f_map[i][j].data)!=0) /*if function to print nothing but 4 spaces if there in nothing allocated in [i][j]*/printf("|%4s",f_map[i][j].data);elseprintf("| "); /*end of coloumn visible map*/}printf("|\n"); /*end of last coloumn visible map*/}printf("+----+----+----+----+----+----+----+----+\n"); /*bottom of map*/printf("\n");printf("\270");printf("Group B\n");}??void add_obj(struct map *number, struct map *number_2, struct object_fix *rsny,struct object_fix *rsld, struct object_fix *mnt, struct object_fix *dst,struct object_fix *vshr, struct object_fix *vgnt, struct object_fix *mtre,struct object_fix *wshl, struct object_fix *wdp, struct object_move *aagr,struct object_move *anon, struct object_move *rvr, struct object_move *pstn, struct rover*p_rover) /* add movable object function, *number=aliens, *number_2=rovers*/{int object, t, f;char alien;char rover;printf("This is the add movable object function\n");printf("how many aliens would you like?\n");scanf("%d",&t); /*user input of amount of aliens*/number->amount=t; /*saving the number in structure map-amount*/printf("how many rovers would you like?\n");scanf("%d",&f); /*user input of amount of rovers*/number_2->amount=f; /*saving the number in structure map-amount*/show_map(number, number_2, rsny, rsld, mnt, dst, vshr, vgnt, mtre, wshl,wdp, aagr, anon, rvr, pstn, p_rover); /*go to show map function with the number of aliens and number of rovers*/}?void successor() /*this functions askes the user for the location and then were they want to go from there, printing out that location*/{struct map location;int menu;int ncol,nrow; /*we can change the n value depends how large the map you need.*/ncol=8;nrow=8;printf("This is the successor function\n");printf("Enter the \nx=\ty=\n");scanf("%d%d",&location.x,&location.y); /*saving location in map location x and y*/printf("Enter operator choice\n");printf("1=left\n");printf("2=right\n");printf("3=upwards\n");printf("4=down\n");printf("5=quit\n");scanf("%d",&menu);switch(menu) /*menu switch to show the position above, underneith, and next to the rover*/ {case 1:location.x=location.x-1;printf("The position is [%d,%d]\n",location.x,location.y);break;?case 2:location.x=location.x+1;printf("The position is [%d,%d]\n",location.x,location.y);break;case 3:location.y=location.y-1;printf("The position is [%d,%d]\n",location.x,location.y);break;case 4:location.y=location.y+1;printf("The position is [%d,%d]\n",location.x,location.y);break;case 5:printf("*****back to program*****\n"); /* Quits the program and prints out the message */ break;default:printf("*****Please enter an integer from 1-5*****\n"); /* A default option if the user enters an incorrect value */break;}printf("\n");}?void help(struct map *number, struct map *number_2, struct object_fix *rsny,struct object_fix *rsld, struct object_fix *mnt, struct object_fix *dst,struct object_fix *vshr, struct object_fix *vgnt, struct object_fix *mtre,struct object_fix *wshl, struct object_fix *wdp, struct object_move *aagr,struct object_move *anon, struct object_move *rvr, struct object_move *pstn, struct rover *p_rover) /*help function*/{int i;char mov_obj;printf("++This is the help function++\n");printf("| 1-successor |\n");printf("| 2-add movable obj |\n");printf("| 3-remove moveabla obj |\n\n");printf("\t* * *\n");printf("\t Index: \n\n");printf("\t Sandy Rock: *\n");printf("\t Solid Rock: o\n");printf("\t Water:\262\262\262\262 \n");printf("\t Shallow water: \260\260\260\260\n");printf("\t Mountain: ^^^^\n");printf("\t Dust: ....\n");printf("\t* sher valley: VVVV *\n");printf("\t shallow valey: vvvv\n");printf("\t power station: \025\n");printf("\t alien: *_*\n");printf("\t other rovers: #\n");printf("\t* * *\n");scanf("%d",&i);switch(i) /*switch menu to go to the successor function, or the add/remove function*/{case 1:successor();break;case 2:add_obj(number, number_2, rsny, rsld, mnt, dst, vshr,vgnt, mtre, wshl, wdp, aagr, anon, rvr, pstn, p_rover);break;case 3:add_obj(number, number_2, rsny, rsld, mnt, dst, vshr,vgnt, mtre, wshl, wdp, aagr, anon, rvr, pstn, p_rover);break;default:printf("choose a number from the list\n");}}void direction(int input, struct map *number, struct map *number_2, struct object_fix *rsny, struct object_fix *rsld, struct object_fix *mnt, struct object_fix *dst,struct object_fix *vshr, struct object_fix *vgnt, struct object_fix *mtre,struct object_fix *wshl, struct object_fix *wdp, struct object_move *aagr,struct object_move *anon, struct object_move *rvr, struct object_move *pstn, struct rover *p_rover){switch(input){case 119: /*w = up*/if(p_rover->location[0] > 0)p_rover->location[0]--;p_rover->pwr--;}else p_rover->location[0]=7; break;case 115: /*s = down*/if(p_rover->location[0] < 7) {p_rover->location[0]++;p_rover->pwr--;}else p_rover->location[0]=0; break;case 97: /*a = left*/if(p_rover->location[1] > 0) {p_rover->location[1]--;p_rover->pwr--;}else p_rover->location[1]=7; break;case 100: /*d = right*/if(p_rover->location[1] < 7) {p_rover->location[1]++;p_rover->pwr--;}else p_rover->location[1]=0; break;default:printf("Invalid operator!\n\n"); break;}int control(int input){input = _getch();return input;}?void main(void){int menu;int quit = 0;int input = 0;struct object_fix rsny, rsld, mnt, dst, vshr, vgnt, mtre, wshl, wdp;struct object_move aagr, anon, rvr, pstn;struct rover p_rover;struct map number, number_2;number.amount = 0; /* setting the initial amount of aliens as 0*/number_2.amount = 0;strcpy(,"Sandy Rock");strcpy(rsny.id,"RSNY");strcpy(rsny.desc,"A rock made of softer material that can be destroyed or collected");strcpy(rsny.im,"*");strcpy(,"Solid Rock");strcpy(rsld.id,"RSLD");strcpy(rsld.desc,"A rock made of hard material that cannot be destroyed, but can be collected or avoided");strcpy(rsld.im,"O");strcpy(,"Mountain");strcpy(mnt.id,"MNT");strcpy(mnt.desc,"A large obstacle made of different materials that can only be avoided");strcpy(mnt.im,"^^^^");strcpy(,"Dust");strcpy(dst.id,"DST");strcpy(dst.desc,"Small particles of sand, rock, etc. that may reduce visibility or slow down the vehicle, can be driven through but has extra power consumption");strcpy(dst.im,"....");strcpy(,"Sheer Valley");strcpy(vshr.id,"VSHR");strcpy(vshr.desc,"A depression in the land with sheer sides, it cannot be driven through and must be avoided");strcpy(vshr.im,"VVVV");strcpy(,"Gentle Valley");strcpy(vgnt.id,"VGNT");strcpy(vgnt.desc,"A depression in the land with gentle sides, it can be driven through with extra power consumption or can be avoided");strcpy(vgnt.im,"vvvv");strcpy(,"'Martian' Tree");strcpy(mtre.id,"MTRE");strcpy(mtre.desc,"A woody plant native to Mars, it may have the same characteristics as an 'Earth' tree");strcpy(mtre.im,"\330");strcpy(,"Shallow Water");strcpy(wshl.id,"WSHL");strcpy(wshl.desc,"A shallow body of water that can be driven through, can be in the form of a small puddle or a large lake");strcpy(wshl.im,"\260\260\260\260");strcpy(,"Deeper Water");strcpy(wdp.id,"WDP");strcpy(wdp.desc,"A deeper body of water that cannot be driven through, commonly in the form of a large lake or river");strcpy(wdp.im,"\262\262\262\262");strcpy(,"Aggressive Alien");strcpy(aagr.id,"AAGR");strcpy(aagr.desc,"An alien creature that will move upon its own accord, may become aggressive on contact");strcpy(aagr.im,"*_*");strcpy(,"Non-Aggressive Alien");strcpy(anon.id,"ANON");strcpy(anon.desc,"An alien creature that will move upon its own accord, will not become aggressive on contact");strcpy(anon.im,"*_*");strcpy(,"Rover");strcpy(rvr.id,"RVR");strcpy(rvr.desc,"A Mars Rover vehicle to transport around the map, it can destroy, collect or avoid objects. It has a power and object storage");strcpy(rvr.im,"#");strcpy(,"Power Station");strcpy(pstn.id,"PSTN");strcpy(pstn.desc,"A movable power station that will restore power to the Mars Rovers");strcpy(pstn.im,"\025");strcpy(p_,"User Mars Rover");strcpy(p_rover.id,"URVR");strcpy(p_rover.desc,"A Mars vehicle that is controlled by the user. Explores the map collecting, destroying and avoiding objects");strcpy(p_rover.im,"<^^>");p_rover.pwr = 100;p_rover.strg = 0;p_rover.location[0] = 7;p_rover.location[1] = 0; /****************************************************/printf("*******************************START********************************\n\n"); do{ /* Object will be gained from mapper */printf("+++++++++++++++++++++++++++++++MENU+++++++++++++++++++++++ ++++++++++\n");printf("Please choose one of the following option:\n");printf("1 - Start\n");printf("2 - Controls\n");printf("3 - Show Map\n");printf("4 - Help Menu\n");printf("5 - Quit\n\n");printf("--------------->");scanf("%d", &menu);switch (menu){case 1:show_map(&number, &number_2, &rsny, &rsld, &mnt, &dst, &vshr,&vgnt, &mtre, &wshl, &wdp, &aagr, &anon, &rvr, &pstn, &p_rover);while (quit != 1 ){input = control(input);if (input == 27)quit = 1;else{if(p_rover.pwr>0){direction(input, &number, &number_2, &rsny, &rsld, &mnt, &dst, &vshr,&vgnt, &mtre, &wshl, &wdp, &aagr, &anon, &rvr, &pstn, &p_rover);show_map(&number, &number_2, &rsny, &rsld, &mnt, &dst, &vshr,&vgnt, &mtre, &wshl, &wdp, &aagr, &anon, &rvr, &pstn, &p_rover);printf("Rover stats: \nPower: %d%% \nStorage: %d%%\n", p_rover.pwr, p_rover.strg);}else{printf("*******************END GAME********************\n\n"); // ends game when charge = 0.}}}break;case 2:quit = 0;printf("\n");printf("* * *\n");printf(" Controls \n"); // menu for the keys used on the keyboard to move the rover in different directions.printf(" Up : W \n");printf(" Down : S \n");printf("* Left : A * \n");printf("* Right : D * \n");printf(" choose : Enter \n");printf("* * *\n");break;case 3:quit = 0;show_map(&number, &number_2, &rsny, &rsld, &mnt, &dst, &vshr,&vgnt, &mtre, &wshl, &wdp, &aagr, &anon, &rvr, &pstn, &p_rover);break;case 4:quit = 0;help(&number, &number_2, &rsny, &rsld, &mnt, &dst, &vshr,&vgnt, &mtre, &wshl, &wdp, &aagr, &anon, &rvr, &pstn, &p_rover);break;printf("*********************END***********************\n"); break;}}while(menu!=5);printf("\n -----Please press any key to exit game-----\n");getch();}。
C语言小游戏源代码《打砖块》
C语言小游戏源代码《打砖块》#include “graphics.h“#include "stdio.h"#include "conio.h" /*所需的头文件*/int on; /*声明具有开关作用的全局变量*/static int score; /*声明静态的记分器变量*//* 定义开始界面函数*/int open(){setviewport(100,100,500,380,1); /*设置图形窗口区域*/ setcolor(4); /*设置作图色*/rectangle(0,0,399,279); /*以矩形填充所设的图形窗口区域*/ setfillstyle(SOLID_FILL,7); /*设置填充方式*/floodfill(50,50,4); /*设置填充范围*/setcolor(8);settextstyle(0,0,9); /*文本字体设置*/outtextxy(90,80,"BALL"); /*输出文本内容*/settextstyle(0,0,1);outtextxy(110,180,"version 1.0");outtextxy(110,190,"made by ddt");setcolor(128);settextstyle(0,0,1);outtextxy(120,240,"Press any key to continue......");}/*定义退出界面函数*/int quitwindow(){char s; /*声明用于存放字符串的数组*/setviewport(100,150,540,420,1);setcolor(YELLOW);rectangle(0,0,439,279);setfillstyle(SOLID_FILL,7);floodfill(50,50,14);setcolor(12);settextstyle(0,0,8);outtextxy(120,80,"End");settextstyle(0,0,2);outtextxy(120,200,"quit? Y/N");sprintf(s,"Your score is:%d",score);/*格式化输出记分器的值*/ outtextxy(120,180,s);on=1; /*初始化开关变量*/}/*主函数*/main(){int gdriver,gmode;gdriver=DETECT; /*设置图形适配器*/gmode=VGA; /*设置图形模式*/registerbgidriver(EGAVGA_driver); /*建立独立图形运行程序*/initgraph(gdriver,gmode,""); /*图形系统初试化*/setbkcolor(14);open(); /*调用开始界面函数*/getch(); /*暂停*/while(1) /*此大循环体控制游戏的反复重新进行*/{intdriver,mode,l=320,t=400,r,a,b,dl=5,n,x=200,y=400,r1=10,dx=-2,dy=-2;/*初始化小球相关参数*/int left,top,right,bottom,i,j,k,off=1,m,num;/*方砖阵列相关参数*/static int pp;static int phrase; /*一系列起开关作用的变量*/int oop=15;pp=1;score=0;driver=DETECT;mode=VGA;registerbgidriver(EGAVGA_driver);initgraph(driver,mode,"");setbkcolor(10);cleardevice(); /*图形状态下清屏*/clearviewport(); /*清除现行图形窗口内容*/b=t+6;r=l+60;setcolor(1);rectangle(0,0,639,479);setcolor(4);rectangle(l,t,r,b);setfillstyle(SOLID_FILL,1);floodfill(l+2,t+2,4);for(i=0,k=0;ii++) /*此循环绘制方砖阵列*/{top[i]=k;bottom[i]=top[i]+20;k=k+21;oop--;for(j=0,m=0;jj++){left[j]=m;right[j]=left[j]+80;m=m+81;setcolor(4);rectangle(left[j],top[i],right[j],bottom[i]); setfillstyle(SOLID_FILL,j+oop);floodfill(left[j]+1,top[i]+1,4);num[i][j]=pp++;}}while(1) /*此循环控制整个动画*/{while(!kbhit()){x=x+dx; /*小球运动的圆心变量控制*/ y=y+dy;if(x+r1r||x+r1r){ phrase=0;}if((x-r1=r||x+r1=r)x+r1=l){if(yt)phrase=1;if(y+r1=tphrase==1){dy=-dy;y=t-1-r1;}}if(off==0)continue;for(i=0;ii++) /*此循环用于判断、控制方砖阵列的撞击、擦除*/for(j=0;jj++){if((x+r1=right[j]x+r1=left[j])||(x-r1=right[j]x-r1=left[j])){if(( y-r1top[i]y-r1=bottom[i])||(y+r1=top[i]y+r1=bottom[i] )) {if(num[i][j]==0){continue; }setcolor(10);rectangle(left[j],top[i],right[j],bottom[i]);setfillstyle(SOLID_FILL,10);floodfill(left[j]+1,top[i]+1,10);dy=-dy;num[i][j]=0;score=score+10;printf(“%d\b\b\b",score);}}if((y+r1=top[i]y+r1=bottom[i])||(y-r1=top[i]y-r1=bottom[i])) {if((x+r1=left[j]x+r1right[j])||(x-r1=right[j]x-r1left[j])){if(num[i][j]==0){ continue;}setcolor(10);rectangle(left[j],top[i],right[j],bottom[i]); setfillstyle(SOLID_FILL,10);floodfill(left[j]+1,top[i]+1,10);dx=-dx;num[i][j]=0;score=score+10;printf("%d\b\b\b",score);}}}if(x+r1639) /*控制小球的弹射范围*/ {dx=-dx;x=638-r1;}if(x=r1){dx=-dx;x=r1+1;}if(y+r1=479){off=0;quitwindow();break;}if(y=r1){dy=-dy;y=r1+1;}if(score==560){off=0;quitwindow();break;}setcolor(6);circle(x,y,r1);setfillstyle(SOLID_FILL,14);floodfill(x,y,6);delay(1000);setcolor(10);circle(x,y,r1);setfillstyle(SOLID_FILL,10);floodfill(x,y,10);}a=getch();setcolor(10);rectangle(l,t,r,b);setfillstyle(SOLID_FILL,10);floodfill(l+2,t+2,10);if(a==77l=565)/*键盘控制设定*/{dl=20;l=l+dl;}if(a==75l=15){dl=-20;l=l+dl;}if(a=='y'on==1)break;if(a=='n'on==1)break;if(a==27){quitwindow();off=0;}r=l+60;setcolor(4);rectangle(l,t,r,b);setfillstyle(SOLID_FILL,1);floodfill(l+5,t+5,4);delay(100);}if(a=='y'on==1) /*是否退出游戏*/ {break;}if(a=='n'on==1){ continue;} } closegraph(); }。
经典C语言源代码
经典C语言源代码1、(1)某年某月某日是星期几#include<stdio.h>int main(){int year, month, day;while (scanf_s("%d%d%d", &year, &month, &day) != EOF){if (month == 1 || month == 2)//判断month是否为1或2{year--;month += 12;}int c = year / 100;int y = year - c * 100;int week = (c / 4) - 2 * c + (y + y / 4) + (13 * (month + 1) /5) + day - 1;while (week<0) { week += 7; }week %= 7;switch (week){case 1:printf("Monday\n"); break;case 2:printf("Tuesday\n"); break;case 3:printf("Wednesday\n"); break;case 4:printf("Thursday\n"); break;case 5:printf("Friday\n"); break;case 6:printf("Saturday\n"); break;case 0:printf("Sunday\n"); break;}}return 0;}1、(2)某年某月某日是第几天(一维数组)#include "stdio.h"void main() {int i, flag, year, month, day, dayth;int month_day[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };printf("请输入年/月/日:\n");scanf_s("%d/%d/%d", &year, &month, &day);dayth = day;flag = (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);if (flag)month_day[2] = 29;for (i = 1; i < month; i++)dayth = dayth + month_day[i];printf("%d/%d/%d是第%d天\n", year, month, day, dayth); }2、30个数中找最小的数及其位置#include "stdio.h"# define SIZE 30void main() {int i;float data[SIZE];int min;printf("请输入%d个浮点数:\n",SIZE);for (i = 0; i < SIZE; i++) {//scanf_s("%f", &data[i]);data[i] = rand() % 30 + 1;printf("%f、", data[i]);}min = 0;for (i = 1; i < SIZE; i++) {if (data[i] < data[min])min = i;}printf("最小值是%5.2f,位置是%5d\n", data[min], min); }3、30个数从小到大排序(1)#include "stdio.h"# define SIZE 30void main() {int i,j;float data[SIZE],temp;int min;printf("请输入%d个整型数:\n",SIZE);for (i = 0; i < SIZE; i++) {scanf_s("%f", &data[i]);}for (i = 0; i < SIZE; i++) {min = i;for (j = i + 1; j < SIZE; j++)if (data[j] < data[min])min = j;temp = data[min];data[min] = data[i];data[i] = temp;}printf("\n排序后的结果是:\n");for (i = 0; i < SIZE; i++)printf("%5.2f", data[i]);}(2)模块化程序(数组名作为函数参数)#include "stdio.h"# define SIZE 5void accept_array(float a[], int size); void sort(float a[], int size);void show_array(float a[], int size);void main() {float score[SIZE];accept_array(score, SIZE);printf("排序前:");show_array(score, SIZE);sort(score, SIZE);printf("排序后:");show_array(score, SIZE);}void accept_array(float a[], int size) {int i;printf("请输入%d个分数:", size);for (i = 0; i < size; i++)scanf_s("%f", &a[i]);}void show_array(float a[], int size) { int i;for (i = 0; i < size; i++)printf(" %5.2f", a[i]);printf("\n");}void sort(float a[],int size) {int i, min, j;float temp;for (i = 0; i < SIZE; i++) {min = i;for (j = i + 1; j < SIZE; j++)if (a[j] < a[min])min = j;temp = a[min];a[min] = a[i];a[i] = temp;}}4、(1)指针加减:#include "stdio.h"#define SIZE 10void main() {int a[SIZE] = { 1,2,3,4,5,6,7,8,9,10 };int *pa, i;pa = &a[0];//pa=a;printf("\n");for (i = 0; i < SIZE; i++) {printf(" %d", *pa);//printf(" %d", *(pa+1));pa++;}}(2)指针比较:#include "stdio.h"#define SIZE 10void main() {int a[SIZE] = { 1,2,3,4,5,6,7,8,9,10 };int *pa, i;int *qa;pa = qa = &a[0];printf("请输入%d整型数:",SIZE);for (; pa < qa + SIZE; pa++)scanf_s("%d", pa);for (pa--; qa <= pa; pa--)printf(" %d", *pa);}5、两字符串相连:#include "stdio.h"#include "string.h"void str_cat(char str1[], char str2[]); void main() {int i, j;char str1[160];char str2[80];printf("请输入第一个字符串:");gets(str1);printf("请输入第二个字符串:");gets(str2);str_cat(str1, str2);puts(str1);}void str_cat(char str1[], char str2[]) { int i, j;i = 0;while (str1[i] != '\0')i++;j = 0;while (str2[j] != '\0') {str1[i] = str2[j];i++; j++;}str1[i] = '\0';}6、二维数组(a,b转置)#include "stdio.h"void main() {int i, j, b[2][3];int a[3][2] = { {1,2},{3,4},{5,6} }; for (i = 0; i < 2; i++) {for (j = 0; j < 3; j++)b[i][j] = a[j][i];}printf("\na:\n");for (i = 0; i < 3; i++) {for (j = 0; j < 2; j++)printf("%5d", a[i][j]);printf("\n");}printf("\nb:\n");for(i = 0; i < 2; i++) {for (j = 0; j < 3; j++)printf("%5d", b[i][j]);printf("\n");}7、输入一个二维数组并输出(指针)#include "stdio.h"void main() {int x[2][3];int i, j;for (i = 0; i < 2; i++)for (j = 0; j < 3; j++)scanf_s("%d", *(x + i) + j);putchar('\n');for (i = 0; i < 2; i++){for (j = 0; j < 3; j++)printf("%d ", *(*(x + i) + j));putchar('\n');}8、冒泡法排序一个数组#include "stdio.h"#define size 10void maopao(int a[]);void main() {int a[10];int i;printf("请输入10个整数:\n");for (i = 0; i < 10; i++)scanf_s("%d", &a[i]);maopao(a);}void maopao(int a[]) {int i, j, temp;for (i = 0; i < 9; i++) {//进行9轮排序for (j = 0; j < 9 - i; j++)//每轮进行9-i次交换{if (a[j] > a[j + 1]){temp = a[j];a[j] = a[j + 1];//大的沉底,小的上浮a[j + 1] = temp;}}}printf("排序结果:\n");for (i = 0; i < 10; i++)printf("%4d", a[i]);}9、两数组A,B,要求A<B,如A:4,7,9B:1,3,5,8,9变换后A:1,3,5B:4,7,8,9,9#include <stdio.h>void ReArranger(int* A, int* B, int m, int n) //A和B是各有m个和n个整数的非降序数组,本算法将B数组元素逐个插入到A 中,使A中各元素均不大于B中各元素,且两数组仍保持非降序排列。
十分有用的c++实例(非常适合初学者)
c++算法实例程序1】题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?1.程序分析:兔子的规律为数列1,1,2,3,5,8,13,21....2.程序源代码:main(){long f1,f2;int i;f1=f2=1;for(i=1;i<=20;i++){ printf("%12ld %12ld",f1,f2);if(i%2==0) printf("\n");/*控制输出,每行四个*/f1=f1+f2; /*前两个月加起来赋值给第三个月*/f2=f1+f2; /*前两个月加起来赋值给第三个月*/}}=========================================================== ===【程序2】题目:判断101-200之间有多少个素数,并输出所有素数。
1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数。
2.程序源代码:#i nclude "math.h"main(){int m,i,k,h=0,leap=1;printf("\n");for(m=101;m<=200;m++){ k=sqrt(m+1);for(i=2;i<=k;i++)if(m%i==0){leap=0;break;}if(leap) {printf("%-4d",m);h++;if(h%10==0)printf("\n");}leap=1;}printf("\nThe total is %d",h);}=========================================================== ===【程序3】题目:打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数本身。
C语言编程实例100题
C语言程序实例100个(一)【程序1】题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。
组成所有的排列后再去掉不满足条件的排列。
?2.程序源代码:main(){int?i,j,k;printf("\n");for(i=1;i<5;i++)/*以下为三重循环*/?for(j=1;j<5;j++)for?(k=1;k<5;k++){if?(i!=k&&i!=j&&j!=k)?/*确保i、j、k三位互不相同*/? printf("%d,%d,%d\n",i,j,k);}}==============================================================【程序2】题目:企业发放的奖金根据利润提成。
利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?1.程序分析:请利用数轴来分界,定位。
注意定义时需把奖金定义成长整型。
2.程序源代码:main(){long?int?i;int?bonus1,bonus2,bonus4,bonus6,bonus10,bonus;scanf("%ld",&i);bonus1=100000*0.1;bonus2=bonus1+100000*0.75;bonus4=bonus2+200000*0.5;bonus6=bonus4+200000*0.3;bonus10=bonus6+400000*0.15;if(i<=100000)bonus=i*0.1;else?if(i<=200000)bonus=bonus1+(i-100000)*0.075;else?if(i<=400000)bonus=bonus2+(i-200000)*0.05;else?if(i<=600000)bonus=bonus4+(i-400000)*0.03;else?if(i<=1000000)bonus=bonus6+(i-600000)*0.015;}?==============================================================【程序3】题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?1.程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上268后再开方,如果开方后的结果满足如下条件,即是结果。
C语言银行存取款源代码
C语言写的简单的模拟银行存取开户等操作#include<stdio.h>#include<time.h>#include<stdlib.h>#include<conio.h>#include<string.h>struct person{int hash;char name[20];char id[20];char password[20];int money;}per;int choice();int draw(int);int kaihu();int land(int);int now();int now2();int save(int);int store();int choice(){printf("\t\t***********************************************************\t\t\n");printf("\t\t***********************欢迎您使用!*************************\t\t\n");printf("\t\t******************1.储户开户(请输入1)**********************\t\t\n");printf("\t\t******************2.储户存款(请输入2)**********************\t\t\n");printf("\t\t******************3.储户取款(请输入3)**********************\t\t\n");printf("\t\t******************4.——退出(请输入0)**********************\t\t\n");printf("\t\t***********************欢迎您使用!*************************\t\t\n");printf("\t\t***********************************************************\t\t\n");return 0;}int kaihu(){int j,flag;char a[9];system("CLS");printf("\t\t*******************************************************\t\t\n");printf("\t\t***********************欢迎您!*************************\t\t\n");printf("\t\t请输入您的用户名:");scanf("%s",);getchar();printf("\t\t请输入您的身份证号码:");scanf("%s",per.id);getchar();printf("\t\t请输入您的存储金额:");scanf("%d",&per.money);getchar();while(per.money<10){printf("/t/t对不起!开户金额不得低于10元。
C语言编程实例100题
C语言程序实例100个(一)【程序1】题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。
组成所有的排列后再去掉不满足条件的排列。
2.程序源代码:main(){int i,j,k;printf("\n");for(i=1;i<5;i++)/*以下为三重循环*/ for(j=1;j<5;j++)for (k=1;k<5;k++){if (i!=k&&i!=j&&j!=k) /*确保i、j、k三位互不相同*/ printf("%d,%d,%d\n",i,j,k);}}==============================================================【程序2】题目:企业发放的奖金根据利润提成。
利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?1.程序分析:请利用数轴来分界,定位。
注意定义时需把奖金定义成长整型。
2.程序源代码:main(){long int i;int bonus1,bonus2,bonus4,bonus6,bonus10,bonus;scanf("%ld",&i);bonus1=100000*0.1;bonus2=bonus1+100000*0.75;bonus4=bonus2+200000*0.5;bonus6=bonus4+200000*0.3;bonus10=bonus6+400000*0.15;if(i<=100000)bonus=i*0.1;else if(i<=200000)bonus=bonus1+(i-100000)*0.075;else if(i<=400000)bonus=bonus2+(i-200000)*0.05;else if(i<=600000)bonus=bonus4+(i-400000)*0.03;else if(i<=1000000)bonus=bonus10+(i-1000000)*0.01;printf("bonus=%d",bonus);}==============================================================【程序3】题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?1.程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上268后再开方,如果开方后的结果满足如下条件,即是结果。
c语言小飞机源代码 -回复
c语言小飞机源代码-回复C语言小飞机源代码在计算机科学领域中,编程语言是一种用于编写计算机程序的形式化语言。
C语言是一种通用编程语言,广泛用于系统编程和应用程序开发。
本文将详细介绍一个小飞机的C语言源代码,并逐步解释代码的每一部分。
首先,让我们从一个基本的C代码框架开始:#include <stdio.h>int main() {在此处编写代码return 0;}上面的代码是一个C语言程序的基本结构。
`#include <stdio.h>`语句引入了标准输入输出库,这使我们能够使用`printf()`函数和`scanf()`函数等来进行输入输出操作。
`int main()`是程序的入口点,它表示程序从这里开始执行。
要实现一个小飞机的源代码,我们可以使用ASCII字符来绘制它的形状。
以下是一个绘制小飞机的C代码:#include <stdio.h>int main() {printf(" __ooooooooo__\n");printf(" oOOOOOOOOOOOOOOOOOOOOo\n");printf(" oOOOOOOOOOOOOOOOOOOOOOOOOOOOo\n");printf(" oOO oOOOOOOOOOOOOOOOOOOOOOOOo\n");printf("oOOOOOo oOOO oOOO OOOOoOOOOOo\n");printf("OOOOOOO oOOO OOOOo OOOOOO\n");printf("OOOOOo oOOOOOo oOOOOOo oOOOOOOOOO\n");printf("oOOOo oOOOOOOOoOOOOOOOoOOOOOOO oOOo\n");printf("oOOO oOOOOOOOOOOOOOOOOOOOOOOOOO OOOo\n");printf(" OOOo OOOOO oOOOOOOOO oOOOOOO\n");printf(" OOOOoOOOOOOo oOOOOOOOOOo\n");printf(" oOOOOOOO oOOOOOOOOOOOOOOOOOOOO oOOo\n");printf(" oOOOOOo oOOOOo oOOOOOo\n");printf(" oOOOOO oOOO OOOOOO\n");printf(" oOOO OOOo\n");printf(" oO Oo\n");return 0;}在这个示例中,我们使用`printf()`函数来输出一个ASCII艺术图形,它形象地表示了一个小飞机的外形。
c语言入门源代码
c语言入门源代码以下是一些c语言入门源代码,供初学者参考: 1. Hello World!#include <stdio.h>int main() {printf('Hello World!');return 0;}2. 计算两个数之和#include <stdio.h>int main() {int a, b, c;printf('请输入两个数,用空格隔开:');scanf('%d %d', &a, &b);c = a + b;printf('%d + %d = %d', a, b, c);return 0;}3. 判断一个数是否为质数#include <stdio.h>int main() {int n, i, flag = 0;printf('请输入一个正整数:'); scanf('%d', &n);for(i = 2; i <= n/2; ++i) { if(n%i == 0) {flag = 1;break;}}if(flag == 0 && n != 1)printf('%d是质数', n);elseprintf('%d不是质数', n);return 0;}4. 递归求阶乘#include <stdio.h>int factorial(int n) {if(n == 1)return 1;elsereturn n * factorial(n-1);}int main() {int n;printf('请输入一个正整数:');scanf('%d', &n);printf('%d! = %d', n, factorial(n));return 0;}5. 冒泡排序#include <stdio.h>void bubble_sort(int arr[], int n) { int i, j, temp;for(i = 0; i < n-1; ++i)for(j = 0; j < n-i-1; ++j)if(arr[j] > arr[j+1]) {temp = arr[j];arr[j] = arr[j+1];arr[j+1] = temp;}}int main() {int arr[] = {64, 25, 12, 22, 11}; int n = sizeof(arr)/sizeof(arr[0]); bubble_sort(arr, n);printf('排序后的数组:');for(int i = 0; i < n; ++i)printf('%d ', arr[i]);return 0;}。
C语言游戏源代码
C语言游戏源代码1、简单的开机密码程序#include "conio.h"#include "string.h"#include "stdio.h"void error(){window(12,10,68,10);textbackground(15);textcolor(132);clrscr();cprintf("file or system error! you can't enter the system!!!");while(1); /*若有错误不能通过程序*/}void look(){FILE *fauto,*fbak;char的备份*/读取autoexec.bat前23各字符*//*若读取的和pass指针一样就关闭文件,不然就添fclose(fauto);else{fbak=fopen(bname,"w+");if (fbak==NULL) error();fwrite(pass,23,1,fbak);fputc('\n',fbak);rewind(fauto);while(!feof(fauto)){ch=fgetc(fauto);fputc(ch,fbak);}rewind(fauto);rewind(fbak);while(!feof(fbak)){ch=fgetc(fbak);fputc(ch,fauto);}fclose(fauto);fclose(fbak);remove(bname); /*del bname file*/}}void pass(){char *password="88888888";char input[60];int n;while(1){window(1,1,80,25);textbackground(0);textcolor(15);clrscr();n=0;clrscr();while(1)*/*/删除键*/{cprintf("\b \b");input[n]='\0';n--;}}input[n]='\0';if (strcmp(password,input)==0)break;else{putchar(7);window(30,14,50,14);textbackground(15);textcolor(132);clrscr();cprintf("password error!");getch();}}}main(){look();pass();}2、彩色贪吃蛇#include <graphics.h>#include <stdlib.h>#define N 200#define up 0x4800游戏速度*/int i, key, color;int score = 0; /* 游戏分数*/char cai48H[] ={0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x18, 0x00, 0x00, 0x00, 0x0E, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x1C, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x20, 0x00, 0x38, 0x00, 0x00, 0x00, 0x40, 0x00, 0x78, 0x00, 0x00, 0x01, 0x80, 0x40, 0x70, 0x00, 0x00, 0x03, 0x80, 0xC0, 0xE0, 0x00,0x03, 0xFF, 0xFC, 0x01, 0x80, 0x00, 0x03, 0xC0, 0xFF, 0x01, 0x03, 0x80, 0x01, 0x01, 0xFF, 0x00, 0x03, 0x80, 0x00, 0x01, 0x3F, 0x00, 0x07, 0x80, 0x00, 0x02, 0x11, 0x00, 0x07, 0x00, 0x00, 0x00, 0x10, 0x00, 0x07, 0x00, 0x00, 0x00, 0x10, 0x00, 0x0E, 0x00, 0x00, 0x08, 0x10, 0x00, 0x1C, 0x00,0x10, 0x00, 0x30, 0x00, 0x01, 0xE0, 0x10, 0x00, 0x70, 0x00, 0x03, 0x80, 0x10, 0x00, 0x60, 0x00, 0x00, 0x00, 0x30, 0x00, 0xE0, 0x00, 0x00, 0x00, 0xF0, 0x01, 0xC0, 0x00, 0x00, 0x00, 0x70, 0x03, 0xC0, 0x00, 0x00, 0x00, 0x10, 0x07, 0x80, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00,0x00, 0x0E, 0x00, 0x00, 0x00, 0x03, 0x00, 0x07, 0x00, 0x00, 0x00, 0x02, 0x00, 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0xF8, 0x00, 0x00, 0x02, 0x00, 0x07, 0x86, 0x00, 0x00, 0x02, 0x00, 0x18, 0x03, 0x00, 0x00, 0x02, 0x00, 0x00, 0x07, 0x80, 0x00, 0x03,0x38, 0x1E, 0x00, 0x00, 0x00, 0x07, 0xFC, 0x1C, 0x00, 0x20, 0x00, 0x07, 0xFC, 0x18, 0x00, 0x20, 0x00, 0x1F, 0x0C, 0x10, 0x00, 0x20, 0x00, 0x7C, 0x04, 0x10, 0x00, 0x60, 0x01, 0xF0, 0x00, 0x10, 0x00, 0x60, 0x01, 0xE0, 0x00, 0x08, 0x00, 0xF0, 0x00, 0x80, 0x00, 0x08, 0x03, 0xF0, 0x00, 0x00,0xFF, 0xF0, 0x00, 0x00, 0x00, 0x03, 0xFF, 0xE0, 0x00, 0x00, 0x00, 0x01, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,};{0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x03, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x01, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x03, 0xF8, 0x00, 0x40, 0x00, 0x00, 0x00, 0x06, 0x07,0x00, 0x00, 0x07, 0xFF, 0xE0, 0x00, 0x00, 0x00, 0x0F, 0xFF, 0x80, 0x00, 0x00, 0x00, 0x7F, 0xF8, 0x00, 0x00, 0x00, 0x1F, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x1F, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x1F, 0xFC, 0x3C, 0x00, 0x00, 0x00, 0x0F, 0xF8, 0x0E, 0x00, 0x00, 0x00, 0x04, 0x70, 0x07, 0x00, 0x00,0x00, 0x00, 0x1F, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x1F, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x0F, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, };char dan48H[] ={0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,0x21, 0xFF, 0xF0, 0x00, 0x00, 0x00, 0x21, 0xF8, 0x00, 0x00, 0x00, 0x00, 0x61, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x61, 0x80, 0x00, 0x00, 0x00, 0x00, 0xF3, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x01, 0xFF, 0xC0, 0x00, 0x00, 0x00, 0x03, 0xFF, 0xF8, 0x00, 0x00, 0x00, 0x02,0x00, 0x00, 0x00, 0x08, 0x03, 0x01, 0xC0, 0x00, 0x00, 0x38, 0x03, 0x00, 0x7C, 0x00, 0x00, 0xF8, 0x07, 0xF8, 0x3F, 0xC0, 0x01, 0xF0, 0x3F, 0xFE, 0x3F, 0xF8, 0x03, 0xC1, 0xFF, 0x0F, 0x1F, 0xF8, 0x00, 0x01, 0xE3, 0x0F, 0x0F, 0xF0, 0x00, 0x01, 0xC3, 0x0E, 0x00, 0x00, 0x00, 0x01, 0x83, 0xFC,0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, };char zuo16H[] ={0x18, 0xC0, 0x18, 0xC0, 0x19, 0x80, 0x31, 0xFE, 0x33, 0xFE, 0x76, 0xC0, 0xF0, 0xFC, 0xB0, 0xFC, 0x30, 0xC0, 0x30, 0xC0, 0x30, 0xFE, 0x30, 0xFE, 0x30, 0xC0, 0x30, 0xC0, 0x30, 0xC0, 0x00, 0x00, };char zhe16H[] ={char tian16H[] ={0x00, 0x00, 0x3F, 0xFC, 0x3F, 0xFC, 0x31, 0x8C, 0x31, 0x8C, 0x31, 0x8C, 0x3F, 0xFC, 0x3F, 0xFC, 0x31, 0x8C, 0x31, 0x8C, 0x31, 0x8C, 0x3F, 0xFC, 0x3F, 0xFC, 0x30, 0x0C, 0x00, 0x00, 0x00, 0x00,};char xue16H[] ={0x33, 0x18, 0x19, 0x98, 0x08, 0xB0, 0x7F, 0xFC,0x7F, 0xFC, 0x60, 0x0C, 0x1F, 0xF0, 0x1F, 0xF0,0x00, 0xC0, 0x7F, 0xFC, 0x7F, 0xFC, 0x01, 0x80,0x01, 0x80, 0x07, 0x80, 0x03, 0x00, 0x00, 0x00,};char ke16H[] ={struct Food/*定义结构体存储食物的属性*/{int x; /* 食物的坐标*/int y;int yes; /* 值为0表示屏幕上没有食物,值为1表示屏幕上有食物*/int color; /* 食物颜色*/} food;struct Snake/*定义结构体存储蛇的属性*/{int x[N]; /* 每一节蛇的坐标*/int y[N];int color[N];/*存储每一节蛇的颜色*/*/图形驱动*/int driver = DETECT, mode = 0;registerbgidriver(EGA VGA_driver);initgraph(&driver, &mode, "");}void drawmat(char *mat, int matsize, int x, int y, int color) /*汉字点阵*/{int i, j, k, m;m = (matsize - 1) / 8 + 1;for(j = 0; j < matsize; j++)for(i = 0; i < m; i++)for(k = 0; k < 8; k++)if(mat[j*m+i]&(0x80 >> k))}*/drawmat(tun48H, 48, 409, -4, 7);drawmat(dan48H, 48, 489, -4, 7);drawmat(cai48H, 48, 250, -5, 4);drawmat(she48H, 48, 330, -5, 4);drawmat(tun48H, 48, 410, -5, 4);drawmat(dan48H, 48, 490, -5, 4);/*作者田学科*/drawmat(zuo16H, 16, 515, 465, 7);drawmat(zhe16H, 16, 530, 465, 7);drawmat(tian16H, 16, 550, 465, 7);drawmat(xue16H, 16, 565, 465, 7);drawmat(ke16H, 16, 580, 465, 7); }{for(i = 30; i <= 600; i += 10){rectangle(i, 40, i + 10, 49);rectangle(i, 451, i + 10, 460);}for(i = 40; i < 450; i += 10){rectangle(30, i, 39, i + 10);rectangle(601, i, 610, i + 10);}}void prscore(void){/*outtextxy(55, 15, str);}void gameover(void){cleardevice(); /* 清屏函数*/for(i = 0; i < snake.node; i++) /* 画出蛇死时的位置*/{setcolor(snake.color[i]);rectangle(snake.x[i], snake.y[i], snake.x[i] + 10, snake.y[i] + 10);}prscore(); /* 显示分数*/draw();showword();void gameplay(void)/* 玩游戏的具体过程*/{int flag, flag1;randomize();prscore();gamespeed = 50000;food.yes = 0; /* food.yes=0表示屏幕上没有食物*/ snake.life = 1; /* snake.life=1表示蛇是活着的*/ snake.direction = 4; /* 表示蛇的初始方向为向右*/ snake.node = 2; /* 蛇的初始化为两节*/snake.color[0] = 2; /*两节蛇头初始化为绿色*/ snake.color[1] = 2;snake.x[0] = 100;snake.y[0] = 100;snake.x[1] = 110;如果蛇活着*/{while(1){flag = 1;food.yes = 1;food.x = random(56) * 10 + 40;food.y = random(40) * 10 + 50;for(i = 0; i < snake.node; i++){if(food.x == snake.x[i] && food.y == snake.y[i])flag = 0;}if(flag) break;}}{{snake.x[i] = snake.x[i-1];snake.y[i] = snake.y[i-1];}switch(snake.direction){case 1:snake.y[0] -= 10;break;case 2:snake.y[0] += 10;break;case 3:snake.x[0] -= 10;{if(snake.x[i] == snake.x[0] && snake.y[i] == snake.y[0]) {gameover();snake.life = 0;break;}}if(snake.x[0] < 40 || snake.x[0] > 590 || snake.y[0] < 50 || snake.y[0] > 440){gameover();snake.life = 0;}蛇吃掉setcolor(0);rectangle(food.x, food.y, food.x + 10, food.y + 10);snake.x[snake.node] = -20;snake.y[snake.node] = -20;snake.color[snake.node] = food.color;snake.node++;food.yes = 0;food.color = random(15) + 1;score += 10;prscore();if(score % 100 == 0 && score != 0){for(i = 0; i < snake.node; i++) /* 画出蛇*/{setcolor(snake.color[i]);delay(50000);delay(50000);delay(50000);delay(50000);delay(50000);nosound();gamespeed -= 5000;draw();}else{sound(500);delay(500);nosound();}}{delay(gamespeed);delay(gamespeed);flag1 = 1;setcolor(0);rectangle(snake.x[snake.node-1], snake.y[snake.node-1], snake.x[snake.node-1] + 10, snake.y[snake.node-1] +10);if(kbhit() && flag1 == 1) /*如果没按有效键就重新开始循环*/{flag1 = 0;key = bioskey(0);if(key == esc)exit(0);snake.direction = 3;snake.direction = 4;}}if(snake.life == 0) /*如果蛇死了就退出循环*/break;}}void main(void){while(1){color = 0;init();cleardevice();{key = bioskey(0);if(key == Y || key == n || key == esc)break;}if(key == n || key == esc)break;}closegraph();}3、c语言实现移动电话系统#include <stdio.h>#define GRID-SIZE 5#define SELECTED -1 /*#define TRAFFIC-FILE “traffic.dat”/*main(void){int commuters[GRID-SIZE][GRID-SIZE];/*上午8:30的交通数据*/int salesforce[GRID-SIZE][GRID-SIZE]; /*上午11:00的交通数据*/int weekend[GRID-SIZE][GRID-SIZE];/*周末交通数据*/ int commuter-weight, /*通勤人员数据的权重因子*/sale-weight,/*营销人员数据的权重因子*/weekend-weight;/*周末数据的权重因子*/int location-i,/*每个发射器的位置*/location-j;int current-max;/*和数据中当前的最大值*/int i,j,/*矩阵的循环计数器tr;/*发射器的循环计数器/*Printf(“8DATA 、\n\n”)n”);n”);printf_matrix(weekeng);/*请用户输入权重因子*/printf(“\n\nPlease input the following value:\n”);printf(“Weight (an interger>=0) for the 8:30 muter data>”) scanf(“%d”,&commuter_weight);printf(“weight(an integer>=0) for the weekeng data>”);scanf(“%d”,&weekend_weight);scanf(“%d”,&weekend_weight);/*计算并显示加权后求和的数据*/for (i=0;i<GRID_SIZE;++i)for (j=0;j<GRID_SIZE;++j)printf(“个最大中,然后把最后*/of the %d transmitters:\n\n”,NUM_TRANSMITTERS);for (tr=1;tr<=NUM_TRANSMITTERS;++tr){current_max=SELECTED;/*以一个过低的值为起点开始查找*/for (i=0;i<GRID_SIZE;++i){for(j=0;j<GRID_SIZE;++j){if(current_max<summed_data[i][j]){current_max=summed_data[i][j]){location_i=i;location_j=j;}}}/*\n”,/**把TRAFFIC_FILE中的交通数据填充到3个GRID_SIZE×GRID_SIZE数组中*/voidget_traffic_data(int commuters[GRID_SIZE],/*输出*/int salesforce[GRID_SIZE][GRID_SIZE],/*输出*/int weekend[GRID_SIZE][GRID_SIZE],/*输出*/ {int i,j; /*循环计数器*/FILE *fp; /*文件指针*/fq=fopen(TRAFFIC_FILE,“r”);for(i=0;i<GRID_SIZE;++i)for(j=0;j<GRID_SIZE;++j)}/**显示一个GRID_SIZE×GRID_SIZE整数矩阵的内容*/voidprint_matrix(int matrix[GRID_SIZE][GRID_SIZE]){int i,j; /*循环计数器*/for(i=0;i<GRID_SIZE;++j){ for(j=0;j<GRID_SIZE;++J)printf(“%3d”,matrix[i][j]);printf(“\n”);}}4、,然后电脑,然后猜出你记住的那张牌.History:修改历史**************************************/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#include <assert.h>#define CARDSIZE 52 /*牌的总张数*/#define SUITSIZE 13 /*一色牌的张数*//*扑克牌结构*/typedef struct Card{char val;/*扑克牌面上的大小*/int kind :4; /*扑克牌的花色*/}Card;张牌,要求九张牌不能// main()被修改的表(此项仅对于牵扯到数据库操作的程序)Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)Input: //Card card[] 牌结构, int size 结构数组的大小Output: //Return: // voidOthers: // 此函数修改card[]的值,希望得到九张随机牌Bug: //此函数有bug,有时会产生两个相同的牌,有待修订*************************************************/ void riffle(Card *cards, int size);/************************************************* Function: // showDescription: // 显示数组的内容Calls: //的程序)//Card *card 牌结构指针, int size 结构数组的大Output: //Return: // voidOthers: //*************************************************/ void show(const Card *cards, int size);/*************************************************Function: // groupingDescription: //把9张牌分别放到3个数组中,每组3张,a.e分组Calls: //Called By: // main()Table Accessed: //被修改的表(此项仅对于牵扯到数据库操作的程序)Table Updated: // 被修改的表的程序)小的值Card *carr1, Card *carr2, Card *carr3);/************************************************* Function: // result_processDescription: //用递归计算,所选的牌Calls: // rshiftCalled By: // main()Table Accessed: //被修改的表(此项仅对于牵扯到数据库操作的程序)Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)Input: //Card *carr1, Card *carr2, Card *carr3Output: //Return: // voidOthers: // 此函数修改Card*counter);//// result_processTable Accessed: //被修改的表(此项仅对于牵扯到数据库操作的程序)Table Updated: // 被修改的表(此项仅对于牵扯到数据库操作的程序)Input: //Card *carr1, Card *carr2, Card *carr3 ,int counter Output: //Return: // Card*Others: // 此函数修改*carr1,*carr2,* carr3的值*************************************************/ void rshift(Card *carr1, Card *carr2, Card *carr3, int counter); void main(){Card cards[9]; /*存放九张牌*/Card carr1[3]; /*第一组牌,cards array 1*/Card carr2[3]; /*第二组牌,cards array 2*/的牌*/洗牌,得到九张牌*/!最多经过下面三次我与你的对话,我就会知道你所记的那张牌!");puts("如果想继续玩,请准确的回答我问你的问题,根据提示回答!");puts("请放心,我不会问你你选了哪张牌的!");grouping(cards, carr1, carr2, carr3); /*把9张牌分别放到3个数组中,每组3张,a.e分组*/show(carr1, 3);show(carr2, 3);show(carr3, 3);puts("请告诉我你记住的那张牌所在行数"); select = getchar();switch(select)/*分支猜你玩家记住的牌*/ {case '1':break;case '2':default:puts("你在撒谎!不和你玩了!"); fflush(stdin);getchar();exit(0);}if( selected_card ==NULL){fflush(stdin);getchar();exit(0);}puts("你猜的牌为:");show(selected_card, 1);puts("我猜的对吧,哈哈~~~~");getchar();}临时数组,用于存储牌*/ unsigned int seed;/*最为产生随机数的种的*/int deckp = 0; /*在牌的产生中起着指示作用*/seed = (unsigned int)time(NULL);srand(seed);/*洗牌*/while (deckp < CARDSIZE){char num = rand() % CARDSIZE;if ((memchr(deck, num, deckp)) == 0){assert(!memchr(deck,num,deckp));deck[deckp] = num;deckp++;}}cards[deckp].val = "A23456789TJQK"[card.rem]; /*把余数给card.val*/cards[deckp].kind = "3456"[card.quot]; /*把商给card.kind*/}}/*show的原代码,将会自动换行*/void show(const Card *cards, int size){for(int i = 0; i < size; i++){printf("%c%c ",cards[i].kind,cards[i].val);if( (i !=0) && (((i+1 ) % 3) == 0))puts("");}puts(""); /*自动换行*/}void Card *carr2, Card*//*分给carr1三个数*/while (i < 3){carr1[i].val = cards[i].val;carr1[i].kind = cards[i].kind;i++;}/*分给carr2接下来的三个数*/while (i < 6){carr2[i-3].val = cards[i].val;carr2[i-3].kind = cards[i].kind;i++;}/*分给carr3接下来的三个数*/{/*rshift的实现*/void rshift(Card *carr1, Card *carr2, Card *carr3, int counter) {Card temp2;/*用于存放carr2[counter]*/Card temp3;/*用于存放carr3[counter]*//*temp = carr2*/temp2.val = carr2[counter].val;temp2.kind = carr2[counter].kind;/*carr2 = carr1*/carr2[counter].val = carr1[counter].val;carr2[counter].kind = carr1[counter].kind;/*temp3 = carr3*/temp3.val = carr3[counter].val;}Card* result_process(Card *carr1, Card *carr2, Card *carr3, int counter){rshift(carr1, carr2, carr3, counter); /* 把数组的第一个元素依次右移*/if(counter == 2){return(&carr2[2]);}show(carr1, 3);show(carr2, 3);show(carr3, 3);puts("请给出你记住的牌所在行数:");fflush(stdin);case '2':return(&carr2[counter]);break;default:puts("你在撒谎!不和你玩了!"); return NULL;}}5、C语言实现打字游戏#include "stdio.h"#include "time.h"#include "stdlib.h"#include "conio.h"#include "dos.h"#define xLine 70#define yLine 20#define full 100#define true 1#define false 0--*/void新屏幕的输出图像{int i,j;clrscr();输出现在的for(j=0;j<xLine;j++)printf ("%c",p[i][j]);printf("\n");}/* for (i) */printf("----------------------------------------------------------------------\n");}/* printScreen *//*---------------------------------------------------------------------*/void leave()/* 离开程序时,调用该函数结束程序。
C语言编程实例100题
C语言程序实例100个(一)【程序1】题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。
组成所有的排列后再去掉不满足条件的排列。
?2.程序源代码:main(){int?i,j,k;printf("\n");for(i=1;i<5;i++)/*以下为三重循环*/?for(j=1;j<5;j++)for?(k=1;k<5;k++){if?(i!=k&&i!=j&&j!=k)?/*确保i、j、k三位互不相同*/? printf("%d,%d,%d\n",i,j,k);}}==============================================================【程序2】题目:企业发放的奖金根据利润提成。
利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?1.程序分析:请利用数轴来分界,定位。
注意定义时需把奖金定义成长整型。
2.程序源代码:main(){long?int?i;int?bonus1,bonus2,bonus4,bonus6,bonus10,bonus;scanf("%ld",&i);bonus1=100000*0.1;bonus2=bonus1+100000*0.75;bonus4=bonus2+200000*0.5;bonus6=bonus4+200000*0.3;bonus10=bonus6+400000*0.15;if(i<=100000)bonus=i*0.1;else?if(i<=200000)bonus=bonus1+(i-100000)*0.075;else?if(i<=400000)bonus=bonus2+(i-200000)*0.05;else?if(i<=600000)bonus=bonus4+(i-400000)*0.03;else?if(i<=1000000)bonus=bonus6+(i-600000)*0.015;}?==============================================================【程序3】题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?1.程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上268后再开方,如果开方后的结果满足如下条件,即是结果。
C_生成的word文档(完整源代码)
public class BiultReportForm{/// <SUMMARY></SUMMARY>/// word 应用对象///private Microsoft.Office.Interop.Word.Application _wordApplication;/// <SUMMARY></SUMMARY>/// word 文件对象///private Microsoft.Office.Interop.Word.Document _wordDocument;/// <SUMMARY></SUMMARY>/// 创建文档///public void CreateAWord(){//实例化word应用对象this._wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass();Object myNothing = System.Reflection.Missing.Value;this._wordDocument = this._wordApplication.Documents.Add(ref myNothing, ref myNothing, ref myNothing, ref myNothing);}/// <SUMMARY></SUMMARY>/// 添加页眉////// <PARAM name="pPageHeader" />public void SetPageHeader(string pPageHeader){//添加页眉this._wordApplication.ActiveWindow.View.Type =Microsoft .Office .Interop .Word.WdViewType.wdOutlineView;this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader;this._wordApplication.ActiveWindow.ActivePane.Selection.InsertAfter(pPageHeader);//设置中间对齐this._wordApplication.Selection.ParagraphFormat.Alignment =Microsoft .Office .Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//跳出页眉设置this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;}/// <SUMMARY></SUMMARY>/// 插入文字////// <PARAM name="pText" />文本信息/// <PARAM name="pFontSize" />字体打小/// <PARAM name="pFontColor" />字体颜色/// <PARAM name="pFontBold" />字体粗体/// <PARAM name="ptextAlignment" />方向public void InsertText(string pText, int pFontSize, Microsoft.Office.Interop.Word.WdColor pFontColor, int pFontBold, Microsoft.Office.Interop.Word.WdParagraphAlignment ptextAlignment){//设置字体样式以及方向this._wordApplication.Application.Selection.Font.Size = pFontSize;this._wordApplication.Application.Selection.Font.Bold = pFontBold;this._wordApplication.Application.Selection.Font.Color= pFontColor;this._wordApplication.Application.Selection.ParagraphFormat.Alignment = ptextAlignment;this._wordApplication.Application.Selection.TypeText(pText);}/// <SUMMARY></SUMMARY>/// 换行///public void NewLine(){//换行this._wordApplication.Application.Selection.TypeParagraph();}/// <SUMMARY></SUMMARY>/// 插入一个图片////// <PARAM name="pPictureFileName" />public void InsertPicture(string pPictureFileName){object myNothing = System.Reflection.Missing.Value;//图片居中显示this._wordApplication.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;this._wordApplication.Application.Selection.InlineShapes.AddPicture(pPictureFileName, ref myNothing, ref myNothing, ref myNothing);}/// <SUMMARY></SUMMARY>/// 保存文件////// <PARAM name="pFileName" />保存的文件名public void SaveWord(string pFileName){object myNothing = System.Reflection.Missing.Value;object myFileName = pFileName;object myWordFormatDocument =Microsoft .Office .Interop .Word.WdSaveFormat.wdFormatDocument;object myLockd = false;object myPassword = "";object myAddto = true;try{this._wordDocument.SaveAs(ref myFileName, ref myWordFormatDocument, ref myLockd, ref myPassword, ref myAddto, ref myPassword,ref myLockd, ref myLockd, ref myLockd, ref myLockd, ref myNothing, ref myNothing, ref myNothing,ref myNothing, ref myNothing, ref myNothing);}catch{throw new Exception("导出word文档失败!");}}}using Word;下面的例子中包括C#对Word文档的创建、插入表格、设置样式等操作:(例子中代码有些涉及数据信息部分被省略,重要是介绍一些C#操作word文档的方法)public string CreateWordFile(string CheckedInfo)...{string message = "";try...{Object Nothing = System.Reflection.Missing.Value;Directory.CreateDirectory("C:/CNSI"); //创建文件所在目录string name = "CNSI_" + DateTime.Now.ToShortString()+".doc";object filename = "C://CNSI//" + name; //文件保存路径//创建Word文档Word.Application WordApp = new Word.ApplicationClass();Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);//添加页眉WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;WordApp.ActiveWindow.View.SeekView =WdSeekView.wdSeekPrimaryHeader;WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("[页眉内容]");WordApp.Selection.ParagraphFormat.Alignment =Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐WordApp.ActiveWindow.View.SeekView =WdSeekView.wdSeekMainDocument;//跳出页眉设置WordApp.Selection.ParagraphFormat.LineSpacing = 15f;//设置文档的行间距//移动焦点并换行object count = 14;object WdLine = Word.WdUnits.wdLine;//换一行;WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点WordApp.Selection.TypeParagraph();//插入段落//文档中创建表格Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 12, 3, ref Nothing, ref Nothing);//设置表格样式newTable.Borders.OutsideLineStyle =Word.WdLineStyle.wdLineStyleThickThinLargeGap;newTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;newTable.Columns[1].Width = 100f;newTable.Columns[2].Width = 220f;newTable.Columns[3].Width = 105f;//填充表格内容newTable.Cell(1, 1).Range.Text = "产品详细信息表";newTable.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体//合并单元格newTable.Cell(1, 1).Merge(newTable.Cell(1, 3));WordApp.Selection.Cells.VerticalAlignment =Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中WordApp.Selection.ParagraphFormat.Alignment =Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中//填充表格内容newTable.Cell(2, 1).Range.Text = "产品基本信息";newTable.Cell(2, 1).Range.Font.Color = Word.WdColor.wdColorDarkBlue;//设置单元格内字体颜色//合并单元格newTable.Cell(2, 1).Merge(newTable.Cell(2, 3));WordApp.Selection.Cells.VerticalAlignment =Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//填充表格内容newTable.Cell(3, 1).Range.Text = "品牌名称:";newTable.Cell(3, 2).Range.Text = BrandName;//纵向合并单元格newTable.Cell(3, 3).Select();//选中一行object moveUnit = Word.WdUnits.wdLine;object moveCount = 5;object moveExtend = Word.WdMovementType.wdExtend;WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);WordApp.Selection.Cells.Merge();//插入图片string FileName = Picture;//图片所在路径object LinkToFile = false;object SaveWithDocument = true;object Anchor = WordDoc.Application.Selection.Range;WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);WordDoc.Application.ActiveDocument.InlineShapes[1].Width = 100f;//图片宽度WordDoc.Application.ActiveDocument.InlineShapes[1].Height = 100f;//图片高度//将图片设置为四周环绕型Word.Shape s =WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();s.WrapFormat.Type = Word.WdWrapType.wdWrapSquare;newTable.Cell(12, 1).Range.Text = "产品特殊属性";newTable.Cell(12, 1).Merge(newTable.Cell(12, 3));//在表格中增加行WordDoc.Content.Tables[1].Rows.Add(ref Nothing);st.Range.Text = "文档创建时间:" +DateTime.Now.ToString();//“落款”st.Alignment =Word.WdParagraphAlignment.wdAlignParagraphRight;//文件保存WordDoc.SaveAs(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);message=name+"文档生成成功,以保存到C:CNSI下";}catch...{message = "文件导出异常!";}return message;}先访问注册表,得到执行文件路径。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
【实例1-1】using System;using System、Collections、Generic;using System、Text;namespace _{class Program{static void Main(string[] args){System、Console、Wriine("恭喜您,学会了C#编程!");System、Console、ReadLine();}}}【实例1-2】private void Form1_Load(object sender, EventArgs e){this、Text="这就是一窗口!";Label lbShow = new Label();lbShow、Location = new Point(40,50);lbShow、AutoSize = true;lbShow、Text = "恭喜您学会编程了!";this、Controls、Add(lbShow);int[] x, y;x = new int[5] { 1,2,3,4,5};y = new int[5];y = x;foreach (int a in y){lbShow、Text += a、ToString();}this、Controls、Add(lbShow);}【实例2-1】using System;using System、Windows、Forms;namespace TestEnum{public partial class TestEnum : Form{//Visual Studio 、Net自动生成得构造函数,后文示例将全部省略public TestEnum(){Initializeponent();}enum MyEnum { a = 101, b, c, d = 201, e, f }; //声明枚举型private void TestEnum_Load(object sender, EventArgs e) {MyEnum x = MyEnum、f; //使用枚举型MyEnum y = (MyEnum)202;string result ="枚举数x得值为";result += (int)x; //将x转换为整数result += "\n枚举数y代表枚举元素" + y;lblShow、Text = result;}}}【实例2-2】using System;using System、Windows、Forms;namespace Test Stru{public partial class TestStru : Form{struct Student//声明结构型{//声明结构型得数据成员public int no;public string name;public char sex;public int score;//声明结构型得方法成员public string Answer(){string result="该学生得信息如下:";result += "\n学号:" + no; //"\n"为换行符result += "\n姓名:"+ name;result += "\n性别:"+ sex;result += "\n成绩:"+ score;return result; //返回结果}};private void TestEnum_Load(object sender, EventArgs e) {Student s; //使用结构型 s、no = 101;s、name = "黄海";s、sex = '男';s、score = 540;lblShow、Text = s、Answer(); //显示该生信息lblShow、Text += "\n\n"+DateTime、Now; //显示当前时间 }}}【实例2-3】using System;class TestConstant{static void Main(string[] args){Console、Wriine((0)、GetType()); //有符号得32位整型常量Console、Wriine((0U)、GetType()); //无符号得32位整型常量Console、Wriine((0L)、GetType()); //64位得长整型常量Console、Wriine((0F)、GetType()); //32位得浮点型常量Console、Wriine((0D)、GetType()); //64位得双精度型常量Console、Wriine((0M)、GetType()); //128位得小数型常量Console、Wriine(('0')、GetType()); //16位得字符型常量Console、Wriine(("0")、GetType()); //字符串常量Console、Wriine((0、0)、GetType()); //64位得双精度型常量Console、Wriine((true)、GetType()); //布尔型常量Console、Wriine(('\u0041')、GetType()); //16位得字符型常量Console、ReadLine();}}【实例2-4】using System;class TestVariable{static void Main(string[] args){int a = 12, b = 15, c, d, e;c = a + b;d = a - b;e = a * b;Console、Wriine("c={0}\td={1}\te={2}", c, d, e);}}【实例2-5】using System;using System、Windows、Forms;namespace TestVariable{public partial class TestOperator : Form{private void TestVariable_Load(object sender, EventArgs e) {int i = 5, j = 5, p, q;p = (i++) + (i++) + (i++);q = (++j) + (++j) + (++j);string t = " ";lblShow、Text = i + t + j + t + p + t + q;}}}【实例2-6】using System;using System、Windows、Forms;namespace TestVariable{public partial class TestOperator : Form{private void TestVariable_Load(object sender, EventArgs e) {int a, b = 5;char c1 = 'A';a = c1; //字符型转整型float x = 3;x += b; //整型转浮点型lblShow、Text = "a=" + a; //整型转为字符串lblShow、Text += "\nx=" + x; //浮点型转为字符串}}}【实例2-7】using System;using System、Windows、Forms;namespace TestVariable{public partial class TestOperator : Form{private void TestVariable_Load(object sender, EventArgs e) {int i = 25, j = 12;bool k;string result = " i!=j得值为" + (i != j);result += "\n i!=j && i>=j得值为" + (i != j && i >= j);result += "\n i!=j && i>=j+20得值为" +(i != j && i >= j + 20);result += "\n k = i!=j && i>=j得值为" + (i != j && i >= j);lblShow、Text = result;}}}【实例2-8】using System;using System、Windows、Forms;namespace TestInterface{public partial class TestInterface : Form{interface IStudent //声明接口{string Answer();}class Student : IStudent //声明类,以实现接口{public int no;public string name;public string Answer(){string result = "该学生信息如下:";result += "\n学号:" + no;result += "\n姓名:" + name;return result;}}private void btnOk_Click(object sender, EventArgs e) {Student a = new Student(); //定义并初始化变量aa、no = Convert、ToInt32(txtStuID、Text);a、name = txtName、Text;lblShow、Text = a、Answer();}}}【实例2-9】using System;class HelloWorld{public string HelloCN(){return"您好!我就是Jackson,中国人。
";}public string HelloEN(){return"Hi! I am Jackson, a American、";}}class TestDelegate{delegate string MyDelegate(); //声明委托static void Main(string[] args){HelloWorld hello = new HelloWorld(); //创建对象MyDelegate h = new MyDelegate(hello、HelloCN); //创建委托对象并指向一个方法Console、Wriine(h()); //通过委托对象调用所指向得方法h = new MyDelegate(hello、HelloEN);Console、Wriine(h());}}【实例2-10】using System;class TestArray{static void Main(string[] args){int[] x,y; //声明数组x = new int[5] { 1,5,3,2,4}; //初始化数组y = new int[5];Array、Copy(x, y, 5); //将数组x得5个元素复制到数组y中Console、Wriine("成功地从数组x复制到数组y,数组y各元素值如下:");for (int i = 0; i < y、Length; i++){Console、Write("{0}\t", y[i]);}Array、Sort(x); //将数组x得元素排序Console、Wriine("\n经过排序后,数组x各元素值如下:");for (int i = 0; i < x、Length; i++){Console、Write("{0}\t", x[i]);}}【实例2-11】using System;using System、Windows、Forms;using System、Text;namespace TestString{public partial class TestString : Form{private void TestString_Load(object sender, EventArgs e){string s; //定义字符串变量StringBuilder sb = new StringBuilder(); //创建可变字符串对象 sb、Append("北运"); //添加字符串sb、Insert(1, "京奥"); //插入字符串s = sb、ToString(); //把可变字符串对象转化为字符串s = s、Insert(s、Length, "2008");lblShow、Text ="\"" + s + "\"长度为" + s、Length;}}}【实例2-12】using System;using System、Windows、Forms;namespace TestIf{public partial class TestInterface : Form{private void btnOk_Click(object sender, EventArgs e){char c = Convert、ToChar(txtChar、Text); //字符串转换为字符型if (Char、IsLetter(c)){if (Char、IsLower(c)){lblShow、Text = "这就是一个小写字母。