پاسخ : پروژه هاي برنامه نويسي
وارد کردن اطلاعات دانشجویان؛جستجو بر اساس دو فیلد؛خذف دانشجو بر اساس دو فیلد؛ذحیره اطلاعات در فایل؛مرتب کردن اسامی بر اساس الفبای لاتین و.....
کد PHP:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include<string.h>
#define MAX 100
struct student {
char name[10] ;
float avrage ;
int unit ;
int amount_course ;
int student_number;
} st[MAX];
void first_evalution(void);
void enter(void);
void search(void);
void search2(void);
void del_name(int);
void display(void);
void top_student();
void fail_student();
void show(int *, int);
void title(int *);
int menu_select(void);
int spare_space(void) ;
void save (void);
void load(void);
void in_style(int l);
void delete_main(void);
void delete_name2(void);
void search_main(void);
static int l;
int main(void)
{
char choice ,ans;
first_evalution();
for(;;){
choice = menu_select() ;
switch(choice) {
case 1: enter();in_style(l); break;
case 2: search_main(); break ;
case 3: top_student(); break;
case 4: fail_student(); break ;
case 5: delete_main(); break ;
case 6: save();break;
case 7: load();break;
case 8: display();break;
case 9:{
printf("are you sure you want to quit ?(y/n)");
ans=getche();
clrscr();
if(ans=='y')
exit(0);break; }
}
}
}
void first_evalution(void)
{
register int i ;
for(i = 0; i < MAX; ++i)
st[i].name[0] = '\0' ;
}
int menu_select(void)
{
char s[10];
int g ;
printf("\n\t(1) << enter information >>");
printf("\n\t(2) << search menu >>");
printf("\n\t(3) << show top students >>");
printf("\n\t(4) << show fail students >>");
printf("\n\t(5) << delete menu >>");
printf("\n\t(6)<< save info >>");
printf("\n\t(7)<< load info >>");
printf("\n\t(8)<< display info >>");
printf("\n\t(9)<< quit from program >>");
do {
printf("\n\t Please enter your choice(1-9): ") ;
gets(s);
clrscr();
g = atoi(s);
} while(g < 0 || g > 9) ;
return (g) ;
}
void enter(void)
{
char name3[30];
float grade, sum_grade = 0;
int t, unit, sum_unit = 0 ,j,i;
t = spare_space() ;
if(t == -1){
printf("\n sorry the list is full.....press any key to continue");
getch();
return ;
}
printf("\n\t enter name:") ;
gets(st[t].name) ;
strcpy(name3,st[t].name);
for(i=0;i<t && st[i].name[0] ;i++)
{
if(strcmp(st[i].name,name3)==0){
printf("the similar name is already in the list. contine any way?(y,n)");
if(getche()=='n')
{
clrscr();
st[t].name[0]='\0';
return ;}}
}
l++;
clrscr();
printf("\n\tenter student number:") ;
scanf("%d", &st[t].student_number) ;
clrscr();
printf("\n\tenter number of course:") ;
scanf("%d", &st[t].amount_course) ;
clrscr();
for(j = 1; j <= st[t].amount_course; j++) {
printf("\n\tenter grade for course %d:",j);
scanf("%f", &grade) ;
clrscr();
printf("\n\tenter unit of course %d:", j);
scanf("%d", &unit) ;
clrscr();
sum_grade += grade * unit ;
sum_unit += unit ;
}
st[t].unit=sum_unit;
st[t].avrage = sum_grade / sum_unit ;
}
int spare_space(void)
{
register int i ;
for(i = 0; st[i].name[0] && i< MAX ; ++i);
if(i == MAX) return -1 ;
return i ;
}
void delete_main(void)
{
char c[10];
int h;
do {
printf("how do you want to delete info ?");
printf("\n\t\t\t(1)=base on name");
printf("\n\t\t\t(2)=base on student number");
gets(c);
clrscr();
h=atoi(c);} while(h<0 || h>2);
if(h==1){
delete_name2();}
if(h==2){
del_name(l);}
if(h!=1 && h!=2)
return;
}
void del_name(int l)
{
int i ;
char ans;
printf("\n\tenter record #(0 - %d):",l) ;
scanf("%d", &i) ;
printf("are u sure you want to delete record %d?(y/n)");
ans=getche();
clrscr();
if(ans=='y'){
if( st[i].name[0]){
st[i].name[0]='\0' ;
printf("deleted Successfully !!");}
else if(!st[i].name[0])
printf("this record is not in the list....press any key to continue...."); }
getch();
clrscr();
}
void delete_name2(void)
{
char name2[30],ans;
int i,h,w=0;
printf("\tenter a name to delete");
gets(name2);
printf("are you sure you want to delete %s information?(y/n)",name2);
ans=getche();
clrscr();
if(ans=='y'){
for(i=0;i<MAX;i++)
{
if(strcmp(st[i].name,name2)==0) {
h=i;
w=1; }}
if(w==1){
st[h].name[0]='\0' ;
printf("deleted Successfully !!");
}
if(w==0) {
printf("the one you are looking for is not in the list.press any key to continue.........."); }}
getch();
clrscr();
}
void display(void)
{
int i, r = 0;
;
title(&r) ;
for(i = 0; i < MAX; ++i)
if(st[i].name[0])
show(&r, i);
gotoxy(114,r);
printf("\n\t please press any key to continue ") ;
getch();
clrscr();
gotoxy(29,r+1);
getch();
}
void fail_student(void)
{
int i, r=0 ,a=0;
for(i = 0; i < MAX; ++i){
if(st[i].name[0] && st[i].avrage < 12){
a=1;
title(&r);
show(&r, i) ; }
}
if(a==0){
printf("there is no fail student!");}
gotoxy(25,r+1);
printf(" please press any key to continue ") ;
getch();
clrscr();
}
void top_student(void)
{
int i, r=0 ,a=0;
for(i = 0; i < MAX; ++i){
if(st[i].name[0] && st[i].avrage >= 17){
a=1;
title(&r);
show(&r, i) ; }
}
if(a==0){
printf("there is no top student!");}
gotoxy(25,r+1);
printf(" please press any key to continue ") ;
getch();
clrscr();
}
void title(int *r)
{
*r = 1;
gotoxy(25,*r);
printf(" << all the info in list are: >>");
gotoxy(6,*r+2);
printf(" name avrage ");
printf(" unit amount_course st_no ");
gotoxy(6,*r+3);
printf(" ________ ________ ");
printf(" ______ ______ ____ ");
*r = 4;
}
void show(int *r, int i)
{
gotoxy(14,*r);
printf("%s ", st[i].name) ;
gotoxy(26,*r);
printf("%.2f ", st[i].avrage) ;
gotoxy(40,*r);
printf("%d ", st[i].unit) ;
gotoxy(54,*r);
printf("%d ", st[i].amount_course) ;
gotoxy(70,*r);
printf("%u ", st[i].student_number) ;
(*r) ++ ;
}
void search_main(void)
{
char j[10];
int y;
do {
printf("how do you want to search?");
printf("\n\t\t\t(1)=base on name");
printf("\n\t\t\t(2)=base on student number");
gets(j);
clrscr();
y=atoi(j);} while (y<0 || y>2);
if(y==1){
search();}
if(y==2){
search2();}
if(y!=1 && y!=2)
return ;
}
void search(void)
{
int i,r=0,m=0,t=0;
char name2[30];
printf("enter a name to search");
gets(name2);
clrscr();
for(i=0;st[i].name[0];i++)
{
if(strcmp(name2,st[i].name)==0){
t=i;
m=1; }}
if(m==1){
title(&r) ;
show(&r, t);}
if (m==0)
printf("the one you are looking for is not in the list......press any key to continue");
getch();
clrscr();
}
void save(void)
{
FILE *fp ;
register int i ;
fp = fopen("project", "wb");
if(!fp) {
printf("\n cannot open file press a key ...");
getch() ;
clrscr();
return ;
}
for(i = 0 ; i < MAX ; i++)
if(*st[i].name)
fwrite(&st[i], sizeof(struct student), 1, fp) ;
clrscr() ;
gotoxy(20, 10) ;
printf("data saved.press a key.");
getch() ;
clrscr();
}
void load(void){
FILE *fp ;
register int i ;
fp = fopen("project", "rb");
if(!fp) {
printf("\n cannot open file press a key ...");
getch() ;
clrscr();
return ;
}
for(i = 0 ; i < MAX; i++) {
fread(&st[i], sizeof(struct student), 1, fp);
if(feof(fp)) {
clrscr() ;
gotoxy(20,10) ;
printf("data are loaded.press a key...........");
getch() ;
clrscr();
return ;
}
}
}
void search2(void)
{
int i,num2,r=0,a;
printf("enter the student number for the one you are looking for");
scanf("%d",&num2);
a=0;
for(i=0;i<MAX;i++){
if(st[i].student_number==num2){
a=1;
title(&r);
show(&r,i);}}
if(a==0)
printf("the one you are lookig for is not in the list...press any key to continue...");
getch();
clrscr();
}
void in_style(int l)
{
int i,j;
char name2[30];
struct student st1={0};
for(i=l-1;i>0;i--){
for(j=0;j<i;j++)
if(strcmp(st[j].name,st[j+1].name)>0){
st1=st[j];
st[j]=st[j+1];
st[j+1]=st1;
}
}
return;
}
پاسخ : پروژه هاي برنامه نويسي
کد زیر به روش بازگشتی عناصر یک آرایه رو چاپ میکنه :
کد PHP:
#include <iostream>
#include <conio>
void Print_array(int a[],int n){
if(n==1)
cout<<a[n-1]<<" ";
else{
Print_array(a,n-1);
cout<<a[n-1]<<" ";
}
}
//===============================
int main(){
int a[]={2,6,8,5,3};
Print_array(a,5);
getch();
}
پاسخ : پروژه هاي برنامه نويسي
جمع و ضرب دو عدد بروش بازگشتی
کد PHP:
#include <iostream>
#include <conio>
int Rec_Add(int a,int b){
if(b==0)
return a;
else
return Rec_Add(a+1,b-1);
}
//===========================
int Rec_Multiply(int a,int b){
if(b==1)
return a;
else
return a+Rec_Multiply(a,b-1);
}
پاسخ : پروژه هاي برنامه نويسي
بزرگترین مقسوم علیه مشترک دو عدد به صورت بازگشتی
کد PHP:
#include <conio.h>
#include <iostream.h>
int gcd(int,int);
int main()
{
int x,y;
cout<<"enter amount for x and y:";
cin>>x>>y;
int k=gcd(x,y);
cout<<k;
getch();
return 0;
}
int gcd(int x,int y)
{
if(y==0)
return x;
else
return gcd(y,x&y);
}
پاسخ : پروژه هاي برنامه نويسي
محاسبه توان یک عدد به صورت بازگشتی
کد PHP:
#include <iostream.h>
#include <conio.h>
int power(int,int);
int main()
{
int exp,base;
cout<<"enter base,exp:";
cin>>base>>exp;
int h=power(base,exp);
cout<<"it equals by:"<<h;
getch();
return 0;
}
int power(int base,int exp)
{
if(exp==1)
return base;
else
return base*power(base,exp-1);
}
پاسخ : پروژه هاي برنامه نويسي
کلاس کار با اعداد حقیقی
کد PHP:
#include"iostream.h"
#include<CONIO.H>
class Complex{
double re,im;
public:
Complex(){re=0,im=0;}
Complex(double r){re=r,im=0;}
Complex(double r,double i){re=r,im=i;}
void setr(double x){re=x;}
void seti(double x){im=x;}
double getr(){return re;}
double geti(){return im;}
void set(double r,double i)
{
re=r;
im=i;
}
void print(ostream &o){
o<<re;
if(im>=0)o<<'+';
o<<im<<'i';
}
Complex operator+(Complex rhs){
Complex h;
h.re=re+rhs.re;
h.im=im+rhs.im;
return h;
}
Complex operator*(Complex rhs){
Complex h;
h.re=re*rhs.re - im*rhs.im;
h.im=im*rhs.re + re*rhs.im;
return h;
}
Complex operator*(double x){
Complex h;
h.re=re*x;
h.im=im*x;
return h;
}
/*
Complex operator *(double x,Complex rhs){
Complex h;
h.setr(x*rhs.getr());
h.seti(x*rhs.geti());
// return c*x;
return h;
}
*/
Complex operator/(Complex rhs){
double d=rhs.re * rhs.re + rhs.im*rhs.im;
Complex h;
h.re=(re*rhs.re + im*rhs.im)/d;
h.im=(im*rhs.re - re*rhs.im)/d;
return h;
}
};
ostream &operator <<(ostream &o,Complex cc){
cc.print(o);
return (o);
}
void main(){
clrscr();
Complex a(2,3),b(-1,4),c;
//a.set(2,3);
//b.set(-1,4);
c=a+b;
cout<<"\t\t\n a + b = "<<c;
c=a*b;
cout<<"\t\t\n a * b = "<<c;
c=a/b;
cout<<"\t\t\n a / b = "<<c;
c=a*2;
cout<<"\t\t\n a* 2 = "<<c;
}
پاسخ : پروژه هاي برنامه نويسي
کد PHP:
#include <iostream.h>
#include <time.h>
#include <stdlib.h>
#define SIZE 200
#define INVALID -1
#ifndef array_list_h
#define array_list_h
class array_list {
public:
array_list(int sz = 0);
~array_list() { clear(); }
bool isEmpty(void) { return (num == 0); }
bool isInList(int sample);
int getLength(void) { return num; }
int getItem(int pos) { return a[pos]; }
void setItem(int pos, int newItem) { a[pos] = newItem; }
void insert(int pos, int newItem);
void remove(int pos);
void bubbleSort(void);
void insertionSort(void);
void selectionSort(void);
void heapSort(void);
void mergeSort(void) { mergeSortStub(0, num-1); }
void quickSort(void) { quickSortStub(0, num-1); }
int sequentialSearch(int key);
int binarySearch(int key) { return binarySearchStub(0, num-1, key); }
void clear() { for (int i = 0; i < SIZE; i++) a[i] = INVALID; num = 0; }
void display() { for (int i = 0; i < num; i++) cout << a[i] << " "; cout << endl; }
private:
void swap(int& x, int& y) { int temp = x; x = y; y = temp; }
int findMin(int curr, int last);
void fixHeap(int heapsize, int root, int k);
void constructHeap(int root);
int deleteMin(void);
void merge(int start, int last);
void mergeSortStub(int start, int last);
int random(int start, int last) { srand(time(NULL)); return start + rand() % (last-start+1); }
int partition(int start, int last);
void quickSortStub(int start, int last);
int binarySearchStub(int start, int last, int key);
int num;
int a[SIZE];
};
array_list::array_list(int sz)
{
int i;
for (i = 0; i < sz; i++)
a[i] = 0;
for (i = sz; i < 200; i++)
a[i] = INVALID;
num = sz;
}
bool array_list::isInList(int sample)
{
bool check = false;
for (int i = 0; i < num; i++)
if (a[i] == sample) {
check = true;
break;
}
return check;
}
void array_list::insert(int pos, int newItem)
{
if (pos > getLength())
return;
for (int i = num; i > pos; i--)
a[i] = a[i-1];
a[pos] = newItem;
num++;
}
void array_list::remove(int pos)
{
if (pos >= getLength())
return;
for (int i = pos; i < num-1; i++)
a[i] = a[i+1];
a[num-1] = INVALID;
num--;
}
//====================== SORTING ==========================//
void array_list::bubbleSort(void)
{
for (int i = 0; i < num-1; i++)
for (int j = i+1; j < num; j++)
if (a[i] > a[j])
swap(a[i], a[j]);
}
void array_list::insertionSort(void)
{
for (int i = 0; i < num; i++) {
int j = i;
while ((j > 0) && (a[j] < a[j-1])) {
swap(a[j], a[j-1]);
j--;
}
}
}
int array_list::findMin(int curr, int last)
{
if (last - curr == 1) {
if (a[curr] < a[last])
return curr;
else
return last;
}
else {
int rest = findMin(curr+1, last);
if (a[curr] < a[rest])
return curr;
else
return rest;
}
}
void array_list::selectionSort(void)
{
for (int i = 0; i < num-1; i++) {
int j = findMin(i, num-1);
swap(a[j], a[i]);
}
}
void array_list::fixHeap(int heapsize, int root, int k)
{
if (2*root+1 > heapsize) // the root has no child
a[root] = k;
else {
int largerSubHeap;
if (2*root+1 == heapsize) // the root has 1 child
largerSubHeap = 2*root+1;
else // the root has 2 children
largerSubHeap = (a[2*root+1] > a[2*root+2]) ? (2*root+1) : (2*root+2);
if (k >= a[largerSubHeap])
a[root] = k;
else {
a[root] = a[largerSubHeap];
fixHeap(heapsize, largerSubHeap, k);
}
}
}
void array_list::constructHeap(int root)
{
int k = a[root];
if (2*root+1 >= num) // the root has no child
return;
else if (2*root+2 == num) // the root has 1 child
constructHeap(2*root+1);
else { // the root has 2 children
constructHeap(2*root+1);
constructHeap(2*root+2);
}
fixHeap(num, root, k);
}
void array_list::heapSort(void)
{
int heapsize;
constructHeap(0);
for (heapsize = num; heapsize >= 2; heapsize--) {
int currentMax = a[0];
int k = a[heapsize-1];
fixHeap(heapsize-1,0, k);
a[heapsize-1] = currentMax;
}
}
void array_list::merge(int start, int last)
{
int i, j, k;
int aux[SIZE];
int mid = (start + last) / 2;
for (i = start; i <= mid; i++)
aux[i] = a[i];
for (i = mid+1; i <= last; i++)
aux[last+mid+1-i] = a[i];
j = start; k = last;
for (i = start; i <= last; i++)
a[i] = (aux[j] < aux[k]) ? aux[j++] : aux[k--];
}
void array_list::mergeSortStub(int start, int last)
{
if (last > start) {
int mid = (last + start) / 2;
mergeSortStub(start, mid);
mergeSortStub(mid+1, last);
merge(start, last);
}
}
int array_list::partition(int start, int last)
{
swap(a[start], a[random(start, last)]);
int pivot = a[start];
int leftwall = start;
for (int i = start+1; i <= last; i++) {
if (a[i] < pivot) {
leftwall++;
swap(a[i], a[leftwall]);
}
}
swap(a[start], a[leftwall]);
return leftwall;
}
void array_list::quickSortStub(int start, int last)
{
if (last > start) {
int pivot = partition(start, last);
quickSortStub(start, pivot-1);
quickSortStub(pivot+1, last);
}
}
//====================== SEARCHING ==============//
int array_list::sequentialSearch(int key)
{
int i = 0;
while ((i < num) && (a[i] != key))
i++;
if (i == num)
return -1;
else
return i;
}
int array_list::binarySearchStub(int start, int last, int key)
{
if (last < start)
return -1;
else {
int mid = (start + last) / 2;
if (key == a[mid])
return mid;
else if (key < a[mid])
return binarySearchStub(start, mid-1, key);
else
return binarySearchStub(mid+1, last, key);
}
}
پاسخ : پروژه هاي برنامه نويسي
یه برنامه که طول رشته رو تا رسیدن به Enter حساب میکنه
کد PHP:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char ch;int i=0;
while(cin.get()!='\n')
i++;
cout<<i;
getch();
}
پاسخ : پروژه هاي برنامه نويسي
n جمله اول فیبوناچی رو حساب میکنه !
کد PHP:
#include <iostream.h>
#include <conio.h >
main(){
int x=1;
int y=1,z=0;
int n;
cout <<"\n enter number:";
cin >>n;
cout<<x;
cout <<y;
for (int i=2;i<n;i++){
z=x+y;
x=y;
y=z;
cout<<z;
}
getch();
}
پاسخ : پروژه هاي برنامه نويسي
تعداد فاصله هایی که تو رشته هست رو میده!
کد PHP:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i=1;char ch;
while((ch=cin.get())!='\n')
{
if(ch==' ')
i++;
}
cout<<i;
getch();
}