求一个50行左右的C语言编程。

我现在正在学习C语言,好到期末考试了,老师要求我们准备一个50行左右的编程,因为我现在是初学所以最好不要太深,而且最好在每一行加一个注释。还请各位高手给个符合要求的例子。... 我现在正在学习C语言,好到期末考试了,老师要求我们准备一个50行左右的编程,因为我现在是初学所以最好不要太深,而且最好在每一行加一个注释。还请各位高手给个符合要求的例子。谢谢了。 展开
 我来答
guaidaoK
2009-06-06 · TA获得超过314个赞
知道答主
回答量:468
采纳率:0%
帮助的人:357万
展开全部
这是一个玩五子棋的例子:
#include <iostream.h>
#include <process.h>
#include <conio.h>
class five
{
int x;
int y;
int m;
int n;
int num_xy;
int num_mn;
char qipan[20][20];
public:
five(int X = 0,int Y =0,int M = 0,int N = 0,int Num_xy = 0,int Num_mn = 0)
{
x = X;
y = Y;
m = M;
n = N;
num_xy = Num_xy;
num_mn = Num_mn;
for(int i = 0 ; i < 20 ; i ++)
{
for(int j = 0 ;j < 20 ; j ++)
qipan[i][j] = '.';
}

}
int getm()
{
return m;
}
int getn()
{
return n;
}
void getpointxy();
void getpointmn();
void machinemn();
void showqipan();
void print();
bool IsxyWin();
bool IsmnWin();
int dangerlevel(int a,int b);
void attack();
};
void five::getpointxy()
{
cout<<"请分别输入第"<<num_xy+1<<"步白棋('o')的横坐标和纵坐标:";
cin >> x >> y;
if(x <= 0 || y <= 0 || x > 20 || y > 20 )
{
cout << "出界,请重新输入:"<<endl ;
getpointxy();
}
else if(qipan[y-1][x-1] != '.' )
{
cout << "请所下的位置已经有棋子,请重新输入:" <<endl;
getpointxy();
}
else if(num_xy+num_mn >= 400)
{
cout << "棋盘已满,平局!" << endl;
exit(0);
}
else
{
qipan[y-1][x-1] = 'o';
num_xy++;
}

}
void five::getpointmn()
{
cout<<"请分别输入第"<<num_mn+1<<"步黑棋('x')的横坐标和纵坐标:";
cin >> m >> n;
if(m <= 0 || n <= 0 || m > 20 || n > 20 )
{
cout << "出界,请重新输入:"<<endl ;
getpointmn();
}
else if(qipan[n-1][m-1]!='.')
{
cout << "请所下的位置已经有棋子,请重新输入:" <<endl;
getpointmn();
}
else if(num_xy+num_mn >= 400)
{
cout << "棋盘已满,平局!" << endl;
exit(0);
}
else
{
qipan[n-1][m-1] = 'x';
num_mn++;
}

}

