《C++捷径教程》读书笔记--Chapter 10--结构与联合
来源:互联网 发布:c语言空格怎么 编辑:程序博客网 时间:2024/05/16 13:47
//--《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(<);
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(<);
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;
}
- 《C++捷径教程》读书笔记--Chapter 10--结构与联合
- 《C++捷径教程》读书笔记--Chapter 9--更多的数据类型与运算符(第一部分)
- 《C++捷径教程》读书笔记--Chapter 9--更多的数据类型与运算符(第二部分)
- 《C++捷径教程》读书笔记--Chapter 15--虚函数与多态(完结)
- 《C++捷径教程》读书笔记--Chapter 1--C++的发展历史
- 《C++捷径教程》读书笔记--Chapter 2--C++概述
- 《C++捷径教程》读书笔记--Chapter 3--基本数据类型
- 《C++捷径教程》读书笔记--Chapter 6--指针(第一部分)
- 《C++捷径教程》读书笔记--Chapter 6--指针(第二部分)
- 《C++捷径教程》读书笔记--Chapter 11--类(第一部分)
- 《C++捷径教程》读书笔记--Chapter 11--类(第二部分)
- 《C++捷径教程》读书笔记--Chapter 11--类(第三部分)
- 《C++捷径教程》读书笔记--Chapter 11--类(第四部分)
- 《C++捷径教程》读书笔记--Chapter 11--类(第五部分)
- 《C++捷径教程》读书笔记--Chapter 11--类(第六部分)
- 《C++捷径教程》读书笔记--Chapter 11--类(完结)
- 《C++捷径教程》读书笔记--Chapter 14--继承(完结)
- 《C++捷径教程》读书笔记--Chapter 16--模板(完结)
- Lucene 全文检索实践一
- Lucene 全文检索实践二
- Cannot create PoolableConnectionFactory
- Lucene 全文检索实践三
- Lucene 全文检索实践四
- 《C++捷径教程》读书笔记--Chapter 10--结构与联合
- PC硬件的动力源泉 谈驱动程序之基础篇[引用]
- 第一次使用blog
- 业务模块的设计原则
- 用ASP.NET上传大文件
- Wdm Driver Concept
- Delphi编程技巧集锦
- VOIP设备的配置及操作系统的要求
- 网页制作规范.