برنامه نویسی شی گرا با ++c

mrkg

New Member
برنامه نویسی

سلام دوستان! :rose:

سیستم پیامک با C# نیاز داشتم.
 
آخرین ویرایش:

v.b.f1

Active Member
سورس ماشین حساب واسه سی ++
البته مشکلات و کمو کاستی هایی داره
تغییرات لازمو خودت اعمال کن


کد:
// C++.cpp : main project file.

#include "stdafx.h"

using namespace System;

#include "iostream"
#include "fstream"
#include "string"
#include "ctype.h"
#include "stdlib.h"
#include "math.h"
using namespace std;

using namespace IO;

void brackets(float[],char*,int,int&);
void operate(float[],char*,int);
void squeeze(float[],char*,int&,int);
void specfunc(float[],char*,int&,int,int);
void strconvert(float*&,char*&,int&,int&);

void strconvert(float*&nexpres,char*&newo,int &nicount,int &oicount)
{
	// a:
	ifstream istring;
ofstream ostring;

ostring.open("cal.dat",ios::out);

char ch;
nicount=0;
int njcount=1;
oicount=0;

//counts all the numbers and operators
do
    {
   cin.get(ch);
    ostring<<ch;
        if(isdigit(ch) || ch=='.')
       {
      nicount++;
      cin.get(ch);
      ostring<<ch;
          for(int tempnj=2;isdigit(ch) || ch=='.' ;tempnj++)
          {
            if(tempnj>njcount)
            {
            njcount=tempnj;
            }
         cin.get(ch);
         ostring<<ch;
          }
      }
          if(isalpha(ch))
            {
           oicount++;
           cin.get(ch);
           ostring<<ch;
              for(;isalpha(ch);)
              {
              cin.get(ch);
              ostring<<ch;
              }
           }
      if(isalnum(ch)==0 && ch!='\n' && ch!='.')
      oicount++;
   }
   while(ch!='\n');

ostring.close();

char **oexpres=new char*[oicount+1];        //+1 for safety

for (int index=0;index<oicount;index++)
{
oexpres[index]=new char[5];                 //max asin+1
}

char **numch=new char*[nicount+1];

for (int index=0;index<nicount;index++)
{
numch[index]=new char[njcount+1];
}


istring.open("cal.dat",ios::in);


//puts numbers and operators in seperate 2-d strings
int ni=0,oi=0;
    do
    {
   istring.get(ch);
        if(isdigit(ch) || ch=='.' && ni<nicount)
       {
      numch[ni][0]=ch;
      istring.get(ch);
            for(int nj=1;isdigit(ch) || ch=='.' && nj<njcount;nj++)
          {
         numch[ni][nj]=ch;
         istring.get(ch);
         }
      ni++;
      }
          if(isalpha(ch))
            {
           oexpres[oi][0]=ch;
           istring.get(ch);
                 for(int oj=1;isalpha(ch) && oj<4;oj++)
              {
              oexpres[oi][oj]=ch;
              istring.get(ch);
              }
           oi++;
           }
      if(isalnum(ch)==0 && ch!='.' && ch!='\n' && oi<oicount)
      {
      oexpres[oi][0]=ch;
      oi++;
      }
   }
   while(ch!='\n');


//converts number string to float a string
nexpres=new float[nicount];

for(int index=0;index<nicount;index++)
{
nexpres[index]=atof(numch[index]);
}

newo=new char[oicount+1];
for(int index=0;index<oicount;index++)
{
     if (isalpha(oexpres[index][0]))
     {
       if(strncmp(oexpres[index],"sin",3)==0)
       {oexpres[index][0]='s';}
       else if(strncmp(oexpres[index],"cos",3)==0)
    {oexpres[index][0]='c';}
       else if(strncmp(oexpres[index],"tan",3)==0)
       {oexpres[index][0]='t';}
       else if(strncmp(oexpres[index],"asin",4)==0)
       {oexpres[index][0]='i';}
       else if(strncmp(oexpres[index],"acos",4)==0)
       {oexpres[index][0]='o';}
       else if(strncmp(oexpres[index],"atan",4)==0)
       {oexpres[index][0]='a';}
       else if(strncmp(oexpres[index],"log",3)==0)
       {oexpres[index][0]='g';}
       else if(strncmp(oexpres[index],"ln",2)==0)
       {oexpres[index][0]='n';}
       else if(strncmp(oexpres[index],"exp",3)==0)
       {oexpres[index][0]='e';}
      else if(strncmp(oexpres[index],"abs",3)==0)
      {oexpres[index][0]='b';}
      else if(strncmp(oexpres[index],"sqrt",4)==0)
      {oexpres[index][0]='q';}
     }
newo[index]=oexpres[index][0];
}
}