int five::dangerlevel(int a, int b)
{
int level = 0;
for(int i = 1 ; a+i<=19; i ++)
{
if(qipan[a+i][b] == '.' && a + i <= 19)
{
i = i+1;
break;
}
if(qipan[a +i][b] == 'x' && a + i <= 19)
break;

}
level += (i-1);
for(i = 1 ; a-i>=0; i ++)
{
if(qipan[a-i][b] == '.' && (a-i) >= 0)
{
i = i+1;
break;
}
if(qipan[a-i][b] == 'x' && (a-i) >= 0)
break;
}
level += (i-1);
for(i = 1 ; b+i<=19 ; i ++)
{
if(qipan[a][b + i] == '.' && b + i <= 19)
{
i = i+1;
break;
}
if(qipan[a][b + i] == 'x' && b + i <= 19)
break;
}
level += (i-1);
for( i = 1 ; b-i>=0 ; i ++)
{
if(qipan[a][b - i] == '.' && (b-i) >= 0)
{
i = i+1;
break;
}
if(qipan[a][b - i] == 'x' && (b-i) >= 0)
break;
}
for( i = 1 ; a+i<=19&& b+i<=19; i ++)
{
if(qipan[a + i][b + i] == '.' && b+i <= 19 && a+i <= 19)
{
i = i+1;
break;
}
if(qipan[a + i][b + i] == 'x' && b+i <= 19 && a+i <= 19)
break;
}
level += (i-1);
for( i = 1 ; a-i>= 0 && b-i >= 0; i ++)
{
if(qipan[a - i][b - i] == '.' && a-i>= 0 && b-i >= 0)
{
i = i+1;
break;
}
if(qipan[a - i][b - i] == 'x' && a-i>= 0 && b-i >= 0)
break;
}
level += (i-1);
for( i = 1 ; a-i>= 0 && b+i <= 19 ; i ++)
{
if(qipan[a - i][b + i] == '.' && a-i>= 0 && b+i <= 19)
{
i = i+1;
break;
}
if(qipan[a - i][b + i] == 'x' && a-i>= 0 && b+i <= 19)
break;
}
level += (i-1);
for( i = 1 ; a + i<= 19 && b-i >= 0 ; i ++)
{
if(qipan[a + i][b - i] == '.' && a + i<= 19 && b-i >= 0)
{
i = i+1;
break;
}
if(qipan[a + i][b - i] == 'x' && a + i<= 19 && b-i >= 0)
break;
}
level += (i-1);
return level;
}

