㈠ 用c語言編寫開平方根程序的問題!
用c++寫吧,我對c語言不是很了解,不過你在求的是平方根啊,就應該輸出的是兩個值啊,是吧?
在輸入和輸出的時候應該用%d吧。只是一個求平方根的問題,就是調用一個函數sqrt()在有一個頭文件就可以了啊!
㈡ c語言寫一程序求100內可開平方的數
#include<stdio.h>
#include<math.h>
voidmain()
{
intn;
for(n=1;n<=100;n++)
if((int)sqrt((double)n)==sqrt((double)n))
printf("%-4d",n);
}
㈢ 求用C語言編寫一個可以進行 加 減 乘 除 平方 開方 的程序,並且要考慮優先順序
#include "stdafx.h"
#define STACK_INIT_SIZE 10
#define STACKINCRESIZE 5
typedef struct
{
SElemType *base;
SElemType *top;
int stack_size;
}SqStack;
Status InitStack(SqStack &S)
{
S.base=(ElemType*)malloc(STACK_INIT_SIZE*sizeof(ElemType));
if(!S.base)exit(OVERFLOW);
S.top=S.base;
S.stack_size=STACK_INIT_SIZE;
return OK;
}
Status Push(SqStack &S,SElemType e)
{
if((S.top-S.base)>=STACK_INIT_SIZE){
S.base=(ElemType*)realloc(S.base,(S.stack_size+STACKINCRESIZE)*(sizeof(SqStack)));
if(!S.base)exit(OVERFLOW);
S.top=S.base+S.stack_size;
S.stack_size+=STACK_INIT_SIZE;
}
*(S.top)++=e;
return OK;
}
Status Pop(SqStack &S,SElemType &e)
{
if(S.top==S.base)return ERROR;
e=*--S.top;
return OK;
}
Status StackEmpty(SqStack &S)
{
if(S.top==S.base)
return TRUE;
else return FALSE;
}
int GetTop(SqStack S,ElemType &e)
{
if(S.top >S.base )
{
e=*(S.top-1) ;
return OK;
}
else return ERROR;
}
/*#include "stdafx.h"
#include "MyStack.h"
int InitStack(SqStack &S)
{
S.base =(ElemType *)malloc(STACKSIZE*sizeof(ElemType));
S.top =S.base ;
S.stacksize =STACKSIZE;
return OK;
}
int GetTop(SqStack S,ElemType &e)
{
if(S.top >S.base )
{
e=*(S.top-1) ;
return OK;
}
else return ERROR;
}
int Push(SqStack &S,Elemtype e)
{
if(S.top -S.base >=S.stacksize )
{
S.base=(ElemType *)realloc(S.base ,(S.stacksize +ADDSIZE)sizeof(ElemType));
S.top =S.stacksize +S.base ;
S.stacksize =S.stacksize +ADDSIZE;
}
*(S.top)++=e;
return OK;
}
int Pop(SqStack &S,ElemType &e)
{
if(S.base !=S.top )
{
e=*(--S.top );
return OK;
}
else return ERROR;
}*/
#include "stdafx.h"
#include "stdafx.h"
#include "MyStack.h"
//#include "MyStack.cpp"
ElemType Precede(ElemType t1,ElemType t2)
{
ElemType f='0';
switch(t2)
{
case '+':
case '-':
if(t1=='('||t1=='=')
f='<';
else f='>';
break;
case '*':
case '/':
if(t1=='*'||t1=='/'||t1==')')
f='>';
else f='<';
break;
case '(':
if(t1==')')
{
cout<<"ERROR"<<endl;
exit(ERROR);
}
else f='<';
break;
case ')':switch(t1)
{
case '(':f='=';
break;
case '=':printf("ERROR2\n");
exit(ERROR);
default: f='>';
}
break;
case'=':switch(t1)
{
case '=':f='=';
break;
case '(':cout<<"ERROR"<<endl;
default:f='>';
}
break;
}
return f;
}
int In(ElemType e)
{
switch(e)
{
case'+':
case'-':
case'*':
case'/':
case'(':
case')':
case'=':return TRUE;
default:return FALSE;
}
}
ElemType Operate(ElemType a,ElemType theta,ElemType b)
{
ElemType re=0;
switch(theta)
{
case'+':re=a+b;
break;
case'-':re=a-b;
break;
case'*':re=a*b;
break;
case'/':re=a/b;
break;
}
return re;
}
ElemType EvaluateExpression()
{
ElemType x,a,b,theta,d;
char c;
char z[6];
SqStack OPTR,OPND;
InitStack(OPTR);Push(OPTR,'=');
InitStack(OPND);
c=getchar();
GetTop(OPTR,x);
while(c!='='||x!='=')
{
if(In(c)) // 是7種運算符之一
switch(Precede(x,c))
{
case'<':Push(OPTR,c); // 棧頂元素優先權低
c=getchar();
break;
case'=':Pop(OPTR,x); // 脫括弧並接收下一字元
c=getchar();
break;
case'>':Pop(OPTR,theta); // 退棧並將運算結果入棧
Pop(OPND,b);
Pop(OPND,a);
Push(OPND,Operate(a,theta,b));
break;
}
else if(c>='0'&&c<='9') // c是操作數
{
int i=0;
do
{
z[i]=c;
i++;
c=getchar();
}while(c>='0'&&c<='9');
z[i]=0;
d=atoi(z);
Push(OPND,d);
}
else // c是非法字元
{
printf("ERROR4\n");
exit(ERROR);
}
GetTop(OPTR,x);
}
GetTop(OPND,x);
return x;
}
// Calculator.cpp : 定義控制台應用程序的入口點。
//
#include "stdafx.h"
#include "Calculator.h"
int _tmain(int argc, _TCHAR* argv[])
{
printf("請輸入算術表達式,負數要用(0-正數)表示,並以=結束\n");
printf("%d\n",EvaluateExpression());
getchar();
system("pause");
return 0;
}
如果需要源碼的話,827222866 ,
㈣ C語言編寫一段程序,求輸入的數的平方以及立方分別是多少
#include <stdio.h>
int main()
{ double x;
scanf("%lf",&x);
printf("%g %g ", x*x,x*x*x);
return 0;
}
㈤ 用C語言編程:輸入一個整數,求它的平方,立方,平方根(結果保留2位小數 )
#include<stdio.h>
#include<math.h>
intmain(void)
{
intn;
scanf("%d",&n);
printf("%d%d%f",n*n,n*n*n,sqrt(n));
return0;
}
㈥ 怎樣用C語言編寫開平方根程序
在C語言中,可以使用庫函數sqrt來實現開根號計算。
1 頭文件:math.h
2 聲明:
double sqrt(double n);
3 功能:
將參數n開平方後,得到算數平方根返回。
4 調用形式:
sqrt(100);
為計算100的平方根。
㈦ c語言寫一程序求100內可以開平方所以數字
#include<stdio.h>
#include<math.h>
voidmain()
{
intn;
for(n=1;n<=100;n++)
if((int)sqrt((double)n)==sqrt((double)n))
printf("%-4d",n);
}
㈧ c語言程序里怎麼開平方
兩種常用的方法。
都需要include <math.h>
1 用sqrt
double sqrt(double n);\
求參數n的平方根
2 用pow
double pow(double n, double e);
計算n的e次冪
這樣
pow(n,0.5);
就是平方根了
明顯 第一種更實用。
㈨ C語言編程 :編一個程序,輸入一個數給出平方
#include
包含stdio.h這個頭文件,因為輸入和輸入函數存放在這里
void
main(void)
定義主函數,返回值類型為空,參數為空
{
int
n;
定義一個變數n
scanf("%d",n)
輸入一個數給n賦值
printf("%d",n*n)
輸出顯示n*n
即n的平方
}
㈩ C語言程序設計中開平方根要怎麼表示開N次方根呢
1、平方根
C語言中sqrt函數是指的開方運算函數,得到的結果是函數變數(可以是數值,也可以是變數名)的算術平方根。
2、N次方根
double pow(double x,double n)
是math.h里一函數,求x的n次冪
x必須>0,
立方根就是1/3次冪了
但這里要用3.0或1.0/3,否則成了整除結果為1
(10)c語言寫一程序求開平方的數擴展閱讀:
C語言函數
double acos(double x) 返回x的反餘弦cos-1(x)值,x為弧度
double asin(double x) 返回x的反正弦sin-1(x)值,x為弧度
double atan(double x) 返回x的反正切tan-1(x)值,x為弧度
double atan2(double y,double x) 返回y/x的反正切tan-1(x)值,y的x為弧度
ldexp()函數:返回x乘以2的exponent次方(次冪)的值
labs()函數:求整數的絕對值(針對long類型)
isgraph()函數:判斷一個字元是否是圖形字元
isdigit()函數:判斷一個字元是否為數字
iscntrl()函數:判斷一個字元是否為控制字元
isalpha()函數:判斷一個字元是否是字母
isalnum()函數:判斷一個字元是否是字母或者數字