void brackets(float nexpres[],char *oexpres,int nxindex,int &oxindex)
{
int bcount=0;

for(int i=0;i<oxindex;i++)  //counts times to perform funtion
{
if (oexpres[i]==')')
bcount++;
}


for(int braks=0;braks<bcount;braks++)
{
    int end=0;
    for(;oexpres[end]!=')';end++)
    {}
    int begin=end;
    for(;oexpres[begin]!='(';begin--)
    {}

    float *number=new float[end-begin];
    char *operators=new char[end-begin];    // chars+ '/0'
   int numpos=0;
int i;
   for(int i=0;i<begin;i++)  //for nested brackets
    {
    if (oexpres[i]=='(' || isalpha(oexpres[i]))
    numpos++;
    }
    i=0;
   for(int k=(begin-numpos);k<(end-numpos);k++) //load up number with bracket elements
    number[i++]=nexpres[k];

    for(int k=begin+1,i=0;k<end;k++)
    operators[i++]=oexpres[k];               //load up with operators

    operate(number,operators,(end-begin-1));
    nexpres[begin-numpos]=number[0];

    int j=(begin-numpos+1);
    for(i=(end-numpos);i<nxindex;i++)
    nexpres[j++]=nexpres[i];

    for(i=end+1,j=begin;i<oxindex;i++)
    oexpres[j++]=oexpres[i];

nxindex-=(end-begin-1);
oxindex-=(end-begin+1);

if(isalpha(oexpres[begin-1]))
specfunc(nexpres,oexpres,oxindex,begin,numpos);

}
}


void operate(float number[],char*operators,int oindex)
{

const char* orderof="^/*-+";

for(int k=0;k<5;k++)              //changes operator according to precedence
{
 for(int i=0;i<oindex;i++)        //marches through the statement
 {
   if(operators[i]==orderof[k])   //checks for desired operator
   {
     switch(operators[i])         //conducts the operation
     {
     case '^':number[i]=pow(number[i],number[i+1]);break;
     case '*':number[i]*=number[i+1];break;
     case '/':number[i]/=number[i+1];break;
     case '+':number[i]+=number[i+1];break;
     case '-':number[i]-=number[i+1];break;
     }
   squeeze(number,operators,oindex,(i+1)); //squeezeout (i+1)

   if(oindex==1)
   i--;

   if(i!=(oindex-1))            //re-test same index with new 'squeezed' values
   i--;
   }
 }
}
}


void squeeze(float number[],char*operators,int &oindex,int squeezeout)
{//sqeezes remaining operation together, maintains integrity of loop
         for(int j=squeezeout;j<oindex;j++)
      {
      number[j]=number[j+1];
      operators[j-1]=operators[j];
      }
      oindex--;
}


void specfunc
(float nexpres[],char *oexpres,int &oxindex,int begin,int numpos)
{
switch(oexpres[begin-1])
{
case 's':nexpres[begin-numpos]=sin(nexpres[begin-numpos]);break;
case 'c':nexpres[begin-numpos]=cos(nexpres[begin-numpos]);break;
case 't':nexpres[begin-numpos]=tan(nexpres[begin-numpos]);break;
case 'i':nexpres[begin-numpos]=asin(nexpres[begin-numpos]);break;
case 'o':nexpres[begin-numpos]=acos(nexpres[begin-numpos]);break;
case 'a':nexpres[begin-numpos]=atan(nexpres[begin-numpos]);break;
case 'g':nexpres[begin-numpos]=log10(nexpres[begin-numpos]);break;
case 'n':nexpres[begin-numpos]=log(nexpres[begin-numpos]);break;
case 'e':nexpres[begin-numpos]=exp(nexpres[begin-numpos]);break;
case 'b':nexpres[begin-numpos]=abs(nexpres[begin-numpos]);break;
case 'q':nexpres[begin-numpos]=sqrt(nexpres[begin-numpos]);break;
}

for(int j=begin;j<oxindex;j++)
oexpres[j-1]=oexpres[j];

oxindex--;
}

void main()
{
float *nexpres;
char *oexpres;
int nxindex,oxindex;

cout<<"//all calculations are in radians"<<endl<<endl;
cout<<"calculate->";

strconvert(nexpres,oexpres,nxindex,oxindex);
brackets(nexpres,oexpres,nxindex,oxindex);
operate(nexpres,oexpres,oxindex);

cout<<"="<<nexpres[0];

//to view all elements
//for(int i=0;i<nxindex;i++)
//cout<<nexpres[i]<<" ";
//cout<<endl;
//for(int i=0;i<oxindex;i++)
//cout<<oexpres[i];
//cout<<endl;

cin.ignore(128, '\n');
}


کد:
# include <iostream>
# include <cmath>

using namespace std;
void help();
//in tabe be onvane help baraye amalgar ha mibashad barname ast;

int fact (float a);
//in tabe factoryel ra hesab mikonad

void error();
//in tabe peyghame khata ra chap mikonad



