| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 948 人关注过本帖
标题:再看一下!怎么改?
只看楼主 加入收藏
freeday_zhao
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2004-12-11
收藏
 问题点数:0 回复次数:1 
再看一下!怎么改?

#include<iostream.h> #include<stdio.h> #include<math.h>

int main() { union{ int anInt[2]; div_t; div_result; };

cout<<"Enter an Integer: "<<'\n'; cin>>anInt[0];

cout<<"Divide by: "<<'\n'; cin>>anInt[1];

div_result=div(anInt[0],anInt[1]);

cout<<"Quotient: "<<div_result.quot<<'\n'; cout<<"Reminder: "<<div_result.rem<<'\n';

return 0; }

搜索更多相关主题的帖子: include return Enter 
2005-01-23 22:20
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
#include &lt;iostream&gt;
#include &lt;cfloat&gt;
using namespace std;

struct Div_result
{
    int quotient;
    int remainder;
};   

void div(int a, int b, Div_result &amp; );


int main()
{
    union Div
    {
        int anInt[2];
        Div_result div_result;
    }myDiv;
   
    cout&lt;&lt;"Enter an Integer: "&lt;&lt;'\n';
   
   
    do
    {
        cin&gt;&gt;myDiv.anInt[0];
        if(cin.fail())
        {
           cout&lt;&lt;"Input error, you should enter an integer number.\n";
            cin.clear();
            cin.get();
            continue;
        }
        else
           break;   
    }while(1);
   
    cout&lt;&lt;"Divide by: "&lt;&lt;'\n';
    do
    {
        cin&gt;&gt;myDiv.anInt[1];
        if(cin.fail())
        {
           cout&lt;&lt;"Input error, you should enter an integer number.\n";
            cin.clear();
            cin.get();
            continue;
        }
        else if(myDiv.anInt[1] == 0)
        {
           cout&lt;&lt;"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&lt;&lt;"Quotient: "&lt;&lt;myDiv.div_result.quotient&lt;&lt;'\n';
    cout&lt;&lt;"Remainder: "&lt;&lt;myDiv.div_result.remainder&lt;&lt;'\n';
   
    system("pause");
    return 0;
}

//  这里无需担忧异常情况,因为先前的输入已经保证分子,分母都为有效整数
void div(int a, int b, Div_result &amp; 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&lt;&lt;pail.int_val;
pail.double_val = 1.38;      // store a double, int value is lost
cout&lt;&lt;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&gt;&gt;prize.id_val.id_num;     // use member name to indicate mode
else
   cin&gt;&gt;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&gt;&gt;prize.id_num;
else
   cin&gt;&gt;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:
 你可以装一个金山词霸,  买正版吧!!! 支持民族工业啊!!!

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2005-01-24 03:27
快速回复:再看一下!怎么改?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028655 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved