-经典C语言源代码

合集下载

C语言程序设计 入门源代码代码集合【精选文档】

C语言程序设计 入门源代码代码集合【精选文档】

#include 〈stdio.h〉void print_star(void){printf("*****************\n"); }void print_welcome(void){printf(”C language,welcome!\n");}void main(){print_star();print_welcome();print_star();getchar();}演示2#include "stdio。

h"int sum(int i,int j){return(i + j);}void main(){int n1,n2;printf("input 2 numbers:\n”);scanf("%d%d”,&n1,&n2);printf("the sum = %d\n",sum(n1,n2));getchar();}演示3#include "stdio。

h"int maxnum(int,int,int);main(){int a,b,c;printf("Please enter 3 numbers:\n");scanf(”%d,%d,%d”,&a,&b,&c);printf(”Maxnum is %d\n",maxnum(a,b,c));}int maxnum(int x,int y,int z){int max=x;if(y>max)max = y;if(z>max)max = z;return max;}演示4#include 〈stdio。

h〉int s1(int n){int j,s;s=0;for(j=1;j<=n;j++)s=s+j;return s;}int sum(int n){int i,s=0;for(i=1;i<=n;i++)s=s+s1(i);return s;}void main(){int n;printf(”n:”);scanf(”%d",&n);printf("s=%d\n”,sum(n));}演示5#include <stdio.h〉void func(int n){int i;for(i=n-1;i〉=1;i--)n=n+i;printf("n=%d\n",n);}void main(){int n;printf("Input n:");scanf("%d”,&n);func(n);printf(”n=%d\n",n);}演示6#include <stdio。

好玩的c语言代码

好玩的c语言代码

好玩的c语言代码以下是一些好玩的 C 语言代码:1. Hello World#include <stdio.h>int main() {printf("Hello World!\n");return 0;}2. 乘法表#include <stdio.h>int main() {int i, j;for(i = 1; i <= 9; i++) {for(j = 1; j <= i; j++) {printf("%d x %d = %d ",j,i,i*j);}printf("\n");}return 0;}3. 猜数字游戏#include <stdio.h>#include <stdlib.h>#include <time.h>int main() {int number, guess, count = 0;srand(time(NULL));number = rand() % 100 + 1; // 随机一个 1~100 的数printf("猜一个 1~100 的数字:\n");do {scanf("%d",&guess);count++;if(guess > number) {printf("太大了,请继续猜:\n");} else if(guess < number) {printf("太小了,请继续猜:\n");} else {printf("恭喜你,猜对了!你一共猜了 %d 次。

\n",count); }} while(guess != number);return 0;}4. 计算圆的周长和面积#include <stdio.h>#define PI 3.1415926535int main() {float r, circumference, area;printf("请输入圆的半径:\n");scanf("%f",&r);circumference = 2 * PI * r; // 周长公式area = PI * r * r; // 面积公式printf("圆的周长:%f\n",circumference);printf("圆的面积:%f\n",area);return 0;}5. 温度转换#include <stdio.h>int main() {float fahrenheit, celsius;printf("请输入华氏度:\n");scanf("%f",&fahrenheit);celsius = (fahrenheit - 32) * 5 / 9; // 华氏度转摄氏度公式 printf("摄氏度:%.2f\n",celsius);return 0;}。

c经典代码大全

c经典代码大全

