PDA

View Full Version : سوال: سوال فوری در مورد class ها در C++‎ برنامه اطلاعات دانشجویان



yahyamoradi
چهارشنبه 06 خرداد 1394, 10:10 صبح
سلام دوستان عزیز !!

یه سوال یا میشه کمک فوری نیاز دارم در مورد بحث class ها در c++

ببنید تیکه کد زیر یه چیز ساده در مورد یه سیستم وارد اطلاعات دانشجویان هست ..

میشه : کاربر جدید وارد کرد - کاربر های وارد شده رو مشاهده کرد - کاربر های موجود را پاک کرد.

این برنامه براحتی و بدون مشکل کار میکنه ... و تنها موردی که داره اینکه که با struct نوشته شده !!!

ولی من میخوام با Class نوشته شده باشه ...

دوستان ممنون میشم راهنمایی کنید .. آخرم ترمه و باید این پروژه رو تا فردا ببرم


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <conio.h>
#include <iomanip>

using namespace std;
int main()
{
FILE *fp, *ft;
char another, choice;

struct student
{
char first_name[50], last_name[50];
char course[100];
int section;
};

struct student e;
char xfirst_name[50], xlast_name[50];
long int recsize;

fp=fopen("users.txt","rb+");

if (fp == NULL)
{
fp = fopen("users.txt","wb+");

if (fp==NULL)
{
puts("Cannot open file");
return 0;
}
}


recsize = sizeof(e);

while(1)
{
system("cls");

cout << "\t\t====== STUDENT DATABASE MANAGEMENT SYSTEM ======";
cout <<"\n\n ";
cout << "\n\n";
cout << "\n \t\t\t 1. Add Records";
cout << "\n \t\t\t 2. List Records";
cout << "\n \t\t\t 3. Modify Records";
cout << "\n \t\t\t 4. Delete Records";
cout << "\n \t\t\t 5. Exit Program";
cout << "\n\n";
cout << "\t\t\t Select Your Choice :=> ";
fflush(stdin);
choice = getche();
switch(choice)
{
case '1' :
fseek(fp,0,SEEK_END);
another ='Y';
while(another == 'Y' || another == 'y')
{
system("cls");
cout << "Enter the Firt Name : ";
cin >> e.first_name;
cout << "Enter the Last Name : ";
cin >> e.last_name;
cout << "Enter the Course : ";
cin >> e.course;
cout << "Enter the Section : ";
cin >> e.section;
fwrite(&e,recsize,1,fp);
cout << "\n Add Another Record (Y/N) ";
fflush(stdin);
another = getchar();
}
break;
case '2':
system("cls");
rewind(fp);
cout << "=== View the Records in the Database ===";
cout << "\n";
while (fread(&e,recsize,1,fp) == 1)
{
cout << "\n";
cout <<"\n" << e.first_name << setw(10) << e.last_name;
cout << "\n";
cout <<"\n" <<e.course << setw(8) << e.section;
}
cout << "\n\n";
system("pause");
break;

case '3' :
system("cls");
another = 'Y';
while (another == 'Y'|| another == 'y')
{
cout << "\n Enter the last name of the student : ";
cin >> xlast_name;

rewind(fp);
while (fread(&e,recsize,1,fp) == 1)
{
if (strcmp(e.last_name,xlast_name) == 0)
{
cout << "Enter new the Firt Name : ";
cin >> e.first_name;
cout << "Enter new the Last Name : ";
cin >> e.last_name;
cout << "Enter new the Course : ";
cin >> e.course;
cout << "Enter new the Section : ";
cin >> e.section;
fseek(fp, - recsize, SEEK_CUR);
fwrite(&e,recsize,1,fp);
break;
}
else
cout<<"record not found";
}
cout << "\n Modify Another Record (Y/N) ";
fflush(stdin);
another = getchar();
}
break;


case '4':
system("cls");
another = 'Y';
while (another == 'Y'|| another == 'y')
{
cout << "\n Enter the last name of the student to delete : ";
cin >> xlast_name;

ft = fopen("temp.dat", "wb");

rewind(fp);
while (fread (&e, recsize,1,fp) == 1)

if (strcmp(e.last_name,xlast_name) != 0)
{
fwrite(&e,recsize,1,ft);
}
fclose(fp);
fclose(ft);
remove("users.txt");
rename("temp.dat","users.txt");

fp=fopen("users.txt","rb+");

cout << "\n Delete Another Record (Y/N) ";
fflush(stdin);
another = getchar();
}

break;

case '5':
fclose(fp);
cout << "\n\n";
cout << "\t\t THANK YOU FOR USING THIS SOFTWARE";
cout << "\n\n";
exit(0);
}
}


system("pause");
return 0;
}

