PDA

View Full Version : برنامه ای برای کنترل سرعت خودروها



beigi1366
دوشنبه 28 بهمن 1392, 11:02 صبح
سلام مهندسین محترم... یه برنامه ای باید بنویسم ولی به مشکل برخوردم..... سوال رو مطرح می کنم بعد برنامه ای که نوشتم رو میذارم اشکالها رو بگید ممنون:
فرض کنید محدوده سرعت مجاز و چراغ سبز یا قرمز راهنمایی رانندگی به قرائتگر خودرو ارسال میشه ... سنسور سرعت خودرو سرعت خودرو را داره یه مقایسه ای توسط کامپیوتر خودرو انجام میشه و اعلام میشه که خودرو تو محدوده سرعت است... یعنی سرعت غیر مجاز نداره با توجه به چراغ راهنمایی رانندگی و محدوده سرعت....
من برنامه زیر رو نوشتم:
CPP]#include<iostream.h>
#include<conio.h>
class tags {
protected: int speedlimit, lights;// the speed_limit is the speed limit for street//
// the lights is green or red, if the light is green its value is one else if the light is red its value is zero.//

public: void set_value( int a, int b)
{
cout<<" get the speed range: ";
cin>>a;
cout<<" get the color of traffic lights, if the green light is one else if the red light is zero: ";
cin>> b;
speedlimit=a;
lights=b;
}
};
class vehicle : public tags
{
public: void reader_rfid(int x, int y);
void sensor(int z);
void ecu(int w);
};
void vehicle:: reader_rfid(int x, int y)
{
set_value(int a, int b);
x=a; y=b;

};
void vehicle::sensor(int z)
{
cout<<"get the speed vehicle: ";
cin>>z;
};
void vehicle::ecu(int w)
{
set_value(a,b);

w=b;

if ( z<=a && w==1) {
cout<<" vehicle is speed range ";
};
};
main()
{
tags l1;
vehicle l2,l3,l4;
l1.set_value(int a, int b);
l2.reader_rfid(int x, int y);
l3.sensor(int w);
l4.ecu(int w);
return 0;
}

beigi1366
دوشنبه 28 بهمن 1392, 11:04 صبح
این برنامه ای من نوشتم اشکال داره کامپایلر اشکال میگیره...
بچه ها تو رو خدا کمک.....
برنامه رو کپی کردم یخورده بهم ریخته

beigi1366
دوشنبه 28 بهمن 1392, 12:35 عصر
من منتظرمااااااا

beigi1366
دوشنبه 28 بهمن 1392, 16:47 عصر
بچه ها کجایین.... بیاین کمک...
حداقل این برنامه رو بگید مشکلش کجاست؟؟؟ دو تا خطا داره:
#include<iostream.h>
#include<conio.h>
class vehicle {
private: int speed, color;
public: void set_value(int s, int c)
{
cout<<" enter speed";
cin>>s;
cout<<"color:";
cin>>c;
speed=s;
color=c;
}
int speed_vehicle(int z);
}l1;
int vehicle::speed_vehicle(int z)
{ cout<<" speed";
cin>>z;
while (z!=0)
{
if(z>speed){
cout<<" vehicle speed how";
z--;
cout<<z;
}
}
}
main()
{
l1.set_value();
l1.speed_vehicle();
getch();
return 0;
}

amirhossein.ha
دوشنبه 28 بهمن 1392, 18:35 عصر
این برنامه دومته بدون مشکل ولی معلوم نیست قراره چی کار بکنه برنامه اولت هم میزارم :

#include<iostream>
#include<cstdlib>
using namespace std;
class vehicle {
private:
int speed, color;
public:
int speed_vehicle();
void set_value();
}l1;
void vehicle::set_value()
{
int s,c;
cout<<"enter speed : \n";
cin>>s;
cout<<"color: \n";
cin>>c;
l1.speed=s;
l1.color=c;
}
int vehicle::speed_vehicle()
{
int z;
cout<<"speed ";
cin>>z;
while (z!=0)
{
if(z>speed)
{
cout<<" vehicle speed how";
z--;
cout<<z;
}
}
}
int main()
{
l1.set_value();
l1.speed_vehicle();
system("pause");
return 0;
}