#include <iostream.h> //包含iostream.h头文件main(){//声明变量,并初始化int a=010,b=10,c=0X10;//以十进制形式显示数据cout<<"DEC:";cout<<" a="<<a;cout<<" b="<<b;cout<<" c="<<c<<endl;//以八进制形式显示数据cout<<"OCT:";cout<<oct; //指定八进制输出cout<<" a="<<a;cout<<" b="<<b;cout<<" c="<<c<<endl;//以十六进制形式显示数据cout<<"HEX:";cout<<hex; //指定十六进制输出cout<<" a="<<a;cout<<" b="<<b;cout<<" c="<<c<<endl;//八、十和十六进制数混合运算并输出cout<<"a+b+c=";cout<<dec; //恢复十进制输出cout<<a+b+c<<endl;//测试八、十和十六进制输入cout<<"DEC:a="; cin>>a;cout<<"OCT:b="; cin>>b;cout<<"HEX:a="; cin>>c;cout<<"DEC:"<<dec<<endl; //指定十进制输出cout<<"a="<<a<<endl;cout<<"b="<<b<<endl;cout<<"c="<<c<<endl;}cin>>dx;cout<<"dy=";cin>>dy;cout<<dx<<"+"<<dy<<"="<<dx+dy<<endl; cout<<dx<<"-"<<dy<<"="<<dx-dy<<endl; cout<<dx<<"*"<<dy<<"="<<dx*dy<<endl;cout<<dx<<"/"<<dy<<"="<<dx/dy<<endl<< endl;//cout<<fx<<"%"<<fy<<"="<<fx%fy<<endl; Error!//测试float和double类型数据的有效位fx=10.0;fy=6.0;float fz=fx/fy;dx=10.0;dy=6.0; double dz=dx/dy;cout<<"fz=";cout<<setprecision(20)<<fx<<"/"<<fy<< "="<<fz<<endl;cout<<"dz=";cout<<setprecision(20)<<dx<<"/"<<dy<< "="<<dz<<endl<<endl;;//float型溢出float x=3.5e14;cout<<"x="<<x<<endl;cout<<"x*x="<<x*x<<endl;cout<<"x*x*x="<<x*x*x<<endl;}#include <iostream.h>main(){//x,y 为操作数,c为运算符int x,y,z;char c1;cin>>x>>c1>>y; //c1//多路选择语句选择不同表达式计算语句switch(c1) {case'+':cout<<x<<"+"<<y<<"="<<x+y<<endl; break;case'-':cout<<x<<"-"<<y<<"="<<x-y<<endl; break;case'*':cout<<x<<"*"<<y<<"="<<x*y<<endl; break;case'/':cout<<x<<"/"<<y<<"="<<x/y<<endl; break;case'%':cout<<x<<"%"<<y<<"="<<x%y<<endl; break;default :cout<<"Wrong !"<<endl; //当不符合上述情况时执行本子句}}#include<iostream.h>float x=365.5; //声明全局变量main() {int x=1,y=2;double w=x+y;{double x=1.414,y=1.732,z=3.14; cout<<"inner:x="<<x<<endl;cout<<"inner:y="<<y<<endl;cout<<"inner:z="<<z<<endl;cout<<"outer:w="<<w<<endl;cout<<"::x="<<::x<<endl; //访问重名的全局变量}cout<<"outer:x="<<x<<endl;cout<<"outer:y="<<y<<endl;cout<<"outer:w="<<w<<endl;//cout<<"inner:z="<<z<<endl;无效 cout<<"::x="<<::x<<endl; //访问重名的全局变量}#include<iostream.h>main() {//显示1,2,3 (10)for(int i=1;i<=10;i++)cout<<i<<" ";cout<<endl;//显示10,9,8 (1)for(int j=10;j>=1;j--)cout<<j<<" ";cout<<endl;//显示1,3,5 (9)for(int k=1;k<=10;k=k+2)cout<<k<<" ";cout<<endl;//显示ABC...Zfor(char c='A';c<='Z';c++)cout<<c;cout<<endl;//显示0,0.1,0.2...1.0for(float x=0;x<=1.0;x=x+0.1)cout<<x<<" ";cout<<endl;//显示0,0.1,0.2...1.0for(floatx1=0;x1<=1.0+0.1/2;x1=x1+0.1)cout<<x1<<" ";cout<<endl;//计算s=1+2+3...+100int s=0;for(int n=1;n<=100;n++)s=s+n;cout<<"s="<<s<<endl;}#include<iostream.h>main(){//计算s=1+2+3...+100int s=0,n=1;while(n<=100) {s=s+n;n++;}cout<<"s="<<s<<endl;//累加键盘输入的数据double x,sum=0.0;cout<<"x=";cin>>x;while(x!=0) {sum+=x;cout<<"x=";cin>>x;}cout<<"sum="<<sum<<endl;}#include<iostream.h>main(){//计算s=1+2+3...+100int s=0,n=0;do {n++;s+=n;}while(n<100);cout<<"s="<<s<<endl;//累加键盘输入的数据double x,sum=0.0;do {cout<<"x=";cin>>x;sum+=x;} while(x!=0);cout<<"sum="<<sum<<endl;}#include<iostream.h>main(){//计算和打印打印乘法九九表for (int i=1;i<=9;i++) {cout<<i;for (int j=1;j<=9;j++)cout<<'\t'<<i<<"*"<<j<<"="<<i*j;cout<<endl;}}#include<iostream.h>main(){int x,sum=0;//定义标号L1L1: cout<<"x=";cin>>x;if (x==-1)goto L2; //无条件转移语句,转到L2语句处elsesum+=x;goto L1; //无条件转移语句,转到L1语句处//定义标号L2L2: cout<<"sum="<<sum<<endl;}#include<iostream.h>main(){//累加键盘输入的数据double x,sum=0.0;while(1) {cout<<"x=";cin>>x;if (x<=0) break;sum+=x;}cout<<"sum="<<sum<<endl;}#include<iostream.h>main(){int i;for (i=1;i<=20;i++){if (i%3==0) //能被 3 整除的整数,返回进行下次循环continue;cout<<i<<" ";}cout<<endl;}#include<iostream.h>main(){//声明数组和变量int a[5],i,sum;double avg;//从键盘上循环为数组赋值for (i=0;i<5;i++) {cout<<"a["<<i<<"]=";cin>>a[i];}//直接显示数组元素cout<<a[0]<<a[1]<<a[2]<<a[3]<<a[4]<<e ndl;//利用for循环显示数组各元素的值 for (i=0;i<5;i++)cout<<a[i]<<" ";cout<<endl;//计算数组元素之和,并显示计算结果 sum=a[0]+a[1]+a[2]+a[3]+a[4];cout<<"sum="<<sum<<endl;//利用循环计算数组的累加和for (sum=0,i=0;i<5;i++)sum+=a[i];//显示累加和及平均值cout<<"sum="<<sum<<endl;avg=sum/5.0;cout<<"avg="<<avg<<endl;}#include<iostream.h>main(){int i,max,index,a[5];//从键盘上为数组赋值for (i=0;i<=4;i++){cout<<"a["<<i<<"]=";cin>>a[i];}// 利用循环遍历数组,找出最大值的元素及其下标max=a[0];for (i=0;i<=4;i++){if (max<a[i]) {max=a[i];index=i;}}cout<<"\nMax="<<max<<"index="<<index;}#include<iostream.h>#define size 5main(){//声明变量int i,j;float t,a[size];//从键盘上为数组赋值for (i=0;i<size;i++){cout<<"a["<<i<<"]=";cin>>a[i];}//对数组按从小到大顺序排序for (i=0;i<size-1;i++)for (j=i+1;j<size;j++)if (a[i]>a[j]){t=a[i];a[i]=a[j];a[j]=t;}//显示排序结果for (i=0;i<size;i++)cout<<a[i]<<" ";cout<<endl;//输入要查找的数据int value;int found; //找到为1,否则为0int low,high,mid;for (i=1;i<=3;i++) {cout<<"value=";cin>>value;//二分法查找数组afound=0;low=0;high=size-1;while(low<=high){mid=(high+low)/2;if (a[mid]==value){found=1;break;}if (a[mid]<value)low=mid+1;elsehigh=mid-1;}if (found)cout<<"The valu foundat:a["<<mid<<"]="<<a[mid]<<endl;elsecout<<"The "<<value<<" is not found!"<<endl;}}#include<iostream.h>main(){//声明变量int i,j;float t,a[5];//从键盘上为数组赋值for (i=0;i<=4;i++){cout<<"a["<<i<<"]=";cin>>a[i];}//对数组按从大到小顺序排序for (i=0;i<=3;i++)for (j=i+1;j<=4;j++)if (a[i]<=a[j]){t=a[i];a[i]=a[j];a[j]=t;}//显示排序结果for (i=0;i<=4;i++)cout<<a[i]<<" ";}#include<iostream.h>main(){//声明二维数组及变量int a[2][3],i,j;//从键盘上为数组a赋值for (i=0;i<2;i++)for (j=0;j<3;j++){cout<<"a["<<i<<"]["<<j<<"]=";cin>>a[i][j];}//显示数组afor (i=0;i<2;i++) {for (j=0;j<3;j++){cout<<a[i][j]<<" ";}cout<<endl;}//找出该数组的最大元素及其下标int h,l,Max=a[0][0];for (i=0;i<2;i++) {for (j=0;j<3;j++){if (Max<a[i][j]) {Max=a[i][j];h=i; l=j;}}}cout<<"Max:"<<"a["<<h<<"]["<<l<<"]="< <a[h][l]<<endl;}#include<iostream.h>main(){//声明字符数组和变量char str[6];int i;//从键盘上输入字符串cout<<"str=";cin>>str;cout<<str<<endl;//按数组和下标变量两种方式显示字符数组cout<<str<<endl;for (i=0;i<6;i++)cout<<str[i];cout<<endl;//字符串反向输出for (i=5;i>=0;i--)cout<<str[i];cout<<endl;//将字符数组变成大写字母后输出for (i=0;i<=5;i++)str[i]-=32; //小写字母转换成大写字母cout<<str<<endl; //显示字符串}#include<iostream.h>main(){//声明变量和指针变量int a,b,c,*ip;//指针变量ip指向变量aa=100;ip=&a; //使指针变量 ip 指向变量acout<<"a="<<a<<endl;cout<<"*ip="<<*ip<<endl;cout<<"ip="<<ip<<endl;//指针变量ip指向变量bip=&b; //使指针变量 ip 指向变量bb=200;cout<<"b="<<b<<endl;cout<<"*ip="<<*ip<<endl;cout<<"ip="<<ip<<endl;//指针变量ip指向变量cip=&c; //使指针变量 ip 指向变量b*ip=a+b;cout<<"c="<<c<<endl;cout<<"*ip="<<*ip<<endl;cout<<"ip="<<ip<<endl;}#include<iostream.h>main(){//声明数组、变量和指针变量int a[2][3],i,j;int* ip;//从键盘上为数组a赋值for (i=0;i<2;i++) //为数组a赋值 for (j=0;j<3;j++){cout<<"a["<<i<<"]["<<j<<"]=";cin>>a[i][j];}//利用下标变量显示数组afor (i=0;i<2;i++) {for (j=0;j<3;j++){cout<<a[i][j]<<" ";}cout<<endl;}//利用指针变量显示数组aip=&a[0][0];for (i=0;i<2;i++) {for (j=0;j<3;j++){cout<<"a["<<i<<"]["<<j<<"]=";cout<<ip<<" ";cout<<*ip<<endl;ip++;}}}#include<iostream.h>main(){//声明数组、变量和指针变量int a[]={1,2,3,4,5,6};int *ip1,*ip2;//测试指针的赋值运算ip1=a;ip2=ip1;cout<<"*ip1="<<(*ip1)<<endl;cout<<"*ip2="<<(*ip2)<<endl;//测试指针的自增自减运算和组合运算ip1++;ip2+=4;cout<<"*ip1="<<(*ip1)<<endl;cout<<"*ip2="<<(*ip2)<<endl;//测试指针变量之间的关系运算int n=ip2>ip1;cout<<"ip2>ip1="<<n<<endl;cout<<"ip2!=NULL="<<(ip2!=NULL)<<endl ; //指针变量之间的减法n=ip2-ip1;cout<<"ip2-ip1="<<n<<endl;}#include<iostream.h>main(){//声明字符型数组和指针变量char str[10];char *strip=str;//输入输出cout<<"str=";cin>>str; //用字符数组输入字符串cout<<"str="<<str<<endl;cout<<"strip="<<strip<<endl;cout<<"strip=";cin>>strip; //用字符指针变量输入字符串cout<<"str="<<str<<endl;cout<<"strip="<<strip<<endl;//利用指针变量改变其指向字符串的内容*(strip+2)='l';cout<<"str="<<str<<endl;cout<<"strip="<<strip<<endl;//动态为字符型指针变量分配内存strip=new char(100);cout<<"strip=";cin>>strip; //用字符指针变量输入字符串cout<<"str="<<str<<endl;cout<<"strip="<<strip<<endl;}#include<iostream.h>main(){// 声明用于存放运动员号码的数组int h[]={1001,1002,1003,1004};// 声明用于存放运动员成绩的数组float x[]={12.3,13.1,11.9,12.1}; //声明用于存放运动姓名的字符型指针数组char *p[]={"Wang hua","Zhang jian","Li wei","Hua ming"};//i,j,it是用做循环控制变量和临时变量int i,j,it;//ft 用做暂存变量float ft;//pt为字符型指针变量用做暂存指针变量char *pt;//用选择法对数组x进行排序,并相应调整数组h和p中的数据for (i=0;i<=3;i++)for (j=i+1;j<=3;j++)if (x[i]>=x[j]) {ft=x[i],x[i]=x[j],x[j]=ft;it=h[i],h[i]=h[j],h[j]=it;pt=p[i],p[i]=p[j],p[j]=pt;}//以下打印排序结果for (i=0;i<=3;i++)cout<<h[i]<<" ,"<<p[i]<<" ,"<<x[i]<<e ndl;}#include<iostream.h>main(){//声明指针数组char*colors[]={"Red","Blue","Yellow","Gre en"};//指向指针的指针变量char **pt;//通过指向指针的变量访问其指向的内容pt=colors;for (int i=0;i<=3;i++) {cout<<"pt="<<pt<<endl;cout<<"*pt="<<*pt<<endl;cout<<"**pt="<<**pt<<endl;pt++;}}#include<iostream.h>main(){//定义结构类型struct books{char title[20];char author[15];int pages;float price;} ;//声明结构变量struct books Zbk={"VC++ ","Zhang",295,35.5};books Wbk;//对结构变量的输出cout<<"Zbk:"<<endl;cout<<Zbk.title <<endl;cout<<Zbk.author<<endl;cout<<Zbk.pages<<endl;cout<<Zbk.price<<endl;cout<<"--------------------"<<endl;//对结构成员的运算Zbk.pages+=10;Zbk.price+=0.5;cout<<"Zbk.pages="<<Zbk.pages<<endl; cout<<"Zbk.price="<<Zbk.price<<endl;cout<<"--------------------"<<endl;//对结构变量的输入输出cout<<"Wbk.title =";cin>>Wbk.title;cout<<"Wbk.author=";cin>>Wbk.author;cout<<"Wbk.pages=";cin>>Wbk.pages;cout<<"Wbk.price=";cin>>Wbk.price;cout<<"Wbk:"<<endl;cout<<Wbk.title <<endl;cout<<Wbk.author<<endl;cout<<Wbk.pages<<endl;cout<<Wbk.price<<endl;cout<<"--------------------"<<endl;//结构变量之间的相互赋值books temp;temp=Wbk;cout<<"temp:"<<endl;cout<<temp.title<<endl;cout<<temp.author<<endl;cout<<temp.pages<<endl;cout<<temp.price<<endl;}#include<iostream.h>main(){int i;//定义结构类型struct student {int num;char name[10];float maths;float physics;float chemistry;double total;};//声明结构数组ststudent st[3];//从键盘上为结构数组输入值cout<<" num name maths physics chemistry "<<endl;for (i=0;i<3;i++){cout<<i+1<<" ";cin>>st[i].num;cin>>st[i].name;cin>>st[i].maths;cin>>st[i].physics;cin>>st[i].chemistry;}//计算每个学生的总成绩for (i=0;i<3;i++)st[i].total=st[i].maths+st[i].physics +st[i].chemistry;//输出结构数组各元素的值for (i=0;i<3;i++){cout<<"st["<<i<<"]: ";cout<<st[i].num<<'\t';cout<<st[i].name<<'\t';cout<<st[i].maths<<'\t';cout<<st[i].physics<<'\t';cout<<st[i].chemistry<<'\t';cout<<st[i].total<<endl;}}#include<iostream.h>main(){//定义结构类型struct human {char name[10];int sex;int age;};//声明结构变量和结构指针变量,并初始化struct human x={"WangPing",1,30},*p=NULL;//结构指针变量指向对象p=&x;//显示结构变量的值cout<<"="<<<<endl;cout<<"x.sex="<<x.sex<<endl;cout<<"x.age="<<x.age<<endl;//利用结构指针显示结构对象中的数据cout<<"(*p).name="<<(*p).name<<endl;cout<<"(*p).sex="<<(*p).sex<<endl;cout<<"(*p).age="<<(*p).age<<endl;cout<<"p->name="<<p->name<<endl;cout<<"p->sex="<<p->sex<<endl;cout<<"p->age="<<p->age<<endl;//通过结构指针为结构对象输入数据cout<<"name:";cin>>(*p).name;cout<<"sex:";cin>>(*p).sex;cout<<"age:";cin>>(*p).age;//显示结构变量的值cout<<"="<<<<endl;cout<<"x.sex="<<x.sex<<endl;cout<<"x.age="<<x.age<<endl;}include<iostream.h>main(){//定义结构类型struct human {char name[10];int sex; int age;};//声明结构变量和结构指针,并初始化 struct human x={"WangPing",1,30},*p=&x;//利用结构指针显示结构中的数据cout<<"(*p).name="<<(*p).name<<endl; cout<<"(*p).sex="<<(*p).sex<<endl;cout<<"(*p).age="<<(*p).age<<endl;cout<<"-------------------------"<<en dl;//利用new运算符为p分配内存p=new human;//从键盘上为p指向的结构对象赋值cout<<"p->name=";cin>>p->name;cout<<"p->sex=";cin>>p->sex;cout<<"p->age=";cin>>p->age;cout<<"-------------------------"<<en dl;//显示p所指结构对象的值cout<<"p->name="<<p->name<<endl;cout<<"p->sex="<<p->sex<<endl;cout<<"p->age="<<p->age<<endl;cout<<"-------------------------"<<en dl;//显示结构变量的值cout<<"="<<<<endl;cout<<"x.sex="<<x.sex<<endl;cout<<"x.age="<<x.age<<endl;//释放p指向的内存delete p;}#include<iostream.h>main(){//定义结构类型struct human {char name[10];int sex;int age;};//声明结构数组和结构指针变量,并初始化humanx[]={{"WeiPing",1,30},{"LiHua",1,25}, {"LiuMin",0,23}},*p=NULL;//用下标变量的输出结构数组的元素for (int i=0;i<3;i++){cout<<x[i].name<<'\t';cout<<x[i].sex<<'\t';cout<<x[i].age<<endl;}cout<<"----------------"<<endl;//用结构指针输出结构数组的元素for (p=x;p<=&x[2];p++){cout<<p->name<<'\t';cout<<p->sex<<'\t';cout<<p->age<<endl;}}#include<iostream.h>main(){//定义一个包含指针成员的结构类型 struct test {char *str;int *ip;} x;//使用结构变量x中的整型指针ipx.ip=new int; //分配1个单元*(x.ip)=100;cout<<"x.ip:"<<x.ip<<'\t'<<*(x.ip)<<e ndl;cout<<"---------------"<<endl;delete x.ip;x.ip=new int[5]; //分配5个单元 for(int i=0;i<5;i++)*(x.ip+i)=100+i;cout<<"x.ip:"<<endl;for(i=0;i<5;i++)cout<<x.ip+i<<'\t'<<(*(x.ip+i))<<endl ;delete x.ip;cout<<"---------------"<<endl;//使用结构变量x中的字符型指针str x.str=new char('A'); //分配1个单元cout<<"x.str:"<<(*x.str)<<endl;cout<<"---------------"<<endl;delete x.str;x.str=new char[5]; //分配多个单元*x.str='G';*(x.str+1)='o';*(x.str+2)='o';*(x.str+3)='d';*(x.str+4)='\0';cout<<"x.str:"<<x.str<<endl;delete x.str;cout<<"---------------"<<endl;//在声明结构变量时初始化test y={"Very Good!",NULL};cout<<"y.str:"<<y.str<<endl;cout<<"y.ip:"<<y.ip<<endl;}#include<iostream.h>main(){//定义date结构struct date{int year;int month;int day;};//定义baby结构struct baby {int num;float weight;date birthday; // date为结构类型};//声明baby结构变量并初始化baby b1={10001,10,{2002,12,25}}; //下列是baby结构变量b1的引用。

