پاسخ : سورس های نوشته شده به زبان C و ++C بارگذاری فایل های 8 بیت و 24 بیت RGB با پسوند bmp را در ++C کد: #include <iostream> #include <fstream> #include <memory.h> #define IMG_OK 0x1 #define IMG_ERR_NO_FILE 0x2 #define IMG_ERR_MEM_FAIL 0x4 #define IMG_ERR_BAD_FORMAT 0x8 #define IMG_ERR_UNSUPPORTED 0x40 class BMPImg { public: BMPImg(); ~BMPImg(); int Load(char* szFilename); int GetBPP(); int GetWidth(); int GetHeight(); unsigned char* GetImg(); // Return a pointer to image data unsigned char* GetPalette(); // Return a pointer to VGA palette private: unsigned int iWidth,iHeight,iEnc; short int iBPP,iPlanes; int iImgOffset,iDataSize; unsigned char *pImage, *pPalette, *pData; // Internal workers int GetFile(char* szFilename); int ReadBmpHeader(); int LoadBmpRaw(); int LoadBmpRLE8(); int LoadBmpPalette(); void FlipImg(); // Inverts image data, BMP is stored in reverse scanline order }; BMPImg::BMPImg() { pImage=pPalette=pData=NULL; iWidth=iHeight=iBPP=iPlanes=iEnc=0; } BMPImg::~BMPImg() { if(pImage) { delete [] pImage; pImage=NULL; } if(pPalette) { delete [] pPalette; pPalette=NULL; } if(pData) { delete [] pData; pData=NULL; } } int BMPImg::Load(char* szFilename) { int iRet; // Clear out any existing image and palette if(pImage) { delete [] pImage; pImage=NULL; } if(pPalette) { delete [] pPalette; pPalette=NULL; } // Get the file into memory iRet=GetFile(szFilename); if(iRet!=IMG_OK) return iRet; // Process the header iRet=ReadBmpHeader(); if(iRet!=IMG_OK) return iRet; if(iBPP<8) // We'll only bother with 8 bit and above return IMG_ERR_UNSUPPORTED; // Get the image data switch(iEnc) { case 0: // Uncompressed iRet=LoadBmpRaw(); // 8 / 24 Bit. (24 bit is in BGR order) break; case 1: // RLE 8 (Indexed 256 colour only) iRet=LoadBmpRLE8(); break; case 2: // RLE 4 (16 Colour indexed, Outdated, not covered here) return IMG_ERR_UNSUPPORTED; case 3: // Bitfields (16/32 bit only, Rare, not covered here) return IMG_ERR_UNSUPPORTED; default: return IMG_ERR_UNSUPPORTED; } if(iRet!=IMG_OK) return iRet; // Flip image to correct scanline reversal FlipImg(); // Load palette if present iRet=LoadBmpPalette(); if(iRet!=IMG_OK) return iRet; // Free the file data delete [] pData; pData=NULL; return IMG_OK; } int BMPImg::GetFile(char* szFilename) { using namespace std; ifstream fIn; unsigned long ulSize; // Open the specified file fIn.open(szFilename,ios::binary); if(fIn==NULL) return IMG_ERR_NO_FILE; // Get file size fIn.seekg(0,ios_base::end); ulSize=fIn.tellg(); fIn.seekg(0,ios_base::beg); // Allocate some space // Check and clear pDat, just in case if(pData) { delete [] pData; pData=NULL; } pData=new unsigned char[ulSize]; if(pData==NULL) { fIn.close(); return IMG_ERR_MEM_FAIL; } // Read the file into memory fIn.read((char*)pData,ulSize); fIn.close(); return IMG_OK; } int BMPImg::ReadBmpHeader() { int iInfo; if(pData==NULL) return IMG_ERR_NO_FILE; if(pData[0x0]!='B' || pData[0x1]!='M') // BMP ID Bytes, should be 'BM' return IMG_ERR_BAD_FORMAT; memcpy(&iImgOffset,&pData[0xA],4); // Offset to image data memcpy(&iInfo,&pData[0xE],4); // Info header size, should be 0x28 if(iInfo!=0x28) return IMG_ERR_BAD_FORMAT; memcpy(&iWidth,&pData[0x12],4); // Image width memcpy(&iHeight,&pData[0x16],4); // Image height memcpy(&iPlanes,&pData[0x1A],2); // Colour planes memcpy(&iBPP,&pData[0x1C],2); // BPP memcpy(&iEnc,&pData[0x1E],4); // Encoding iDataSize=(iWidth*iHeight*(iBPP/8)); // Calculate Image Data size return IMG_OK; } int BMPImg::LoadBmpRaw() { if(pImage) { delete [] pImage; pImage=NULL; } // Allocate space for the image data pImage=new unsigned char[iDataSize]; if(pImage==NULL) return IMG_ERR_MEM_FAIL; memcpy(pImage,&pData[iImgOffset],iDataSize); return IMG_OK; } int BMPImg::LoadBmpRLE8() { unsigned char bOpCode,bVal; unsigned char *pSrc; int iDcode=1,iCount,iPos,iIndex; // Allocate space for the image if(pImage) delete [] pImage; pImage=new unsigned char[iDataSize]; if(pImage==NULL) return IMG_ERR_MEM_FAIL; // Get the start of the RLE data pSrc=&pData[iImgOffset]; iPos=0; iIndex=0; while(iDcode) { // Stay on even bytes while(iPos%2) { iPos++; } bOpCode=pSrc[iPos]; bVal=pSrc[iPos+1]; iPos+=2; if(bOpCode>0) // Run mode, Repeat 'bVal' 'OpCode' times { for(iCount=0;iCount!=bOpCode;iCount++) { pImage[iIndex]=bVal; ++iIndex; } } else // Absolute Mode (Opcode=0), various options { switch(bVal) { case 0: // EOL, no action break; case 1: // EOF, STOP! iDcode=0; break; case 2: // Reposition, Never used break; default: // Copy the next 'bVal' bytes directly to the image for(iCount=bVal;iCount!=0;iCount--) { pImage[iIndex]=pSrc[iPos]; ++iIndex; ++iPos; } break; } } if(iIndex>iDataSize) // Stop if image size exceeded. iDcode=0; } return IMG_OK; } int BMPImg::LoadBmpPalette() { int iIndex; unsigned char *pPalPos, *pDatPos; if(pPalette) { delete [] pPalette; pPalette=NULL; } if(iBPP>8) // NULL Palette for RGB images return IMG_OK; // Create space for palette pPalette=new unsigned char[768]; if(pPalette==NULL) return IMG_ERR_MEM_FAIL; // Set starting position for pointers pPalPos=pPalette; pDatPos=&pData[0x36]; // Get colour values, skip redundant 4th value for(iIndex=0;iIndex!=256;++iIndex) { pPalPos[0]=pDatPos[2]; // Red pPalPos[1]=pDatPos[1]; // Green pPalPos[2]=pDatPos[0]; // Blue pPalPos+=3; pDatPos+=4; } return IMG_OK; } void BMPImg::FlipImg(void) { unsigned char bTemp; unsigned char *pLine1, *pLine2; int iLineLen,iIndex; iLineLen=iWidth*(iBPP/8); pLine1=pImage; pLine2=&pImage[iLineLen * (iHeight - 1)]; for( ;pLine1<pLine2;pLine2-=(iLineLen*2)) { for(iIndex=0;iIndex!=iLineLen;pLine1++,pLine2++,iI ndex++) { bTemp=*pLine1; *pLine1=*pLine2; *pLine2=bTemp; } } } int BMPImg::GetBPP() { return iBPP; } int BMPImg::GetWidth() { return iWidth; } int BMPImg::GetHeight() { return iHeight; } unsigned char* BMPImg::GetImg() { return pImage; } unsigned char* BMPImg::GetPalette() { return pPalette; }
پاسخ : سورس های نوشته شده به زبان C و ++C رسم دو لوزی در کنار هم کد: #include <iostream> #include <conio> void fasele(int n){ for(int i=1;i<=n;i++) cout<<" "; return; } //--------------------- int main(){ int n; cout<<"Enter n\n"; cin>>n; int a=n,b=n-1,c=n-1; for(int i=1;i<=2*n-1;i+=2){ fasele(a); for(int j=1;j<=i;j++) cout<<"*"; fasele(b); fasele(c); for(int k=1;k<=i;k++){ cout<<"*";} cout<<endl; a--; b--; c--; } int d=2,e=1,f=1; for(int i=2*n-3;i>=1;i-=2){ fasele(d); for(int j=1;j<=i;j++) cout<<"*"; fasele(e); fasele(f); for(int k=1;k<=i;k++) cout<<"*"; cout<<endl; e++; f++; d++; } cout<<"---------------------------------------------------\n\n"; cout<<" Designer : Salar Ashgi\n"; getch(); }
پاسخ : سورس های نوشته شده به زبان C و ++C پیدا کردن ورژن سیستم عامل کد: #include <iostream> #include <conio> #include <stdlib> int main(){ system("ver"); getch(); }
پاسخ : سورس های نوشته شده به زبان C و ++C یک کد برای ترتیب نزولی بر اساس معدل دانشجو #include <stdio.h> #include <conio.h> #include <string.h> #define N 4 main() { int ave[N],i,j,t; char name[N][20],str[20]; for(i=0;i<N;i++) { printf("Enter Name[%d]: ",i+1); scanf("%s",name); printf("Enter Average[%d]: ",i+1); scanf("%d",&ave); } for(j=N;j>0;j--) for(i=0;i<j-1;i++) { if(ave<ave[i+1]) { t=ave; ave=ave[i+1]; ave[i+1]=t; strcpy(str,name); strcpy(name,name[i+1]); strcpy(name[i+1],str); } } printf("\nSorted name acording to ave:\n"); for(i=0;i<N;i++) { printf("%s has average %d \n",name,ave ); } getch(); }
پاسخ : سورس های نوشته شده به زبان C و ++C چاپ مثلث کد: #include<iostream.h> #include<conio.h> int main() { int n,i,j; cout<< "Enter num:"; cin>> n; for(i = n; i >= 1; --i){ for(j = 1; j <= i; ++j) cout<< '*'; cout<< '\n'; } getch(); return 0; }
پاسخ : سورس های نوشته شده به زبان C و ++C یک منوی ساده در عین حال زیبا کد: #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <dos.h> #include <graphics.h> //Menu Global Item #define pixTOrc(x) (8*(x-1)) //convert pixel into row and col format #define INC 5 //Increment Distance Between Menu Items #define ROW 15 //Row Value for Menu Item #define COL 8 //Column Value for Menu Item #define MAXITEM 5 //Total menu items // To display the Inventory Main menu options typedef char option[15]; option mainMenu[]= { "NEW", "OPEN", "SAVE", "ABOUT ME", "CLOSE" }; // Function to displays all the menu prompt messages from the pointer array of option a[] void normalvideo(int x,int y,char *str) { x=pixTOrc(x); y=pixTOrc(y); outtextxy(x,y,str); } // Function to move the cursor on the menu prompt with a reverse video color void reversevideo(int x,int y,char *str) { x=pixTOrc(x); y=pixTOrc(y); setcolor(YELLOW); //Selected Item sound(400); delay(100); nosound(); outtextxy(x,y,str); setcolor(WHITE); //Unselected Item sound(500); delay(100); nosound(); } //Keep Track of which arrow key is pressed char menu() { int i,done; settextstyle(TRIPLEX_FONT,HORIZ_DIR,3); setcolor(WHITE); //Initial Menu Item Color for(i = 1; i < MAXITEM; i++) normalvideo(COL, (i*INC)+ROW, mainMenu); reversevideo(COL,ROW, mainMenu[0]); i = done = 0; do { int key; /**Status Bar Logic**/ //Message will be displayed as status bar guide-line setfillstyle(SOLID_FILL,BLUE); settextstyle(SMALL_FONT,HORIZ_DIR,5); bar(pixTOrc(2),pixTOrc(52.5),pixTOrc(75),pixTOrc(55)); setcolor(LIGHTCYAN); switch(i){ case 0 : outtextxy(pixTOrc(5),pixTOrc(52.75),"New --> Create New file"); break; case 1 : outtextxy(pixTOrc(5),pixTOrc(52.75),"Open --> Open Existing file"); break; case 2 : outtextxy(pixTOrc(5),pixTOrc(52.75),"Save --> Save file"); break; case 3 : outtextxy(pixTOrc(5),pixTOrc(52.75),"About Me --> Programmer : Vivek Patel"); break; case 4 : outtextxy(pixTOrc(5),pixTOrc(52.75),"Close the Program --> BYE C U"); break; } /**status Bar ends**/ //Restore Orignal Color and Font Setting setcolor(WHITE); settextstyle(TRIPLEX_FONT,HORIZ_DIR,3); key = getch(); switch (key) { case 00: key = getch(); switch (key) { case 72: normalvideo(COL, (i*INC)+ROW, mainMenu); i--; if (i == -1) i = MAXITEM-1; reversevideo(COL,(i*INC)+ROW,mainMenu); break; case 80: normalvideo(COL, (i*INC)+ROW, mainMenu); i++; if (i == MAXITEM) i = 0; reversevideo(COL, (i*INC)+ROW, mainMenu); break; } break; case 13: done = 1; } } while (!done); return(i+49); } //Advertise Screen will displayed to utilize empty screen area //It can be utilize for some effective...work void advertise(){ setcolor(BLUE); outtextxy(pixTOrc(30),pixTOrc(20),"URL : cpp.blogfa.com"); outtextxy(pixTOrc(30),pixTOrc(26),"Mail : cpp.myblog@gmail.com"); setcolor(YELLOW); } /* The function is used to display the main menu*/ //Actual code for all the menu utility resides in this //Function... void control_menu() { char choice; do { choice = menu(); switch (choice) { case '1': //New setcolor(BLUE); outtextxy(pixTOrc(40),pixTOrc(15),"New"); advertise(); getch(); setfillstyle(SOLID_FILL,LIGHTGRAY); bar(pixTOrc(2),pixTOrc(14),pixTOrc(75),pixTOrc(50)); advertise(); break; case '2': //Open setcolor(BLUE); outtextxy(pixTOrc(40),pixTOrc(15),"Open"); advertise(); getch(); setfillstyle(SOLID_FILL,LIGHTGRAY); bar(pixTOrc(2),pixTOrc(14),pixTOrc(75),pixTOrc(50)); advertise(); break; case '3': //Save setcolor(BLUE); outtextxy(pixTOrc(40),pixTOrc(15),"Save"); advertise(); getch(); setfillstyle(SOLID_FILL,LIGHTGRAY); bar(pixTOrc(2),pixTOrc(14),pixTOrc(75),pixTOrc(50)); advertise(); break; case '4': //Modify the status of item in inventory setcolor(BLUE); outtextxy(pixTOrc(40),pixTOrc(15),"About Me"); advertise(); getch(); setfillstyle(SOLID_FILL,LIGHTGRAY); bar(pixTOrc(2),pixTOrc(14),pixTOrc(75),pixTOrc(50)); advertise(); break; case '5': //Close the program setcolor(BLUE); outtextxy(pixTOrc(40),pixTOrc(15),"CLOSE"); advertise(); delay(1000); setfillstyle(SOLID_FILL,LIGHTGRAY); bar(pixTOrc(2),pixTOrc(14),pixTOrc(75),pixTOrc(50)); advertise(); goto out; } } while (choice != MAXITEM); out: } void main() { int i,j; int gd=DETECT,gm=0; initgraph(&gd,&gm,"c:\\tc\\bgi\\"); ///code as space holder\\\\\ setfillstyle(SOLID_FILL,LIGHTGRAY); bar(0,0,640,480); setcolor(DARKGRAY); rectangle(0,0,639,480); rectangle(1,1,638,479); setcolor(BLACK); rectangle(2,1,637,478); rectangle(3,1,636,477); settextstyle(TRIPLEX_FONT,HORIZ_DIR,4); setcolor(BLUE); //outtextxy(pixTOrc(,pixTOrc(2)," MENU -->> (Simple Style) "); setfillstyle(HATCH_FILL,DARKGRAY); for(i=15,j=70;i<40||j>40;i++,j--){ bar(pixTOrc(j),pixTOrc(7),pixTOrc(i),pixTOrc(7.5)); delay(10); bar(pixTOrc(j),pixTOrc(7),pixTOrc(70),pixTOrc(7.5)); delay(20); } bar(pixTOrc(7),pixTOrc(14),pixTOrc(25),pixTOrc(50)); ///code as space holder\\\\\ //Calling Menu control_menu(); closegraph(); }</pre>
پاسخ : سورس های نوشته شده به زبان C و ++C سورس تبدیل عبارت میانوندی (Infix) به پسوندی (Postfix) کد: #include <stdio.h> #include <conio.h> char A[100]; int top=-1; int k =0; void push(char x){ if(top<99) A[++top]=x; } char pop(){ if(top>-1) return A[top--]; return 0; } char peak(){ if(top>-1) return A[top]; return 0; } int operand(char x){ //if('A'<x<'Z'||'a'<x<'z') for(char ch = 'a'; ch < 'z'; ch++) if(x == ch) return 1; return 0; } void infix2postfix(char *infixexp,char *postfixexp) { push('#'); for(int i=strlen(infixexp),k=0;i > 0;i--){ if(infixexp==')'){ //true push(')'); continue; } if(isoperand(infixexp)){ //true postfixexp[k++]=infixexp; continue; } if(infixexp=='('){ //true while(peak()!=')') postfixexp[k++]=pop(); pop(); continue; } if(!operand(infixexp)){ push(infixexp); continue; } } while(peak()!='#') postfixexp[k++]=pop(); postfixexp[k]=0; } int main() { char infix[100]; printf("Please Enter A infix Expression.\n"); scanf("%s",infix); char postfix[100]; infix2postfix(infix,postfix); printf("\nThe Postfix Form is : %s",postfix); getch(); }
پاسخ : سورس های نوشته شده به زبان C و ++C 4 عمل اصلی چند جمله ای ها ++C کد: #include<conio.h> #include<iostream.h> #define Max 20 class Poly; class PolyNode { friend Poly; float Coef; int Pow; }; class Poly { int n; PolyNode Data[Max]; public: void Poly::SortPoly(void); void ReadPoly(void); void WritePoly(void); void AddPoly(Poly a,Poly b); void SubtractPoly(Poly a,Poly b); void MulPoly(Poly a,Poly b); void Poly::ItemPoly(float Coef,int Pow,Poly b); void Poly::TaghsimPoly(int k,Poly a,Poly b); }; void Poly::SortPoly(void) { int i,j; PolyNode item; for(i=n-1;i>0;i--) for(j=0;j<i;j++) if(Data[j].Pow<Data[j+1].Pow) { item=Data[j]; Data[j]=Data[j+1]; Data[j+1]=item; } while(i<n-1) if(Data.Pow==Data[i+1].Pow) { Data.Coef+=Data[i+1].Coef; for(j=i+1;j<n-1;j++)Data[j]=Data[j+1]; n--; }else i++; } void Poly::ReadPoly(void) { int i; cout<<"\nPlease enter parts of poly : "; cin>>n; cout<<"\nPlease enter Polynomial : \n\n"; for(i=0;i<n;i++) { cin>>Data.Coef; cout<<" x^"<<endl; cin>>Data.Pow; cout<<" +"<<endl; } } void Poly::WritePoly(void) { int i; cout<<"\n"; for(i=0;i<n;i++)cout<<Data.Coef<<"X^"<<Data.Pow<<" + "; } void Poly::AddPoly(Poly a,Poly b) { int i,j,k; i=j=k=0; while(i<a.n&&j<b.n) { if(a.Data.Pow>b.Data[j].Pow) { Data[k].Coef=a.Data.Coef; Data[k++].Pow=a.Data[i++].Pow; } else if(a.Data.Pow<b.Data[j].Pow) { Data[k].Coef=b.Data[j].Coef; Data[k++].Pow=b.Data[j++].Pow; } else if(a.Data.Coef+b.Data[j].Coef) { Data[k].Coef=a.Data.Coef+b.Data[j].Coef; Data[k++].Pow=a.Data[i++].Pow; j++; } else { i++; j++; } } while(i<a.n) { Data[k].Coef=a.Data.Coef; Data[k++].Pow=a.Data[i++].Pow; } while(j<b.n) { Data[k].Coef=b.Data[j].Coef; Data[k++].Pow=b.Data[j++].Pow; } n=k; } void Poly::SubtractPoly(Poly a,Poly b) { int i,j,k; i=j=k=0; while(i<a.n&&j<b.n) { if(a.Data.Pow>b.Data[j].Pow) { Data[k].Coef=a.Data.Coef; Data[k++].Pow=a.Data[i++].Pow; } else if(a.Data.Pow<b.Data[j].Pow) { Data[k].Coef=b.Data[j].Coef; Data[k++].Pow=b.Data[j++].Pow; } else if(a.Data.Coef-b.Data[j].Coef) { Data[k].Coef=a.Data.Coef-b.Data[j].Coef; Data[k++].Pow=a.Data[i++].Pow; j++; } else { i++; j++; } } while(i<a.n) { Data[k].Coef=a.Data.Coef; Data[k++].Pow=a.Data[i++].Pow; } while(j<b.n) { Data[k].Coef=b.Data[j].Coef; Data[k++].Pow=b.Data[j++].Pow; } n=k; } void Poly::MulPoly(Poly a,Poly b) { int i,j,k; i=j=k=0; while(i<a.n) { j=0; while(j<b.n) { Data[k].Coef=a.Data.Coef*b.Data[j].Coef; Data[k++].Pow=a.Data.Pow+b.Data[j++].Pow; } i++; } n=k; } void Poly::ItemPoly(float Coef,int Pow,Poly b) { int j,t; j=t=0; while(j<b.n) { Data[t].Coef=Coef*b.Data[j].Coef; Data[t++].Pow=Pow+b.Data[j++].Pow; } n=t; } void Poly::TaghsimPoly(int k,Poly a,Poly b) { Poly d,e; p: if(a.Data[0].Pow>=b.Data[0].Pow) { Data[k].Pow=a.Data[0].Pow-b.Data[0].Pow; Data[k].Coef=a.Data[0].Coef/b.Data[0].Coef; d.ItemPoly(Data[k].Coef,Data[k].Pow,b); e.SubtractPoly(a,d); k++; } n=k; if(e.Data[0].Pow>=b.Data[0].Pow)TaghsimPoly(k,e,b); else { cout<<"\nMod Is : \n"; e.WritePoly(); } } void main(void) { Poly a,b,c; char Sign; a.ReadPoly(); a.SortPoly(); cout<<"\nPlease enter sign(+,-,*,/) : "; cin>>Sign; b.ReadPoly(); b.SortPoly(); switch(Sign) { case'+': c.AddPoly(a,b); break; case'-': c.SubtractPoly(a,b); break; case'*': c.MulPoly(a,b); c.SortPoly(); break; case'/': c.TaghsimPoly(0,a,b); break; } cout<<"\n\nAnswer Is : \n"; c.WritePoly(); getche();
پاسخ : سورس های نوشته شده به زبان C و ++C pop double stack کد: #include <stdio.h> #define MAXSIZE 100 int sp = 0; double stack[MAXSIZE]; void push(double f) { if (sp < MAXSIZE) stack[sp++] = f; else printf("error: stack is full\n"); } double pop() { if (sp > 0) return stack[--sp]; else { printf("error: stack is empty\n"); return 0; } }
پاسخ : سورس های نوشته شده به زبان C و ++C محل شروع یک رشته را در رشته دیگر پیدا میکند . کد: #include <conio.h> #include <iostream.h> #include <stdio.h> void main() { char s1[100],s2[100]; clrscr(); cout<<"reshteh aval ra vared konid :"<<endl; gets(s1); cout<<"reshteh dovom ra vared konid :"<<endl; gets(s2); for(int k=0;s2[k];k++); for(int i=0;s1;i++){ for(int p=i,pp=0;s2[pp]&&(s1[p]==s2[pp]);p++,pp++); if(pp==k) cout<<"mahale shoro reshteye dovom "<<i+1<<endl;} getch();}