beigi1366
دوشنبه 28 بهمن 1392, 20:37 عصر
خیلی ممنون اقای امیرحسین
من برنامه ای که اصلاح کردین رو تو توربو سی اجرا می کنم دو تا اشکال میگیره با حذف مواردی که مربوط به ویژوال استودیو است.
من میخوام برنامه ای بنویسم که اگه سرعت خودرو بیشتر از حد مجاز دریافتی است سرعتش کم بشه تا به سرعت مجاز برسه ... زمانی هم که چراغ راهنمایی رانندگی قرمزه اگه سرعت خودرو صفر نیست سرعت رو صفر کنه!!!!
نمیدونم این برنامه هایی که نوشتم جوابگو مثالم هست یا نه؟؟؟
خواهش می کنم کمکم کنید....

beigi1366
دوشنبه 28 بهمن 1392, 20:39 عصر
برنامه اجرا شد... راست میگین معلوم نیست میخواد چیکار کنه!!!!!

amirhossein.ha
دوشنبه 28 بهمن 1392, 22:08 عصر
بهتره از توربو سی بهتره از کامپایلر های دیگه مثه code::blocks یا Dev-c++ یا visual stdio استفاده کنید
پیشنهاد من code::blocks میتونید از اینجا (http://www.codeblocks.org/) دانلودش کنید

amirhossein.ha
دوشنبه 28 بهمن 1392, 22:32 عصر
اینم کدش اگه با جاییش مشکلی داری بگو تا توضیح بدم :

#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;

class vehicle {
public :
void GetSpeed();
void SetSpeed(int);
void GetColor();
void SetColor(string);
void GetLimit();
void SetLimit(int);
bool CheckSpeed();
void ChangeSpeed();
private:
int Speed;
bool Color;
int Limit;
}V1;
void vehicle::GetSpeed()
{
system("cls");
int a;
cout<<"Enter Speed :\n";
cin>>a;
SetSpeed(a);
}
void vehicle::SetSpeed(int a)
{
Speed=a;
}
void vehicle::GetColor()
{
system("cls");
string a;
cout<<"Enter The Color of Traffic lights(Green , Yellow , Red) :\n";
cin>>a;
while(a!="Green" && a!="green" && a!="Red" && a!="red" && a!="Yellow" && a!="Yellow")
{
cout<<"Enter The Color of Traffic lights(Green , Yellow , Red) :\n";
cin>>a;
}
SetColor(a);
}
void vehicle::SetColor(string a)
{
if(a=="Red" || a=="red")
Color=1;
else
Color=0;
}
void vehicle::GetLimit()
{
system("cls");
int a;
cout<<"Enter Speed Limit :\n";
cin>>a;
SetSpeed(a);
}
void vehicle::SetLimit(int a)
{
Limit=a;
}
bool vehicle::CheckSpeed()
{
system("cls");
printf("Your Speed is : %d" , Speed);
if(Color)
if(Speed>0)
return 0;
if(Speed>Limit)
return 0;
return 1;
}
void vehicle::ChangeSpeed()
{
system("cls");
cout<<"Not Allowed Speed !!!\n";
if(Color)
{
cout<<"The Traffic light is Red. You should Stop !!!\n";
Speed=0;
return;
}
printf("You drive faster than Speed Limit. You should reduce your Speed(Limit = %d)!!!\n",Limit);
Speed=Limit;
return;
}
int main()
{
system("color 17");
V1.GetSpeed();
V1.GetColor();
if(V1.CheckSpeed())
cout<<"Allowed Speed .\n";
else
{
while(!V1.CheckSpeed())
V1.ChangeSpeed();
}
return 0;
}



کامپایل هم میشه
من با code::blocks کامپایلش کردم و مشکلی نداشت.

beigi1366
دوشنبه 28 بهمن 1392, 23:06 عصر
خیلی ممنونم....
راستش من با توربو سی اجرا می کنم bool و string رو نمی شناسه!!!
چطوری میتونم درستش کنم؟؟؟
فایل هدر ها رو : iostream.h . conio.h گذاشتم با اونهایی که شما گذاشتی کار نمیکنه..
بازم ممنون.. لطف بزرگی کردین..

beigi1366
سه شنبه 29 بهمن 1392, 09:50 صبح
اقای امیر حسین میتونید راهنمایی کنید با code block چطوری برنامه رو اجرا کنم؟؟؟ اصلا باهاش کار نکردم اجرا می کنم خطا میگیره !!!!

linux
سه شنبه 29 بهمن 1392, 10:52 صبح
بهتره از توربو سی بهتره از کامپایلر های دیگه مثه code::blocks یا Dev-c++ یا visual stdio استفاده کنید
پیشنهاد من code::blocks میتونید از اینجا (http://www.codeblocks.org/) دانلودش کنید
اینهایی که فرمودید IDE هستند!

amirhossein.ha
سه شنبه 29 بهمن 1392, 14:28 عصر
اینهایی که فرمودید IDE هستند!

بله درسته من اشتباه کردم


در مورد code::blocks : اگه فایل *.cpp دارید با همین برنامه باز کنید بعد کامپایل رو از منوی بالای صفحه انتخاب کنید یا
یه پروژه بسازید از نوع console بعد اون کامپایل کنید

beigi1366
سه شنبه 29 بهمن 1392, 15:25 عصر
ببخشیدا....
ولی چرا من برنامه ای نمی تونم در code block اجرا کنم؟
f9 رو میزم این چیزا رو پایین می نویسه:
"speed ve - Debug": The compiler's setup (GNU GCC Compiler) is invalid, so Code::Blocks cannot find/run the compiler.
Probably the toolchain path within the compiler options is not setup correctly?! (Do you have a compiler installed?)
Goto "Settings->Compiler...->Global compiler settings->GNU GCC Compiler->Toolchain executables" and fix the compiler's setup.
Skipping...
Nothing to be done (all items are up-to-date).

run رو میزنم :
Probably the toolchain path within the compiler options is not setup correctly?! (Do you have a compiler installed?)
Goto "Settings->Compiler...->Global compiler settings->GNU GCC Compiler->Toolchain executables" and fix the compiler's setup.
Skipping...
Nothing to be done (all items are up-to-date).

Target uses an invalid compiler; run aborted

beigi1366
چهارشنبه 30 بهمن 1392, 08:11 صبح
مهندسین... من چیکار کنم؟؟؟ کسی نیست بگه چطوری اجرا کنم؟؟؟

beigi1366
چهارشنبه 30 بهمن 1392, 09:37 صبح
اقای امیر حسین چرا دیگه به تاپیکم سر نمیزنید؟؟!!!
برنامه رو با کمی تغییر در ویزوال استودیو اجرا کردم... دستتون درد نکنه...
فقط یه مشکلی هست: برنامه در مورد سرعت مجاز کار نمی کنه... منظورم اینه که محدوده سرعت مجاز رو از روی تابلوهای محدودیت سرعت که در خیابون هست بخونه. سرعت خودرو رو هم بگیره . بعد اگر سرعت خودرو بیشتر از محدوده مجاز بود یکی یکی کم کنه تا سرعت ماشین به محدوده سرعت مجاز برسه....
برای این چیکار کنم؟؟/
اما در مورد چراغ درست کار میکنه.... مرسی
خواهش می کنم جواب بدین.....

amirhossein.ha
چهارشنبه 30 بهمن 1392, 15:18 عصر
یه روز نبودم

مگه قرار نیست وقتی سرعت بیشتر از حد مجاز بود یکی یکی کم بشه تا به حد مجاز برسه ؟؟
خب نیازی نیست یدونه یدونه کم بشه مستقیم سرعت رو برابر با حد قرار میدیم ولی بازم اون قسمت رو اونجور که میخواید درست کردم :

#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;

class vehicle {
public :
void GetSpeed();
void SetSpeed(int);
void GetColor();
void SetColor(string);
void GetLimit();
void SetLimit(int);
bool CheckSpeed();
void ChangeSpeed();
private:
int Speed;
bool Color=0;
int Limit;
}V1;
void vehicle::GetSpeed()
{
system("cls");
int a;
cout<<"Enter Speed :\n";
cin>>a;
SetSpeed(a);
}
void vehicle::SetSpeed(int a)
{
Speed=a;
}
void vehicle::GetColor()
{
system("cls");
string a;
cout<<"Enter The Color of Traffic lights(Green , Yellow , Red) :\n";
cin>>a;
while(a!="Green" && a!="green" && a!="Red" && a!="red" && a!="Yellow" && a!="yellow")
{
cout<<"Enter The Color of Traffic lights(Green , Yellow , Red) :\n";
cin>>a;
}
SetColor(a);
}
void vehicle::SetColor(string a)
{
if(a=="Red" || a=="red")
Color=1;
else
Color=0;
}
void vehicle::GetLimit()
{
system("cls");
int a;
cout<<"Enter Speed Limit :\n";
cin>>a;
SetSpeed(a);
}
void vehicle::SetLimit(int a)
{
Limit=a;
}
bool vehicle::CheckSpeed()
{
system("cls");
printf("Your Speed is : %d" , Speed);
if(Color)
if(Speed>0)
return 0;
if(Speed>Limit)
return 0;
return 1;
}
void vehicle::ChangeSpeed()
{
system("cls");
cout<<"Not Allowed Speed !!!\n";
if(Color)
{
cout<<"The Traffic light is Red. You should Stop !!!\n";
Speed=0;
return;
}
printf("You drive faster than Speed Limit. You should reduce your Speed(Limit = %d)!!!\n",Limit);
while(Speed>Limit)
{
Speed--;
printf("Speed : %d" , Speed);
}
return;
}
int main()
{
system("color 17");
V1.GetSpeed();
V1.GetLimit();
V1.GetColor();
if(V1.CheckSpeed())
cout<<"Allowed Speed .\n";
else
{
while(!V1.CheckSpeed())
V1.ChangeSpeed();
}
return 0;
}

beigi1366
چهارشنبه 30 بهمن 1392, 16:28 عصر
مرسی
اما من میخوام سرعت ماشین بیشتر از حد مجاز بود .... برابر بشه سرعت خودرو با سرعت مجاز.
اما برنامه رو اصلاح کردین سرعت غیر مجاز بود سرعت ماشین رو صفر می کنه..

amirhossein.ha
چهارشنبه 30 بهمن 1392, 16:42 عصر
الان مشکل چیه ؟؟
اون کد مشکل داشت این درسته :
#include <iostream>
#include <cstdio>
#include <cstdlib>

using namespace std;

class vehicle {
public :
void GetSpeed();
void SetSpeed(int);
void GetColor();
void SetColor(string);
void GetLimit();
void SetLimit(int);
bool CheckSpeed();
void ChangeSpeed();
private:
int Speed;
bool Color;
int Limit;
}V1;
void vehicle::GetSpeed()
{
system("cls");
int a;
cout<<"Enter Speed :\n";
cin>>a;
SetSpeed(a);
}
void vehicle::SetSpeed(int a)
{
Speed=a;
}
void vehicle::GetColor()
{
system("cls");
string a;
cout<<"Enter The Color of Traffic lights(Green , Yellow , Red) :\n";
cin>>a;
while(a!="Green" && a!="green" && a!="Red" && a!="red" && a!="Yellow" && a!="yellow")
{
cout<<"Enter The Color of Traffic lights(Green , Yellow , Red) :\n";
cin>>a;
}
SetColor(a);
}
void vehicle::SetColor(string a)
{
if(a=="Red" || a=="red")
Color=1;
else
Color=0;
}
void vehicle::GetLimit()
{
system("cls");
int a;
cout<<"Enter Speed Limit :\n";
cin>>a;
SetLimit(a);
}
void vehicle::SetLimit(int a)
{
Limit=a;
}
bool vehicle::CheckSpeed()
{
system("cls");
printf("Your Speed is : %d" , Speed);
if(Color)
if(Speed>0)
return 0;
if(Speed>Limit)
return 0;
return 1;
}
void vehicle::ChangeSpeed()
{
system("cls");
cout<<"Not Allowed Speed !!!\n";
if(Color)
{
cout<<"The Traffic light is Red. You should Stop !!!\n";
Speed=0;
system("pause");
return;
}
printf("You drive faster than Speed Limit. You should reduce your Speed(Limit = %d)!!!\n",Limit);
while(Speed>Limit)
{
Speed--;
printf("Speed : %d\n" , Speed);
}
system("pause");
return;
}
int main()
{
system("color 17");
V1.GetSpeed();
V1.GetLimit();
V1.GetColor();
if(V1.CheckSpeed())
cout<<"Allowed Speed .\n";
else
{
while(!V1.CheckSpeed())
V1.ChangeSpeed();
}
return 0;
}

beigi1366
پنج شنبه 01 اسفند 1392, 09:32 صبح
سلام. اقای امیرحسین یه زحمتی می کشید:
این دو تا برنامه را جدا گونه بنویسید؟؟؟ من هر کاری کردم نتونستم یا ارور میگرفت یا جوابی که می خواستم نمیشد.
برنامه یک) برنامه ای که سرعت خودرو و رنگ چراغ راهنمایی رانندگی رو بگیره اگه چراغ قرمز بود و سرعت صفر نبود یکی یکی سرعت رو کم کنه تا سرعت صفر بشه.
برنامه دو) برنامه ای که سرعت خودرو رو بگیره و محدوده مجاز سرعت را هم بگیره اگر سرعت خودرو زیادتر از حد مجاز بود یکی یکی از سرعت خودرو کم بشه تا به سرعت مجاز برسه.....
برنامه سه: برنامه ای که هر دو برنامه یک و دو را شامل بشه
.................................................. .................................................. .................................................. ..............................
برنامه های قبلی مشکل دارن.....
مثلا سرعت خودرو را یکی یکی کم نمیکنه تا به سرعت مجاز برسه...........
مرسی

