PDA

View Full Version : سوال: چند تغییر در برنامه



behxam
پنج شنبه 09 خرداد 1392, 19:51 عصر
سلام دوستان عزیز یه برنامه دارم به زبان C++ با Cfree نوشته شده میخوام چند تا تغییر بدم این برنامه چند تا چیز داره که به درد من نمیخوره میخوام اونارو بردارم کلا دو تا از خاصیتاشو برنامه من باید
1.دریافت نام2.نام خانوادگی3شماره دانشجووییو معدل ایارو داشته باشه الان این برنامه نام خانوادگی نمیگیره به جاش چند تا چیز که من نمیخامو میگیره میخوام اونو دیلیت و نام خانوادگیو جایگزینش کنم(خط 9و10 اضافی و به جاشون نام خانوادگی جایگزین بشه)

#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();
system("cls");
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);
system("cls");
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')
{
system("cls");
st[t].name[0]='\0';
return ;}}
}
l++;
system("cls");
printf("\n\tenter student number:") ;
scanf("%d", &st[t].student_number) ;
system("cls");

printf("\n\tenter number of course:") ;
scanf("%d", &st[t].amount_course) ;
system("cls");
for(j = 1; j <= st[t].amount_course; j++) {

printf("\n\tenter grade for course %d:",j);
scanf("%f", &grade) ;
system("cls");
printf("\n\tenter unit of course %d:", j);
scanf("%d", &unit) ;
system("cls");
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);
system("cls");
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();
system("cls");
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();
system("cls");
}
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();
system("cls");
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();
system("cls");

}
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();
system("cls");
//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();
system("cls");
}
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();
system("cls");
}
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);
system("cls");
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);
system("cls");
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();
system("cls");
}


void save(void)
{
FILE *fp ;
register int i ;
fp = fopen("project", "wb");
if(!fp) {
printf("\n cannot open file press a key ...");
getch() ;
system("cls");
return ;
}
for(i = 0 ; i < MAX ; i++)
if(*st[i].name)
fwrite(&st[i], sizeof(struct student), 1, fp) ;
system("cls");
//gotoxy(20, 10) ;
printf("data saved.press a key.");
getch() ;
system("cls");
}

void load(void){
FILE *fp ;
register int i ;
fp = fopen("project", "rb");
if(!fp) {
printf("\n cannot open file press a key ...");
getch() ;
system("cls");
return ;
}
for(i = 0 ; i < MAX; i++) {
fread(&st[i], sizeof(struct student), 1, fp);
if(feof(fp)) {
system("cls");
//gotoxy(20,10) ;
printf("data are loaded.press a key...........");
getch() ;
system("cls");
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();
system("cls");
}
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;
}