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語言的高級編程