C语言代码大全

C语言代码大全

------------------------------------------------------------------------摘自宋鲁生程序设计大赛乘法口诀表#include <stdio.h>#include <conio.h>void main(void){int i,j,x,y;clrscr();printf("\n\n * * * 乘法口诀表* * * \n\n");x=9;y=5;for(i=1;i<=9;i++){gotoxy(x,y);printf("%2d ",i);x+=3;}x=7;y=6;for(i=1;i<=9;i++){gotoxy(x,y);printf("%2d ",i);y++;}x=9;y= 6;for(i=1;i<=9;i++){for(j=1;j<=9;j++){gotoxy(x,y);printf("%2d ",i*j);y++;}y-=9;x+=3;}printf("\n\n");}用一维数组统计学生成绩#include <stdio.h>void main(){char SelectKey,CreditMoney,DebitMoney;while(1){do{clrscr();puts("=========================");puts("| Please select key: |");puts("| 1. Quary |");puts("| 2. Credit |");puts("| 3. Debit |");puts("| 4. Return |");puts("=========================");SelectKey = getch();}while( SelectKey!='1' && SelectKey!='2' && SelectKey!='3' &&SelectKey!='4' );switch(SelectKey){case '1':clrscr();puts("================================");puts("| Your balance is $1000. |");puts("| Press any key to return... |");puts("================================");getch();break;case '2':do{clrscr();puts("==================================");puts("| Please select Credit money: |");puts("| 1. $50 |");puts("| 2. $100 |");puts("| 3. Return |");puts("==================================");CreditMoney = getch();}while( CreditMoney!='1' && CreditMoney!='2' && CreditMoney!='3' );switch(CreditMoney){case '1':clrscr();puts("=========================================");puts("| Your Credit money is $50,Thank you! |");puts("| Press any key to return... |");puts("=========================================");getch();break;case '2':clrscr();puts("==========================================");puts("| Your Credit money is $100,Thank you! |");puts("| Press any key to return... |");puts("==========================================");getch();break;case '3':break;}break;case '3':do{clrscr();puts("====================================");puts("| Please select Debit money: |");puts("| 1. $50 |");puts("| 2. $100 |");puts("| 3. $500 |");puts("| 4. $1000 |");puts("| 5. Return |");puts("====================================");DebitMoney = getch();}while( DebitMoney!='1' && DebitMoney!='2' && DebitMoney!='3' &&DebitMoney!='4' && DebitMoney!='5' );switch(DebitMoney){case '1':clrscr();puts("===========================================");puts("| Your Debit money is $50,Thank you! |");puts("| Press any key to return... |");puts("===========================================");getch();break;case '2':clrscr();puts("===========================================");puts("| Your Debit money is $100,Thank you! |");puts("| Press any key to return... |");puts("===========================================");getch();break;case '3':clrscr();puts("===========================================");puts("| Your Debit money is $500,Thank you! |");puts("| Press any key to return... |");puts("===========================================");getch();break;case '4':clrscr();puts("===========================================");puts("| Your Debit money is $1000,Thank you! |");puts("| Press any key to return... |");puts("===========================================");getch();break;case '5':break;}break;case '4':clrscr();puts("================================");puts("| Thank you for your using! |");puts("| Good bye! |");puts("================================");return;}}模拟ATM(自动柜员机)界面#include <stdio.h> void main(){int Password=0,Number=0,price=58,i=0;while( Password != 1234 ){if( i >= 3 )return;i++;puts("Please input Password: ");scanf("%d",&Password);}i=0;while( Number!=price ){do{puts("Please input a number between 1 and 100: ");scanf("%d",&Number);printf("Your input number is %d\n",Number);}while( !(Number>=1 && Number<=100) );if( Number >= 90 ){printf("Too Bigger! Press any key to try again!\n");}else if( Number >= 70 && Number < 90 ){printf("Bigger!\n");}else if( Number >= 1 && Number <= 30 ){printf("Too Small! Press any key to try again!\n");}else if( Number > 30 && Number <= 50 ){printf("Small! Press any key to try again!\n");}else{if( Number == price ){printf("OK! You are right! Bye Bye!\n");}else if( Number < price ){printf("Sorry,Only a little smaller! Press any key to try again!\n");}else if( Number > price ){printf(" Sorry, Only a little bigger! Press any key to try again!\n");}getch();}}用二维数组实现矩阵转置/* 用二维数组实现矩阵的转置*/#include <stdio.h>#define ROW 3#define COL 4main(){int matrixA[ROW][COL],matrixB[COL][ROW];int i,j; clrscr();printf("Enter elements of the matrixA,");printf("%d*%d:\n",ROW,COL);for( i=0; i<ROW; i++ ){for( j=0; j<COL; j++ ){scanf("%d",&matrixA[i][j]);}}for( i=0; i<ROW; i++ ){for( j=0; j<COL; j++ ){matrixB[j][i] = matrixA[i][j];}}printf("MatrixB,");printf("%d*%d:\n",COL,ROW);for( i=0; i<COL; i++ ){for( j=0; j<ROW; j++ ){printf("%8d",matrixB[i][j]);}printf("\n");}printf("\n Press Any Key to Quit... \n");getch();}求解二维数组的最大/最小元素#define MAXN 20int a[MAXN][MAXN];main(){int min, /* 存储最小值*/max; /* 存储最大值*/int row,col,n;clrscr();printf("Please input the order of the matrix:\n");/* 输入方阵的阶次*/ scanf("%d",&n);printf("Please input the elements of the matrix,\n from a[0][0] to a[%d][%d]:\n",n-1,n-1);for(row=0;row<n;row++)for(col=0;col<n;col++)scanf("%d",&a[row][col]);for(min=a[0][0],row=0;row<n;row++){/* 从每行选出最大数*/for(max=a[row][0],col=1;col<n;col++)/*从row行选出最大数*/if(max<a[row][col])max=a[row][col];if(min>max)/* 保存至row行的最小数*/min=max;}printf("The minimum of maximum number is %d\n",min);for(max=a[0][0],row=0;row<n;row++){/* 每行选出最小数*/for(min=a[row][0],col=1;col<n;col++)/* 从row行选出最小数*/ if(min>a[row][col])min=a[row][col];if(max<min)/*保存至row行的最大数*/max=min;}printf("The maximum of minimum numbers is %d\n",max);printf("\nPress any key to quit...\n");getch();}利用数组求前n个质数#define N 50main(){int primes[N];int pc,m,k; clrscr();printf("\n The first %d prime numbers are:\n",N);primes[0]=2;/*2是第一个质数*/pc =1;/*已有第一个质数*/m =3;/*被测试的数从3开始*/while(pc<N){/*调整m使它为下一个质数*/k=0;while(primes[k]*primes[k]<=m)if(m%primes[k]==0){/*m是合数*/m+=2;/*让m取下一个奇数*/k=1;/*不必用primes[0]=2去测试m,所以k从一开始*/}elsek++;/*继续用下一个质数去测试*/primes[pc++]=m;m+=2;/*除2外,其余质数均是奇数*/}/*输出primes[0]至primes[pc-1]*/for(k=0;k<pc;k++)printf("%4d",primes[k]);printf("\n\n Press any key to quit...\n ");getch();}编制万年历#include "stdio.h"long int f(int year,int month){/*f(年,月)=年-1,如月<3;否则,f(年,月)=年*/if(month<3) return year-1;else return year;} long int g(int month){/*g(月)=月+13,如月<3;否则,g(月)=月+1*/if(month<3) return month+13;else return month+1;} long int n(int year,int month,int day){/*N=1461*f(年、月)/4+153*g(月)/5+日*/return 1461L*f(year,month)/4+153L*g(month)/5+day;} int w(int year,int month,int day){/*w=(N-621049)%7(0<=w<7)*/return(int)((n(year,month,day)%7-621049L%7+7)%7);} int date[12][6][7];int day_tbl[ ][12]={{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}};main(){int sw,leap,i,j,k,wd,day;int year;/*年*/char title[]="SUN MON TUE WED THU FRI SAT";clrscr();printf("Please input the year whose calendar you want to know: ");/*输入年*/scanf("%d%*c",&year);/*输入年份值和掠过值后的回车*/sw=w(year,1,1);leap=year%4==0&&year%100||year%400==0;/*判闰年*/for(i=0;i<12;i++)for(j=0;j<6;j++)for(k=0;k<7;k++)date[i][j][k]=0;/*日期表置0*/for(i=0;i<12;i++)/*一年十二个月*/for(wd=0,day=1;day<=day_tbl[leap][i];day++){/*将第i+1月的日期填入日期表*/date[i][wd][sw]=day;sw=++sw%7;/*每星期七天,以0至6计数*/if(sw==0) wd++;/*日期表每七天一行,星期天开始新的一行*/} printf("\n|==================The Calendar of Year %d=====================|\n|",year);for(i=0;i<6;i++){/*先测算第i+1月和第i+7月的最大星期数*/for(wd=0,k=0;k<7;k++)/*日期表的第六行有日期,则wd!=0*/wd+=date[i][5][k]+date[i+6][5][k];wd=wd?6:5;printf("%2d %s %2d %s |\n|",i+1,title,i+7,title);for(j=0;j<wd;j++){printf(" ");/*输出四个空白符*//*左栏为第i+1月,右栏为第i+7月*/for(k=0;k<7;k++)if(date[i][j][k])printf("%4d",date[i][j][k]);else printf(" ");printf(" ");/*输出十个空白符*/for(k=0;k<7;k++)if(date[i+6][j][k])printf("%4d",date[i+6][j][k]);else printf(" ");printf(" |\n|");}/*scanf("%*c");/*键入回车输出下一个月的日历*/}puts("=================================================================|") ;puts("\n Press any key to quit...");getch();}对数组元素排序rest(int a[], int n){int i,low,high,t; for(i=0,low=0,high=n-1;i<=high;) {if(a[i]>0){/*a[i]与a[high]交换,随之high减1*/t=a[i];a[i]=a[high];a[high]=t;high--;}else if(a[i]==0)i++; /* 掠过该元素*/else{/*a[i]与a[low]交换,随之low增1, i增1*/t=a[i];a[i]=a[low];a[low]=t;low++;i++;}}}int s[]={8,4,0,-1,6,0,-5};main(){int i;clrscr();printf("\n The arry before rest is:\n");for(i=0;i<sizeof(s)/sizeof(s[0]);i++)printf("%4d",s[i]);rest(s,sizeof(s)/sizeof(s[0]));printf("\n The arry after rest is:\n");for(i=0;i<sizeof(s)/sizeof(s[0]);i++)printf("%4d",s[i]);printf("\n Press any key to quit...\n");getch();}任意进制数的转换/* 函数trans将无符号整数n翻译成d(2<=d<=16)进制表示的字符串s */ #define M sizeof(unsigned int)*8int trans(unsigned n, int d, char s[]){static char digits[] ="0123456789ABCDEF"; /* 十六进制数字的字符*/char buf[M+1];int j, i = M;if(d<2||d>16){s[0]='\0'; /* 不合理的进制,置s为空字符串*/return 0; /* 不合理的进制,函数返回0 */}buf[i]='\0';do{buf[--i]=digits[n%d]; /*译出最低位,对应字符存入对应工作数组中*/n/=d;}while(n);/* 将译出在工作数组中的字符串复制到s */for(j=0;(s[j]=buf[i])!='\0';j++,i++);/* 其中控制条件可简写成s[j]=buf[i] */return j;}/* 主函数用于测试函数trans() */main(){unsigned int num = 253;int scale[]={2,3,10,16,1};char str[33];int i;clrscr();for(i=0;i<sizeof(scale)/sizeof(scale[0]);i++){if(trans(num,scale[i],str))printf("%5d = %s(%d)\n",num,str,scale[i]);elseprintf("%5d => (%d) Error! \n",num,scale[i]);}printf("\n Press any key to quit...\n");getch();}判断回文数/* 函数circle用于判断正整数n的d进制数表示形式是否是回文数*/ int circle(int n, int d){int s=0,m=n;while(m){s=s*d+m%d;m/=d;}return s==n;}/* main函数用于测试circle函数*/int num[]={232,27,851};int scale[]={2,10,16};main(){int i,j;clrscr();for(i=0;i<sizeof(num)/sizeof(num[0]);i++)for(j=0;j<sizeof(scale)/sizeof(scale[0]);j++)if(circle(num[i],scale[j]))printf("%d -> (%d) is a Circle Number!\n",num[i],scale[j]);elseprintf("%d -> (%d) is not a Circle Number!\n",num[i],scale[j]);printf("\n Press any key to quit...\n");getch();}求解钢材切割的最佳订单#include <stdio.h>#define N 20#define DELTA 2int bestlen;int bestsele[N];int sele[N];int n;int orderlen[N];int total;main(){int i;clrscr();printf("\n Please enter total length of the steel:\n");/* 输入钢材总长*/scanf("%d",&total);printf("\n Please enter number of order:\n"); /* 输入定单数*/ scanf("%d",&n);printf("\n Please enter the orders:\n"); /* 输入各定单*/for(i=0;i<n;i++)scanf("%d",&orderlen[i]);bestlen=0; /*最佳解用料的初值*/for(i=0;i<n;i++)sele[i]=bestsele[i]=0; /*置当前选择和最佳选择初值*/try(); /* 调用函数求解*/for(i=0;i<n;i++) /* 输出结果*/if(bestsele[i])printf("order %d length = %d\n",i+1,orderlen[i]);printf("\n Press any key to quit...");getch();}try(){int i,len;for(len=i=0;i<n;i++) /* 求当前选中的用料量*/if(sele[i])len+=orderlen[i]+DELTA;if(len-DELTA<=total) /* 注意最后一段可能不需要切割*/{if(bestlen < len){/* 找到一个更好的解*/bestlen = len;for(i=0;i<n;i++)bestsele[i]=sele[i];}for(i=0;i<n;i++) /* 对所有未选定单逐一作选中尝试循环*/if(!sele[i]){sele[i]=1; /* 做选中尝试*/try();sele[i]=0;}}}指向数组的指针main(){int x,y,z; /* 定义三个int型变量*/int *xp = &x, /* 定义指针变量xp,并赋值为x的地址,使xp指向x */ *yp = &y, /* 定义指针变量yp,并赋值为y的地址,使yp指向y */*zp = &z; /* 定义指针变量zp,并赋值为z的地址,使zp指向z */int t;printf("\nPlease input x,y,z:\n");scanf("%d%d%d",xp,yp,zp); /* 通过变量的指针,为变量输入值*/ if(*xp>*yp) /* 通过指向变量的指针引用变量的值*/{t=*xp; /* 通过指向变量的指针引用变量的值*/*xp=*yp;/* 通过指向变量x的指针xp,引用变量x的值*/*yp=t; /* 通过指向变量y的指针yp,引用变量y的值*/}if(*xp>*zp) /* 通过指向变量的指针,引用变量的值*/{t=*xp; /* 通过指向变量x的指针xp,引用变量x的值*/*xp=*zp;/* 通过指向变量x的指针xp,引用变量x的值*/*zp=t; /* 通过指向变量z的指针zp,引用变量z的值*/}if(*yp>*zp) /* 通过指向变量的指针,引用变量的值*/{t=*yp; /* 通过指向变量的指针,引用变量的值*/*yp=*zp;/* 通过指向变量y的指针yp,引用变量y的值*/*zp=t;/* 通过指向变量z的指针zp,引用变量z的值*/}printf("x = %d\ty = %d\tz = %d\n",x,y,z);printf("\nPress any key to quit...\n");getch();}阿拉伯数字转换为罗马数字#include <stdio.h>#define ROWS 4#define COLS 4int nums[ROWS][COLS]={{1000,1000,1000,1000},{900,500,400,100},{90,50,40,10},{9,5,4,1}};char *roms[ROWS][COLS]={{"m","m","m","m"}, {"cm","d","cd","c"},{"xc","l","xl","x"},{"ix","v","iv","i"}}; main(int argc,char *argv[ ]){int low,high;char roman[25]; if(argc<2){ printf("Usage:roman decimal_number\n");/*运行程序需带整数参数*/}high=low=atoi(argv[1]);/*将第一个参数转换成整数*/checknum(low);if(argc>2){/*带两个参数*/high=atoi(argv[2]);checknum(high);if(low>high){low=high;high=atoi(argv[1]);}}elselow=1;for(;low<=high;low++){to_roman(low,roman);printf("%d\t%s\n",low,roman);}} checknum(int val)/*检查参数合理性*/{if(val<1||val>9999){printf("The number must be in range 1..9999.\n");exit(0);}}to_roman(int decimal,char roman[ ])/*将整数转换成罗马数字表示*/ {int power,index;roman[0]='\0';for(power=0;power<ROWS;power++)for(index=0;index<COLS;index++)while(decimal>=nums[power][index]){strcat(roman,roms[power][index]);decimal-=nums[power][index];}}通讯录的输入输出#include <stdio.h>#define ZIPLEN 10#define PHONLEN 15/*struct addr类型定义*/ struct addr{char *name;/*姓名*/char *address;/*地址*/char zip[ZIPLEN];/*邮政编码*/char phone[PHONLEN];/*电话号码*/}; main()/*本主函数示意上述输入输出函数的用法*/{struct addr p[100];int i,j;clrscr();for(i=0;readaddr(p+i);i++);for(j=0;j<i;j++) writeaddr(p+j);puts("\n Press any key to quit...");getch();} /* 函数readaddr用于输入一个通信录函数*/int readaddr(struct addr *dpt){int len;char buf[120];/*输入字符串的缓冲区*/ printf("\nPlease input theName:\n");/*输入姓名*/if(scanf("%s",buf)==1){len=strlen(buf);dpt->name=(char *)malloc(len+1);/*申请存贮姓名的空间*/ strcpy(dpt->name,buf);}else return 0;/*Ctrl+Z结束输入*/printf("Please input the Address:\n");/*输入地址*/if(scanf("%s",buf)==1){len=strlen(buf);dpt->address=(char *)malloc(len+1);/*申请存贮地址的空间*/ strcpy(dpt->address,buf);}else{/*Ctrl+Z结束输入*/free(dpt->name);/*释放存贮姓名的空间*/return 0;}printf("Please input the Zip code:\n");/*输入邮编*/if(scanf("%s",buf)==1)strncpy(dpt->zip,buf,ZIPLEN-1);else{free(dpt->name);/*释放存贮姓名的空间*/free(dpt->address);/*释放存贮地址的空间*/return 0;/*Ctrl+Z结束输入*/}printf("Please input the Phone number:\n");/*输入电话号码*/ if(scanf("%s",buf)==1)strncpy(dpt->phone,buf,PHONLEN-1);else{free(dpt->name);free(dpt->address);return 0;/*Ctrl+Z结束输入*/}return 1;} /* 函数writeaddr用于输出通讯录*/int writeaddr(struct addr*dpt){printf("Name : %s\n", dpt->name);/*输出姓名*/printf("Address : %s\n", dpt->address);/*输出地址*/printf("Zip : %s\n", dpt->zip);/*输出邮编*/printf("Phone : %s\n\n", dpt->phone);/*输出电话号码*/}扑克牌的结构表示enum suits{CLUBS,DIAMONDS,HEARTS,SPADES}; struct card{enum suits suit;char value[3];};struct card deck[52];char cardval[][3]= {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};char suitsname[][9]={"CLUBS","DIAMONDS","HEARTS","SPADES"}; main() {int i,j;enum suits s;clrscr();for(i=0;i<=12;i++)for(s=CLUBS;s<=SPADES;s++){j=i*4+s;deck[j].suit=s;strcpy(deck[j].value,cardval[i]);}for(j=0;j<52;j++)printf("(%s%3s)%c",suitsname[deck[j].suit],deck[j].value,j%4==3?'\n':'\t');puts("\nPress any key to quit...");getch();}用“结构”统计学生成绩#include <stdio.h>#define N 200#define SCORES 5#define NUMLEN 10struct std_type{char no[NUMLEN];/*学号*/char *name;/*名字符串指针*/int scores[SCORES];/*五门功课的成绩*/};struct std_type students[N];int order[N];int total[N]; /*[函数]输入一个学生信息函数*/int readastu(struct std_type *spt){int len,j;char buf[120];/*输入字符串的缓冲区*/ printf("\nNumber : ");/*输入学号*/if(scanf("%s",buf)==1)strncpy(spt->no,buf,NUMLEN-1);elsereturn 0;/*Ctrl+Z结束输入*/printf("Name : ");/*输入姓名*/if(scanf("%s",buf)==1){len=strlen(buf);spt->name=(char *)malloc(len+1);/*申请存贮姓名的空间*/ strcpy(spt->name,buf);}else return 0;/*Ctrl+Z结束输入*/printf("Scores : ");/*输入成绩*/for(j=0;j<SCORES;j++)if(scanf("%d",spt->scores+j)!=1)break;if(j==0)/*一个成绩也未输入*/{free(spt->name);/*释放存贮姓名的空间*/return 0;}for(;j<SCORES;j++)/*少数未输入的成绩用0分代之*/ spt->scores[j]=0;return 1;} /*[函数]输出一个学生信息的函数*/int writeastu(struct std_type *spt){int i; printf("Number : %s\n",spt->no);/*输出学号*/printf("Name : %s\n",spt->name);/*输出姓名*/printf("Scores : ");/*输出成绩*/for(i=0;i<SCORES;i++)printf("%4d",spt->scores[i]);printf("\n\n");} main(){int n,i,j,t; clrscr();for(n=0;readastu(students+n);n++);/*采用冒泡法对学生信息数组排序*/for(i=0;i<n;i++){order[i]=i;/*预置第i个输入的学生*/for(t=0,j=0;j<SCORES;j++)/*求第i个学生的总分*/t+=students[i].scores[j];total[i]=t;}/*冒泡排序*/for(i=0;i<n-1;i++)/*共扫视n-1遍*/for(j=0;j<n-1-i;j++)if(total[order[j]]<total[order[j+1]]){/*交换名次*/t=order[j];order[j]=order[j+1];order[j+1]=t;}for(j=0;j<n;j++)/*输出*/writeastu(students+order[j]);printf("\n Press any key to quit...\n");getch();}报数游戏#include <stdio.h>struct ele{int no;struct ele *link;}main(){int n,m,i;struct ele *h,*u,*p;clrscr();printf("Please input n&m:\n");scanf("%d%d",&n,&m);/*输入n和m*/h=u=(struct ele *)malloc(sizeof(struct ele));/*形成首表元*/ h->no=1;for(i=2;i<=n;i++)/*形成其余的n-1个表元*/{u->link=(struct ele *)malloc(sizeof(struct ele));u=u->link;u->no=i;/*第i个表元置编号i*/}u->link=h;/*末表元后继首表元,形成环*/puts("\nThe numbers of who will quit the cycle in turn are:"); while(n){for(i=1;i<m;i++)/*掠过m-1个表元*/u=u->link;p=u->link;/*p指向第m个表元*/u->link=p->link;/*第m个表元从环中脱钩*/printf("%4d",p->no);free(p);/*释放第m个表元占用的空间*/n--;}printf("\n\n Press any key to quit...\n");getch();}学生成绩管理程序/*学生成绩管理程序编制一个统计学生考试分数的管理程序。