void five::attack()
{
int num1=1,num2=1,num3=1,num4=1;
int A = 20 , B = 20 ,max = 0;
for(int a = 0 ; a < 19 ; a ++)
{
for(int b = 0 ; b < 19 ; b ++)
{
if(qipan[a][b] == '.')
{
int level = 0;
for(int i = 1 ; a+i<=19; i ++)
{
if(qipan[a+i][b] == '.' && a + i <= 19)
{
i = i+1;
break;
}
if(qipan[a +i][b] == 'o' && a + i <= 19)
break;

}
level += (i-1);
for(i = 1 ; a-i>=0; i ++)
{
if(qipan[a-i][b] == '.' && (a-i) >= 0)
{
i = i+1;
break;
}
if(qipan[a-i][b] == 'o' && (a-i) >= 0)
break;
}
level += (i-1);
for(i = 1 ; b+i<=19 ; i ++)
{
if(qipan[a][b + i] == '.' && b + i <= 19)
{
i = i+1;
break;
}
if(qipan[a][b + i] == 'o' && b + i <= 19)
break;
}
level += (i-1);
for( i = 1 ; b-i>=0 ; i ++)
{
if(qipan[a][b - i] == '.' && (b-i) >= 0)
{
i = i+1;
break;
}
if(qipan[a][b - i] == 'o' && (b-i) >= 0)
break;
}
for( i = 1 ; a+i<=19&& b+i<=19; i ++)
{
if(qipan[a + i][b + i] == '.' && b+i <= 19 && a+i <= 19)
{
i = i+1;
break;
}
if(qipan[a + i][b + i] == 'o' && b+i <= 19 && a+i <= 19)
break;
}
level += (i-1);
for( i = 1 ; a-i>= 0 && b-i >= 0; i ++)
{
if(qipan[a - i][b - i] == '.' && a-i>= 0 && b-i >= 0)
{
i = i+1;
break;
}
if(qipan[a - i][b - i] == 'o' && a-i>= 0 && b-i >= 0)
break;
}
level += (i-1);
for( i = 1 ; a-i>= 0 && b+i <= 19 ; i ++)
{
if(qipan[a - i][b + i] == '.' && a-i>= 0 && b+i <= 19)
{
i = i+1;
break;
}
if(qipan[a - i][b + i] == 'o' && a-i>= 0 && b+i <= 19)
break;
}
level += (i-1);
for( i = 1 ; a + i<= 19 && b-i >= 0 ; i ++)
{
if(qipan[a + i][b - i] == '.' && a + i<= 19 && b-i >= 0)
{
i = i+1;
break;
}
if(qipan[a + i][b - i] == 'o' && a + i<= 19 && b-i >= 0)
break;
}
level += (i-1);
if(level >= max)
{
n = a + 1;
m = b + 1;
max = level;
}
}
else
continue;
}
}
qipan[n-1][m-1] = 'x';
}
void five::machinemn()
{

int Num1=1,Num2=1,Num3=1,Num4=1,A11 = 21,A12 = 21,A21 = 21,A22 = 21,A31 = 21,A32 = 21,A41 = 21,A42 = 21;
for(int i = x ; i < x+5&& x+5<20 ; i ++)
{
if(qipan[y-1][i] == '.')
{
A11=i;
break;
}
if(qipan[y-1][i] == 'x')
break;
if(qipan[y-1][i] == 'o')
Num1++;
}
for(i = x-2; i >= 0 ; i --)
{
if(qipan[y-1][i] == '.')
{
A12 = i;
break;
}
if(qipan[y-1][i] == 'x')
{
break;
}
if(qipan[y-1][i] == 'o')
Num1++;
}
////

for( i = y ; i < y+5 && y+5 < 20 ; i ++)
{
if(qipan[i][x-1] == '.')
{
A21 = i;
break;
}
if(qipan[i][x-1] == 'x')
{
break;
}
if(qipan[i][x-1] == 'o')
Num2++;
}
for(i = y-2; i >= 0 ; i --)
{
if(qipan[i][x-1] == '.')
{
A22 = i;
break;
}
if(qipan[i][x-1] == 'x')
{
break;
}
if(qipan[i][x-1] == 'o')
Num2++;
}
////
for(i = 0 ; i < 5 && x+i<20 && y+i<20; i++ )
{
if(qipan[y+i][x+i] == '.')
{
A31 = i;
break;
}
if(qipan[y+i][x+i] == 'x')
{
break;
}
if(qipan[y+i][x+i] =='o')
Num3++;
}
for(i = 0 ; x - i -2>=0 && y-i-2>=0 && i < 5;i++ )
{
if(qipan[y-i-2][x-i-2] == '.')
{
A32 = i;
break;
}
if(qipan[y-i-2][x-i-2] == 'x')
break;
if(qipan[y-i-2][x-i-2] == 'o')
Num3++;
}
////
for(i = 0 ; x + i < 20 && y - i - 2 >= 0 && i < 5;i ++)
{
if(qipan[y-i-2][x+i] == '.')
{
A41 = i;
break;
}
if(qipan[y-i-2][x+i] == 'x')
{
break;
}
if(qipan[y-i-2][x+i] == 'o')
Num4++;
}
for(i = 0 ; y + i < 20 && x - i - 2 >= 0 && i < 5;i ++)
{
if(qipan[y+i][x-i-2] == '.')
{
A42 = i;
break;
}
if(qipan[y+i][x-i-2] == 'x')
{
break;
}
if(qipan[y+i][x-i-2] == 'o')
Num4++;
}
////
if(num_xy == 0 && num_xy == 0)
{
m = n = 10;
qipan[m-1][n-1] = 'x';
}
else if(num_xy == 1)
attack();
else if(num_xy == 1)
{
m = x+1;
n = x+1;
qipan[m-1][n-1] = 'x';
}
else if(Num1 == 4 && A11 == 21 && A12 != 21) //一排四个右边一头堵住
{
n =1 + y-1;
m = 1 + A12;
qipan[y-1][A12] = 'x';
}
else if(Num1 == 4 && A12 == 21 && A11 != 21) //一排四个左边边一头堵住
{
n =1 + y-1;
m = 1 + A11;
qipan[y-1][A11] = 'x';
}
else if(Num2 == 4 && A21 == 21 && A22 != 21) //一列四个下边一头堵住
{
n =1 + A22;
m = 1 + x-1;
qipan[A22][x-1] = 'x';
}
else if(Num2 == 4 && A22 == 21 && A21 != 21) //一列四个上边一头堵住
{
n =1 + A21;
m = 1 + x-1;
qipan[A21][x-1] = 'x';
}
else if(Num3 == 4 && A31 == 21 && A32 != 21) //一左上右下斜列四个右边一头堵住
{
n =1 + y - A32 - 2;
m = 1 + x - A32 - 2;
qipan[y-A32-2][x-A32-2] = 'x';
}
else if(Num3 == 4 && A32 == 21 && A31 != 21) //一左上右下斜列四个左边一头堵住
{
n =1 + y + A31;
m = 1 + x + A31;
qipan[y+A31][x+A31] = 'x';
}
else if(Num4 == 4 && A41 == 21 && A42 != 21) //一左下右上斜列四个右边一头堵住
{
n =1 + y + A42;
m = 1 + x - A42 - 2;
qipan[y+A42][x-A42-2] = 'x';
}
else if(Num4 == 4 && A42 == 21 && A41 != 21) //一左下右上斜列四个左边一头堵住
{
n =1 + y - A41 - 2;
m = 1 + x + A41;
qipan[y-A41-2][x+A41] = 'x';
}
else if(Num1==3 && A11 != 21 && A12 != 21) //一排3 个两头没堵
{
n =1 + y-1;
m = 1 + A11;
if(dangerlevel(y-1,A11) >= dangerlevel(y-1,A12))
qipan[y-1][A11] = 'x';
else
qipan[y-1][A12] = 'x';

}
else if(Num2==3&& A21 != 21 && A22 != 21) //一列 3 个两头没堵
{
n =1 + A21;
m = 1 + x-1;
if(dangerlevel(A21,x-1) >= dangerlevel(A22,x-1))
qipan[A21][x-1] = 'x';
else
qipan[A22][x-1] = 'x';
}
else if(Num3 == 3&& A31 != 21 && A32 != 21) //一左上右下斜列3 个两头没堵
{
n =1 + y + A31;
m = 1 + x + A31;
if(dangerlevel(y+A31,x+A31) >= dangerlevel(y-A32 - 2,y - A32 -2))
qipan[y+A31][x+A31] = 'x';
else
qipan[y-A32-2][x-A32 -2] = 'x';

}
else if(Num4==3 && A41 != 21 && A42 != 21) //一左下右上斜列3 个两头没堵
{
n =1 + y - A41 - 2;
m = 1 + x + A41;
if(dangerlevel(y-A41-2,x+A41) >= dangerlevel(y-A42-2,x+A42))
qipan[y-A41-2][x+A41] = 'x';
else
qipan[y-A42-2][x+A42] = 'x';
}
else if(Num1 == 2&& A11 != 21 && A12 != 21) //一排2个两头没堵
{
n =1 + y-1;
m = 1 + A11;
qipan[y-1][A11] = 'x';
}

else if( Num2 == 2&& A21 != 21 && A22 != 21) //一列2个两头没堵
{
n =1 + A21;
m = 1 + x-1;
qipan[A21][x-1] = 'x';
}
else if(Num3 == 2&& A31 != 21 && A32 != 21) //一左上右下斜列2 个两头没堵
{
n =1 + y + A31;
m = 1 + x + A31;
qipan[y+A31][x+A31] = 'x';
}
else if(Num4==2 && A41 != 21 && A42 != 21) //一左下右上斜列2个两头没堵
{
n =1 + y - A41 - 2;
m = 1 + x + A41;
qipan[y-A41-2][x+A41] = 'x';
}
else
attack();
}

