经典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、海滩上有一堆桃子,五只猴子来分。

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

相关文档
最新文档