《C++捷徑教程》讀書筆記–Chapter 10–結構與聯合

來源:互聯網
上載者:User

//--《C++捷徑教程》讀書筆記--Chapter 10--結構與聯合
//--Chapter 10--結構與聯合
//--11/24/2005 Thurs.
//--Computer Lab
//--Liwei

//--程式#1  使用數組結構
#include <iostream>
#include <cctype>
#include <cstring>
#include <cstdlib>
using namespace std;

const int SIZE=10;
struct inv_type{
 char item[40];
 double cost;
 double retail;
 int on_hand;
 int lead_time;
} invtry[SIZE];

void enter(),init_list(),display();
void update(),input(int i);
int menu();

int main()
{
 char choice;
 init_list();

 for(;;){
  choice=menu();
  switch(choice){
  case 'e': enter();
          break;
  case 'd': display();
      break;
  case 'u': update();
      break;
  case 'q': return 0;
  }//switch
 }//for

 cout<<endl<<"======================="<<endl;
    //getchar();
    return 0;
}

void init_list()
{
 int t;
 for(t=0; t<SIZE; t++)
  *invtry[t].item='/0';
}

int menu()
{
 char ch;
 cout<<endl;

 do{
  cout<<"(E)nter/n";
  cout<<"(D)isplay/n";
  cout<<"(U)pdate/n";
  cout<<"(Q)uit/n/n";
  cout<<"choose one: ";
  cin>>ch;
 }while(!strchr("eduq", tolower(ch)));
 
 return tolower(ch);
}

void enter()
{
 int i;
 for(i=0; i<SIZE; i++)
  if(!*invtry[i].item)
   break;

 if(i==SIZE){
  cout<<"List full./n"; 
  return;
 }

 input(i);
}

void input(int i)
{
 cout<<"Item: ";
 cin>>invtry[i].item;

 cout<<"Cost: ";
 cin>>invtry[i].cost;

 cout<<"Retail price: ";
 cin>>invtry[i].retail;

 cout<<"On hand: ";
 cin>>invtry[i].on_hand;

 cout<<"Lead time to resupply (in days): ";
 cin>>invtry[i].lead_time;
}

void update()
{
 int i;
 char name[80];

 cout<<"Enter item: ";
 cin>>name;

 for(i=0; i<SIZE; i++)
  if(!strcmp(name,invtry[i].item))
   break;

 if(i==SIZE){
  cout<<"Item not found./n"; 
  return;
 }

 cout<<"Enter new info: /n";
 input(i);
}

void display()
{
 int t;

 for(t=0; t<SIZE; t++){
  //if(*invtry[t].item){
   cout<<invtry[t].item<<endl;
   cout<<"Cost: $"<<invtry[t].cost;
   cout<<"/nRetail: $";
   cout<<invtry[t].retail<<endl;
   cout<<"On hand: "<<invtry[t].on_hand;
   cout<<"/nResupply time: ";
   cout<<invtry[t].lead_time<<" days /n/n";
  //}//if
 }//for

}

//--程式#2  傳遞一個結構給函數
#include <iostream>
using namespace std;

struct sample{
 int a;
 char ch;
};

void f1(sample parm);

int main()
{
 struct sample arg;
 arg.a=1000;
 arg.ch='X';

 f1(arg);

 cout<<endl<<"======================="<<endl;
    //getchar();
    return 0;
}

void f1(sample parm)
{
 cout<<parm.a<<" "<<parm.ch<<endl;
}

//--程式#3  結構的賦值
#include <iostream>
using namespace std;

struct stype{
 int a,b;
};

int main()
{
 stype svar1,svar2;

 svar1.a=svar1.b=10;
 svar2.a=svar2.b=20;

 cout<<"Structures before assignment./n";
 cout<<"svar1: "<<svar1.a<<' '<<svar1.b;
 cout<<endl;
 cout<<"svar2: "<<svar2.a<<' '<<svar2.b;
 cout<<endl<<endl;

 svar2=svar1;

 cout<<"Structures after assignment./n";
 cout<<"svar1: "<<svar1.a<<' '<<svar1.b;
 cout<<endl;
 cout<<"svar2: "<<svar2.a<<' '<<svar2.b;
 cout<<endl<<endl;

 cout<<endl<<"======================="<<endl;
    //getchar();
    return 0;
}