amirhossein.ha
جمعه 02 اسفند 1392, 10:56 صبح
خب همون برنامه رو چند قسمت بکن دیگه :‌
1 :‌

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>

using namespace std;

class vehicle {
public :
void GetSpeed();
void SetSpeed(int);
void GetColor();
void SetColor(string);
bool CheckSpeed();
void ChangeSpeed();
private:
int Speed;
bool Color;
string LowerCase(string);
}V1;
string vehicle::LowerCase(string s)
{
for(int i=0;i<s.size();i++)
if(s[i]>='A' && s[i]<='Z')
s[i] = s[i] ^ ' ';
return s;
}
void vehicle::GetSpeed()
{
system("cls");
int a;
cout<<"Enter Speed :\n";
cin>>a;
SetSpeed(a);
}
void vehicle::SetSpeed(int a)
{
Speed=a;
}
void vehicle::GetColor()
{
system("cls");
string a;
cout<<"Enter The Color of Traffic lights(Green , Yellow , Red) :\n";
cin>>a;
a=LowerCase(a);
while(a!="green" && a!="red" && a!="yellow")
{
cout<<"Enter The Color of Traffic lights(Green , Yellow , Red) :\n";
cin>>a;
a=LowerCase(a);
}
SetColor(a);
}
void vehicle::SetColor(string a)
{
a=LowerCase(a);
if(a=="red")
Color=1;
else
Color=0;
}
bool vehicle::CheckSpeed()
{
system("cls");
printf("Your Speed is : %d\n" , Speed);
if(Color)
if(Speed>0)
return 0;
return 1;
}
void vehicle::ChangeSpeed()
{
system("cls");
cout<<"Not Allowed Speed !!!\n";
if(Color)
{
cout<<"The Traffic light is Red. You should Stop !!!\n";
while(Speed>0)
{
Speed--;
printf("Speed : %d\n" , Speed);
}
system("pause");
return;
}
}
int main()
{
system("color 17");
V1.GetSpeed();
V1.GetColor();
if(V1.CheckSpeed())
cout<<"Allowed Speed .\n";
else
{
while(!V1.CheckSpeed())
V1.ChangeSpeed();
}
return 0;
}



