经典C语言源代码

合集下载

人工智能c语言源代码

人工智能c语言源代码

人工智能c语言源代码人工智能是一个广泛的领域,涉及多种算法和技术。

在C语言中,可以实现一些基本的人工智能功能。

下面是一个简单的示例,展示了如何使用C语言编写一个简单的决策树算法:c.#include <stdio.h>。

// 定义决策树节点结构。

typedef struct DecisionTreeNode {。

char question;struct DecisionTreeNode yesNode;struct DecisionTreeNode noNode;} DecisionTreeNode;// 创建决策树节点。

DecisionTreeNode createNode(char question) {。

DecisionTreeNode node = (DecisionTreeNode)malloc(sizeof(DecisionTreeNode));node->question = question;node->yesNode = NULL;node->noNode = NULL;return node;}。

// 决策树算法。

void decisionTree() {。

DecisionTreeNode root = createNode("Is it a mammal?");root->yesNode = createNode("Does it have fur?");root->yesNode->yesNode = createNode("It's amammal!");root->yesNode->noNode = createNode("It's not a mammal!");root->noNode = createNode("Does it have feathers?");root->noNode->yesNode = createNode("It's a bird!");root->noNode->noNode = createNode("It's not abird!");printf("%s\n", root->question);char answer;scanf(" %c", &answer);DecisionTreeNode currentNode = root;while (currentNode != NULL) {。

温度采集C语言源代码

温度采集C语言源代码

#include <reg52.h>#include <stdio.h>#define uchar unsigned char#define uint unsigned intsbit ds=P2^2; //温度传感器信号线sbit dula=P2^6; //数码管段选线sbit wela=P2^7; //数码管位选线sbit beep=P2^3; //蜂鸣器uint temp; //定义整型的温度数据float f_temp; //定义浮点型的温度数据uint warn_l1=260; //定义温度下限值是温度值乘以10后的结果uint warn_l2=250; //定义温度下限值uint warn_h1=300; //定义温度上限值uint warn_h2=320; //定义温度上限值sbit led0=P1^0; //控制发光二极管sbit led1=P1^1; //控制发光二极管sbit led2=P1^2; //控制发光二极管sbit led3=P1^3; //控制发光二极管unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f, //带小数点的0~9编码0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef}; //不带小数点的0~9编码void delay(uint z)//延时函数{uint x,y;for(x=z;x>0;x--)for(y=110;y>0;y--);}void dsreset(void) //18B20复位,初始化函数{uint i;ds=0;i=103;while(i>0)i--;ds=1;i=4;while(i>0)i--;}bit tempreadbit(void) //读1位数据函数{uint i;bit dat;ds=0;i++; //i++ 起延时作用ds=1;i++;i++;dat=ds;i=8;while(i>0)i--;return (dat);}uchar tempread(void) //读1个字节数据函数{uchar i,j,dat;dat=0;for(i=1;i<=8;i++){j=tempreadbit();dat=(j<<7)|(dat>>1); //读出的数据最低位在最前面,这样刚好一个字节在DAT里}return(dat);}void tempwritebyte(uchar dat) //向18B20写一个字节数据{uint i;uchar j;bit testb;for(j=1;j<=8;j++){testb=dat&0x01;dat=dat>>1;if(testb) //写1{ds=0;i++;i++;ds=1;i=8;while(i>0)i--;}else{ds=0; //写0i=8;while(i>0)i--;ds=1;i++;i++;}}}void tempchange(void) //DS18B20 开始获取温度并转换{dsreset();delay(1);tempwritebyte(0xcc); // 写跳过读ROM指令tempwritebyte(0x44); // 写温度转换指令}uint get_temp() //读取寄存器中存储的温度数据{uchar a,b;dsreset();delay(1);tempwritebyte(0xcc);tempwritebyte(0xbe);a=tempread(); //读低8位b=tempread(); //读高8位temp=b;temp<<=8; //两个字节组合为1个字temp=temp|a;f_temp=temp*0.0625; //温度在寄存器中为12位分辨率位0.0625°temp=f_temp*10+0.5; //乘以10表示小数点后面只取1位,加0.5是四舍五入f_temp=f_temp+0.05;return temp; //temp是整型}////////////////////显示程序//////////////////////////void display(uchar num,uchar dat) //num是第几个数码管,dat是要显示的数据{uchar i;dula=0;P0=table[dat]; //编码赋给P0口dula=1;dula=0;wela=0;i=0XFF;i=i&(~((0X01)<<(num))); //用i来存储位选数据,只有一位为0P0=i;wela=1;wela=0;delay(1);}void dis_temp(uint t) //显示温度数值函数t传递的是整型的温度值{uchar i;i=t/100; //除以100得到商,为温度的十位display(0,i); //在第1个数码管上显示i=t%100/10; //100取余再除以10得到商,为温度的个位display(1,i+10); //在第2个数码管上显示i=t%100%10; //100取余再用10取余,为温度的小数位display(2,i);}//////////////////////////////////////////////void warn(uint s,uchar led) //蜂鸣器报警,灯闪烁{ //s控制音调,led控制灯uchar i;i=s;dula=0;wela=0;beep=0; //蜂鸣器响P1=(led); //控制相应的灯亮while(i--){dis_temp(get_temp()); //用温度显示函数起到延时的作用}beep=1; //蜂鸣器不响P1=0X00; //控制相应的灯灭i=s;while(i--){dis_temp(get_temp()); //用温度显示函数起到延时的作用}}void deal(uint t) //温度处理函数{uchar i;if((t>warn_l2)&&(t<=warn_l1)) //大于25度小于27度{warn(40,0x01); //第一个灯亮,蜂鸣器发出“滴”声}else if(t<=warn_l2) //小于25度{warn(10,0x03); //第一个和第二个灯亮,蜂鸣器发出“滴”声}else if((t<warn_h2)&&(t>=warn_h1)) //小于32度大于30度{warn(40,0x04); //第三个灯亮,蜂鸣器发出“滴”声}else if(t>=warn_h2) //大于32度{warn(10,0x0c); //第三个和第四个灯亮,蜂鸣器发出“滴”声}Else //在27度和30度之间时只是调用显示函数延时{i=40;while(i--){dis_temp(get_temp());}}}void init_com(void) //串口初始化函数{TMOD = 0x20;PCON = 0x00;SCON = 0x50;TH1 = 0xFd; //波特率9600TL1 = 0xFd;TR1 = 1;}void comm(char *parr) //串口数据发送函数{do{SBUF= *parr++; //发送数据while(!TI); //等待发送完成标志为1 TI=0; //标志清零}while(*parr); //保持循环直到字符为'\0' }void main() //主函数{uchar buff[4],i;dula=0;wela=0;init_com();while(1){tempchange(); //温度转换函数for(i=10;i>0;i--){dis_temp(get_temp()); //获取温度并显示}deal(temp); //进行温度处理sprintf(buff,"%f",f_temp); //将浮点型温度格式化为字符型for(i=10;i>0;i--){dis_temp(get_temp());} //温度显示comm(buff); //串口发送数据for(i=10;i>0;i--){dis_temp(get_temp());} //温度显示}}。

吃豆人C语言源代码

吃豆人C语言源代码

#include "graphics.h"#include "stdlib.h"#include "dos.h"#include "bios.h"#include "stdio.h"#include "time.h"#include "graphics.h"#include "conio.h"#define LEFT 0x4b00#define RIGHT 0x4d00/*{int x;int y;};{int x;int y;/*02,1,2,2,2,1,1,2,1,1,0,0,0,1,1,4,1,1,0,2, 2,1,1,0,2,1,1,2,0,1,1,2,2,2,2,2,0,0,0,2, 2,4,1,0,2,1,1,2,1,1,1,0,1,1,1,1,0,1,1,2, 2,1,2,1,2,1,1,2,1,3,2,2,1,1,1,1,2,2,1,2, 2,1,2,1,2,1,1,1,1,1,1,1,1,0,0,0,1,1,1,2, 2,1,2,1,0,1,1,1,1,2,1,0,1,2,2,2,1,1,1,2, 2,1,0,1,0,1,2,1,1,2,1,0,1,2,1,1,4,1,1,2, 2,1,0,2,0,1,2,1,1,2,1,0,1,2,1,1,1,1,1,2, 2,1,0,2,1,1,2,1,1,2,1,0,2,2,1,0,0,0,1,2,2,1,1,2,1,1,2,1,1,2,1,0,2,1,1,2,2,1,1,2,2,1,2,2,1,2,2,1,1,1,1,0,1,4,1,2,0,0,1,2,2,1,0,0,0,0,0,4,0,1,1,0,1,1,1,1,0,0,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2};/*数组就是地图( level 1)*/ ;int b[15][20]={2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,0,0,0,0,0,1,0,0,0,1,1,1,1,1,1,0,2,2,1,2,2,2,2,1,2,1,1,0,0,0,1,1,4,1,1,0,2,2,1,1,0,2,2,1,2,0,1,2,2,2,2,2,2,0,0,0,2,2,4,1,0,2,1,1,2,1,1,1,0,1,1,2,1,0,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2};/*数组就是地图( level 3)*/int xx[5][2];/*判断敌人方向用地结构体*/int false=0; /*判断输赢地参数*//*************************选择关卡********************************************************************/ voidlevelchoose(int x){intj,k;switch(x){case 1:break;case 2:for(j=0;j<15;j++)for(k=0;k<20;k++)a[j][k]=b[j][k];break;case 3:for(j=0;j<15;j++)for(k=0;k<20;k++)a[j][k]=c[j][k];}}{/*XXXXXXX\n");printf("XXXXXXXXXX XXXXXXX XXXXXXXXXX XXXXX XXXXXXXXXXXXX XXXXXXXX XXXXX\n");printf("XXXXXXXXXXX XXXXX XXXXXXXXXXXXXXX XXXXXXXXXXXX XXXXXXXXXX XXXX\n");printf("XXXXXXXXXXX XXXXX XXXX XXXXXXXXXXXX XXX\n");printf("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XX\n");printf("X XXXXXX XXXXXXXX XXX XXXX XXXXX XXXXXX XXXXX X\n");printf("X XXXXX XXXXXX XXXXXXX XXX XXX XXXX XXXXX XXXXXX XXXXX X\n");printf("X XXXXXX XXXXX XXXXXXX XXXXX XX XX XXXXX XXXXXX XXXX X\n");printf("X XXXXXX XXXX X XXXXX XXXXXX XX XX XXXX X XXXXX XXXX X\n");printf("X XXXXXX XXXX X XXXXX XXXXXX XX XX XXXX X XXXXX X XXX X\n");printf("X XXXXXX XXXX X XXXXX XXXXXXXXXX X XX X XXXX X XXXXX X XXX X\n");printf("X XXXXX XXXX XXX XXXX XXXXXXXXXX X X XXX XXX XXXX XX XX X\n");printf("X XXXX XXX XXXX XXXXXXXXXX X X XXX XXX XXXX XX XX X\n");printf("X XXXXXXXXXXX XXXX XXXXXX XX X X XXX XXXX XXX X X\n");printf("X XXXXXXXXXX XXXXX XXX XXXXXX XX X X XX XXXXX XXX XXXX X\n");printf("X XXXXXXXXXX XXXXX XXXX XXXX XX XXXXXX XXXXX XXX XXXX X\n");}{}{union REGS r;r.h.ah=0x86;r.x.cx=microsec>>16;r.x.dx=microsec;int86(0x15,&r,&r);}/***************************吃豆子地函数*********************************************************************************/drawblackdou(intx,int y){setcolor(0);circle(100+y*20,100+x*20,3);/*吃到豆子后就加一*/sum++;/*吃到后这里就成为普通平地*/ a[x][y]=1;}{/*/*/*/*}{inti;/*line(1,1,9,9);line(0,5,10,5);line(9,1,1,9);/*在堆中申请200字节空间*/ save=malloc(200);/*将雪花位图保存到save中*/ getimage(0,0,10,10,save);/*清屏*/cleardevice();/*设置随机数地种子数*/randomize();/*计算雪花位置地横坐标数组*/for(i=0;i<62;i++)sx[i]=(i+2)*10;/*******************以下地键控while循环控制播放音乐和下雪动画********************************************//*如果未按键,执行循环体*/while(!kbhit()){Pr();{/*/*}/*/*/*/*Pr();/*{putimage(snow[i].x,snow[i].y,save,COPY_PUT);if(snow[i].y>500) snow[i].y=10-random(200);}change++;if(change==140)change=10;}nosound();cleardevice();}/*****************图形系统初始化函数************************************************************************/ voidInit(void){intgd=DETECT,gm;initgraph(&gd,&gm,"c:\\tc");cleardevice();}/***************************开始函数{inti,j;/*{}/*{}/*{}/*代表豆子*/else if(a[i][j]==0){setcolor(YELLOW);circle(100+j*20,100+i*20,3);}/*敌人也自己地开始坐标*/you.x=5;you.y=9;them[0].x=2;them[0].y=15;them[1].x=4;them[1].y=1;them[2].x=8;them[2].y=16;them[3].x=12;them[3].y=13;them[4].x=13;them[4].y=7;}/************************敌人移动地过程********************************************************************/ voidmovethem(struct play *them){inti,loop;{else{loop:/*{{them[i].x++;goto loop;}}else if(xx[i][0]==2){them[i].x++;if(a[them[i].x][them[i].y]==2){them[i].x--;goto loop;}}else if(xx[i][0]==3){them[i].y++;if(a[them[i].x][them[i].y]==2){them[i].y--;goto loop;}{}}}}{inti;/*/*}/*********************胜利地话******************************************************************************* ****/win(){cleardevice();settextstyle(0,0,4);while(!kbhit()){setcolor(rand()%13+1);outtextxy(200,200,"YOU WIN!");delay(1000);}}/*********************失败地话******************************************************************************* ****/false1(){{}}{inti;/*}{cleardevice();DrawSnow();hello();getchar();instruction();scanf("%d",&choice);levelchoose(choice);getchar();loop:/*开始画面*/begain();while(!kbhit()){/*重画敌人*/for(i=0;i<5;i++)setfillstyle(SOLID_FILL,GREEN);circle(them[i].y*20+100,them[i].x*20+100,9); TimeDelay(280000);/*{else if(key==DOWN){you.x++;if(a[you.x][you.y]==2)you.x--;else if(a[you.x][you.y]==0)drawblackdou(you.x,you.y);}else if(key==RIGHT)you.y++;if(a[you.x][you.y]==2) you.y--;else if(a[you.x][you.y]==0) drawblackdou(you.x,you.y); }else if(key==LEFT){you.y--;}/*}/*}/*closegraph();}。

经典C语言源代码

经典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.hvoid main() {int i, flag, year, month, day, dayth;int month_day[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; 牰湩晴尨请输入年/ 月/日:\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;牰湩晴尨请输入%d个浮点数:\n,SIZE);for (i = 0; i < SIZE; i++) {//scanf_s(%f, &data[i]);data[i] = rand() % 30 + 1;, data[i]);、printf(%f}min = 0;for (i = 1; i < SIZE; i++) {if (data[i] < data[min])min = i;}牰湩晴尨最小值是%5.2f,位置是]\n, data[min], min); }3、30个数从小到大排序(1)#include stdio.h# define SIZE 30void main() {int i,j;float data[SIZE],temp;int min;牰湩晴尨请输入%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);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);牰湩晴尨排序前:);show_array(score, SIZE);sort(score, SIZE);牰湩晴尨排序后:);show_array(score, SIZE);}void accept_array(float a[], int size) { int i;牰湩晴尨请输入%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++)%5.2f, a[i]); printf(printf(\);}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(\);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];牰湩晴尨请输入%d整型数:,SIZE); for (; pa < qa + SIZE; pa++)scanf_s(%d, pa);for (pa--; qa <= pa; pa--)%d, *pa);printf(}5、两字符串相连:#include stdio.h #include string.hvoid str_cat(char str1[], char str2[]); void main() {int i, j;char str1[160];char str2[80];牰湩晴尨请输入第一个字符串:); gets(str1);牰湩晴尨请输入第二个字符串:);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';}转置)a,b、二维数组(6#include stdio.h void main() {int i, j, b[2][3];6} };for (i = 0; i < 2; i++) {for (j = 0; j < 3; j++)b[i][j] = a[j][i];}printf(\a:\n);for (i = 0; i < 3; i++) {for (j = 0; j < 2; j++)printf(], a[i][j]);printf(\);}printf(\b:\n);for(i = 0; i < 2; i++) {for (j = 0; j < 3; j++)printf(], b[i][j]);printf(\);}}7、输入一个二维数组并输出(指针)#include stdio.hvoid 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;牰湩晴尨请输入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轮排序9-ifor (j = 0; j < 9 - i; j++)//每轮进行次交换{if (a[j] > a[j + 1]){temp = a[j];a[j] = a[j + 1];//大的沉底,小的上浮a[j + 1] = temp;}}}牰湩晴尨排序结果:\n);for (i = 0; i < 10; i++)printf(M, 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语言源代码(供初学者使用)

#include<stdio.h>main(){float pi=3.14159265,r;printf("enter radius:\n");scanf("%f",&r);printf("r=%.2f,c=%.2f,area=%.2f\n",r,2*pi*r,pi*r*r);system("pause");}#include<stdio.h>main(){int m,n,x,y;printf("inputm,n:\n");scanf("%d%d",&m,&n);if (n%2==1){printf("error!! n bu shi ji shu!\n",n);return;}/*n在这里不能是奇数*/x=(4*m-n)/2;y=(n-2*m)/2;if((x>=0)&&(y>=0))printf("x=%d,y=%d\n",x,y); elseprintf("shu ru cuo wu!\n");getch();}#include<stdio.h>#include<math.h>#include<stdlib.h>main(){float a,b,C;printf("enter 3number(a,b,C):\n"); scanf("%.2f %.2f %.2f",&a,&b,&C); s=0.5*a*b*sinC;printf("s=%.2f\n",s);system("pause");}#include<stdio.h>main(){int ds,nl,yf;char c;printf("shu ru ds;\n");scanf("%d",&ds);if (ds<14||ds>1184){printf("input error!press any end...\n"); scanf("%c",&c);exit(0);}/*输入的数字必须是-14~1184之间*/nl=(ds+115)%100;yf=(ds+115)/100;printf("nl=%d,yf=%d\n",nl,yf);system("pause");}#include<stdio.h>#include<string.h>main(){char s1[100],s2[100],s3[100],s4[100]; printf("input a string:\n");gets(s1);strcpy(s1,s2);strcat(s1,"--------------");strcpy(s3,strcat(s1,"--------------"));strcat(strcpy(s3,strcat(s1,"--------------")),s2);strcpy(s4,strcat(strcpy(s3,strcat(s1,"--------------")),s2)); puts(s4);system("pasue");getch();}#include<stdio.h>#include<math.h>main(){float x1,x2,a,b,c;printf("input 3 number(a,b,c):\n");scanf("%f%f%f",&a,&b,&c);x1=(-b+sqrt(b*b-4*a*c))/(2*a);x2=(-b-sqrt(b*b-4*a*c))/(2*a);printf("x1=%f,X2=%f\n",x1,x2);system("pause");getch();}#include<stdio.h>main(){int a,b,c,t;printf("input 3 number:\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("1:%d,2:%d,3:%d\n",a,b,c);system("pause");}#include<stdio.h>#include<stdlib.h>#include<conio.h>main(){float pi=3.14159265,r;textbackground(YELLOW);/* 设置背景色为黄色,注意颜色应该大写,可更改 */ textcolor(RED); /* 设置文件颜色为红色,可更改 */clrscr(); /* 清屏,使设置生效 */printf("enter radius:");scanf("%f",&r);if(r<0)printf("Enter Error!\n");elseprintf("r=%.2f,c=%.2f,area=%.2f\n",r,2*pi*r,pi*r*r); system("pause");/* 暂停,按任一键继续 */#include<stdio.h>#include<math.h>#include<conio.h>main(){float a,b,c,delt,x1,x2,p,q;textcolor(YELLOW);clrscr();printf("Input a b c:\n");scanf("%f%f%f",&a,&b,&c);if(a==0){printf("It's not a quadratic equation!\n");system("pause");return;}delt=b*b-4*a*c;if(delt>=0){x1=(-b+sqrt(delt))/(2*a);x2=(-b-sqrt(delt))/(2*a);printf("x1=%.3f x2=%.4f\n",x1,x2);}else{p=-b/(2*a);q=sqrt(-delt)/(2*a);printf("p=%.4fq=%.4f\n",p,q);}system("pause");getch();}}#include<stdio.h>main(){float pi=3.14159265,r;int k=0;while(k<=3){printf("enter radius:\n");scanf("%f",&r);printf("r=%.2f,c=%.2f,area=%.2f\n",r,2*pi*r,pi*r*r); printf("press any key to continue\npress esc to exit."); k++;}}#include<stdio.h>#include<stdlib.h>#include<time.h>#include<conio.h>main(){int a,b,c,oper;long limit,i=0;char char1;textcolor(GREEN);/*设置字体颜色为绿色*/clrscr(); /*清屏,是设置生效*/while(i<=3){printf("qing xuan ze jia huo jian(1or2,1:+,2:-\n"); scanf("%d",&oper);printf("Enter max (<10000):\n");scanf("%ld",&limit);srand((unsigned long)time(0));a=rand()*limit/RAND_MAX;b=rand()*limit/RAND_MAX;while((a<b)&&(oper==2)){a=rand()*limit/RAND_MAX;b=rand()*limit/RAND_MAX;}char1=(oper==2?'-':'+');printf("%d%c%d\n",a,char1,b);scanf("%d",&c);if((oper==2&&a-b==c)||(oper!=2&&a+b==c)) printf("OK!You are very clever!\n"); elseprintf("The result is not correct!\n"); i++;}getch();}#include<stdio.h>#include<conio.h>main(){int y,i=0;textcolor(YELLOW);/*天下事无难易之分只有做与不做之别*/ textbackground(GREEN);clrscr();/*清屏,是设置生效*/while(i<=3){printf("Input year:\n");scanf("%d",&y);if(y%4==0){if(y%100==0){if(y%400==0)printf("y shi run nian!\n"); elseprintf("y bu shi run nian !\n"); }elseprintf("y shi run nian!\n");}elseprintf("y bu shi run nian!");i++;}getche();}#include<stdio.h>#include<time.h>#include<string.h>main(){int xz,wday1,hour1;struct tm *timeptr;time_t secsnow;char s1[30],s2[30],s3[30];printf("input whom do you say to?:\n"); scanf("%d",&xz);if(xz==1)strcpy(s1,"mother");else if(xz==2)strcpy(s1,"father");elsestrcpy(s1,"");time(&secsnow);timeptr=localtime(&secsnow);wday1=timeptr->tm_wday;if(wday1==6)strcpy(s2,"Happy saturday!");else if(wday1==0)strcpy(s2,"Happy sunday");elsestrcpy(s2,"");hour1=timeptr->tm_hour;if(hour1>=4&&hour1<=10)strcpy(s3,"Good morning!");else if(hour1>=17&&hour1<=22)strcpy(s3,"Good afternoon!");elsestrcpy(s3,"Good evening!");printf("%s%s%s",s1,s2,s3);getch();}#include<stdio.h>#include<conio.h>main(){int day,year,month,i=0;textbackground(BROWN);clrscr();while(i<=3){printf("Input 2 number:\n");scanf("%d%d",&year,&month);switch(month){case 1:case 3:case 5:case 7:case 8:case 10:case 12: day=31;break;case 4:case 6:case 11:day=30;break;case 2:day=28;if((year%4==0&&year%100!=0)||year%400==0)day=29;break;deflault:printf("Invalid month input!\n");return;}printf("There are%d days in %d.%d\n",day,year,month); i++;getch();}}。

经典C语言编程30例

经典C语言编程30例

经典C语言源代码30例题目:海滩上有一堆桃子,五只猴子来分。

第一只猴子把这堆桃子凭据分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。

第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?1.程序分析:2.程序源代码:main(){int i,m,j,k,count;for(i=4;i<10000;i+=4){ count=0;m=i;for(k=0;k<5;k++){j=i/4*5+1;i=j;if(j%4==0)count++;elsebreak;}i=m;if(count==4){printf("%d\n",count);break;}}}【程序81】题目:809*??=800*??+9*??+1 其中??代表的两位数,8*??的结果为两位数,9*??的结果为3位数。

求??代表的两位数,及809*??后的结果。

1.程序分析:2.程序源代码:output(long b,long i){ printf("\n%ld/%ld=809*%ld+%ld",b,i,i,b%i);}main(){long int a,b,i;a=809;for(i=10;i<100;i++){b=i*a+1;if(b>=1000&&b<=10000&&8*i<100&&9*i>=100)output(b,i); }}【程序82】题目:八进制转换为十进制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);}【程序83】题目:求0—7所能组成的奇数个数。

学生管理系统c语言源代码

学生管理系统c语言源代码

学生管理系统c语言源代码学生管理系统c语言源代码#include stdio.h#include dos.h#include string.h#include stdlib.h#include malloc.h#define SIZE 8struct student{char name;char num;int score;float ave;struct student *next;}stu[SIZE],temp,s;void shuru(){int i,j,sum,length,flag=1,a;FILE *fp;while(flag==1){printf(“Define a rangeclass number:");scanf("%d",printf("Input the total number of the class(a):"); scanf("%d",length);if(lengtha)flag=0;}for(i=0;ilength;i++){printf("\n请输入学生的信息:");printf("\n输入姓名:");scanf("%s",stu[i].name);printf("\n输入序号.:");scanf("%s",stu[i].num);printf("\n输入成绩:\n");sum=0;for(j=0;jj++){printf("score %d:",j+1);scanf("%d",stu[i].score[j]);sum+=stu[i].score[j];}stu[i].ave=sum/3.0;}学生管理系统c语言源代码fp=fopen("stu1.txt","w");for(i=0;ilength;i++)if(fwrite(stu[i],sizeof(struct student),1,fp)!=1)printf("File write error\n");fclose(fp);fp=fopen("stu1.txt","r");printf("\name\ NO. score1 score2 score3 sum ave\n");for(i=0;ilength;i++){fread(stu[i],sizeof(struct student),1,fp);printf("%3s%5s%7d%7d%7d%7d%10.2f\n",stu[i].name,stu[i].num,stu[i ].score,stu[i].score,stu[i].score,sum=stu[i].score+stu[i].score+stu[i].score,stu[i].ave);}}void chaxun(){ FILE *fp, *fp1;char n,name;int i,j,k,t,m,flag=1;if((fp=fopen("stu1.txt","r"))==NULL){printf("Can not open the file.");exit(0);}printf("\noriginal data:\n");k=i;printf("\nPlease select the menu(1.number ):"); scanf("%d",switch(m){case 1:printf("\nchaxun number:");scanf("%s",n);for(flag=1,i=0;ii++){if(strcmp(n,stu[i].num)==0){j=i;flag=0;break;}}break;case 2:printf("\nchaxun name:");scanf("%s",name);for(flag=1,i=0;ii++){if(strcmp(name,stu[i].name)==0){j=i;flag=0;break;学生管理系统c语言源代码}}}if(!flag){printf("\nYou can find:\n");fp1=fopen("stu2.txt","w");printf(" name NO. score1 score2 score3ave\n");fwrite(stu[j],sizeof(struct student),1,fp1);printf("%-15s%11s%7d%7d%7d%10.2f",stu[j].name,stu[j].num,stu[j].score,stu[j].score,stu[j].score,stu[j].ave);}else printf("\nNot found!");fclose(fp);fclose(fp1);}xiugai(){ int a;printf("\nplease select the menu(1.CHARU 2.__ ):");scanf("%d",switch(a){case 1:Insert(); break;case 2:Delete(); break;}}Insert(){ FILE *fp;int i,j,t,n;printf("\nNO.:");scanf("%s",s.num);printf("name:");scanf("%s",);printf("score1,score2,score3:");scanf("%d,%d,%d",s.score,s.score,s.score);s.ave=(s.score+s.score+s.score)/3.0;if((fp=fopen("stu1.txt","r"))==NULL){printf("Can not open the file.");exit(0);}printf("\noriginal data:\n");for(i=0;fread(stu[i],sizeof(struct student),1,fp)!=0;i++) {printf("\n%-15s%11s",stu[i].name,stu[i].num);for(j=0;jj++)学生管理系统c语言源代码printf("%7d",stu[i].score[j]);printf("%10.2f",stu[i].ave);}fclose(fp);n=i;for(t=0;stu[t].aves.avett++);printf("\nnow:\n");fp=fopen("stu1.txt","w");for(i=0;ii++){fwrite(stu[i],sizeof(struct student),1,fp);printf("\n%-15s%11s",stu[i].name,stu[i].num);for(j=0;jj++)printf("%7d",stu[i].score[j]);printf("%10.2f",stu[i].ave);}fwrite(s,sizeof(struct student),1,fp);printf("\n%-15s%11s%7d%7d%7d%10.2f",,s.num,s.score,s.score, s.score,s.ave);for(i=t;ii++){fwrite(stu[i],sizeof(struct student),1,fp);printf("\n%-15s%11s",stu[i].name,stu[i].num);for(j=0;jj++)printf("%7d",stu[i].score[j]);printf("%10.2f",stu[i].ave);}fclose(fp);}Delete(){ FILE *fp;int i,j,t,n,flag;char number;if((fp=fopen("stu1.txt","rb"))==NULL){printf("Can not open the file.");exit(0);}printf("\noriginal data:");for(i=0;fread(stu[i],sizeof(struct student),1,fp)!=0;i++) {printf("\n%-15s%11s",stu[i].name,stu[i].num);for(j=0;jj++)printf("%7d",stu[i].score[j]);printf("%10.2f",stu[i].ave);}fclose(fp);n=i;学生管理系统c语言源代码printf("\nInput number deleted:");scanf("%s",number);for(flag=1,i=0;flagii++){if(strcmp(number,stu[i].num)==0){for(t=i;tt++){strcpy(stu[t].num,stu[t+1].num);strcpy(stu[t].name,stu[t+1].name);for(j=0;jj++)stu[t].score[j]=stu[t+1].score[j];stu[t].ave=stu[t+1].ave;}n=n-1;elseprintf("\n Not found!");printf("\nNow,the content of file:\n");fp=fopen("stu1.txt","wb");for(i=0;ii++)fwrite(stu[i],sizeof(struct student),1,fp);fclose(fp);fp=fopen("stu1.txt","r");for(i=0;fread(stu[i],sizeof(struct student),1,fp)!=0;i++)printf("%-15s%11s%7d%7d%7d%10.2f\n",stu[i].name,stu[i].num,stu[i].score, stu[i].score,stu[i].score,stu[i].ave);fclose(fp);}paixu(){FILE *fp;int i,j,n;if((fp=fopen("stu1.txt","r"))==NULL){printf("Can not open the file.");exit(0);}printf("\nfile'stu1.txt':");for(i=0;fread(stu[i],sizeof(struct student),1,fp)!=0;i++) {printf("\n%-15s%11s",stu[i].name,stu[i].num);for(j=0;jj++)printf("%7d",stu[i].score[j]);printf("%10.2f",stu[i].ave);}fclose(fp);n=i;for(i=0;ii++)for(j=i+1;jj++)学生管理系统c语言源代码if(stu[i].avestu[j].ave){temp=stu[i];stu[i]=stu[j];stu[j]=temp;}printf("\nnow:");fp=fopen("stu1.txt","w");for(i=0;ii++){fwrite(stu[i],sizeof(struct student),1,fp);printf("\n%-15s%11s",stu[i].name,stu[i].num);tongji(){ FILE *fp;int i,j,k,labe1,b;int a5=0;int a6=0;int a7=0;int a8=0;int a9=0; int a10=0; float t;if((fp=fopen("stu1.txt","r"))==NULL){printf("Can not open the file.");exit(0);}printf("\nfile'stu1.txt':");for(i=0;fread(stu[i],sizeof(struct student),1,fp)!=0;i++){printf("\n%-15s%11s",stu[i].name,stu[i].num); for(j=0;jj++)printf("%7d",stu[i].score[j]);printf("%10.2f",stu[i].ave);}fclose(fp);k=i;for(i=0;ii++){labe1=0;if(stu[i].ave60){labe1++;t=labe1/(float)k*100;}}printf("\nbujigelv:");printf("%f%",t);printf("\n");for(j=0;jj++){a5=0;a6=0;a7=0;a8=0;a9=0;a10=0;k=i;printf("kemu is %d:\n",j);for(i=0;ii++)学生管理系统c语言源代码{b=stu[i].score[j]/10;if(b6)a5++;elseif(b=6b7)a6++;elseif(b=7b8)a7++;elseif(b=8b9)a8++;if(b=9b10)a9++;elseif(b==10)a10++;}printf(" 不及格is %d\n",a5);printf(" 60--69 is %d\n",a6);printf(" 70--79 is %d\n",a7);printf(" 80--89 is %d\n",a8);printf(" 90--99 is %d\n",a9);printf(" 100 is %d\n",a10);}}main(){int a;printf(" ____\n"); printf(" 欢迎进入学生成绩管理系统\n");printf(" ____\n"); while(1){printf("\n选择菜单:\n");printf("\n");printf(" 1.输入 2.查询 3.排序 4.修改 5.统计 6.退出\n"); scanf("%d",switch(a){case 1: shuru();break;case 2: chaxun(); break;case 3: paixu(); break;case 4: xiugai(); break;学生管理系统c语言源代码case 5: tongji();break; case 6: exit(0); }。

经典C语言源代码

经典C语言源代码

经典C语言源代码1、(1)某年某月某日是星期几#include<>int main(){int year, month, day;while (scanf_s("%d%d%d", &year, &month, &day) != EOF) {if (month == 1 || month == 2)d.%d", &start[0],&start[1], &start[2]);printf("请输入结束日期,如:\n");scanf_s("%d.%d.%d", &end[0], &end[1], &end[2]);、int sum = 0;for (int mid = start[0]; mid < end[0]; mid++) { if ((mid % 400 == 0) || (mid % 4 == 0 && mid % 100 != 0)) {sum = sum + 366;}elsesum = sum + 365;}sum = sum - indexday(start[0],start[1],start[2]) +indexday(end[0],end[1],end[2]);printf("在%d.%d.%d-%d.%d.%d之间有%d天\n",start[0],start[1],start[2],end[0],end[1],end[2], sum);}"int indexday(int year, int month, int day) {int i, flag, dayth;int month_day[] ={ 0,31,28,31,30,31,30,31,31,30,31,30,31 };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];return dayth;}&18、递归求1*1+2*2+3*3+n*n#include ""long Element(int n) {if (n == 1)return 1 * 1;elsereturn Element(n - 1) + n*n;})void main() {int n;printf("请输入n的值:\n");scanf_s("%d", &n);printf("所求值为%d\n", Element(n));}19、最大公约数(辗转相除)#include<>void main() /* 辗转相除法求最大公约数 */ >{int m, n, a, b, t, c;printf("Input two integer numbers:\n");scanf_s("%d%d", &a, &b);m = a; n = b;while (b != 0) /* 余数不为0,继续相除,直到余数为0 */ {c = a%b; a = b; b = c;}printf("The largest common divisor:%d\n", a);printf("The least common multiple:%d\n", m*n / a);~}20、杨辉三角#include<>void main(){int i, j, n, k;printf("Enter n:"); scanf_s("%d", &n);for (i = 1; i <= n; i++){?k = 1;for (j = 1; j<i; j++){printf("%3d", k);k = k*(i - j) / j;}//每次要打印的下一个数等于前一个数乘以其所在行数和列数的差再除以其列数printf("%3d", k);printf("\n");}}】21、约瑟夫#include <>void main(){int n, m, i, s=0;printf ("Enter n: m: ");scanf("%d%d", &n, &m);for (i=2; i<=n; i++) s=(s+m)%i;printf ("The winner is %d\n", s+1); }—22、斐波拉契#include<>void main(){long f, f1, f2; int i, n;printf("Enter n : ");scanf_s("%d", &n);f1 = 1;f2 = 1;printf("%10d%10d", f1, f2);for (i = 1; i <= n; i++)…{f = f1 + f2;printf("%10d", f);f1 = f2; f2 = f;if (i % 10 == 0)printf("\n");}}23、海滩上有一堆桃子,五只猴子来分。

(整理)C语言入门经典案例及源代码.

(整理)C语言入门经典案例及源代码.

循环控制输出图案【程序1】题目:输出9*9口诀。

1.程序分析:分行与列考虑,共9行9列,i控制行,j控制列。

2.程序源代码:#include "stdio.h"main(){int i,j,result;printf("\n");for (i=1;i<10;i++){ for(j=1;j<10;j++){result=i*j;printf("%d*%d=%-3d",i,j,result);/*-3d表示左对齐,占3位*/}printf("\n");/*每一行后换行*/}}【程序2】题目:要求输出国际象棋棋盘。

1.程序分析:用i控制行,j来控制列,根据i+j的和的变化来控制输出黑方格,还是白方格。

2.程序源代码:#include "stdio.h"main(){int i,j;for(i=0;i<8;i++){for(j=0;j<8;j++)if((i+j)%2==0)printf("%c%c",219,219);elseprintf(" ");printf("\n");}}==============================================================【程序3】题目:打印楼梯,同时在楼梯上方打印两个笑脸。

1.程序分析:用i控制行,j来控制列,j根据i的变化来控制输出黑方格的个数。

2.程序源代码:#include "stdio.h"main(){int i,j;printf("\1\1\n");/*输出两个笑脸*/for(i=1;i<11;i++){for(j=1;j<=i;j++)printf("%c%c",219,219);printf("\n");}}【程序4】题目:打印出如下图案(菱形)****************************1.程序分析:先把图形分成两部分来看待,前四行一个规律,后三行一个规律,利用双重for循环,第一层控制行,第二层控制列。

c语言经典游戏代码

c语言经典游戏代码

c语⾔经典游戏代码C语⾔精品游戏主⾓和怪物源码//C语⾔多线程-主⾓和怪物#include#include#define bool int //定义int变量为bool变量,bool不是真就是假int a=0,b=20;//主⾓的坐标int x=1,y=0;//怪物的坐标int i=1;//i值为真HANDLE hMutex;//1.坐标void GamePosition(HANDLE g_hout,int x,int y){COORD pos;//点的结构体pos.X=x;//横坐标pos.Y=y;//纵坐标SetConsoleCursorPosition(g_hout,pos);//设置控制平台光标位置}DWORD WINAPI Func(LPVOID lpParamter)//多线程的功能函数6.线程是画怪物{HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);//7.拿到这张纸WaitForSingleObject(hMutex, INFINITE);//13.⾃⼰进来,⾃⼰⽤洗⼿间GamePosition(hout,x,y),printf('●');//8.在纸上画怪物ReleaseMutex(hMutex);//14.放弃使⽤权while(1)//9.怪物在横坐标为从0-10依次循环移动{if(x>=0&&i==1){printf(' ');GamePosition(hout,++x,y);printf('●');Sleep(1000);if(x==10)}else if(x<>{printf(' ');GamePosition(hout,--x,y);printf('●');Sleep(1000);if(x==0)i=1;}}return 0;}int main(){HANDLE hThread = CreateThread(NULL, 0, Func, NULL, 0, NULL);//5.创建线程hMutex = CreateMutexA(NULL, FALSE, '123');//创建互斥锁(量)//10.关上洗⼿间HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);//2.拿到这张纸WaitForSingleObject(hMutex, INFINITE);//11.等待你的同事出来 15步接着GamePosition(hout,a,b),printf('☆');//3.在纸上画主⾓ReleaseMutex(hMutex);//12.同事出来了,放弃了洗⼿间的使⽤权while(1){if(kbhit())switch(getch())//控制左右 4.主⾓上下左右移动{case 'w':case 'W':if(b>3)GamePosition(hout,a,b),printf(' '),GamePosition(hout,a,--b),printf('☆'); break;case 's':case 'S':if(b<20)gameposition(hout,a,b),printf('>break;col = rand() % 10;if(map[row][col] == '0'){map[row][col] = 'x';num++;}}while(num <>for (row = 0;row <=>for (col = 0;col <=>if(map[row][col] != 'x'){int cnt = 0;for (num = 0;num <=>if(row + delta[num][0] <>continue;}if(row + delta[num][0] > 9){continue;}if(col + delta[num][1] <>continue;}if(col + delta[num][1] > 9){continue;}if(map[row + delta[num][0]][col + delta[num][1]]== 'x'){ cnt++;}}map[row][col] = '0' + cnt;}}}for (row = 0;row <>for(col = 0;col < 10;col="">printf('* ');}num = 0;int x,y;do{printf('please enter the coordinate of array:'); scanf('%d%d',&x,&y);show[x-1][y-1] = 1;if(map[x-1][y-1] == '0'){for (num = 0;num <=>if(x-1 + delta[num][0] <>continue;}if(x-1 + delta[num][0] > 9){continue;}if(y -1+ delta[num][1] <>continue;}if(y-1 + delta[num][1] > 9){continue;}show[x-1+delta[num][0]][y-1+delta[num][1]] = 1; }}if (map[x-1][y-1]!= 'x'&&map[x-1][y-1] != '0'){for (num = 0;num <=>int cnt = 0;if(x-1 + delta[num][0] <>continue;}if(x-1 + delta[num][0] > 9){continue;}if(y-1 + delta[num][1] <>if(y-1 + delta[num][1] > 9){continue;}if( map[x -1 + delta[num][0]][y -1+ delta[num][1]] != 'x'){ show[x-1 + delta[num][0]][y -1+ delta[num][1]] = 1 ; }}}if(map[x-1][y-1] == 'x') {printf('game over!\n');for (row = 0;row <>for(col = 0;col < 10;col="">printf('%c ',map[row][col]);}printf('\n');}return 0;}system('cls');printf('mine sweeping:\n');for (row = 0;row <>for(col = 0;col < 10;col="">if (show[row][col] == 1){printf('%c ', map[row][col]);}else{printf('* ');}}printf('\n');}for (row = 0;row <>for(col = 0;col < 10;col=""> if (show[row][col] == 1 ){num++;}}}printf('num:%d\n',num);}while(num <>printf('you win!');return 0;}。

C语言程序设计经典100例(全)文本文档

C语言程序设计经典100例(全)文本文档
{
for(j=1;j<=i;j++)
printf("%c%c",219,219);
printf("\n");
}
}
【程序11】
题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月
后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?
if(y>z)
{t=y;y=z;z=t;}/*交换z,y的值*/
printf("small to big: %d %d %d\n",x,y,z);
}
==============================================================
【程序6】
题目:用*号输出字母C的图案。
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;
if((i+j)%2==0)
printf("%c%c",219,219);
else
printf(" ");
printf("\n");
}
}
==============================================================

