#include <iostream>
#include <cfloat>
using namespace std;
struct Div_result
{
int quotient;
int remainder;
};
void div(int a, int b, Div_result & );
int main()
{
union Div
{
int anInt[2];
Div_result div_result;
}myDiv;
cout<<"Enter an Integer: "<<'\n';
do
{
cin>>myDiv.anInt[0];
if(cin.fail())
{
cout<<"Input error, you should enter an integer number.\n";
cin.clear();
cin.get();
continue;
}
else
break;
}while(1);
cout<<"Divide by: "<<'\n';
do
{
cin>>myDiv.anInt[1];
if(cin.fail())
{
cout<<"Input error, you should enter an integer number.\n";
cin.clear();
cin.get();
continue;
}
else if(myDiv.anInt[1] == 0)
{
cout<<"Input error, it can not be 0, please enter it again.\n";
cin.clear();
cin.get();
continue;
}
else
break;
}while(1);
div(myDiv.anInt[0],myDiv.anInt[1], myDiv.div_result);
cout<<"Quotient: "<<myDiv.div_result.quotient<<'\n';
cout<<"Remainder: "<<myDiv.div_result.remainder<<'\n';
system("pause");
return 0;
}
//
这里无需担忧异常情况,因为先前的输入已经保证分子,分母都为有效整数
void div(int a, int b, Div_result & div_result)
{
div_result.quotient = a/b;
div_result.remainder = a%b;
}
// union 是个我不喜欢的东西,处于节约存储空间的目的,C++ 保留了union,
// 但是很多程序员,对于 union 更本不予理睬, 认为这是多此一举, 也就是说, 你可以用struct
来替代 union.而反过来却并非一定可行, 原因是 union
数据变量每次只允许存储一类其 member variable type.
A union is a data format that can hold different data types but onlyone type at a time. That is, whereas a structue can hold, say, an intand long and a double, a union can hold an int or a long or a double.The syntax is like that for a structure, but the meaning is different.For example, consider the following declaration:
union one4all
{
int int_val;
long long_val;
double double_val;
};
You can use a one4all variable to hold an int, a long, or a double, just as long as you do so at different times:
one4all pail;
pail.int_val = 15;
// store an int
cout<<pail.int_val;
pail.double_val = 1.38;
// store a double, int value is lost
cout<<pail.double_val;
Thus, pail can serve as an int variable on one occasion and as a doublevariable at another time. The member name identifies the capacity inwhich the variable is acting. Because a union holds only one value at atime, it has to have space to hold its largest member. Hense, the sizeof the union is the size of its largest member.
One use for a union is to save space when a data item can use two ormore formats but never simulataneously. For example, suppose you managea mixed inventory of widgets, some of which have an integer ID, andsome of which have a string ID. In that case, you could use thefollowing:
struct widget
{
char brand[20];
int type;
unionid
// format depends on widget type
{
longid_num;
// type 1 widgets
char id_char[20];
// other widgets
} id_val;
};
...
widget prize;
...
if(prize.type == 1)
cin>>prize.id_val.id_num;
// use member name to indicate mode
else
cin>>prize.id_val.id_char;
An anonymous union has no name; in essence, its members becomevariables that share the same address. Naturally, only one member canbe current at a time:
struct widget
{
char brand[20];
int type;
union
// anonymous union
{
long id_num;
// type 1 widgets
char id_char[20]; // other widgets
};
};
...
widget prize;
...
if(prize.type == 1)
cin>>prize.id_num;
else
cin>>prize.id_char;
Because the union is anonymous, id_num and id_char are treated as twomembers of prize that share the same address. The need for an anintermediate identifier id_val is eliminated. It is up to theprogrammer to keep track of which choice is active.
To live41:
你可以装一个金山词霸,
买正版吧!!! 支持民族工业啊!!!