Tboy Cá voi
Tổng số bài gửi : 178 Cảm ơn : 413 Danh vọng : 4 Join date : 03/01/2011 Age : 32 Đến từ : TPHCM
| Tiêu đề: Robot- (Mảng 2 chiều-Tìm kiếm) Thu Jan 27, 2011 6:28 pm | |
| | Thu Jan 27, 2011 6:28 pm Robot- (Mảng 2 chiều-Tìm kiếm) | | | - Code:
-
//CHUONG TRINH ROBOT #include <stdio.h> #include <conio.h> #define MaxA 100 #define MaxB 200 //========================Cac ham trong chuong trinh============================ void Nhapmatran(int Matran[][MaxA],int Dong,int Cot); void Xuatmatran(int Matran[][MaxA],int Dong,int Cot); void Nhaplenh(int Lenh[],int N); void Xuatlenh(int Lenh[],int N); void Tong(int Matran[][MaxA],int Lenh[],int Dong,int Cot,int N); //============================Chuong trinh chinh================================ void main() { int dong,cot,a[MaxA][MaxA],b[MaxB],n; //=========================Nhap va xuat ma tran================================ do { printf("Nhap vao so dong va cot cua ma tran: "); scanf("%d%d",&dong,&cot); }while(dong<1||cot<1||dong>MaxA||cot>MaxA); Nhapmatran(a,dong,cot); printf("Ma tran vua nhap: \n"); Xuatmatran(a,dong,cot); //======Gioi thieu cac lenh cua chuong trinh.Nhap va xuat day lenh nhap vao===== printf("Gioi thieu dieu khien duong di cua Robot:\n"); printf("[1] la Robot di qua phai\n"); printf("[2] la Robot di xuong\n"); printf("[3] la Robot di qua trai\n"); printf("[4] la Robot di len\n"); do { printf("Hay nhap vao tong so lenh ban se thuc hien doi voi Robot: "); scanf("%d",&n); }while(n<1||n>MaxB); Nhaplenh(b,n); printf("Day lenh chuong trinh:("); Xuatlenh(b,n); //===========In ra man hinh cac vi tri Robot di qua va tinh tong================ Tong(a,b,dong,cot,n); getch(); } void Nhapmatran(int Matran[][MaxA],int Dong,int Cot) { for(int i=0;i<Dong;i++) for(int j=0;j<Cot;j++) { printf("A[%d][%d]= ",i+1,j+1); scanf("%d",&Matran[i][j]); } } void Xuatmatran(int Matran[][MaxA],int Dong,int Cot) { for(int i=0;i<Dong;i++) { for(int j=0;j<Cot;j++) printf("%5d ",Matran[i][j]); printf("\n\n"); } } void Nhaplenh(int Lenh[],int N) { for(int i=0;i<N;i++) do { printf("Lenh thu %d: ",i+1); scanf("%d",&Lenh[i]); if(Lenh[i]<1||Lenh[i]>4) printf("So ban nhap phai nam trong doan [1..4]\n"); }while(Lenh[i]<1||Lenh[i]>4); } void Xuatlenh(int Lenh[],int N) { for(int i=0;i<N;i++) { printf(" %d",Lenh[i]); if(i<N-1) printf(","); else printf(")"); } } void Tong(int Matran[][MaxA],int Lenh[],int Dong,int Cot,int N) { int i=0,j=0,s=Matran[i][j]; // vi tri bat dau cua Robot la (0,0) nen i,j=0 printf("\nKet qua:Tong bang %d ",Matran[i][j]); for(int k=0;k<N;k++) { switch(Lenh[k]) { case 1: j+=1; break; case 2: i+=1; break; case 3: j-=1; break; case 4: i-=1; break; } if(i<0) i=0; else if(i==Dong) i=Dong-1; if(j<0) j=0; else if(j==Cot) j=Cot-1; printf("+ %d",Matran[i][j]); s+=Matran[i][j]; } printf(" = %d\n",s); } Tboy
| | | | | |
|