C/C++ 编程常见代码

这篇博客详细介绍了C/C++编程的基础练习,涵盖了加法器、累加器、阶乘计算、冒泡排序、字符串处理、一元二次方程解法等经典问题,结合谭浩强C语言教材的例题,适合初学者巩固编程基础。
摘要由CSDN通过智能技术生成

1.加法器、累加器:(多实现 1+2+…+n 以及阶乘 n! 的功能)

一、求累加
int main(){
   
		
		int i =0;
		int res = 0;
		int n;
		cin >> n;
		
		while(i<=n){
   	
			res += i;
			i++;	
		}
		//其中 res为最终结果 n表示加到多少 i表示每次加的数 
		
		cout << res <<endl;
		return 0;
	}
二、求某数阶乘

int main()
{
   
	int i = 1;
	int sum = 1; 
	int n;
	cin >> n;
	
	while(i <=n){
   
		
		sum *=i;    // 1. sum = 1*1 =1
		i++;		// 2. i =2;
					// 3. sum = 1*2=2 以此类推 
	}
	
	cout << "其阶乘为:" << sum <<endl;
	
  return 0;
}

2. 求两个数或数组中的最大值与最小值

int main(){

	int a, b,max;
	cout << "请连续输入待比较的两个数,空格隔开:" <<endl;
	cin >> a >> b;
	max = (a>b) ? a : b; //使用双目运算符取得最大值
	cout << max <<endl;
		
	int arr[5] = {
   5, 2, 13, 23, 66};
	int arr_max, arr_min;
	arr_max = arr[0]; //默认第一位元素为最值 
	arr_min = arr[0];
	
	for(int i =0; i <5; i++)
	{
   
			if(arr_max < arr[i])
			{
   
				arr_max = arr[i];		
			}else if(arr_min > arr[i]){
     //往后遍历,若还有更大的则更新最值 否则最小值更新
				arr_min = arr[i]; 
		}
	}
	cout << arr_min << " " <<arr_max <<endl;
	
	return 0;
} 

3.冒泡排序,必须会默写

#define LEN 5
int main(){
   
	int a[LEN] = {
   2, 13, 5, 55, 20};
	for(int i=0; i<LEN; i++)
	{
   
		for(int j=0; j<LEN-i-1; j++){
   
		 //注意两个for循环的初始条件与终止条件 都从0k开始
			if(a[j] > a[j+1]){
   
				int  t = a[j];
				a[j] = a[j+1];
				a[j+1] = t;
			}
		}
	}
	return 0;
}

4.简单的switch-case多分支条件语句

int main(){
   
	int score;
	cout << "请在0~100范围内输入你的分数(满分100分):" <<endl;
	cout << " 60分以下不及格 60~70及格 70~80良好 80以上优秀  " <<endl;
	cin >> score;
	
	switch(score/10){
   
		case 6 :
			cout<<"及格"<<endl; 
			break;
		case 7 :
			cout<<"良好"<<endl; 
			break;
		case 8 :
			cout<<"优秀"<<endl; 
			break;
		case 9 :
			cout<<"优秀"<<endl; 
			break;
		case 10 :
			cout<<"满分"<<endl; 
			break;
		default:
			cout<<"不及格"<<endl; 
			break;
	}
	return 0;
} 

5.求两个数的最大公约数、最小公倍数(不可含0) (谭浩强C语言例题)

int main(){
   
	int x, y, r;
	cout << "请输入待运算的两个数, 中间用空格隔开" <<endl; 
	cin >> x >> y;
	int mul = x * y;
	
	if(x < y) //将比如 8,2 与 2,8 的操作统一输出
	{
   
		r = x;
		x = y;
		y = r;  
	 } 
	 
	r = x % y;  //直接判断两数是否可以整除 
	
	while(r){
   
		x = y;
		y = r;
		r = x % y; 
		//辗转相除法
		//直到余数不为0 
		//将余数作为除数 较小者作为被除数 继续求余判断
	}
	cout << "最大公约数:" << y << ",最小公倍数:" << mul / y <<endl;	 
	
	return 0;
} 

6.斐波那契数列递归

int fib(int n){
   
	if(n == 1){
   

		return 1;
	}else if(n == 2){
   

		return 1;
	}else{
   

		return fib(n-2) + fib(n-1);
	}
}

1, 1, 2, 3, 5…以此类推

7.判断某数是否是素数 (谭浩强C语言例题)