void five::showqipan()
{
int p=0,q=0;
for(int i = 0 ; i <= 20 ; i++)
{

if(p<10)
{
cout << p <<' ';
}
else
cout << p ;
p++;
}
cout << endl;
for(i = 0 ; i < 20 ; i++)
{
if(i>=0)
{
q++;
if(q<10)
cout << q <<' ';
else
cout << q;
}
for(int j = 0 ; j < 20 ; j ++)
cout << qipan[i][j]<<' ' ;

cout << endl;
}
}

void five::print()
{
int p=0,q=0;
for(int i = 0 ; i <= 20 ; i++)
{

if(p<10)
{
cout << p <<' ';
}
else
cout << p ;
p++;
}
cout << endl;
for(i = 0 ; i < 20 ; i++)
{
if(i>=0)
{
q++;
if(q<10)
cout << q <<' ';
else
cout << q;
}
for(int j = 0 ; j < 20 ; j ++)
{
cout << qipan[i][j]<<' ' ;
}

cout << endl;
}
}

bool five::IsxyWin()
{
int num1=1,num2=1,num3=1,num4=1;
for(int i = x ; i < x+5&& x+5<20 ; i ++)
{
if(qipan[y-1][i] != 'o')
break;
if(qipan[y-1][i] == 'o')
num1++;
}
for(i = x-2; i >= 0 ; i --)
{
if(qipan[y-1][i] != 'o')
break;
if(qipan[y-1][i] == 'o')
num1++;
}
for( i = y ; i < y+5 ; i ++)
{
if(qipan[i][x-1] != 'o')
break;
if(qipan[i][x-1] == 'o')
num2++;
}
for(i = y-2; i >= 0 ; i --)
{
if(qipan[i][x-1] != 'o')
break;
if(qipan[i][x-1] == 'o')
num2++;
}
for(i = 0 ; i < 5 && x+i<20 && y+i<20; i++ )
{
if(qipan[y+i][x+i] != 'o')
break;
if(qipan[y+i][x+i] =='o')
num3++;
}
for(i = 0 ; x - i -2>=0 && y-i-2>=0 && i < 5;i++ )
{
if(qipan[y-i-2][x-i-2] != 'o')
break;
if(qipan[y-i-2][x-i-2] == 'o')
num3++;
}
for(i = 0 ; x + i < 20 && y - i - 2 >= 0 && i < 5;i ++)
{
if(qipan[y-i-2][x+i] != 'o')
break;
if(qipan[y-i-2][x+i] == 'o')
num4++;
}
for(i = 0 ; y + i < 20 && x - i - 2 >= 0 && i < 5;i ++)
{
if(qipan[y+i][x-i-2] != 'o')
break;
if(qipan[y+i][x-i-2] == 'o')
num4++;
}
if(num1>=5||num2>=5||num3>=5||num4>=5)
return 1;
else
return 0;

}
bool five::IsmnWin()
{
int num1=1,num2=1,num3=1,num4=1;
for(int i = m ; i < m+5&& m+5<20 ; i ++)
{
if(qipan[n-1][i] != 'x')
break;
if(qipan[n-1][i] == 'x')
num1++;
}
for(i = m-2; i >= 0 ; i --)
{
if(qipan[n-1][i] != 'x')
break;
if(qipan[n-1][i] == 'x')
num1++;
}
for( i = n ; i < n+5 ; i ++)
{
if(qipan[i][m-1] != 'x')
break;
if(qipan[i][m-1] == 'x')
num2++;
}
for(i = n-2; i >= 0 ; i --)
{
if(qipan[i][m-1] != 'x')
break;
if(qipan[i][m-1] == 'x')
num2++;
}
for(i = 0 ; i < 5 && m+i<20 && n+i<20; i++ )
{
if(qipan[n+i][m+i] != 'x')
break;
if(qipan[n+i][m+i] =='x')
num3++;
}
for(i = 0 ; m - i -2>=0 && n-i-2>=0 && i < 5;i++ )
{
if(qipan[n-i-2][m-i-2] != 'x')
break;
if(qipan[n-i-2][m-i-2] == 'x')
num3++;
}
for(i = 0 ; m + i < 20 && n - i - 2 >= 0 && i < 5;i ++)
{
if(qipan[n-i-2][m+i] != 'x')
break;
if(qipan[n-i-2][m+i] == 'x')
num4++;
}
for(i = 0 ; n + i < 20 && m - i - 2 >= 0 && i < 5;i ++)
{
if(qipan[n+i][m-i-2] != 'x')
break;
if(qipan[n+i][m-i-2] == 'x')
num4++;
}
if(num1>=5||num2>=5||num3>=5||num4>=5)
return 1;
else
return 0;

}

