برای دیدن نسخه كامل اینجا را كلیك كنید : برنامه بردن عدد در مبنای دو با پشته
farnaz.m
2008/4/10, 09:43 AM
برنامه بنویسید که عددی را از ورودی دریافت کرده ان را به مبنای 2 تبدیل کند از پشته برای این کار استفاده کنید
rf.ariyapoor
2008/4/10, 06:15 PM
الگوریتمش خیلی ساده هست
شما باید عدد مورد نظرتون رو تا وقتی که برابر صفر نشده بر 2 تقسیم کنی و تو هر مرحله باقیمانده رو تو پشته بریزی و پس از اینکه عدد مورد نظر صفر شد باقیمانده ها رو از تو پشته باید بخونی و چون نحوه خوندن داده ها از پشته last in first out هست اگر باقیمانده ها رو به همون صورتی که از پشته می خونی چاپ کنی یا ذخیره کنی عدد مورد نظرت تو مبنای 2 بدست میاد
rf.ariyapoor
2008/4/11, 02:37 PM
این هم کد برنامه
#include <stdio.h>
#include <conio.h>
template <class KeyType>
class Stack
{
int Top;
KeyType *stack;
int MaxSize;
public:
Stack(int MaxStackSize=100);
bool IsFull();
bool IsEmpty();
void push(const KeyType& item);
KeyType* pop(KeyType &);
};
template <class KeyType>
Stack<KeyType>::Stack(int MaxStackSize)
:MaxSize(MaxStackSize)
{
stack=new KeyType[MaxSize];
Top=-1;
}
template <class KeyType>
bool Stack<KeyType>::IsFull()
{
if(Top==MaxSize-1)
return true;
else return false;
}
template <class KeyType>
bool Stack<KeyType>::IsEmpty()
{
if(Top==-1)return true;
else return false;
}
template <class KeyType>
void Stack<KeyType>::push(const KeyType& item)
{
if(IsFull())
printf("\nStack is full\n");
else
stack[++Top]=item;
}
template <class KeyType>
KeyType *Stack<KeyType>::pop(KeyType& item)
{
if(IsEmpty()){
printf("\nStack is Empty\n");
return 0;
}
item=stack[Top--];
return &item;
}
void main ()
{
int Number=0,Value=0;
Stack<int> stack;
printf("\nplease Enter Number:");
scanf("%d",&Number);
while (Number)
{
stack.push(Number%2);
Number=Number/2;
}
printf("\n");
while(stack.pop(Value))
printf("%d",Value);
getch();
return;
}فایل exe رو هم گذاشتم
Powered by vBulletin® Version 4.1.12 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.