#include <conio.h>
#include <iostream.h>
#include <stdio.h>
void main()
{ int l,a,d,i=0,z=0,j,k,b=0,r=1,f=0,c=0,q=0;
clrscr();
cout<<"addad ra vared konid : "<<endl;
cin>>l;
cout<<"mabnaye kononi : "<<endl;
cin>>a;
cout<<"mabnaye darkhasti : "<<endl;
cin>>d;
int n=l;
while(n){
n/=10;
i++;}
for(j=1;j<=i;j++){
b=l%10;
l/=10;
for(k=2;k<j;k++)
r*=r;
c=b*r;
f+=c;
r=a;}
int t=f; r=1;b=0;c=0;
while(t){
t/=d;
z++;}
for(j=1;j<=z;j++){
b=f%d;
f/=d;
for(k=2;k<j;k++)
r*=10;
c=b*r;
q+=c;
r=10;}
cout<<"addade vared shode dar mabnaye jadid barabare "<<q<<" mibashad."<<endl;
getch();}
برنامه دریافت زمان برحسب ساعت ، دقیقه و ثانیه و محاسبه آن برحسب ثانیه
توضیحات :
این برنامه زمان را برحسب ساعت و دقیقه و ثانیه گرفته و محاسبه می کند که این زمان برحسب ثانیه چه مقدار خواهد شد.
download
سلام
آقا اینو میشه به زبان خیلی خیلی ساده C بنویسی؟
چیز خاصی نداره دوست عزیز
به جای دستورات cout و cin سی پلاس پلاس معادل اونا رو در سی بنویسید
#include <stdio.h>
#include <conio.h>
main(){
int x[50];
int i,y,z;
scanf("%d",&y);
x[0]=1;
x[1]=1;
printf("%d,",x[0]);
printf("%d,",x[1]);
for(i=2;i<y;i++) {
z=x[i-2]+x[i-1];
printf("%d,",z); }
getch();
}
#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();
}
#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();
}
#include
#include
#include
#include
int main()
{
// ba tarife yek araye be sorate tasadofi adadhaye beine 1 ta 6
// ra dar arraye gharar midahim
// in kar be vasile tabehaye Srand() va Rand() anjam mishe
//***********************************************************************
int Array[6];
srand(time(0));
for (int j = 0; j<= 5; j++ )
Array[j] = 0;
int n = 0;
for (int i = 1; i<= 100; i++ )
{
// dastore Rand() yek adade tasadofi beyne 1 ta 32767 ra entekhab mikonad
// baraye inke in adad dar baze 1 ta 6 gharar begirad
// as dastore zir estefade mikonim
int num = rand() % 6 + 1;
// in dastor baraye in ast ke addadhaye entekhab shode tekrari nabashand
if ( num == Array[0] || num == Array[1]|| num == Array[2]
|| num == Array[3]|| num == Array[4]|| num == Array[5])
continue;
Array[n] = num;
n = n+1;
if (n > 6 )
break;
}
//***********************************************************************
// in halghe for() baraye namayeshe adadhaye entekhab shode ast
// for (int k = 0; k<= 5; k++ )
// {
// cout << Array[k];
// cout << endl;
// }
int m[6] ;
for (int e1 = 0; e1<= 5; e1++ )
m[e1] = 0 ;
cout << " Enter A number Less of 1 for Exit "<< endl << endl;
int w ;
// halghe for() tanha 10 bar tekrar mishavad yani har nafar faghat 10 bar
// forsat darad adadha ra hads bezanad
//***********************************************************************
for(int v=1;v<=10;v++)
{
w = 0 ;
cout << " Enter Six Number Between 1 to 6 : ";
// in halghe adadhaye neveshte shode ra mikhanad
// agar bishtar as 6 bood peighame khata neshan dade mishavad
// agar karbar adade 0 ra vared karde bashad barname exit mishavad
for (int e =0; e<= 5; e++ )
{
cin >> m[e] ;
}
for (int e3 =0; e3<= 5; e3++ )
{
if (m[e3] > 6 )
{
cout < w = 1;
for (int e2 = 0; e2<= 5; e2++ )
m[e2] = 0 ;
break;
}
if ( m[e] < 1 )
exit(0);
}
// cout << "nvbvg"<< m[0]<<" "< if ( w == 0 )
{
// ba moghayese adadhaye vared shode va adadhaye tasadofi bejaye adadhaye
// eshtebah "F" gharar migirad
cout << endl<< " " ;
for ( int z = 0; z<= 5 ; z++ )
{
if ( m[z]== Array[z] )
cout << m[z]<< " " ;
else
cout << "F ";
}
cout << endl ;
}
cout << endl ;
}
return 0;
}
#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 equal(Poly a,Poly b);
void moshtaghpoly(void);
void search(void);
void SortPoly(void);
void ReadPoly(void);
void WritePoly(void);
void AddPoly(Poly a,Poly b);
void SubtractPoly(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[i].Pow==Data[i+1].Pow)
{
Data[i].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[i].Coef;
cout<<" x^";
cin>>Data[i].Pow;
cout<<" +"<<endl;
}
}
void Poly::WritePoly(void)
{
int i;
cout<<"\n";
for(i=0;i<n;i++)cout<<Data[i].Coef<<"X^"<<Data[i].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[i].Pow>b.Data[j].Pow)
{
Data[k].Coef=a.Data[i].Coef;
Data[k++].Pow=a.Data[i++].Pow;
}
else if(a.Data[i].Pow<b.Data[j].Pow)
{
Data[k].Coef=b.Data[j].Coef;
Data[k++].Pow=b.Data[j++].Pow;
}
else if(a.Data[i].Coef+b.Data[j].Coef)
{
Data[k].Coef=a.Data[i].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[i].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[i].Pow>b.Data[j].Pow)
{
Data[k].Coef=a.Data[i].Coef;
Data[k++].Pow=a.Data[i++].Pow;
}
else if(a.Data[i].Pow<b.Data[j].Pow)
{
Data[k].Coef=b.Data[j].Coef;
Data[k++].Pow=b.Data[j++].Pow;
}
else if(a.Data[i].Coef-b.Data[j].Coef)
{
Data[k].Coef=a.Data[i].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[i].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::moshtaghpoly(void){
int i;
for(i=0;i<n;++i){
Data[i].Coef*=Data[i].Pow;
Data[i].Pow--;}}
void Poly::search(void){
int x;
cout<<"\nenter a number for search:";
cin>>x;
for(int i=0;i<n;++i){
if(x==Data[i].Coef)
cout<<"\nThe number founded!";
break;}
cout<<"\nThe number not found";
}
void Poly::equal(Poly a,Poly b){
if(a.n!=b.n)
cout<<"not equal";
for(int i=0;i<a.n;++i)
if(a.Data[i].Pow!=b.Data[i].Pow||a.Data[i].Coef!=b.Data[i].Coef)
cout<<"equql!";
else
cout<<"not equal!";}
void main(void)
{
Poly a,b,c;
char Sign;
a.ReadPoly();
a.SortPoly();
cout<<"\nPlease enter sign(+,-) : ";
cin>>Sign;
b.ReadPoly();
b.SortPoly();
b=a;
b.WritePoly();
switch(Sign)
{
case'+':
c.AddPoly(a,b);
break;
case'-':
c.SubtractPoly(a,b);
break;}
c.SortPoly();
cout<<"\n\nAnswer Is : \n";
c.WritePoly();
a.moshtaghpoly();
cout<<"\nmoshtaghe a is:";
a.WritePoly();
cout<<"in a:";
a.search();
getche();
}
#include<iostream.h>
int main()
{
int n,b;
cin>>n;
while(n!=0)
{
b=n%10;
n=n/10;
cout<<b<<"\t";
}
return 0;
}
#include<iostream.h>
int main()
{
int n,b,sum=0;
cin>>n;
while(n!=0)
{
b=n%10;
sum+=b;
n=n/10;
}
cout<<"sum = "<<sum;
return 0;
}
#include<iostream.h>
int main()
{
int i=0,n,c=0,sum=0,b;
while(i!=10)
{
cin>>n;
while(n!=0)
{
b=n%10;
sum+=b;
n=n/10;
}
if(sum>20)
{c++;sum=0;}
i++;
}
cout<<"Tedad= "<<c;
return 0;
}
#include <iostream.h>
#include <conio.h>
main()
{
int A[10][10],n, i ,j,sum;
cout<<"tedad Rows ? ";
cin>>n;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cout<<A[i][j];
for(i=0,j=0;i<n;i++,j++)
sum+=A[i][j];
cout<<"sum : "<<sum;
}
سلام دوست عزیز
من باید تا 5 روز دیگه یک پروژه تحویل بدم که دفترچه تلفن هستش
همه قسمتهاشو نوشتم فقط یه الگوریتم میخوام که بتونه اسامی داخل دفترچه رو بر اساس نام یا نام خانوادگی مرتب کنه (یعنی آرایه ای از کارکتر)
برای مثال
abolha
ali
behruz
behzad
.
.
.
زبان برنامه نویس من هم c++ هستش
ممنون
Altaiir7 is online now گزارش تخلف ويرايش/حذف پيغام
#include <iostream.h>
#include <stdlib.h>
#include <cstring.h>
#include <iomanip.h>
#include <conio.h>
#include <fstream.h>
struct Phone
{
char name[15];
char family[15];
char tel[12];
Phone *next;
};
Phone* Start_ptr = NULL;
Phone* current = NULL;
void Select ( int ); // which choice user selected;
void Add_new();
void Print_all();
void Menu1();
void Menu2();
void Edit_current();
void Delete_current ();
void Delete_all();
void Tel_or_name( int );
void Read_from_file();
void Write_to_file();
void Sort_name( int );
void Add_tail_from_file( Phone* );
Phone* Search( char* , int);
enum MENU{ ADD=1, SEARCH_NAME, SEARCH_NO, SORT_NAME, SORT_FAMIL, SHOW_ALL, SAVE, EXPORT_CSV, EXIT, UNINSTALL };
int main()
{
Read_from_file();
int choice;
do
{
Menu1();
cin >> choice;
clrscr();
Select ( choice );
}while ( choice != EXIT );
Write_to_file();
cout <<"Thank you" << endl;
Delete_all();
return 0;
}
/*===================Select=======================*/
void Select( int choose )
{
switch ( choose )
{
case ADD :
Add_new(); break;
case SEARCH_NAME :
Tel_or_name(1); break;
case SEARCH_NO :
Tel_or_name(2); break;
case SORT_NAME :
Sort_name(1); break;
case SORT_FAMIL :
Sort_name(2); break;
case SHOW_ALL :
Print_all(); break;
case SAVE :
Write_to_file();
cout <<"We will save your data automatically when your work finished" << endl;
getch();
break;
case EXIT :
return;
default :
cout <<"Select again :" << endl;
}
return;
}
/*=================Add_new=====================*/
void Add_new()
{
Phone* temp = new Phone;
Phone* temp2 = Start_ptr;
clrscr();
cout << "Enter the name : ";
cin >> temp->name;
cout << "Family : ";
cin >> temp->family;
cout << "Phone number : ";
cin >> temp->tel;
temp->next = NULL;
if ( Start_ptr == NULL )
{
Start_ptr = temp;
}
else
{
while( temp2->next != NULL )
{
temp2 = temp2->next;
}
temp2->next = temp;
}
return;
}
/*==============Print_all=======================*/
void Print_all()
{
Phone* temp = Start_ptr;
char show[35];
if ( Start_ptr == NULL )
{
cout <<"The list is empty!" << endl;
getch();
return;
}
else
{
cout<<"\nName Phone "
<<"\n----------------------------------- ------------" << endl;
do
{
strcpy(show, "");
strcat(show, temp->family);
strcat(show, ", ");
strcat(show, temp->name);
show[0] = toupper(show[0]);
cout << setiosflags( ios::left )
<< setw(36) << show << setw(12) <<temp->tel <<endl;
temp = temp->next;
}while(temp != NULL);
}
cout << endl;
getch();
return;
}
/*=================Search=======================*/
Phone* Search( char* temp_search , int choice )
{
/* If list is empty */
if ( Start_ptr == NULL )
{
cout <<"List is empty!" << endl;
return NULL;
}
/* Search by name */
if ( choice == 1 )
{
while( current != NULL && strcmp( current->name, temp_search ) != 0 )
{
current = current->next;
}
}
/* Search by tel */
if ( choice == 2 )
{
while( current != NULL && strcmp( current->tel, temp_search ) != 0 )
{
current = current->next;
}
}
/* If record found */
if ( current != NULL )
{
cout << "Record found" << endl
<< current->name << " " << current->family << " : " << current->tel << endl;
return current;
}
/* If record !found */
else
{
cout <<"Record NOT found" << endl;
current = Start_ptr; //move back the current pointer to fisrt node
return NULL;
}
}
/*====================Delete_current====================*/
void Delete_current()
{
Phone* temp = NULL;
Phone* prev = NULL;
/* If it`s the fisrt node */
if ( current == Start_ptr )
{
temp = Start_ptr;
Start_ptr = Start_ptr->next; //If we have only 1 node, start_ptr will point to NULL
delete temp;
temp = NULL;
}
/* If it`s in the middle of list or the last node */
else
{
prev = Start_ptr;
while( prev->next != current )
{
prev = prev->next;
}
prev->next = current->next;// If it`s the last node prev will point to NULL
delete current;
current = Start_ptr;
}
}
/*=================Delete_all=================*/
void Delete_all()
{
if ( Start_ptr == NULL )
{
return; // we have no memory allocated
}
Phone* temp = Start_ptr;
while( Start_ptr != NULL )
{
temp = Start_ptr;
Start_ptr = Start_ptr->next;
delete temp;
}
}
/*=================Tel_or_name================*/
void Tel_or_name(int choose)
{
Phone* temp_del = NULL;
char temp_search[15];
int choice;
current = Start_ptr;
cout <<"Enter the "<<(choose == 1 ? "name" : "tel") <<" to search : ";
cin >> temp_search;
temp_del = Search( temp_search, choose );
while ( temp_del != NULL )
{
Menu2();
cin >> choice;
switch( choice )
{
case 1: current = current->next; temp_del = Search( temp_search, choose ); break;
case 2: Delete_current(); break;
case 3: Edit_current(); break;
case 4: return;
}
}
getch();
}
/*==============Write_to_file=============================*/
void Write_to_file()
{
Phone* temp = Start_ptr;
ofstream outFile("Data.dat" , ios::out );
if( !outFile )
{
cerr << "Some error ocured during writing to file." << endl;
getch();
return;
}
while( temp != NULL )
{
outFile << temp->name << " " << temp->family << " " << temp->tel;
if( temp->next != NULL )
{
outFile << endl;
}
temp = temp->next;
}
outFile.close();
cout <<"Data saved successfully." << endl;
}
/*===============Read_from_file=============================*/
void Read_from_file()
{
ifstream inputFile("Data.dat" , ios::in );
if ( !inputFile )
{
cout << "Data couldn`t be loaded." << endl;
getch();
return;
}
do
{
Phone* temp = new Phone;
inputFile >> temp->name;
inputFile >> temp->family;
inputFile >> temp->tel;
temp->next = NULL;
Add_tail_from_file( temp );
//Where should I place delete temp?????
}while( !inputFile.eof() );
cout <<"Data loaded successfully" << endl;
}
/*==============Add_tail_from_file=================*/
void Add_tail_from_file( Phone* temp )
{
if ( Start_ptr == NULL )
{
Start_ptr = temp;
}
else
{
Phone* temp2 = Start_ptr;
while ( temp2->next != NULL )
{
temp2 = temp2->next;
}
temp2->next = temp;
}
}
/*================Sort_name=================================*/
void Sort_name(int choice)
{
/* If list is empty */
if ( Start_ptr == NULL )
{
cout <<"The list is empty!" << endl;
getch();
return;
}
/* Determine the size of list */
int counter = 1;
Phone* temp = Start_ptr;
while( temp->next != NULL )
{
temp = temp->next;
counter++;
}
/* an Array of pointers to struct Phone. I couldn`t do this part by dynamic memory
allocation i.e Phone* sort = new Phone[counter]
or some thing like that. if you could help, please send me your suggestion .*/
Phone* sort[1000];
sort[0] = Start_ptr;
for ( int cnt = 1; cnt < counter; cnt++ )
{
sort[cnt] = sort[cnt-1]->next;
}
sort[counter] = NULL;
/* bubble sort */
/* This part could be better too.for example if user enter capital 'F' it comes
before 'a'. I had to change all letter to lower case but has no time. */
for ( int i = 0; i < counter; i++ )
{
for ( int j = 0; j < counter - i - 1; j++)
{
/* Sort by name */
if ( choice == 1 )
{
if ( strcmp(sort[j]->name, sort[j+1]->name) > 0 )
{
Phone* temp2 = sort[j];
sort[j] = sort[j+1];
sort[j+1] = temp2;
}
}
/* Sort by family */
else
{
if ( strcmp(sort[j]->family, sort[j+1]->family) > 0 )
{
Phone* temp2 = sort[j];
sort[j] = sort[j+1];
sort[j+1] = temp2;
}
}
}
}
/* Showing sorted list */
char show[35];
int index= 0;
cout<<"\nName Phone "
<<"\n----------------------------------- ------------" << endl;
do
{
strcpy(show, "");
strcat(show, sort[index]->family);
strcat(show, ", ");
strcat(show, sort[index]->name);
show[0] = toupper(show[0]);
cout << setiosflags( ios::left )
<< setw(36) << show << setw(12) <<sort[index]->tel <<endl;
}while(sort[++index] != NULL);
cout << endl;
getch();
return;
}
/*====================Menu1=======================*/
void Menu1()
{
clrscr();
cout << "1. Add new phone"
<<"\n2. Search for name"
<<"\n3. Search for number"
<<"\n4. Sort by name"
<<"\n5. Sort by family"
<<"\n6. Show all list"
<<"\n7. Save data"
<<"\n9. Exit"
<<"\n\nYour choice : ";
}
/*====================Menu2=======================*/
void Menu2()
{
cout << "\n1. Find next"
<<"\n2. Delete current person"
<<"\n3. Edit current person"
<<"\n4. continue" << endl
<<"\nYour choice : ";
}
void Edit_current()
{
strcpy(current->name,"");
strcpy(current->family,"");
cout << "Enter the name : ";
cin >> current->name;
cout << "Family : ";
cin >> current->family;
cout << "Phone number : ";
cin >> current->tel;
clrscr();
}
/*====================End=========================*/