int main(){
float result,zakhire ,a[50];
char b;
int flag=0,resultint, aint;

for (int i=1; i<50; i++){

    if (i==1) {
        help();
        cout <<"Enter Number:";
        cin>>result;
        i+=1;}
    do{
    do{
    system("cls");
    help();
    cout<<"Last result= "<<result<<endl;
    cout<<"Enter Operator: ";
    cin>>b;
    }while (b!='f' && b!='l'&& b!='z'&& b!='b'&& b!='c'&& b!='+'&& b!='m'&& b!='n'&& b!='-'&& b!='*'&& b!='/'&& b!='o'&& b!='s'&& b!='r'&&b!='q'&& b!='p'&& b!='\\');
    flag=3;

    if (b=='\\'){
                if (result!=floor(result)){
                error();
                flag=2;
                }//end if
               
    }//end if
   

    }while(flag==2); //*/
    switch (b){
    case 'r':
        i=0;
        system("cls");
        continue;
    case 'f':
            result= fact(result);
            continue;
    case 's':
            result=sin(3.1416/(180/result));
            resultint=result*100;
            result=resultint*1./100;           
            continue;
    case 'o':
            result=cos(3.1416/(180/result));
            resultint=result*100;
            result=resultint*1./100;           
            continue;
    case 'm':
            resultint=result;
            if(resultint%180!=90){
                result=3.1416/(180/result);
                result=tan(result);
                resultint=result*100;
                result=resultint*1./100;}//end if
            else
                error();
             continue;
    case 'n':
            resultint=result;
            if(resultint%180!=0){
                result=3.1416/(180/result);
                result=1/(tan(result));
                resultint=result*100;
                result=resultint*1./100;}//end if
            else
                error();
             continue;
    case 'z':
            zakhire=result;           
            continue;
    case 'b':
            result=zakhire;           
            continue;
    case 'l':
            if(result>0)
                result=log10(result);           
            else
                error();
            continue;
    }//end switch
        if (b=='c') break;
//end for*/
       
    system("cls");
    help();
    cout<<"Last result= "<<result<<" "<<b<<endl;
    cout <<"Enter Number:";
    cin>>a[i];
   
    switch(b){
    case '+':
        result+=a[i] ;break;
    case '-':
        result -= a[i]; break;
    case '*':
        result *= a[i]; break;
    case '/':
        if (a[i]==0){
                error();
                break;
            }
        else{
            result/=a[i]; break;}
    case 'p':
        result= pow(result,a[i]); break;
    case 'q':
        result=pow(result, 1/a[i]); break;
    case '\\':
        resultint=result;
        aint=a[i];
            if (a[i]==floor(a[i])){
                result = resultint%aint; break;
                }//end if
            else{
                error();
                break;
                }//end else

    }//end switch
   
}//end for

return 0;
}//end main


void help(){
cout<<"      In the name of God\n\n";
    cout<<"//Created By Sepehr Mohammad\\\\"<<<<endl;
    cout<<"//  Http://cplusprogram.tk  \\\\"<<endl<<endl;
cout<<"HELP:"<<endl;
cout<<"Enter in Operator"<<endl;
cout<<"Close:[c]"<<endl;
cout<<"Reset:[r]"<<endl;
cout<<"Save in Memory:[z]"<<endl;
cout<<"Load from Memory:[b]"<<endl;
cout<<"Plus:[+]    ;example(2+3=5)"<<endl;
cout<<"Subtract:[-]    ;example(5-3=2)"<<endl;
cout<<"Multiply:[*]    ;example(2*3=6)"<<endl;
cout<<"Division:[/]    ;example(5/2=2.5 $ 5/0=Undefined)"<<endl;
cout<<"Reminder:[\\]    ;example(5\\2=1 $ 5.3\\2.1=Undefined)"<<endl;
cout<<"power:[p]    ;example(2 p 3=8)"<<endl;
cout<<"sqrt:[q]    ;example(8 s 3=2)"<<endl;
cout<<"factorial:[f]    ;example(4f=24)"<<endl;
cout<<"Sin:[s]         ;example(30s=0.5)"<<endl;
cout<<"Cos:[o]         ;example(60o=0.5)"<<endl;
cout<<"tan:[m]         ;example(45m=1)"<<endl;
cout<<"Cot:[n]         ;example(45n=1)"<<endl;
cout<<"log:[l]         ;example(10l=1)"<<endl;
cout<<endl;
}//end help

int fact (float a){
int b=a, result=1;
    for (int i=2; i<=b; i++){
    result*=i;
        }//end for
return result;
}//end fact

void error(){
    system("cls");
    cerr<<endl<<"!!Undefined!!"<<endl<<endl;
    system("pause");
}//end error


//end


شرمنده فقط همینا رو داشتم
 
آخرین ویرایش:

mrkg

New Member
ممنون از لطفت جناب v.b.f1 ولی اینا که گذاشتی اون چیزی که من می خوام نیست
 

v.b.f1

Active Member
منم نگفتم اونه گفتم یه چیزیه در حد اون که حداقل بتونی ازش تو برنامه ات استفاده کنی دوست عزیز

اگرم تونستم پیدا کنم چشم میزارم
 

جدیدترین ارسال ها

بالا