C语言通讯录源代码IV可运行

C语言通讯录源代码IV可运行

#include <stdio.h>#include <stdio.h>#include <string.h>#define maxlen 100struct persons{char name[10];char addr[20];char phnum[10];}persons[maxlen];;;;typedef struct lnode{char name[10];char addr[20];char phnum[10];struct lnode *next;}listnode,*linklist;;;;linklist head=NULL,r=NULL;listnode *s,*p0,*p1,*p2,*p3,*p4,*p5,*p6,*p7; int i;char name1[10],ch;char str1[20];FILE *fp; ;void creat(){ int j;long k;fp=fopen("people.txt","r+t");if(fp!=NULL){for(i=1;i<maxlen;i++){ j=fgetc(fp);if(j==EOF)return;k=i-1;fseek(fp,k*sizeof(struct persons),0);fread(&persons[i],sizeof(struct persons),1,fp); s=(linklist)malloc(sizeof(listnode));strcpy(s->name,persons[i].name);strcpy(s->addr,persons[i].addr);strcpy(s->phnum,persons[i].phnum);if(head==NULL)head=s;elser->next=s;r=s;}}else{ fp=fopen("people.txt","w"); i=1;}};;;void List(){ p1=head;while(p1!=NULL){ printf("\n\n\tname:%s\n",p1->name);printf("\n\n\taddr:%s",p1->addr);printf("\n\n\tphnum:%s",p1->phnum);p1=p1->next;}};;;void Delete(){ printf("\n\n\tplease input the name:");gets(name1); p4=head;if(strcmp(p4->name,name1)==0){ p4=p4->next;head=p4;}else{ while(strcmp(p4->next->name,name1)!=0) p4=p4->next;p5=p4->next;p4->next=p5->next;free(p5);}};;;void Find(){ printf("\n\n\tplease input the name:");p0=head;gets(name1);while(strcmp(name1,p0->name)!=0&&p0!=NULL) p0=p0->next;if(p0==NULL)printf("\n\n\tIt is not exit in the addr-book!");else{ printf("\n\n\tname:%s\n",p0->name);printf("\n\n\taddr:%s",p0->addr);printf("\n\n\tphnum:%s",p0->phnum);}};;;void Input(){ s=(linklist)malloc(sizeof(listnode));printf("\n\n\tplease input the sb's meg:");printf("\n\n\t\tname:");scanf("%s",s->name);printf("\n\n\t\tAddr:");scanf("%s",s->addr);printf("\n\n\t\tphnum:");scanf("%s",s->phnum);if(head==NULL)head=s;elser->next=s;r=s;};;;void Alter(){int j;printf("\n\n\tPlease input the name:");gets(name1);p3=head;while(strcmp(name1,p3->name)!=0&&p3!=NULL)p3=p3->next;if(p3==NULL)printf("\n\n\tIt is not exit in the addr-book!");else{ printf("\n\n\tplease input the new meg!");printf("\n\n\t\tname:");scanf("%s",name1);strcpy(p3->name,name1);printf("\n\n\t\tAddr:");scanf("%s",name1);strcpy(p3->addr,name1);printf("\n\n\t\tphnum:");scanf("%s",name1);strcpy(p3->phnum,name1);}};;;void Save(){ int j;fp=fopen("people.txt","w");for(p2=head,j=0;p2!=NULL;j++,p2=p2->next){ strcpy(persons[j].name,p2->name);strcpy(persons[j].addr,p2->addr);strcpy(persons[j].phnum,p2->phnum);fwrite(&persons[j],sizeof(struct persons),1,fp);}};;;void main(){ creat();do{printf("\n\t***********************************************\n");printf("\n\n\tWELCOME TO USE XIESHENGQING's Address book\n");printf("\n\t**********************************************\n"); printf("\n\n\t\tPlease make a choice below:");printf("\n\t\t1.List all the meg");printf("\n\t\t2.Delete a piece of meg");printf("\n\t\t3.Find a piece of meg");printf("\n\t\t4.Add a piece of meg");printf("\n\t\t5.Alter a piece of meg");printf("\n\t\t6.Save and Quit");printf("\n\t\t7.Create an address book");printf("\n\n\n");printf("\tInput Your Choice:");ch=getche();switch(ch){ case '1': List(); break;case '2': Delete();break;case '3': Find();break;case '4': Input();break;case '5': Alter();break;case '7': Save();break;case '6': creat();fclose(fp);exit(0);break;default:printf("\n\t********************************************\n");printf("\n\t The num should 1-4!!! \n");printf("\n\t********************************************\n");break;}}while(1);}。