//--程式#4  系統時間
#include <iostream>
#include <ctime>
using namespace std;

int main()
{
 struct tm *ptr;
 time_t lt;

 lt=time('/0');
 ptr=localtime(&lt);

 cout<<ptr->tm_hour<<':'<<ptr->tm_min;
 cout<<':'<<ptr->tm_sec;

 cout<<endl<<"======================="<<endl;
    //getchar();
    return 0;
}

//--程式#5  系統時間
#include <iostream>
#include <ctime>
using namespace std;

int main()
{
 struct tm *ptr;
 time_t lt;

 lt=time('/0');
 ptr=localtime(&lt);

 cout<<asctime(ptr);

 cout<<endl<<"======================="<<endl;
    //getchar();
    return 0;
}

//--程式#6  結構引用的用法
#include <iostream>
using namespace std;

struct mystruct{
 int a;
 int b;
};

mystruct &f(mystruct &var);

int main()
{
 mystruct x,y;
 x.a=10;
 x.b=20;

 cout<<"Original x.a and x.b: ";
 cout<<x.a<<' '<<x.b<<endl;

 y=f(x);

 cout<<x.a<<' '<<x.b<<endl;

 cout<<y.a<<' '<<y.b<<endl;

 cout<<endl<<"======================="<<endl;
    //getchar();
    return 0;
}

mystruct &f(mystruct &var)
{
 var.a=var.a*var.a;
 var.b=var.b/var.b;
 mystruct b=var;
 
 return var;//注意這裡是引用的 引用
}

//--程式#7  聯合的用法
#include <iostream>
using namespace std;

void disp_binary(unsigned u);

union swap_bytes{
 short int num;
 char ch[2];
};

int main()
{
 swap_bytes sb;
 char temp;

 sb.num=15;// 0000 0000 0000 1111
 
 cout<<"Original bytes: ";
 disp_binary(sb.ch[1]);
 cout<<"    ";
 disp_binary(sb.ch[0]);
 cout<<endl<<endl;

 temp=sb.ch[0];
 sb.ch[0]=sb.ch[1];
 sb.ch[1]=temp;

 cout<<"Exchanged bytes: ";
 disp_binary(sb.ch[1]);
 cout<<"    ";
 disp_binary(sb.ch[0]);
 cout<<endl<<endl;

 cout<<endl<<"======================="<<endl;
    //getchar();
    return 0;
}

void disp_binary(unsigned u)
{
 register int t;

 for(t=128; t>0; t=t/2)
  if( u&t)
   cout<<"1 ";
  else
   cout<<"0 ";

}

//--程式#8  聯合的用法 以二進位形式輸出ASCII
#include <iostream>
#include <conio.h>
using namespace std;

struct byte{
 unsigned a:1;
 unsigned b:1;
 unsigned c:1;
 unsigned d:1;
 unsigned e:1;
 unsigned f:1;
 unsigned g:1;
 unsigned h:1;
};

union bits{
 char ch;
 struct byte bit;
}ascii;

void disp_bits(bits b);

int main()
{
 do{
  cout<<"Enter a char: ";
  cin>>ascii.ch;
  cout<<"/nASCII: ";
  disp_bits(ascii);
 }while(ascii.ch!='q');

 cout<<endl<<"======================="<<endl;
    //getchar();
    return 0;
}

void disp_bits(bits b)
{
 if(b.bit.h) cout<<"1 ";
  else cout<<"0 ";
 if(b.bit.g) cout<<"1 ";
  else cout<<"0 ";
 if(b.bit.f) cout<<"1 ";
  else cout<<"0 ";
 if(b.bit.e) cout<<"1 ";
  else cout<<"0 ";
 if(b.bit.d) cout<<"1 ";
  else cout<<"0 ";
 if(b.bit.c) cout<<"1 ";
  else cout<<"0 ";
 if(b.bit.b) cout<<"1 ";
  else cout<<"0 ";
 if(b.bit.a) cout<<"1 ";
  else cout<<"0 ";

 cout<<endl<<endl;

}

//--程式#9  匿名聯合
#include <iostream>
using namespace std;

int main()
{
    union{
  short int count;
  char ch[2];
 };

 ch[0]='X';
 ch[1]='Y';
 cout<<ch[0]<<ch[1]<<endl;
 cout<<count<<endl;

 cout<<endl<<"======================="<<endl;
    //getchar();
    return 0;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.