void main()
{
five a;
int temp;
cout << "欢迎体验本游戏!这是本游戏的棋盘:" << endl;
a.showqipan();
cout << "双人游戏请按1,人机游戏请按2,退出请按3:";
cin >> temp;
if(temp == 3)
exit(1);
if(temp == 1)
{
cout << "您选择了双人游戏,下面游戏开始:" << endl;
for(int i = 0 ; ; i ++)
{
a.getpointxy();
a.print();
if(a.IsxyWin())
{
cout << "白棋胜!" << endl;
break;
}
a.getpointmn();
a.print();
if(a.IsmnWin())
{
cout << "黑棋胜!" << endl;
break;
}
}
}
if(temp == 2)
{
int temp1=0;
cout << "您选择了人机游戏,您是白棋('o'),电脑是黑棋('x'),下面游戏开始:" << endl;
cout << "您先下请按1,电脑先下请按2:";
cin >> temp1;
if(temp1 == 1)
{
for(int i = 0 ; ; i ++)
{
a.getpointxy();
a.print();
if(a.IsxyWin())
{
cout << "你赢了!恭喜!" << endl;
break;
}
a.machinemn();
a.print();
cout << "电脑走(" << a.getm() << ',' << a.getn() << ')' << endl;
if(a.IsmnWin())
{
cout << "哈哈,你输了!" << endl;
break;
}
}
}
if(temp1 == 2)
{
for(int i = 0 ; ; i ++)
{
a.machinemn();
a.print();
if(a.IsmnWin())
{
cout << "哈哈,你输了!" << endl;
break;
}
a.getpointxy();
a.print();
if(a.IsxyWin())
{
cout << "你赢了!恭喜!" << endl;
break;
}

}
}
}
cout << "Press any key to EXIT\n";
getch();
}
davidluoye
2009-06-20
知道答主
回答量:36
采纳率:0%
帮助的人:20万
展开全部
打开vc++6.0,新建---工程选项卡里选择win32 Console Application 填上工程名,选择一下存放路径,确定,选择一个简单的工程,完成。然后把代码复制粘贴进去,就ok了