C语言初学者经典代码(5条)

C语言初学者经典代码(5条)

C语言初学者经典代码(5条)1.打印“Hello, world!”#include <stdio.h>int main() {printf("Hello, world!");return 0;}这段代码使用了C标准库中的printf()函数,它能够在控制台中打印文本信息。

在main()函数中调用该函数,并传入字符串“Hello, world!”作为参数。

程序运行后,在控制台中就能看到输出的文本信息。

2.计算两个数的和并输出结果:#include <stdio.h>int main() {int a = 5, b = 2;int sum = a + b;printf("The sum of %d and %d is %d", a, b, sum);return 0;}这段代码定义了两个整型变量a和b,并将它们的值分别设置为5和2。

然后将它们相加得到和并赋值给整型变量sum。

最后通过printf()函数输出结果文本信息。

其中%d是占位符,表示输出一个整型变量的值。

3.判断一个数是否为偶数:#include <stdio.h>int main() {int num = 4;if (num % 2 == 0) {printf("%d is an even number.", num);} else {printf("%d is an odd number.", num);}return 0;}这段代码定义了一个整型变量num,并将它的初始值设置为4。

然后使用if语句判断num是否为偶数。

如果是,就输出相应的文本信息;如果不是,也输出相应的文本信息。

在if语句中,%d同样表示输出一个整型变量的值。

