需要帮助我写的程序 [英] Need help with a program I am writing

查看:60
本文介绍了需要帮助我写的程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个程序来计算一个数字(基数),该数字可以是一个整数,负数或零,而不包括数学库。这就是我到目前为止所写的内容。请帮助我。


#include< iostream>

使用命名空间std;


int main()

{

double x,y;


cout<< 输入基数:;

cin>> x;

cout<< 输入整数指数:

cin>> y;


double i = 1;

double product = x;


while(y> i )

{

产品* = x;

i ++;

}


返回0;

}


我的问题是我如何设置while语句来计算让我们说4提升到-2?我是否在身体中加入了if语句?非常感谢您的帮助。

解决方案

如果您使用函数执行指数部分然后从主函数调用该函数,那么看看这个程序是如此简单>

这里是'我今天发布的代码4有人问你同样的问题:


#include< stdio.h>

#include< conio.h>

#include< iostream.h>


int integerPower(int,int); //这是一个原型

void main()// void main或int main不行,但我更喜欢void main

{clrscr(); //只需在每次运行程序时清除屏幕4

int exp;

int base;


printf("输入整数基数和指数:");

cin>> base> ;> exp;

cout<<<<<""<<<<<<<<" equals ="<< integerPower(base, exp);

getch(); //等待一个角色返回主程序

}


int integerPower(int b,int e)

{

int product = 1;


for(int i = 1; i< = e; i ++)

product * = b;

返回产品;

}

如果您需要任何关于此代码的帮助,请发布给我......

这是我在c ++上的第一堂课,我没有一位教授可以完全教我,所以我基本上都是这样。我必须只用头文件< iostream>编写程序。没有其他的。此外,我应该只使用while循环并考虑指数的正负值。


我必须说感谢您的回复和帮助。对于如何仅使用所需的头文件进行此练习,您还有其他建议吗?





/>


如果您使用函数执行指数部分然后从主函数调用该函数,那么这个程序是如此简单


这是'我今天发布的代码4有人问你同样的问题:


#include< stdio.h>

#include< ; conio.h>

#include< iostream.h>


int integerPower(int,int); //这是一个原型

void main()// void main或int main不行,但我更喜欢void main

{clrscr(); //只需在每次运行程序时清除屏幕4

int exp;

int base;


printf("输入整数基数和指数:");

cin>> base> ;> exp;

cout<<<<<""<<<<<<<<" equals ="<< integerPower(base, exp);

getch(); //等待一个角色返回主程序

}


int integerPower(int b,int e)

{

int product = 1;


for(int i = 1; i< = e; i ++)

product * = b;

返回产品;

}

如果您需要任何有关此代码的帮助,请发布给我......


你不需要其他头文件...


#include< conio.h>我用它来写clrscr();正如我告诉你每次看到输出时为你清除屏幕但你可能根本不使用它,但每次你运行程序时你会看到之前写的相同的句子,所以如果你多次运行这个程序你将有一个非常DIRTY屏幕(输出的黑屏)....


也#include< stdio.h>没有必要我只是用它因为我认为我可以使用printf和scanf但不要担心你可以删除它alllll


和关于for循环你可以使用任何其他循环就像现在一样但是现在我不能为你提供while循环因为我现在必须睡觉...但是答应发布你tommorow


顺便说一下ru from ??


I need to write a program that will compute a number (base) raised to a power which can be an integer, negative or zero with out including the math library. This is what I wrote thus far. Please help me.

#include <iostream>
using namespace std;

int main()
{
double x, y;

cout << "Enter a base number: ";
cin >> x;
cout << "Enter a integer exponent: "
cin >> y;

double i = 1;
double product = x;

while ( y > i)
{
product *= x;
i++;
}

return 0;
}

My problem is how do I set up the while statement to compute lets say 4 raised to the -2? Do I include an if statement in the body? Your help is greatly appreciated.

解决方案

look hunny this program is so easy if you used functions to do the exponential part and then call that function from the main

here''s the code i''v posted it today 4 someone asking your same question :

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

int integerPower(int ,int ); //this is a prototype
void main() //void main or int main won''t matter but i prefer void main
{ clrscr(); //just to clear the screen 4 you each time you run your program
int exp;
int base;

printf("Enter integer base and exponent:");
cin>>base>>exp;
cout<<base<<"to the power of"<<exp<<"equals="<<integerPower(base,exp);
getch(); //waits for a character to return to main program
}

int integerPower(int b,int e)
{
int product=1;

for(int i=1; i<=e; i++)
product*=b;
return product;
}
if you need any help with this code just post me ......


This is my first lesson in c++ and I don''t have a professor to teach me throughly so I am basically winging it. I must write the program only with the header file <iostream> nothing else. Also I should only use the while loop and take into consideration positive, negative values for the exponent.


I must say thanking you for responding and for your help. Do you have any other suggestion as to how to do this exercise with only the required header file?







look hunny this program is so easy if you used functions to do the exponential part and then call that function from the main

here''s the code i''v posted it today 4 someone asking your same question :

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

int integerPower(int ,int ); //this is a prototype
void main() //void main or int main won''t matter but i prefer void main
{ clrscr(); //just to clear the screen 4 you each time you run your program
int exp;
int base;

printf("Enter integer base and exponent:");
cin>>base>>exp;
cout<<base<<"to the power of"<<exp<<"equals="<<integerPower(base,exp);
getch(); //waits for a character to return to main program
}

int integerPower(int b,int e)
{
int product=1;

for(int i=1; i<=e; i++)
product*=b;
return product;
}
if you need any help with this code just post me ......


hunny you don''t need the other header files ...

#include<conio.h> i used it in order to write clrscr(); which as i told you clears the screen for you each time you see the output but you may not use it at all but each time you run your program you will see the same sentences that were written before so if you run this program many times you will have a very DIRTY screen ( the black screen for output)....

also #include<stdio.h> is not necessary i just used it coz i thought i may use printf and scanf but don''t worry you can remove it at alllll

and about the for loop you can use any other loop just like the while but right now i can''t provide you with the while loop coz i have to sleep now ...but promise to post you tommorow

by the way where r u from ??


这篇关于需要帮助我写的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