ETH官方钱包

切換
舊版
前往
大廳
主題

C++上課筆記(8)

查理 | 2013-12-19 20:06:51 | 巴幣 0 | 人氣 366

#include <iostream>
using namespace std;
int main()
{
   char str[]="dev_c++";
   char *k;
   k=str;
   while(*k!='\0')
   {
       if(*k>=97 && *k<=122)
          *k-=32;
       k++;  
   }
   cout<<str<<endl;
   system("pause");
   return 0;
}
----------------------------------------------------------
#include <iostream>
using namespace std;
int main()
{
   int a[3][4]={ {10,20,30,40},{50,60,70,80},{90,100,110,120} };
   int i,j;
   for(i=0;i<3;i++)
     for(j=0;j<4;j++)
        cout<<*(*(a+i)+j)<<endl;

   cout<<a<<"  "<<a[0]<<endl;
   cout<<a[1]<<endl;
   cout<<a[2]<<endl;

   system("pause");
   return 0;
}

------------------------------------------------------------------------
#include <iostream>
using namespace std;
int main()
{
   void print(char*);
   char user[20];
   cout<<"input name:";
   cin>>user;
   print(user);
   system("pause");
   return 0;
}

void print(char*  name)   //  void print(char name[])
{
    cout<<"Hello "<<name<<endl;
}
-------------------------------------------------------------------
call by Value

#include <iostream>
using namespace std;
int main()
{
   void swap(int,int);
   int a,b;
   cout<<"input a,b:";
   cin>>a>>b;
   swap(a,b);
   cout<<"a="<<a<<"  b="<<b<<endl;
   system("pause");
   return 0;
}
void swap(int x,int y)
{
    int tmp;
    tmp=x;
    x=y;
    y=tmp;   
    cout<<"x="<<x<<"  y="<<y<<endl;  
    
}
----------------------------------------------------
call by Address

#include <iostream>
using namespace std;
int main()
{
   void swap(int*,int*);
   int a,b;
   cout<<"input a,b:";
   cin>>a>>b;
   swap(&a,&b);
   cout<<"a="<<a<<"  b="<<b<<endl;
   system("pause");
   return 0;
}
void swap(int *x,int *y)
{
    int tmp;
    tmp=*x;
    *x=*y;
    *y=tmp;   
    cout<<"*x="<<*x<<"  *y="<<*y<<endl;  
    
}
---------------------------------------------------------
#include <iostream>
using namespace std;
int main()
{
   int s[]={85,76,90,72};
   double w[]={0.3,0.3,0.2,0.2};
   int *a;
   double *b;
   a=s;
   b=w;
   double sum=0;
   int i;
   for(i=0;i<4;i++)
   {
      sum+= *a * *b;
      cout<<(*a)*(*b)<<endl;
      a++;
      b++;   
   }
   cout<<sum<<endl;

   system("pause");
   return 0;
}
---------------------------------------------------------------
#include <iostream>
using namespace std;
int main()
{
   char str[]="dev_c++";
   char* stringMax(char*);
   
   cout<<stringMax(str)<<endl;

   system("pause");
   return 0;
}

char*  stringMax(char* k)
{
    char* ans=k;
    while(*k!='\0')
    {
       if(*k>='a' && *k<='z')
         *k-=32;
       k++;
    }
    return ans;      
      
}
----------------------------------------------------------------
#include <iostream>
using namespace std;
int main()
{
   struct student
   {
      int id;
      char name[20];
      double h,w;
   };
   struct student a;
   cout<<"input id:";
   cin>>a.id;
   cout<<"input name:";
   cin>>a.name;
   cout<<"input h:";
   cin>>a.h;
   cout<<"input w:";
   cin>>a.w;
   cout<<a.id<<endl;
   cout<<a.name<<endl;
   cout<<a.h<<endl;
   cout<<a.w<<endl;
   system("pause");
   return 0;
}
---------------------------------------------------
#include <iostream>
using namespace std;
int main()
{
   struct student
   {
      int id;
      char name[20];
      double h,w;
   };
   struct student a={1,"mary",160.5,58};
   cout<<a.id<<endl;
   cout<<a.name<<endl;
   cout<<a.h<<endl;
   cout<<a.w<<endl;

   struct student  *p;
   p=&a;
   cout<<p->id<<endl;
   cout<<p->name<<endl;
   cout<<p->h<<endl;
   cout<<p->w<<endl;


   system("pause");
   return 0;
}
----------------------------------------------
#include <iostream>
using namespace std;
struct student
{
      int id;
      char name[20];
      double h,w;
};
int main()
{
  void printStudent(struct student*);
  struct student x={6,"joe",170.5,68.5};
  printStudent(&x);  
   system("pause");
   return 0;
}

void printStudent(struct student *p)
{
   cout<<p->id<<endl;
   cout<<p->name<<endl;
   cout<<p->h<<endl;
   cout<<p->w<<endl;
    
}

-------------------------------------------------------------
#include <iostream>
using namespace std;
int main()
{
  struct Book
  {
    int num;
    char name[20];
    int price,qty;
  };
  struct Book  b;
  cout<<"input num:";
  cin>>b.num;
  cout<<"input name:";
  cin>>b.name;
  cout<<"input price:";
  cin>>b.price;
  cout<<"input qty:";
  cin>>b.qty;
  int tal=b.price*b.qty;
  cout<<b.num<<endl;
  cout<<b.name<<endl;
  cout<<b.price<<endl;
  cout<<b.qty<<endl;
  cout<<tal<<endl;
   system("pause");
   return 0;
}
-----------------------------------------------------------
#include <iostream>
using namespace std;
struct student
{
      int id;
      char name[20];
      double h,w;
};
int main()
{
  struct student a[3];
  int i;
  for(i=0;i<3;i++)
  {
   cout<<"input id:";
   cin>>a[i].id;
   cout<<"input name:";
   cin>>a[i].name;
   cout<<"input h:";
   cin>>a[i].h;
   cout<<"input w:";
   cin>>a[i].w;          
  }
  printf("id    name           h     w \n");
  printf("=================================\n");
  for(i=0;i<3;i++)
    printf("%3d   %-10s %5.1lf %5.1lf  \n",a[i].id,a[i].name,a[i].h,a[i].w);

   system("pause");
   return 0;
}

----------------------------------------------------------------------
#include <iostream>
using namespace std;
struct student
{
      int id;
      char name[20];
      double h,w;
};
int main()
{
  struct student a[3];
  int i;
  for(i=0;i<3;i++)
  {
   cout<<"input id:";
   cin>>a[i].id;
   cout<<"input name:";
   cin>>a[i].name;
   cout<<"input h:";
   cin>>a[i].h;
   cout<<"input w:";
   cin>>a[i].w;          
  }
  struct student *p;
  p=a;
  printf("id    name           h     w \n");
  printf("=================================\n");
  for(i=0;i<3;i++)
  {  
      printf("%3d   %-10s %5.1lf %5.1lf  \n",p->id,p->name,p->h,p->w);
      p++;
  }


   system("pause");
   return 0;
}

創作回應

更多創作