%s表示输出一个字符串的值。

4.输出乘法表:#include <stdio.h>int main() {for (int i = 1; i <= 9; ++i) {for (int j = 1; j <= i; ++j) {printf("%d*%d=%d ", j, i, i*j);}printf("\n");}return 0;}这段代码使用了两个嵌套的for循环,分别控制行和列的输出。

经典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语言编程23例

经典C语言编程23例

经典C语言编程23例【程序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;elsebonus=bonus10+(i-1000000)*0.01;printf("bonus=%d",bonus);}==============================================================【程序3】题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?1.程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上168后再开方,如果开方后的结果满足如下条件,即是结果。

c语言代码小游源代码

c语言代码小游源代码

c语言代码小游源代码C语言代码小游 - 用代码玩转游戏世界在计算机编程领域中,C语言是一门广泛应用于系统开发和游戏开发的编程语言。

它的特点是简洁高效,同时也因为其强大的功能而受到广大开发者的喜爱。

本文将以C语言代码小游为主题,介绍一些有趣的小游戏,并通过代码实例展示它们的实现过程。

1. 猜数字游戏猜数字游戏是一款简单而又经典的小游戏。

在游戏开始时,程序会生成一个随机数,玩家需要通过输入来猜测这个数是多少。

程序会根据玩家的猜测给出相应的提示,直到玩家猜中为止。