-Geek-
چهارشنبه 06 خرداد 1394, 11:34 صبح
اگه لازم نیست که حتما همین کد رو تبدیل کنی پس همینجا یکی دو تا کد آماده گذاشته شده که میتونی از اونا به جای این استفاده کنی.
لینک به برنامه نویس (http://barnamenevis.org/showthread.php?150444-%D9%85%D8%AC%D9%85%D9%88%D8%B9%D9%87-%D8%A8%D8%B1%D9%86%D8%A7%D9%85%D9%87-%D9%87%D8%A7%DB%8C-%D9%86%D9%88%D8%B4%D8%AA%D9%87-%D8%B4%D8%AF%D9%87-%D8%A8%D9%87-%D8%B2%D8%A8%D8%A7%D9%86-C-%D9%88-C)

yahyamoradi
چهارشنبه 06 خرداد 1394, 12:09 عصر
اگه لازم نیست که حتما همین کد رو تبدیل کنی پس همینجا یکی دو تا کد آماده گذاشته شده که میتونی از اونا به جای این استفاده کنی.
لینک به برنامه نویس (http://barnamenevis.org/showthread.php?150444-%D9%85%D8%AC%D9%85%D9%88%D8%B9%D9%87-%D8%A8%D8%B1%D9%86%D8%A7%D9%85%D9%87-%D9%87%D8%A7%DB%8C-%D9%86%D9%88%D8%B4%D8%AA%D9%87-%D8%B4%D8%AF%D9%87-%D8%A8%D9%87-%D8%B2%D8%A8%D8%A7%D9%86-C-%D9%88-C)

سلام ممنون بابت پاسخگویی متاسفانه تو تایپکی که ارشاره کردین باز هم سورس ها با struct نوشته شده بودن ... بعدشم

مشکلی هست هیچ کودوم از اون برنامه ها اجرا نمیشه و ارور the system cannot find the file specified میدن
نسخه ویژال من : visual studio 2010

-Geek-
چهارشنبه 06 خرداد 1394, 12:41 عصر
خوب اونا از یکسری توابع مثل conio.h استفاده میکردن که این تابع رو توی turbo++ و borland و اینا استفاده میکنن. فکر کنم هنوزم بشه .
به هر حال من کاری کردم که نیازی نباشه از این کتابخانه استفاده کنیم و کد رو به اینصورت ویرایش کردم.
من این کد رو توی لینوکس با کامپایلر gcc کامپایل کردم و بدون مشکل کار کرد!
یه نکته اینکه این برنامه فقط اطلاعات یک دانشجو رو همزمان میتونه ذخیره بکنه . ببینم میتونم کمی دستکاریش کنم ؟؟!!!


#include <iostream>
using namespace std;
class student{
public:
char name[20];
int id;
char family[25];
student *next;
};
student *first,*last;
//////////////////////////
void input(){
student *temp=new student;
cout<<"plese enter the id : "<<endl;
cin>>temp->id;
cout<<"plese enter the name : "<<endl;
cin>>temp->name;
cout<<"plese enter the family : "<<endl;
cin>>temp->family;
if (first==NULL){
first=last=temp;
}
else {
temp->next=last;
last=temp;
}
}
/////////////////////////////////
void output(){
student *temp=new student;
temp=first;
while(temp!=NULL){
cout<<temp->id<<endl;
cout<<temp->name<<endl;
cout<<temp->family<<endl;
cout<<"*************************************";
temp=temp->next;
}
cout<<"end of record"<<endl;
}
/////////////////////////////////////
void search(int id){
student *temp=new student;
int find;
temp=first;
while(temp!=NULL){
if (id==temp->id){
cout<<temp->id<<endl;
cout<<temp->name<<endl;
cout<<temp->family<<endl;
find=1;
break;
}
else{
find=0;
temp=temp->next;
}
}
if (find==0){ cout<<"not find record"<<endl;}
}
////////////////////////////////////
int main(){
int stat;
for (;;){
// system("cls");
cout<<"***************************************"<<endl;
cout<<"num 1 for input data " <<endl;
cout<<"num 2 for list data " <<endl;
cout<<"num 3 for search data " <<endl;
cout<<"num 4 for exit" <<endl;
cout<<"***************************************"<<endl ;
cin>>stat;
// system("cls");
switch(stat){
case 1:
input();
break;
case 2:
output();
break;
case 3:
int key;
cout<<"plese enter id for search : " ;
cin>>key;
search(key);
break;
case 4:
exit(0);
}
}
return 0;
}

yahyamoradi
چهارشنبه 06 خرداد 1394, 12:51 عصر
درود !

نمیدونم چرا به جز همون کد اولی که گذاشتم تو پست اول . هیچ سورس دیگه ایی کار نمیکنه همشون ارور system cannot find the file specified میدن ... واقعا موندم چیکار کنم

-Geek-
چهارشنبه 06 خرداد 1394, 13:24 عصر
یه کاری بکن.
توی همون کد خودت فقط struct رو به class تبدیل کن‌(اسمشون رو عوض کن)
بعدش بهتره اعضای کلاس رو هم public بکنی
یعنی کلاست اینطوری بشه


class student
{
public:
char first_name[50], last_name[50];
char course[100];
int section;
};


اینکارو انجام بده و ببین چی میشه
من نمیتونم کامپایلش کنم چون همونطور که گفتم تو لینوکس هستم و به کتابخانه conio.h دسترسی ندارم.

yahyamoradi
چهارشنبه 06 خرداد 1394, 16:05 عصر
برنامه اول به شکل زیر ادیت کردم :

سوال : تو خط 101 -- ببنید الان برای حذف یه کاربر حتما باید نام خانوداگی طرف وارد کرد ... من یه سری تغییراتی انجام دادم اما بازم نام خ میخواد -- میخوام با گرفتن شماره دانشجویی کاربر حذف بشه !!!!


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <conio.h>
#include <iomanip>

using namespace std;
int main()
{
FILE *fp, *ft;
char another, choice;

struct student
{
char first_name[50], last_name[50];
char reshte[100];
int shomare;
};

struct student e;
char xfirst_name[50], xsh_name[50];
long int recsize;

fp=fopen("users.txt","rb+");

if (fp == NULL)
{
fp = fopen("users.txt","wb+");

if (fp==NULL)
{
puts("Cannot open file");
return 0;
}
}


recsize = sizeof(e);

while(1)
{
system("cls");

cout << "\n 1. Add New Useresw:";
cout << "\n 2. Show Adding Users";
cout << "\n 3. Delete Users";
cout << "\n\n";
cout << "\t\t\t => Mikhahid Che Kari Anjam Dahid? = ";
fflush(stdin);
choice = getche();
switch(choice)
{
case '1' :
fseek(fp,0,SEEK_END);
system("cls");
cout << "\n Firt Name : ";
cin >> e.first_name;
cout << "\n Last Name : ";
cin >> e.last_name;
cout << "\n Reshte Tahsili : ";
cin >> e.reshte;
cout << "\n Shomare Daneshjoei : ";
cin >> e.shomare;
fwrite(&e,recsize,1,fp);
another = getchar();
break;

case '2':
system("cls");
rewind(fp);
cout << ">> Namayeshe Karbaran :";
cout << "\n";
while (fread(&e,recsize,1,fp) == 1)
{
cout << "\n";
cout <<"\n Firt Name= " <<e.first_name;
cout << "\n";
cout <<"\n Last Name= " << e.last_name;
cout << "\n";
cout <<"\n Reshte Tahsili= " <<e.reshte;
cout << "\n";
cout <<"\n Shomare Daneshjoei= " << e.shomare;
cout << "\n";
cout << "--------------------------------";
}
cout << "\n\n";
system("pause");
break;

case '3':
system("cls");
cout << "\n Shomare Daneshjoei Karbari ke Mikhahid 'Hazf' Koond ra vared konid : ";
cin >> xsh_name;

ft = fopen("temp.dat", "wb");

rewind(fp);
while (fread (&e, recsize,1,fp) == 1)

if (strcmp(e.shomare,xsh_name) != 0)
{
fwrite(&e,recsize,1,ft);
}
fclose(fp);
fclose(ft);
remove("users.txt");
rename("temp.dat","users.txt");
fp=fopen("users.txt","rb+");
another = getchar();
system("pause");
break;

}
}

return 0;
}

yahyamoradi
چهارشنبه 06 خرداد 1394, 16:29 عصر
دوستان اگر کسی میتونه انجام بده ... هزینشم میدم