int main()
{
   
  int n, i;
  bool isPrime = true;
 
  cout << "输入一个正整数: ";
  cin >> n;
 
  for(i = 2; i <= n / 2; ++i)
  {
   
      if(n % i == 0) //只能被1 和 自身整除的数 才能是素数 
      {
   
          isPrime = false;
          break;
      }
  }
  if (isPrime)
      cout << "是素数";
  else
      cout << "不是素数";
  return 0;
}

8.大写字母转化为小写字母

 
int main()
{
   
	
	char a;
	a = getchar();
	printf("%c", a+32); // C或C语言中字符数据均以ASCII值存储 大写字母与小写字母相差32
	
  return 0;
}

9.统计输入字符中单词的个数 (谭浩强C语言例题)

#include<stdio.h>
#include<iostream>
#define N 30

using namespace std;
int main()
{
   
	char b[N];
	int word,count=0,i; //word代表是否是单词 1代表是 0代表不是单词 而是空格 
						//count 代表单词个数	 
	puts("输入的字符长度要小于等于N"); // 例如->  hello world 
	gets(b);
	
	for(i=0;b[i]!='\0';i++)
		if(b[i]==' ')  //若为空格则不是单词 
		   word=0; 
		else if(word==0) //若是空格则遇到的新单词使得count加一 
		{
   
			count++;
			word=1;
		}
	printf("%d",count);
	return 0;
}

10.求Sn=a+aa+aaa+…+aa…aaa(有n个a)之值,其中a是一个数字,为2。 例如,n=5时=2+22+222+2222+22222,n由键盘输入。 (谭浩强C语言例题)

#include<iostream>
using namespace std;


int main()
{
   
    
    int n,i,Sn =0,a=0;
    cin >> n;  //n代表循环次数
    
    for(i=0; i<n; i++){
   
    	a = a*10 +2;   // 0, 2, 22, 222 
    	Sn += a;		/0+2+22 
	}
	
	cout << Sn;
	return 0;
}

11.求Sn=1!+2!+3!+4!+5!+…+n!之值,其中n是一个数字(n不超过20) 注意使用int类型 当n为19时会溢出 (谭浩强C语言例题)

#include<iostream>
using namespace std;

long long fac(long long n){
   
	long long i = 1;
	long long sum = 1; 
	while(i <=n){
   
		sum *=i;    // 1. sum = 1*1 =1
		i++;		// 2. i =2;
					// 3. sum = 1*2=2 以此类推 
	}
	return sum;
}

int main()
{
   
	long long n,sum=0,a = 0;
	cin >> n;
	
	for(long long i=n; i>0; i--){
   
		sum += fac(i); 
	}
	cout << sum << endl;
	return 0;
}

12.求一个3×3矩阵对角线元素之和。 (谭浩强C语言例题)

在这里插入图片描述

#include<iostream>
using namespace std;

int main(){
   
	
	int a[3][3];
	for(int i=0; i<3; i++)
	{
   
		for(int j=0;j<3; j++){
   
			cin >> a[i][j];
		}
	}  //两个for循环输入二维数组
	
	int sum1 = 0;
	int sum2 = 0;
	for(int i=0; i<3;i++)
	{
   
	  sum1 += a[i][i]; //主对角线求和
	}
	
	int j=2;
	for(int i=0;i<3;i++)
	 {
    
	 	
	 	sum2 += a[i][j]; 
	 	//副对角线求和 注意变量j定义在循环外 不然值不会发生改变
		//j分别是2 1 0 
		 j--;
	 	
	 }
	cout << sum1 << " " << sum2;
	return 0;
} 

13.用选择法对10个整数从小到大排序。 (谭浩强C语言例题)

在这里插入图片描述

#include<iostream>
#define LEN 10
using namespace std;

void swap(int *a,int *b)  //交换两个数 引用& 或者指针* 均可
{
   
    int temp = *a;
    *a = *b;
    *b = temp;
}

void selection_sort(int arr[], int len)
{
   
    int i,j;

        for (i = 0 ; i < len - 1 ; i++)
    {
   
                int min = i; //默认选择最小值 
                for (j = i + 1; j < len; j++)    
                        if (arr[j] < arr[min])    
                                min = j;    		
                swap(&arr[min], &arr[i]);    
                //调用数组元素时 元素前加引用 
        }
}