c语言源代码

c语言源代码

c语言源代码在计算机科学领域,C语言是一种被广泛应用的程序设计语言,它以其简洁、高效和可移植性而闻名。

本文将提供一些C语言的源代码示例,帮助读者更好地理解和应用这门编程语言。

1. Hello World程序#include <stdio.h>int main() {printf("Hello, World!");return 0;}上述代码是C语言中经典的Hello World程序。

它使用了stdio.h头文件,其中包含了用于输入输出的函数。

main()函数是程序的入口点,它执行printf函数并打印出"Hello, World!"的字符串。

最后,return语句表示程序正常结束。

2. 计算两个数的和#include <stdio.h>int main() {int num1, num2, sum;printf("请输入两个整数:");scanf("%d %d", &num1, &num2);sum = num1 + num2;printf("两个数的和为:%d", sum);return 0;}以上代码展示了如何用C语言编写一个简单的计算两个数的和的程序。

通过使用scanf函数,用户可以在程序运行时输入两个整数。

程序将这两个数相加,并使用printf函数打印出结果。

3. 判断一个数是否为质数#include <stdio.h>int main() {int num, i, isPrime = 1;printf("请输入一个正整数:");scanf("%d", &num);for (i = 2; i <= num / 2; ++i) {if (num % i == 0) {isPrime = 0;break;}}if (isPrime)printf("%d是质数", num);elseprintf("%d不是质数", num);return 0;}上述代码展示了如何用C语言编写一个判断一个数是否为质数的程序。

