编C语言程序“编程计算1到50中是7的倍数的数值之和”,使用for循环语句。

编C语言程序“编程计算1到50中是7的倍数的数值之和”,使用for循环语句。,第1张

#include <stdioh>

int main()

{

    int i,s = 1;

    for(i = 1 ; i <= 50; ++i){

        if( i % 7 == 0)

            s += i;

    }

    printf("%d\n",s);

    return 0;

}

#include "stdioh"

int main(int argc,char argv[]){

int i,k,t,m,p;

char s[50];

printf("Please enter an arithmetic expression(only + & -)\n");

scanf("%49s",s);

for(p=m=t=i=0;s[i];i++){

sscanf(s+i,"%d%n",&k,&t);

p  m-=k : m+=k;

if(s[i+=t]=='+')

p=0;

else if(s[i]=='-')

p=1;

else if(s[i]=='\0')

i--;

}

printf("\n%s = %d\n",s,m);

return 0;

}

运行样例:

用牛顿迭代法求方程(2(X-4)+3)X-6=0的根。

其迭代公式为X2=X1-F(X1)/F'(X1)

F'(X1)为对方程求导。本题中P'(X1)=(6x1-8)x1-3;

编译显示正确,但一运行就死机,我已经死了3次了。(一开始还以为电脑的问题)

#include<iostreamh>

#include<mathh>

void main(void)

{float x1,x2=100;

do

{x1=x2;

x2=(float)x1-(((2x1-4)x1+3)x1-6)/((6x1-8)x1-3);

}while(fabs(x2-x1)>pow(10,-5));

cout<<x2;

}

楼上的弄错了吧,楼主要的是计算“平方”和“立方”,不是“平方和”和“立方和”。

#include <stdioh>

int main()

{

int i;

for(i = 0; i <= 10; i++)

printf("%d\t%d\t%d\n", i, i i, i i i);

return 0;

}

计算的过程是这样的。

b=2+(a+=a++,a2,++a); a=2

先计算

2+(a+=a++,a2,++a) a=2

先计算

a+=a++,a2,++a a=2

先计算

a++,a2,++a a=2

先计算

a++ a++=2 a=3

再计算

a2 a2 = 6

再计算

++a ++a=4 a=4

逗号表达式的值是最右边的值

a++,a2,++a = 4

a+=a++,a2,++a

+=表达式最左边的 a=2

+=表达式右边的 a=4

结果a=6

#include <stdioh>

struct s_node

{

int data;

struct s_node next;

};

typedef struct s_node s_list;

typedef s_list link;

link operator=NULL;

link operand=NULL;

link push(link stack,int value)

{

link newnode;

newnode=(link) malloc(sizeof(s_list));

if(!newnode)

{

printf("\nMemory allocation failure!!!");

return NULL;

}

newnode->data=value;

newnode->next=stack;

stack=newnode;

return stack;

}

link pop(link stack,int value)

{

link top;

if(stack !=NULL)

{

top=stack;

stack=stack->next;

value=top->data;

free(top);

return stack;

}

else

value=-1;

}

int empty(link stack)

{

if(stack==NULL)

return 1;

else

return 0;

}

int is_operator(char operator)

{

switch (operator)

{

case '+': case '-': case '': case '/': return 1;

default:return 0;

}

}

int priority(char operator)

{

switch(operator)

{

case '+': case '-' : return 1;

case '': case '/' : return 2;

default: return 0;

}

}

int two_result(int operator,int operand1,int operand2)

{

switch(operator)

{

case '+':return(operand2+operand1);

case '-':return(operand2-operand1);

case '':return(operand2operand1);

case '/':return(operand2/operand1);

}

}

void main()

{

char expression[50];

int position=0;

int op=0;

int operand1=0;

int operand2=0;

int evaluate=0;

printf("\nPlease input the inorder expression:");

gets(expression);

while(expression[position]!='\0'&&expression[position]!='\n')

{

if(is_operator(expression[position]))

{

if(!empty(operator))

while(priority(expression[position])<= priority(operator->data)&&

!empty(operator))

{

operand=pop(operand,&operand1);

operand=pop(operand,&operand2);

operator=pop(operator,&op);

operand=push(operand,two_result(op,operand1,operand2));

}

operator=push(operator,expression[position]);

}

else

operand=push(operand,expression[position]-48);

position++;

}

while(!empty(operator))

{

operator=pop(operator,&op);

operand=pop(operand,&operand1);

operand=pop(operand,&operand2);

operand=push(operand,two_result(op,operand1,operand2));

}

operand=pop(operand,&evaluate);

printf("The expression [%s] result is '%d' ",expression,evaluate);

getch();

}

问题补充,因字数限制,挪到这

1拉格朗日插值简介:

对给定的n个插值节点x1,x2,…,xn,及其对应的函数值y1=f(x1), y2=f(x2),…, yn=f(xn);使用拉格朗日插值公式,计算在x点处的对应的函数值f(x);

2一维拉格朗日插值c语言程序:

Int lagrange(x0, y0, n, x, y)

Float xo[], yo[], x;

Int n;

Float y

{

Int i, j;

Float p;

y=0;

If (n>1)

{

For(i=0;i<n;i++)

{

P=1;

For(j=1;j<n;j++)

{

If(i!=J)

P=p(x-x0[j]/x0[i]-x0[j]);

}

y=y+py0[i];

Return(0);

}

Else

Return(-1);

}

3例题。已知函数如下表所示,求x=0472处的函数值:

X 046 047 048 049

Y 0484655 04903745 0502750 0511668

计算这个问题的c语言程序如下:

#minclude stdio

#includeM<nathh>

Main()

{

Float x0[4]={ 046, 047,048,049};

Float y0[4]={ 0484655 ,04903745 ,0502750 ,0511668};

Float x, y;

Int n, rtn;

N=4;

X=0472;

Rth=lagrange(x0,y0,n,x,&y);

If(rtn=0)

{

Prinf(“Y(0472)=:%f\n”,y);

}

Else

{

Prinf(“n must be larger than 1\n”);

}

}

计算结果:Y(0472)=:0495553

4问题补充

我的问题与上面的例子类似,计算三维空间一点(x,y,z)对应的函数值(Vx,Vy,Vz)不同的是自变量(point_coordinatetxt)为三维空间散乱点(不是正方体的顶点),因变量(point_datatxt)为矢量(向量 )。插值算法比较多,常数法,拉格朗日插值,埃特金插值,三阶样条插值等。最简单的就是常数法,查找离目标点(x,y,z)距离最近的已知自变量(Xi,Yi,Zi),把该点的函数值赋给目标点做函数值,求高手帮忙写写。

以上就是关于编C语言程序“编程计算1到50中是7的倍数的数值之和”,使用for循环语句。全部的内容,包括:编C语言程序“编程计算1到50中是7的倍数的数值之和”,使用for循环语句。、【c语言】键盘上键入一个算数表达式,计算其算数值,整个算数表达式要作为一个字符串来处理、数值计算方法上机题编程,,,用c语言编程序,用牛顿迭代法求18的倒数,精度为0.0005,求大神解等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zz/10114415.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存