A. 求c语言编的程序 急用
/*显示一个立方体*/
#include <dos.h>
#include <math.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#define PI 3.1415926
/*定义按键*/
#define ESC 0x11b
/*以下4个键,依次是上 下 左 右*/
#define X_axis_clkwise 0x4800
#define X_axis_Cntclkwise 0x5000
#define Y_axis_clkwise 0x4b00
#define Y_axis_Cntclkwise 0x4d00
/*以下2个键,依次是A, D*/
#define Z_axis_clkwise 0x1e61
#define Z_axis_Cntclkwise 0x2064
#define Distance_forward 0x1177
#define Distance_Backward 0x1f73
/*以下6个键,依次是U, J, I, K, O, L*/
#define X_Delta_Plus 0x1675
#define X_Delta_Minus 0x246a
#define Y_Delta_Plus 0x1769
#define Y_Delta_Minus 0x256b
#define Z_Delta_Plus 0x186f
#define Z_Delta_Minus 0x266c
/*绕X轴旋转矩阵*/
float X_Rotate_Matrix[4][4] = { 1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1 };
/*绕Y轴旋转矩阵*/
float Y_Rotate_Matrix[4][4] = { 1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1 };
/*绕Z轴旋转矩阵*/
float Z_Rotate_Matrix[4][4] = { 1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1 };
/*平移矩阵*/
float Transist_Matrix[4][4] = { 1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1 };
/*透视投影变换矩阵*/
float Perspective_Projection[4][4] = { 1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 0, 0,
0, 0, 0, 1 };
int num;
float *Matrix_Mul(float *pMatrix1, int Num_Row_Matrix1, int Num_Column_Matrix1,
float *pMatrix2, int Num_Row_Matrix2, int Num_Column_Matrix2 ) {
/*实现两个矩阵:
输入参数: *pMatrix1: 指向第一个矩阵
Num_Row_Matrix1: 第一个矩阵的行数
Num_Column_Matrix1: 第一个矩阵的列数
余下三个参数类推;
return 指向运算结果的float类型指针.*/
int i, j, m, n;
float *pNewMatrix1, *pNewMatrix2, Sum;
if( Num_Column_Matrix1 != Num_Row_Matrix2) {
printf("Invalid Matrixs!\n");
return 0;
}
pNewMatrix1 = malloc(Num_Row_Matrix1 * Num_Column_Matrix2 * 4);
/*申请内存空间, Size(/bytes) = 第一个矩阵的行数 * 第二个矩阵的列数 * 4(= sizeof(float))*/
pNewMatrix2 = pNewMatrix1;
/*具体算法详见如下代码*/
for( i = 0; i < Num_Row_Matrix1; i++) {
for( n = 0; n < Num_Column_Matrix2; n++) {
Sum = 0;
for( j = 0; j < Num_Column_Matrix1; j++)
Sum += (*(pMatrix1+i*Num_Column_Matrix1+j)) * (*(pMatrix2+j*Num_Column_Matrix2+n));
*(pNewMatrix1++) = Sum;
}
}
return pNewMatrix2;
}
/*转换成齐次坐标矩阵*/
void Matrix_Convertion(float *pMatrix, int Num_Row) {
int i, j;
for(i = 0; i < Num_Row; i++) {
if((*(pMatrix+i*4+3)) != 0) {
*(pMatrix+i*4) = (*(pMatrix+i*4)) / (*(pMatrix+i*4+3));
*(pMatrix+i*4+1) = (*(pMatrix+i*4+1)) / (*(pMatrix+i*4+3));
*(pMatrix+i*4+2) = (*(pMatrix+i*4+2)) / (*(pMatrix+i*4+3));
}
}
}
/*取得投影坐标*/
float *Get_X_Y(float *pMatrix, int Num_Row) {
int i, j, Num;
float *pNewMatrix;
Num = 0;
for(i = 0; i < Num_Row; i++) {
if((*(pMatrix+i*4+3)) != 0)
Num++;
}
pNewMatrix = malloc(Num * 2 * 4);
/*存放格式,{(x1, y1),(x2, y2), ... ,(xn, yn)}*/
for(i = 0; i < Num; i++) {
if((*(pMatrix+i*4+3)) != 0) {
*(pNewMatrix+i*2) = (*(pMatrix+i*4))+300; /*显示在屏幕中心, x = 300*/
*(pNewMatrix+i*2+1) = (*(pMatrix+i*4+1))+200; /*显示在屏幕中心, y = 200*/
}
}
return pNewMatrix;
}
/*设置旋转矩阵, Rotate around aixs labled with X or Y or Z*/
void SetMatrix_X(float X_Angle) {
float CosX, SinX;
SinX = sin(X_Angle * PI /128);
CosX = cos(X_Angle * PI /128);
X_Rotate_Matrix[1][1] = CosX;
X_Rotate_Matrix[1][2] = SinX;
X_Rotate_Matrix[2][1] = -1 * SinX;
X_Rotate_Matrix[2][2] = CosX;
}
void SetMatrix_Y(float Y_Angle) {
float CosY, SinY;
SinY = sin(Y_Angle * PI /128);
CosY = cos(Y_Angle * PI /128);
Y_Rotate_Matrix[0][0] = CosY;
Y_Rotate_Matrix[0][2] = -1 * SinY;
Y_Rotate_Matrix[2][0] = SinY;
Y_Rotate_Matrix[2][2] = CosY;
}
void SetMatrix_Z(float Z_Angle) {
float CosZ, SinZ;
SinZ = sin(Z_Angle * PI /128);
CosZ = cos(Z_Angle * PI /128);
Z_Rotate_Matrix[0][0] = CosZ;
Z_Rotate_Matrix[0][1] = SinZ;
Z_Rotate_Matrix[1][0] = -1 * SinZ;
Z_Rotate_Matrix[1][1] = CosZ;
}
/*类同*/
void Set_Transist_Matrix(float X, float Y,float Z) {
Transist_Matrix[3][0] = X;
Transist_Matrix[3][1] = Y;
Transist_Matrix[3][2] = Z;
}
/*类同*/
void Set_Perspective_Projection(float k) {
Perspective_Projection[2][3] = -1/k;
}
/*初始化图形驱动*/
void InitGraph(void) {
int gd=DETECT,gm;
initgraph(&gd,&gm,"E:\\TC");
}
/*生成立方体*/
float *Cube(void) {
int i, j, k;
float *pPoints1, *pPoints2;
num = 0;
for( i = -50; i <= 50; i += 20)
for( j = -50; j <= 50; j += 20)
for( k = -50; k <= 50; k += 20)
num++;
pPoints1 = malloc( num * 4 * 4 );
pPoints2 = pPoints1;
for( i = -50; i <= 50; i += 20)
for( j = -50; j <= 50; j += 20)
for( k = -50; k <= 50; k += 20) {
*(pPoints1++) = i;
*(pPoints1++) = j;
*(pPoints1++) = k;
*(pPoints1++) = 1;
}
return pPoints2;
}
/*Functions used for drawing & Clearing*/
void Plot_NewPoints(float *pPoints) {
int i;
for(i=0;i<num;i++)
putpixel( (int) (*(pPoints+i*2)), (int) (*(pPoints+i*2+1)), 7);
}
void Clear_OldPoints(float *pPoints) {
int i;
for(i=0;i<num;i++)
putpixel( (int) (*(pPoints+i*2)), (int) (*(pPoints+i*2+1)), 0);
}
/*Function used for controlling*/
void Operate(int Switch, float *Ang_Rot_X, float *Ang_Rot_Y, float *Ang_Rot_Z,
float *X_Delta, float *Y_Delta, float *Z_Delta,float *Distance) {
switch(Switch) {
case X_axis_clkwise: (*Ang_Rot_X)--; break;
case X_axis_Cntclkwise: (*Ang_Rot_X)++; break;
case Y_axis_clkwise: (*Ang_Rot_Y)--; break;
case Y_axis_Cntclkwise: (*Ang_Rot_Y)++; break;
case Z_axis_clkwise: (*Ang_Rot_Z)--; break;
case Z_axis_Cntclkwise: (*Ang_Rot_Z)++; break;
case X_Delta_Plus: (*X_Delta)--; break;
case X_Delta_Minus: (*X_Delta)++; break;
case Y_Delta_Plus: (*Y_Delta)--; break;
case Y_Delta_Minus: (*Y_Delta)++; break;
case Z_Delta_Plus: (*Z_Delta)++; break;
case Z_Delta_Minus: (*Z_Delta)--; break;
case Distance_forward: (*Distance)++; break;
case Distance_Backward: (*Distance)--; break;
default: (*Ang_Rot_Y)++; break;
}
}
int main() {
int i, j, Key;
float *pMatrix1, *pMatrix2;
float *pBasePoints;
float *pPerspectivePoints;
float Ang_Rot_Xaxis, Ang_Rot_Yaxis, Ang_Rot_Zaxis;
float X_Delta, Y_Delta, Z_Delta;
float Distance;
clrscr();
InitGraph();
/*Varieties initialized*/
pBasePoints = Cube();
Ang_Rot_Xaxis = 0;
Ang_Rot_Yaxis = 0;
Ang_Rot_Zaxis = 0;
X_Delta = 0;
Y_Delta = 0;
Z_Delta = 0;
Distance = 200;
Key = 0;
while(Key != ESC) {
if( bioskey(1) )
Key = bioskey(0);
Operate(Key, &Ang_Rot_Xaxis, &Ang_Rot_Yaxis, &Ang_Rot_Zaxis,
&X_Delta, &Y_Delta, &Z_Delta, &Distance);
SetMatrix_X(Ang_Rot_Xaxis);
SetMatrix_Y(Ang_Rot_Yaxis);
SetMatrix_Z(Ang_Rot_Zaxis);
Set_Transist_Matrix(X_Delta, Y_Delta, Z_Delta);
Set_Perspective_Projection(Distance);
/*The following may be known by you
pay your attention specially to the pair of malloc & free */
pMatrix1 = Matrix_Mul( (float*)X_Rotate_Matrix, 4, 4, (float*)Y_Rotate_Matrix, 4, 4);
pMatrix2 = Matrix_Mul( pMatrix1, 4, 4, (float*)Z_Rotate_Matrix, 4, 4);
free(pMatrix1);
pMatrix1 = Matrix_Mul( pMatrix2, 4, 4, (float*)Transist_Matrix, 4, 4);
free(pMatrix2);
pMatrix2 = Matrix_Mul( pMatrix1, 4, 4, (float*)Perspective_Projection, 4, 4);
free(pMatrix1);
pMatrix1 = Matrix_Mul( pBasePoints, num, 4, pMatrix2, 4, 4);
free(pMatrix2);
Matrix_Convertion( pMatrix1, num);
pPerspectivePoints = Get_X_Y(pMatrix1, num);
Plot_NewPoints(pPerspectivePoints);
delay(5000);
Clear_OldPoints(pPerspectivePoints);
free(pPerspectivePoints);
free(pMatrix1);
}
free(pBasePoints);
closegraph();
return 0;
}
B. c语言矩阵
int
a[3][4]={{1,3,5,7},{2,4,6,8},{15,17,34,12}};这完全是举一个例子而已,没特别的意思,这用的是一个二维数组,可用循环求出最大值tc中显示不出汉字没什么影响,win-tc有中文运行环境,不过有些amd的cpu不支持,如果用c-free或vc++6.0的话就可以显示了。
C. C语言关于矩阵
#include <stdio.h>
int main()
{
int a[2][3],b[3][2],c[2][2];
int i,k,g;
for(i=0;i<2;i++)
for(k=0;k<2;k++)
c[i][k]=0;
for(i=0;i<2;i++)
for(k=0;k<3;k++)
{
scanf("%d",&a[i][k]);
b[k][i]=a[i][k];
}
for(i=0;i<2;i++)
{
for(k=0;k<2;k++)
{
for(g=0;g<3;g++)
c[i][k]+=(a[i][g]*b[g][k]);
printf("%d ",c[i][k]);
}
printf("\n");
}
return 0;
}
D. c语言 编写一个程序输入行数(列数)n,输出如下正方形矩阵
二维数组可以的,三元组也行
E. C语言 关于矩阵
方法1:直接定义10*10的数组,读取全部数据,再根据选择的行列数打印输出部分数据。
方法2:通过文件流指针的移动,跨过不需要的内容。(这里控制文件流指针可利用ftell和fseek函数来移动指定字节数)我是利用fscanf读取需要的内容,遇到需要跳行就用fgets来跳过。(注意常量参数根据自己需求修改)。
说明:边读取边打印输出,就不需要变量存储。我的代码是把读取出来的内容存放在三维数组中(文件内容你没指定,如是单纯数字,二维数组即可,我是作为字符串处理,故用三维数组。),数组是根据实际大小,动态申请,写成两个独立函数。
下面是代码:
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#define MAXR 10//文件中矩阵最大行
#define MAXC 10//文件中矩阵最大列
#define MS 3//矩阵中元素字符串最大字符数(包含结束符号,不可小于实际内容)
#define MBUF 100//文件中一行最大字符数,可设置大一点
char ***getMem(int r,int c,int len);//动态申请一个三维数组
void freeMem(char ***datas,int r,int c);//释放数组空间
void showDatas(char ***datas,int r,int c);//打印输出结果
int main()
{
int r=MAXR+1,c=MAXC+1,i=0,j=0;
char ***datas=NULL,str[3],buf[MBUF];
FILE *fp=NULL;
printf("请输入要获取的行数和列数: ");
while(r<0 || r>MAXR || c<0 || c>MAXC)scanf("%d%d",&r,&c);
datas=getMem(r,c,MS);
fp=fopen("C:\test.data","r");
if(!fp) return 1;
while(fscanf(fp,"%s",str)!=-1)
{
if(i<c) strcpy(datas[j][i],str),i++;
if(i==c)
{
if(!fgets(buf,sizeof(buf),fp)) break;
i=0,j++;
}
if(j==r) break;
}
showDatas(datas,r,c);
freeMem(datas,r,c);
return 0;
}
void showDatas(char ***datas,int r,int c)
{
int i,j;
for(i=0;i<r;i++,printf(" "))
for(j=0;j<c;j++)
printf("%s ",datas[i][j]);
printf(" ");
}
void freeMem(char ***datas,int r,int c)
{
int i,j;
if(!datas)
{
for(i=0;i<r;i++,free(datas[i]))
for(j=0;j<c;j++)
free(datas[i][j]);
free(datas);
}
}
char ***getMem(int r,int c,int len)
{
int i,j;
char ***datas=NULL,**dr=NULL;
datas=(char ***)malloc(sizeof(char **)*r);
if(!datas) return NULL;
for(i=0;i<r;i++)
{
dr=(char **)malloc(sizeof(char *)*c);
if(!dr) return NULL;
else
{
for(j=0;j<c;j++)
{
dr[j]=(char *)malloc(sizeof(char)*len);
if(!dr[j]) return NULL;
}
datas[i]=dr;
}
}
return datas;
}
F. 如何用C语言定义矩阵
矩阵其实就是二维数组,在进行编码的时候,矩阵就会被定义成为二维数组
G. C语言中如何定义矩阵
两种方式可以参考:
1、最简单的就是二维数组,比如存储全是整形的一个m*n的矩阵。然后可以定义int a[m][n]。
输入或者输出可以用两层循环来完成,外层控制行m比如for(i=0;i<m;++i),内层控制列n比如for(j=0;j<n;++j);
2、第二种方式就是压缩矩阵进行存储,如果学了数据结构应该比较好理解。
结构体进行封装,比如:
第一步:先定义一个有效数据的位置
typedef struct node
{
int hang;int lie;int data;//用来存储一个有效数据位的行列和值
}node;
typedef struct matrix
{
node *m;//一个数组,用来存储所有的node数据
int sum;//记录一共有多少个有效数据位
}matrix;
H. 怎样用C语言写矩阵
用二维数组,如下:
#include <stdio.h>
main()
{
int i, j, a[4][4];
for ( i = 1; i < 4; i++ )
{
for ( j = 1; j < 4; j++ )
{
scanf ("%d", &a[i][j]);
}
}
//这样就可以将一个3*3 的矩阵存在2维数组中了
for ( i = 1; i < 4; i++ )
{
for ( j = 1; j < 4; j++ )
{
printf (" %-4d ", a[i][j]);
}
printf ("\n");
}
//这样就可以显示矩阵
return 0;
}
I. 用c语言编写一个简单的立方体
#include<stdio.h>
#include"graphics.h" /*画图需要的*/
void main()
{
int graphdriver=0;
int graphmode=0;
initgraph(&graphdriver,&graphmode,"");
cleardevice(); /*以上是图形驱动*/
setbkcolor(1); /*设置背景颜色*/
setcolor(RED); /*设置前景颜色*/
setfillstyle(1,3); /*设置颜色填充模式和颜色*/
bar3d(100,200,400,350,100,1); /*画立方体和填充正面*/
floodfill(450,300,WHITE);
floodfill(250,150,WHITE);
getch();
closegraph();
}
以上是一个简单的长方体,立方体是一个道理,根据你自己要求改嘛
可以参考C语言的高级编程