C语言游戏源代码

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语言经典代码

C语言经典代码
}
**********用星号输出C图案******************************************************************* 2 ******************
#include<stdio.h>
main()
{
printf("Hello C word\n");
{
temp2=a[j];
a[j]=temp1;
temp1=temp2;
}
break;
}
}
}
for(i=0;i<11;i++)
{
#include<stdio.h>
main()
{
int i,j,k,n;
printf("'water flower'number is:");
for(n=100;n<100000;n++)
{
i=n/100;
j=n/10%10;
{
t=x;
x=z;
z=t;
}
if(y>z)
{
t=z;
z=y;
y=t;
}
printf("small to big:%d%d%d",x,y,z);
#include<stdio.h>
main()
{
int x,y,z,t;
scanf("%d%d%d",&x,&y,&z);
if(x>y)

C语言源代码

C语言源代码

C语言源代码C 语言作为一门经典的编程语言,在计算机科学领域中具有举足轻重的地位。

C 语言源代码是用 C 语言编写的程序的原始文本形式,它是程序员思想的具体体现,也是计算机能够理解和执行的指令集合。

C 语言源代码的基本组成部分包括预处理指令、变量声明、函数定义、控制结构等。

预处理指令通常以“”开头,比如“include <stdioh>”,它用于在编译前对源代码进行一些预处理操作,如包含所需的头文件。

变量声明用于指定程序中使用的数据类型和名称。

C 语言中有多种数据类型,如整型(int)、浮点型(float、double)、字符型(char)等。

例如,“int age =25;”声明了一个名为 age 的整型变量,并初始化为 25。

函数是 C 语言中的重要概念,它将一段具有特定功能的代码封装起来,方便重复使用和代码的组织。

一个简单的函数可能如下所示:```cint add(int a, int b) {return a + b;}```在上述代码中,“add”是函数名,“int”表示函数返回值的类型,“a”和“b”是函数的参数。

控制结构用于决定程序的执行流程,包括顺序结构、选择结构(如ifelse 语句)和循环结构(如 for 循环、while 循环)。

比如,ifelse 语句用于根据条件执行不同的代码块:```cif (age >= 18) {printf("You are an adult\n");} else {printf("You are a minor\n");}```for 循环用于重复执行一段代码一定的次数:```cfor (int i = 0; i < 5; i++){printf("%d\n", i);}```while 循环则在条件为真时持续执行代码:```cint count = 0;while (count < 10) {printf("%d\n", count);count++;}```C 语言源代码的编写需要遵循严格的语法规则。

二分查找算法C语言源程序代码

二分查找算法C语言源程序代码

二分查找算法C语言源程序代码以下是一个使用二分查找算法的C语言源代码程序,包括了详尽的注释解释每一步的操作和算法原理。

```c#include <stdio.h>//二分查找函数int binarySearch(int arr[], int left, int right, int target) //当左边界大于右边界时,表示查找失败,返回-1if (left > right)return -1;}//计算数组中间元素的下标int mid = left + (right - left) / 2;//判断中间元素是否等于目标值if (arr[mid] == target)return mid;}//如果中间元素大于目标值,说明目标值可能在左半部分,调用递归在左半部分继续查找if (arr[mid] > target)return binarySearch(arr, left, mid - 1, target);}//如果中间元素小于目标值,说明目标值可能在右半部分,调用递归在右半部分继续查找if (arr[mid] < target)return binarySearch(arr, mid + 1, right, target);}int maiint arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91};int n = sizeof(arr) / sizeof(arr[0]); // 计算数组的长度int target = 23; // 目标值int result = binarySearch(arr, 0, n - 1, target); // 调用二分查找函数if (result == -1)printf("目标值不存在\n");} elseprintf("目标值在数组中的位置为:%d\n", result);}return 0;```该程序首先定义了一个名为 `binarySearch` 的函数,用于实现二分查找算法。

经典C语言编程100例

经典C语言编程100例

经典C语言编程100例(1-30)【程序1】题目:有1、2、3、4个数字,能组成多少个互不1.1、22.{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);}}万提万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数?1.2.{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)elsebonus=bonus10+(i-1000000)*0.01; printf("bonus=%d",bonus);}==================================== ==========================1.100后2.#include"math.h"main(){longinti,x,y,z;for(i=1;i<100000;i++){x=sqrt(i+100);/*x为加上100后开方后的结果*/*/}}题目:输入某年某月某日,判断这一天是这一年的第几天?1.程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天。

c语言源代码

c语言源代码

c语言源代码下面是一个简单的 C 语言源代码示例,用于计算并输出斐波那契数列的前 n 个数字。

该程序首先要求用户输入要生成的斐波那契数列的长度,然后使用迭代的方式生成并输出这些数字。

```c#include <stdio.h>// 函数声明void generateFibonacci(int n);int main() {int n;// 提示用户输入要生成的斐波那契数列的长度printf("请输入要生成的斐波那契数列的长度:");scanf("%d", &n);// 调用函数生成并输出斐波那契数列generateFibonacci(n);return 0;}// 生成斐波那契数列的函数定义void generateFibonacci(int n) {int i, first = 0, second = 1, next;// 输出斐波那契数列的前 n 个数字printf("斐波那契数列的前 %d 个数字为:\n", n);for (i = 0; i < n; ++i) {if (i <= 1)next = i;else {next = first + second;first = second;second = next;}printf("%d ", next);}printf("\n");}```这个程序首先包含了 `<stdio.h>` 头文件,这是 C 标准库的一部分,用于输入输出操作。

然后定义了一个函数`generateFibonacci`,用来生成并输出斐波那契数列的前 n 个数字。

在 `main` 函数中,程序提示用户输入要生成的斐波那契数列的长度,然后调用 `generateFibonacci` 函数来实现具体的生成和输出操作。

相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

经典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中各元素,且两数组仍保持非降序排列。

相关文档
最新文档