以下是一个简单的猜数字游戏的C语言代码实现:```c#include <stdio.h>#include <stdlib.h>#include <time.h>int main() {int number, guess, count = 0;srand(time(0));number = rand() % 100 + 1;printf("猜数字游戏开始!\n");do {printf("请输入一个数:");scanf("%d", &guess);count++;if (guess > number) {printf("太大了!\n");} else if (guess < number) {printf("太小了!\n");} else {printf("恭喜你,猜对了!你用了%d次猜中了答案%d。

\n", count, number);}} while (guess != number);return 0;}```2. 井字棋游戏井字棋游戏是一款经典的二人对战游戏。

在游戏开始时,程序会绘制一个3x3的棋盘,玩家轮流在棋盘上放置自己的棋子,先连成一条线的玩家获胜。

以下是一个简单的井字棋游戏的C语言代码实现:```c#include <stdio.h>char board[3][3] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};void drawBoard() {printf(" %c | %c | %c \n", board[0][0], board[0][1], board[0][2]);printf("---+---+---\n");printf(" %c | %c | %c \n", board[1][0], board[1][1], board[1][2]);printf("---+---+---\n");printf(" %c | %c | %c \n", board[2][0], board[2][1], board[2][2]);}int checkWin() {for (int i = 0; i < 3; i++) {if (board[i][0] == board[i][1] && board[i][1] ==board[i][2] && board[i][0] != ' ') {return 1;}if (board[0][i] == board[1][i] && board[1][i] == board[2][i] && board[0][i] != ' ') {return 1;}}if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[0][0] != ' ') {return 1;}if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[0][2] != ' ') {return 1;}return 0;}int main() {int row, col, player = 1;printf("井字棋游戏开始!\n");do {drawBoard();if (player % 2 == 1) {printf("轮到玩家1(X)下棋:");} else {printf("轮到玩家2(O)下棋:");}scanf("%d %d", &row, &col);if (board[row][col] == ' ') {if (player % 2 == 1) {board[row][col] = 'X';} else {board[row][col] = 'O';}player++;} else {printf("该位置已经被占用,请重新选择!\n"); }} while (!checkWin() && player <= 9);drawBoard();if (checkWin()) {if (player % 2 == 1) {printf("恭喜玩家2(O)获胜!\n");} else {printf("恭喜玩家1(X)获胜!\n");}} else {printf("游戏结束,平局!\n");}return 0;}```通过以上两个简单的C语言代码小游戏,我们可以看到C语言的强大之处。