2 :


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>

using namespace std;

class vehicle {
public :
void GetSpeed();
void SetSpeed(int);
void GetColor();
void SetColor(string);
void GetLimit();
void SetLimit(int);
bool CheckSpeed();
void ChangeSpeed();
private:
int Speed;
bool Color;
int Limit;
}V1;
void vehicle::GetSpeed()
{
system("cls");
int a;
cout<<"Enter Speed :\n";
cin>>a;
SetSpeed(a);
}
void vehicle::SetSpeed(int a)
{
Speed=a;
}
void vehicle::GetLimit()
{
system("cls");
int a;
cout<<"Enter Speed Limit :\n";
cin>>a;
SetLimit(a);
}
void vehicle::SetLimit(int a)
{
Limit=a;
}
bool vehicle::CheckSpeed()
{
system("cls");
printf("Your Speed is : %d" , Speed);
if(Speed>Limit)
return 0;
return 1;
}
void vehicle::ChangeSpeed()
{
system("cls");
cout<<"Not Allowed Speed !!!\n";
printf("You drive faster than Speed Limit. You should reduce your Speed(Limit = %d)!!!\n",Limit);
while(Speed>Limit)
{
Speed--;
printf("Speed : %d\n" , Speed);
}
system("pause");
return;
}
int main()
{
system("color 17");
V1.GetSpeed();
V1.GetLimit();
if(V1.CheckSpeed())
cout<<"Allowed Speed .\n";
else
{
while(!V1.CheckSpeed())
V1.ChangeSpeed();
}
return 0;
}