// 奇数魔方.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdio.h"
#define N 10

void creat(int a[N][N],int n)//创建魔方数组
{
int i,j,k;
i=0;
j=n/2;
for(k=1;k<=n*n;k++)
{
a[i][j]=k;
i--;
j++;
if(k%n==0)
{
i=i+2;
j=j-1;
}
if(i<0)
i=i+n;
if(j>=n)
j=j-n;
}

}

void print(int a[N][N],int n)//输出数组
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%6d",a[i][j]);
printf("\n");
}

}

int main(int argc, char* argv[])
{
int a[N][N];
int n;
printf("请输入魔方的维数(n<=10):n=");
scanf("%d",&n);
creat(a,n);
print(a,n);

}
运行环境vc++6.0,win-tc上运行的话修改一下头文件

这是又一个程序

// 行列式求值.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include "stdio.h"

#define N 4

void swap(int j,int n,int a[N][N])
// 把a[n][n]中的除n-1行和j列以外的元素放到a的前(n-1)^2个元素中
{
int x,y;
for(y=j;y<n;y++)
for(x=0;x<=n-1;x++)//只交换前n行
a[x][y]=a[x][y+1];
}

int hls(int n, int a[N][N])//求a的前n^2个元素组成的行列式的值
{
//思路:
//保存b[x][y]=a[x][y];
//把a[n][n]中的n-1行和0列上以外的元素放到a的前(n-1)*(n-1)个元素中
//计算a[n-1][0]*hls(n-1,a)*(-1)^(n-1+0)
//恢复a
//...
////把a[n][n]中的n-1行和j列以外的元素放到a的前(n-1)*(n-1)个元素中
//计算a[n-1][j]*hls(n-1,a)*(-1)^(n-1+j)
//恢复a
//...
//返回上述之和
int x,y,j,s=0,b[N][N];
if(n==2) return a[0][0]*a[1][1]-a[0][1]*a[1][0];

for(j=0;j<n;j++)
{
for(x=0;x<N;x++)
for(y=0;y<N;y++)
b[x][y]=a[x][y];

swap(j,n-1,a);
if((j+n-1)%2)
s-=a[n-1][j]*hls(n-1,a);
else
s+=a[n-1][j]*hls(n-1,a);

for(x=0;x<N;x++)
for(y=0;y<N;y++)
a[x][y]=b[x][y];
}

return s;
}

void main()
{
int a[N][N];
printf("输入%d个数:",N*N);
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
{
scanf("%d",&a[i][j]);
printf("\n");
}
printf("结果=%d\n",hls(N,a));
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式