经典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语言拼图小游戏源代码
int i;
for(i=0;i<=8;i++){
if (num[i]==0)
return i;
}
}
change(int a,int b){ /*交换num[a],num[b]*/
int temp;
int i,j;
for(i=0;i<=2;i++){
gotoxy(24,8+i*3);
for(j=0;j<=2;j++){
if (num[j+i*3]!=0)
printf("\332\304\304\304\304\267");
break;
case 80: /*按上*/
if(pos>=3) change(pos,pos-3);
break;
case 77: /*按左*/
temp=num[a];
num[a]=num[b];
num[b]=temp;
}

这涉及有没有解的问题
不管怎么样,九格游戏最后的结果只有两个:
123 | 123
456 | 456
78 | 87 (证明从略)
而要两两交换而始终有解的话,(从原序列开始)必须:相邻交换的次数为偶
但我们用一种更有效的方法:
每一个总与其下第二个交换.
第7,8个与0,1交换,只要交换次数多,仍可获得相同的效果.
gotoxy(26,14);
printf("\324\315\315\315\315\315\315\315\315\315\315\315\315\274\n");

C语言经典源程序100例

C语言经典源程序100例

C语言经典源程序100例1. Hello, World!这是C语言中最基本的程序,用于显示"Hello, World!"。

```c#include <stdio.h>int main() {printf("Hello, World!\n");return 0;}```2. 计算两数之和这个程序用于计算两个整数的和,并将结果输出。

```c#include <stdio.h>int main() {int num1, num2, sum;printf("请输入两个整数:");scanf("%d %d", &num1, &num2);sum = num1 + num2;printf("两数之和为:%d\n", sum);return 0;}```3. 判断奇偶数这个程序用于判断一个整数是奇数还是偶数。

```c#include <stdio.h>int main() {int num;printf("请输入一个整数:");scanf("%d", &num);if (num % 2 == 0) {printf("该数是偶数。

\n");} else {printf("该数是奇数。

\n");}}```4. 求输入数字的平均值这个程序用于求输入数字的平均值。

```c#include <stdio.h>int main() {int count, i;double num, sum = 0.0, average;printf("请输入数字的个数:");scanf("%d", &count);printf("请输入这 %d 个数字:\n", count); for (i = 0; i < count; i++) {scanf("%lf", &num);sum += num;}average = sum / count;printf("平均值为:%lf\n", average);}```5. 判断闰年这个程序用于判断一个年份是否为闰年。

C#100例经典源代码

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;elsebonus=bonus10+(i-1000000)*0.01;printf("bonus=%d",bonus);}==============================================================【程序3】题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?1.程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上268后再开方,如果开方后的结果满足如下条件,即是结果。

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语言游戏源代码

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语言编程源代码31

C语言编程源代码31

1.程序分析:2.程序源代码:#include"string.h"#include"stdio.h"main(){char str1[20],str2[20],*p1,*p2;int sum=0;printf("please input two strings\n"); scanf("%s%s",str1,str2);p1=str1;p2=str2;while(*p1!='\0'){if(*p1==*p2){while(*p1==*p2&&*p2!='\0'){p1++;p2++;}}elsep1++;if(*p2=='\0')sum++;p2=str2;}printf("%d",sum);getch();}===================================== =========================【程序97】题目:从键盘输入一些字符,逐个把它们送到磁盘上去,直到输入一个#为止。

1.程序分析:2.程序源代码:#include"stdio.h"main(){FILE*fp;char ch,filename[10];scanf("%s",filename);if((fp=fopen(filename,"w"))==NULL) {printf("cannot open file\n");exit(0);}ch=getchar();ch=getchar();while(ch!='#'){fputc(ch,fp);putchar(ch);ch=getchar();}fclose(fp);}===================================== =========================【程序98】题目:从键盘输入一个字符串,将小写字母全部转换成大写字母,然后输出到一个磁盘文件“test”中保存。

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

相关文档
最新文档