3 :


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>

using namespace std;

class vehicle {
public :
void GetSpeed();
void SetSpeed(int);
void GetColor();
void SetColor(string);
void GetLimit();
void SetLimit(int);
bool CheckSpeed();
void ChangeSpeed();
private:
int Speed;
bool Color;
int Limit;
string LowerCase(string);
}V1;
string vehicle::LowerCase(string s)
{
for(int i=0;i<s.size();i++)
if(s[i]>='A' && s[i]<='Z')
s[i] = s[i] ^ ' ';
return s;
}
void vehicle::GetSpeed()
{
system("cls");
int a;
cout<<"Enter Speed :\n";
cin>>a;
SetSpeed(a);
}
void vehicle::SetSpeed(int a)
{
Speed=a;
}
void vehicle::GetColor()
{
system("cls");
string a;
cout<<"Enter The Color of Traffic lights(Green , Yellow , Red) :\n";
cin>>a;
a=LowerCase(a);
while(a!="green" && a!="red" && a!="yellow")
{
cout<<"Enter The Color of Traffic lights(Green , Yellow , Red) :\n";
cin>>a;
a=LowerCase(a);
}
SetColor(a);
}
void vehicle::SetColor(string a)
{
a=LowerCase(a);
if(a=="red")
Color=1;
else
Color=0;
}
void vehicle::GetLimit()
{
system("cls");
int a;
cout<<"Enter Speed Limit :\n";
cin>>a;
SetLimit(a);
}
void vehicle::SetLimit(int a)
{
Limit=a;
}
bool vehicle::CheckSpeed()
{
system("cls");
printf("Your Speed is : %d" , Speed);
if(Color)
if(Speed>0)
return 0;
if(Speed>Limit)
return 0;
return 1;
}
void vehicle::ChangeSpeed()
{
system("cls");
cout<<"Not Allowed Speed !!!\n";
if(Color)
{
cout<<"The Traffic light is Red. You should Stop !!!\n";
while(Speed>0)
{
Speed--;
printf("Speed : %d\n" , Speed);
}
system("pause");
return;
}
printf("You drive faster than Speed Limit. You should reduce your Speed(Limit = %d)!!!\n",Limit);
while(Speed>Limit)
{
Speed--;
printf("Speed : %d\n" , Speed);
}
system("pause");
return;
}
int main()
{
system("color 17");
V1.GetSpeed();
V1.GetLimit();
V1.GetColor();
if(V1.CheckSpeed())
cout<<"Allowed Speed .\n";
else
{
while(!V1.CheckSpeed())
V1.ChangeSpeed();
}
return 0;
}