int main(){
   
	
	int a[LEN]
#include #include #include #include #include #include #define G 9.8 /*重力加速度*/ #define PI 3.141593 /*圆周率*/ #define L1 60 /*小屋运动的范围*/ #define T1 100 #define R1 200 #define B1 450 #define AMD1 5 /*修订数*/ #define AMD2 1.78 /*修订数*/ /*鼠标信息宏定义*/ #define WAITING 0xff00 #define LEFTPRESS 0xff01 #define LEFTCLICK 0xff10 #define LEFTDRAG 0xff19 #define RIGHTPRESS 0xff02 #define RIGHTCLICK 0xff20 #define RIGHTDRAG 0xff2a #define MIDDLEPRESS 0xff04 #define MIDDLECLICK 0xff40 #define MIDDLEDRAG 0xff4c #define MOUSEMOVE 0xff08 int Keystate; int MouseExist; int MouseButton; int MouseX; int MouseY; int up[16][16],down[16][16],mouse_draw[16][16],pixel_save[16][16]; void MouseMath()/*计算鼠标的样子*/ { int i,j,jj,k; long UpNum[16]={ 0x3fff,0x1fff,0x0fff,0x07ff, 0x03ff,0x01ff,0x00ff,0x007f, 0x003f,0x00ff,0x01ff,0x10ff, 0x30ff,0xf87f,0xf87f,0xfc3f }; long DownNum[16]={ 0x0000,0x7c00,0x6000,0x7000, 0x7800,0x7c00,0x7e00,0x7f00, 0x7f80,0x7e00,0x7c00,0x4600, 0x0600,0x0300,0x0300,0x0180 }; for(i=0;i=0;k--) up[i][k]=0; for(k=jj;k>=0;k--) down[i][k]=0; for(k=0;k<16;k++)/*四种组合方式*/ { if(up[i][k]==0&&down;[i][k]==0) mouse_draw[i][k]=1; else if(up[i][k]==0&&down;[i][k]==1) mouse_draw[i][k]=2; else if(up[i][k]==1&&down;[i][k]==0) mouse_draw[i][k]=3; else mouse_draw[i][k]=4; } } mouse_draw[1][2]=4;/*特殊点*/ } /*鼠标光标显示*/ void MouseOn() { int x=MouseX,y=MouseY; int i,j; int color; for(i=0;i<16;i++)/*画鼠标*/ { for(j=0;j<16;j++) { pixel_save[i][j]=getpixel(x+j,y+i);/*保存原来的颜色*/ if(mouse_draw[i][j]==1) putpixel(x+j,y+i,0); else if(mouse_draw[i][j]==2) putpixel(x+j,y+i,15); } } } /*隐藏鼠标*/ void MouseOff() { int i,j,x,y,color; x=MouseX; y=MouseY; for(i=0;i<16;i++)/*原位置异或消去*/ for(j=0;j<16;j++) { if(mouse_draw[i][j]==3||mouse_draw[i][j]==4) continue; color=getpixel(x+j,y+i); putpixel(x+j,y+i,color^color); putpixel(x+j,y+i,pixel_save[i][j]); } } /*鼠标状态值初始化*/ void MouseReset() { _AX=0x00; geninterrupt(0x33); } /*设置鼠标左右边界 lx:左边界 rx:右边界 */ void MouseSetX(int lx,int rx) { _CX=lx; _DX=rx; _AX=0x07; geninterrupt(0x33); } /*设置鼠标上下边界 uy:上边界 dy:下边界 */ void MouseSetY(int uy,int dy) { _CX=uy; _DX=dy; _AX=0x08; geninterrupt(0x33); } /*设置鼠标当前位置 x:横向坐标 y:纵向坐标 */ void MouseSetXY(int x,int y) { _CX=x; _DX=y; _AX=0x04; geninterrupt(0x33); } /*获取鼠标按下键的信息*/ /*是否按下左键 返回值: 1=按下 0=释放*/ int LeftPress() { _AX=0x03; geninterrupt(0x33); return(_BX&1); } /*是否按下中键 返回值同上 */ int MiddlePress() { _AX=0x03; geninterrupt(0x33); return(_BX&4); } /*是否按下右键 返回值同上 */ int RightPress() { _AX=0x03; geninterrupt(0x33); return(_BX&2); } /*获取鼠标当前位置*/ void MouseGetXY() { _AX=0x03; geninterrupt(0x33); MouseX=_CX; MouseY=_DX; } /*鼠标按键情况,返回0表示只移动,返回1表示左右键同时按下,2表示只按了左键,3表示只按了右键*/ int MouseStatus() { int x,y; int status; int press=0; int i,j,color; status=0;/*默认鼠标没有移动*/ x=MouseX; y=MouseY; while(x==MouseX&&y==MouseY&&status;==0&&press;==0) { if(LeftPress()&&RightPress;()) press=1; else if(LeftPress()) press=2; else if(RightPress()) press=3; MouseGetXY(); if(MouseX!=x||MouseY!=y) status=1; } if(status)/*移动情况才重新显示鼠标*/ { for(i=0;i<16;i++)/*原位置异或消去*/ for(j=0;j<16;j++) { if(mouse_draw[i][j]==3||mouse_draw[i][j]==4) continue; color=getpixel(x+j,y+i); putpixel(x+j,y+i,color^color); putpixel(x+j,y+i,pixel_save[i][j]); } MouseOn();/*新位置显示*/ } if(press!=0)/*有按键的情况*/ return press; return 0;/*只移动的情况*/ } /*定义玩家的结构体*/ struct Ren{ int x,y; int life; int color; int lr;/*1表示左,2表示右。*/ }; /*绘制游戏界面*/ void Desktop() { setcolor(14); line(320,0,320,480); rectangle(L1-20,T1-40,R1+20,B1+10); rectangle(640-(R1+20),(T1-40),640-(L1-20),B1+10); outtextxy(25,20,"P1"); outtextxy(345,20,"P2"); } /*把一个数字n转换成字符串,并存储在a中,带符号+-*/ void numtostr(int n,char a[5]) { int w,e; e=n; n=abs(n); a[3]=(n)+'0'; w=n/10; a[2]=(w)+'0'; w=w/10; a[1]=(w)+'0'; a[4]='\0'; if(e<0) a[0]='-'; else a[0]='+'; } /*把速度和角度装换成字符串输出*/ void AngleSpeed(double s,double angle) { int ss,aa; char zzs[5],zza[5]; int left,top,right,bottom; left=275; top=50; right=left+90; bottom=top+10; ss=(int)(s); aa=(int)((angle)*180/PI); numtostr(ss,zzs); numtostr(aa,zza); setfillstyle(1,15); setcolor(10); bar(left,top,right,bottom); outtextxy(left+5,top+3,zzs); outtextxy((left+right)/2+5,top+3,zza); circle(right-6,top+3,2); } /*实现人机对抗的函数*/ void Fire (int a[4],double *v,double *angle,int n)/*a数组存放对射的两点,v和angle存放机器射击的角度和速度,n表式机器射击的准确度*/ { int t; double vx,vy; double sx,sy; int m; m=12*4/n; randomize(); m=random(m)-m/2; t=20; sx=(double)(a[2]-a[0]); sy=(double)(a[3]-a[1]); vx=sx/(double)(t); vy=(sy-0.5*PI*(double)(t*t))/(double)(t); *angle=atan((-vy)/vx); *v=sqrt(vx*vx+vy*vy); *v=(*v)*(AMD2+0.01*(double)(m)); AngleSpeed(*v,*angle); } /*绘制生命线的函数*/ void LifePicture(int life,int color,int location) { char lm[5]; int l,t,r,b; l=50; t=20; r=l+200; b=t+10; numtostr(life,lm); setfillstyle(1,color); setcolor(15); if(location==1||location==3) { bar(l,t,r,b); setfillstyle(1,4); bar(l,t+(b-t)/4,l+life,t+3*(b-t)/4); setfillstyle(1,color); bar(r+10,t,r+50,b); outtextxy(r+10+5,t+2,lm); } else { l=320+50; r=l+200; bar(l,t,r,b); setfillstyle(1,4); bar(l,t+(b-t)/4,l+life,t+3*(b-t)/4); setfillstyle(1,color); bar(r+10,t,r+50,b); outtextxy(r+10+5,t+2,lm); } } /*绘制小屋的函数*/ void RenPicture(int x,int y,int color) { setcolor(color); setwritemode(1); line(x,y-40,x-10,y-30);/*画头*/ line(x,y-40,x+10,y-30); line(x-10,y-30,x+10,y-30); line(x-5,y-30,x-5,y-10);/*画脖子*/ line(x+5,y-30,x+5,y-10); line(x-20,y-10,x+20,y-10);/*画身子*/ line(x-20,y+10,x+20,y+10); line(x-20,y-10,x-20,y+10); line(x+20,y-10,x+20,y+10); } /*绘制箭的函数*/ void PictureBullets (int wx,int wy,int tx,int ty) { setcolor(RED); line(wx,wy,tx,ty); line(wx-1,wy-1,tx,ty); line(wx+1,wy+1,tx,ty); } /*绘制小屋上箭的函数*/ void InitialArrow (int x,int y,int a[4]) { int addx,addy; addx=(a[2]-a[0])/6; addy=(a[3]-a[1])/6; PictureBullets(x+addx,y+addy,x,y); } /*判断点qx,qy在直线的什么位置*/ int PointPlace(int qx,int qy,int x1,int y1,int x2,int y2)/*返回0表示在直线上,当斜率存在时:1表示在直线的上面,2表示在直线的下面,当斜率不存在时:3表示在左面,4表示在右面*/ { int s; if(x1==x2) { if(qxx1) return 4; else return 0; } else { s=(int)(((double)(y1-y2))/((double)(x1-x2))*((double)(qx-x1))+(double)(y1)); if(qys) return 2; else return 0; } } /*根据两点坐标计算出两点距离和斜率。*/ void DistanceAngle (int twoxy1[4],double *distance,double *angle) { double a,b; if(twoxy1[0]!=twoxy1[2]) { a=(double)((double)((double)twoxy1[3]-(double)twoxy1[1])/(double)((double)twoxy1[0]-(double)twoxy1[2])); *angle=atan(a); if(twoxy1[0]<twoxy1[2]) *angle=PI+(*angle); } else if(twoxy1[1]<twoxy1[3]) *angle=PI/2; else *angle=-PI/2; b=(double)((double)(twoxy1[3]-twoxy1[1])*(double)(twoxy1[3]-twoxy1[1])+(double)(twoxy1[2]-twoxy1[0])*(double)(twoxy1[2]-twoxy1[0])); *distance=sqrt(b); } /*由速度角度算sx,sy随时间的变化*/ void RelativePosition(int *sx,int *sy,double v,double angle,double t) { *sx=(int)((v*cos(angle))*t); *sy=(int)((v*sin(angle))*t-0.5*G*t*t); } /*用鼠标画一条直线,把直线的两点坐标放在twoxy数组内。*/ void TwoPoints(int twoxy[4],int dx,int dy) { int i,q=1; int ddx=dx,ddy=dy; double speed=0.0,angle=0.0; twoxy[0]=0; twoxy[1]=0; setcolor(13); line(dx,dy+30,dx,dy-30); line(dx-30,dy,dx+30,dy); setcolor(4); MouseOn();/*显示鼠标*/ setwritemode(1); i=0; while(q) { if(i==1) { MouseOff(); DistanceAngle(twoxy,&speed;,&angle;); AngleSpeed(speed/AMD1,angle); InitialArrow (ddx,ddy,twoxy); setcolor(4); line(twoxy[0],twoxy[1],twoxy[2],twoxy[3]); MouseOn(); if((twoxy[2]!=MouseX)||(twoxy[3]!=MouseY)) { twoxy[2]=MouseX; twoxy[3]=MouseY; MouseOff(); DistanceAngle(twoxy,&speed;,&angle;); AngleSpeed(speed/AMD1,angle); InitialArrow (ddx,ddy,twoxy); setcolor(4); line(twoxy[0],twoxy[1],twoxy[2],twoxy[3]); MouseOn(); } } if(MouseStatus()) { sound(1000);/*响声函数*/ delay(10000); nosound(); delay(1000000); delay(1000000); delay(1000000); delay(1000000); if(i==0) { twoxy[0]=MouseX; twoxy[1]=MouseY; twoxy[2]=MouseX; twoxy[3]=MouseY; i=1; } else { MouseOff(); DistanceAngle(twoxy,&speed;,&angle;); AngleSpeed(speed/AMD1,angle); InitialArrow (ddx,ddy,twoxy); setcolor(4); line(twoxy[0],twoxy[1],twoxy[2],twoxy[3]); setcolor(13); line(dx,dy+30,dx,dy-30); line(dx-30,dy,dx+30,dy); q=0; i=0; } } } } /*发射箭,speed1和speed2控制速度,返回中弹位置*/ int Launch(int lx,int ly,int tx,int ty,int hm,int grade) { double speed1=0.01; int speed2=1000; int a[4]; int xx[2],xy[2]; double s=0.0,angle=0.0,t=0.0; lx=lx;ly=ly-50; if(hm==3) { a[0]=lx; a[1]=ly; a[2]=tx; a[3]=ty; Fire (a,&s,&angle;,grade); } else { TwoPoints(a,lx,ly); DistanceAngle(a,&s,&angle;); s=s/AMD1; } RelativePosition(&xx;[0],&xy;[0],s,angle,t-1); RelativePosition(&xx;[1],&xy;[1],s,angle,t); for(t=0.0;ly-xy[1]<480;t=t+speed1) { RelativePosition(&xx;[0],&xy;[0],s,angle,t-1); RelativePosition(&xx;[1],&xy;[1],s,angle,t); if(PointPlace(lx+xx[1],ly-xy[1],tx,ty-40,tx+10,ty-30)==2&&PointPlace;(lx+xx[1],ly-xy[1],tx,ty-40,tx-10,ty-30)==2&&PointPlace;(lx+xx[1],ly-xy[1],tx-10,ty-30,tx+10,ty-30)==1) { sound(4000);/*响声函数*/ delay(10000); nosound(); return 1; } if(PointPlace(lx+xx[1],ly-xy[1],tx-5,ty-30,tx+5,ty-30)==2&&PointPlace;(lx+xx[1],ly-xy[1],tx-5,ty-10,tx+5,ty-10)==1&&PointPlace;(lx+xx[1],ly-xy[1],tx-5,ty-30,tx-5,ty-10)==4&&PointPlace;(lx+xx[1],ly-xy[1],tx+5,ty-30,tx+5,ty-10)==3) { sound(3000);/*响声函数*/ delay(10000); nosound(); return 2; } if(PointPlace(lx+xx[1],ly-xy[1],tx-20,ty-10,tx-20,ty+10)==4&&PointPlace;(lx+xx[1],ly-xy[1],tx+20,ty-10,tx+20,ty+10)==3&&PointPlace;(lx+xx[1],ly-xy[1],tx-20,ty-10,tx+20,ty-10)==2&&PointPlace;(lx+xx[1],ly-xy[1],tx-20,ty+10,tx+20,ty+10)==1) { sound(2000);/*响声函数*/ delay(10000); nosound(); return 3; } if(ly-xy[1]<1) { delay(speed2); continue; } if(lx+xx[1]640-1) { return 0; } PictureBullets (lx+xx[0],ly-xy[0],lx+xx[1],ly-xy[1]); delay(speed2); PictureBullets (lx+xx[0],ly-xy[0],lx+xx[1],ly-xy[1]); } return 0; } /*小屋移动的函数*/ int MoveRen(struct Ren *p) { int a,k=19200,b=0,d; int q=1; randomize(); for(;q;) { if(b==1) { p->lr=3; } RenPicture(p->x,p->y,p->color); if(p->lr==3) { b=1; delay(10000); delay(10000); delay(10000); delay(10000); delay(10000); delay(10000); //sleep(1); d=random(10); if(d==0) k=19200; if(d==1) k=19712; if(d==2) k=18432; if(d==3) k=20480; if(d==4) k=7181; p->lr=1; } else { k=bioskey(0); } RenPicture(p->x,p->y,p->color); switch(k){ case 19200: /*按向左键*/ a=(p->x)-5; if(p->lr==1) { if(a>L1&&ax=a; break; } } else { if(a>640-R1&&ax=a; break; } } break; case 19712: /*按向右键*/ a=(p->x)+5; if(p->lr==1) { if(a>L1&&ax=a; break; } } else { if(a>640-R1&&ax=a; break; } } break; case 18432: /*按向上键*/ a=(p->y)-5; if(p->lr==1) { if(a>T1&&ay=a; break; } } else { if(a>T1&&ay=a; break; } } break; case 20480: /*按向下键*/ a=(p->y)+5; if(a>T1&&ay=a; } break; case 7181: /*enter键的扫描码*/ if(b==1) p->lr=3; q=0; break; case 283: return 0; } } RenPicture(p->x,p->y,p->color); return 1; } /*游戏开始前画面*/ int GameStar()/*返回1表示单人游戏初级,2表示单人游戏中级,3表示单人游戏高级,4表示两人对战,5表示退出游戏*/ { int q,k,h=0; for(;1;) { q=1; cleardevice();/*清屏函数*/ setcolor(15); settextstyle(0,0,5); outtextxy(100,100,"Start Game!"); settextstyle(0,0,1); outtextxy(20,300,"keys used:"); outtextxy(20,300," Arrow keys"); outtextxy(20,310," The left mouse button"); outtextxy(20,320," Enter"); outtextxy(20,330," Esc to Quit!"); setcolor(5); outtextxy(250,400,"One player!"); outtextxy(250,420,"Two players!"); outtextxy(250,440,"Quit!"); setwritemode(1); setcolor(6); rectangle(245,395+h*20,345,415+h*20); for(;q;) { setcolor(6); k=bioskey(0); sound(1000);/*响声函数*/ delay(10000); nosound(); if(k==20480) { rectangle(245,395+h*20,345,415+h*20); h=(h+1)%3; rectangle(245,395+h*20,345,415+h*20); }else if(k==7181) { if(h==0)/*单人游戏,选择等级*/ { cleardevice();/*清屏函数*/ setcolor(2); outtextxy(20,30," Esc to back!"); outtextxy(250,240,"Lower"); outtextxy(250,260,"Middle"); outtextxy(250,280,"Higher"); setcolor(4); rectangle(245,235+h*20,300,255+h*20); for(;q;) { k=bioskey(0); sound(1000);/*响声函数*/ delay(10000); nosound(); if(k==20480) { rectangle(245,235+h*20,300,255+h*20); h=(h+1)%3; rectangle(245,235+h*20,300,255+h*20); }else if(k==7181) { return h+1; }else if(k==283) { h=0; k=1; q=0; break; }else{} } } if(h==1)/*两人对抗*/ return 4; if(h==2)/*退出游戏*/ return 5; }else if(k==283) { return 5; }else {} } } } /*退出游戏画面*/ void GameOver() { cleardevice();/*清屏函数*/ setcolor(14); settextstyle(0,0,6); outtextxy(100,200,"Game Over!"); settextstyle(1,0,1); outtextxy(400,400,"Producer:ChenChen"); outtextxy(400,410," QQ:804620957"); outtextxy(400,420," Time:2010.5.28"); } /*主函数*/ void main() { int gd=DETECT,gm; int q=0,schoose=1; int out=1; int pmc=1; int cla2s=1; struct Ren ren1,ren2; initgraph(&gd;,&gm;,""); /* registerbgidriver(EGAVGA_driver);*/ cleardevice();/*清屏函数*/ MouseMath();/*计算鼠标形状,一开始必须使用,后面就不用了*/ MouseSetY(0,479); MouseSetX(0,649); MouseSetXY(100,100); for(;out;) { pmc=GameStar(); cleardevice();/*清屏函数*/ settextstyle(1,0,1);/*初始化*/ schoose=1; ren2.x=540;ren2.y=320;ren2.life=200;ren2.color=3;ren2.lr=2; if(pmc0&&ren2;.life>0;schoose++) { if(schoose%2) { RenPicture(ren1.x,ren1.y,ren1.color); if(MoveRen(&ren1;)==0) break; q=Launch(ren1.x,ren1.y,ren2.x,ren2.y,ren1.lr,cla2s); if(q==1) ren2.life=ren2.life-40; if(q==2) ren2.life=ren2.life-20; if(q==3) ren2.life=ren2.life-10; if(ren2.life<0) ren2.life=0; LifePicture(ren2.life,ren2.color,ren2.lr); } else { RenPicture(ren2.x,ren2.y,ren2.color); if(MoveRen(&ren2;)==0) break; q=Launch(ren2.x,ren2.y,ren1.x,ren1.y,ren2.lr,cla2s); if(q==1) ren1.life=ren1.life-40; if(q==2) ren1.life=ren1.life-20; if(q==3) ren1.life=ren1.life-10; if(ren1.life<0) ren1.life=0; LifePicture(ren1.life,ren1.color,ren1.lr); } } if(ren1.liferen2.life) { settextstyle(0,0,6); setcolor(ren1.color); outtextxy(150,280,"P1 win!"); settextstyle(1,0,1); } else { settextstyle(0,0,6); setcolor(15); outtextxy(150,280,"Drew!"); settextstyle(1,0,1); } getch(); } GameOver(); getch(); closegraph(); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值