如何用语句和算法写程序

如何用语句和算法写程序,第1张

算法步骤:第一步,要确定表示和的变量s和计数变量i,并赋值,一般情况下,赋s=0 i=0第二步,确定使用哪种循环结构,本题使用当型循环结构,确定判断条件i≤9 满足条件时,执行第三步,不满足条件时,执行第四步;第三步:执行,i=i+1第四步:输出s;程序结束.程序如下:S=0i=0WHILE i<=9 S=S+1/2^i i=i+1ENDPRINT SEND运行该程序,输出:S=1.9980. 解析 分 析: 算法分析: 第一步 选择一个变量S表示和,并赋给初值0 再选取一个循环变量i,并赋值为0;第二步 开始进入WHILE循环语句,首先判断i是否小于9;第三步 为循环表达式(循环体) 用WEND来控制循环;第四步 用END来结束程序. 根据算法语句编写相应的程序语言,见参考答案.试题 解析: 算法步骤:第一步,要确定表示和的变量s和计数变量i,并赋值,一般情况下,赋s=0 i=0第二步,确定使用哪种循环结构,本题使用当型循环结构,确定判断条件i≤9 满足条件时,执行第三步,不满足条件时,执行第四步;第三步:执行,i=i+1第四步:输出s;程序结束.可写出程序如下:S=0i=0WHILE i<=9 S=S+1/2^i i=i+1ENDPRINT SEND运行该程序,输出:S=1.9980. (12分) 考点: 程序语言.

刚好这两天也在玩链表,想写个模板.给你一段代码

#include<stdio.h>

#include<stdlib.h>

#include<malloc.h>

typedef int DataType//数据类型,如果这里更改了类型,

//要注意更改链表输出函数中输出格式.

struct _Node

{

DataType data

struct _Node *next

}Node

typedef struct _Node list_single  

list_single *Node_Create(DataType data)//新建结点

{

    list_single *p=NULL    

    p=(list_single *)malloc(sizeof(list_single))

    if(p==NULL)

        {  

        printf("malloc fair!\n")  

        exit(1)

    }     

    p->data=data

    p->next=NULL

    //printf("data=%d\n", p->data)

    return p

}

void Push_Back(list_single *head, DataType data)//表尾添加 

{  

    

    list_single *p= NULL     

    if (head==NULL)  

    {  

        head = Node_Create(data)  

        p=head

    }  

    else  

    {  

        p=head  

        while (p->next)  

        {  

           p= p->next  

        }           

    }  

    p->next = Node_Create(data) 

}  

void List_Print(list_single *head)//打印链表

{

    list_single *p=head

    printf("Head")

    while(p->next)

    {

        p=p->next

        //printf("%3c",p->data)

        printf(" ->%d",p->data )//注意保持这个输出格式与DataType一致

    }

    printf("\n")

}

int List_Count(list_single *head)//表长

{

    int k=0

    list_single *p=head    

    while(p->next)

    {

        p=p->next

        k++        

    }

    return k

}

list_single *List_Create(void)//建表

{

    int x

    list_single *head=NULL

    list_single *p=NULL

    head=Node_Create(NULL)//建头结点

    p=head

    if(p==NULL)

        {  

        printf("malloc fair!\n")  

        exit(1)

    }     

    printf("输入一个数x\n")//追加结点

    scanf("%d",&x)

    while(x!=-1)

    {

        Push_Back(p,x)//追加结点到表尾,或者只用下面两句代码,效果一样

        //p->next = Node_Create(x)         

        //p=p->next

        scanf("%d",&x)

    }    

    return head//返回表头

}

int main() 

{

list_single *p

p=List_Create()//建表

List_Print(p)//输出

printf("Size:%d\n",List_Count(p))//输出表长

return 0

}

/*

你提供的那个代码是伪代码,我帮你写成了

C

的代码。

你提供的伪代码,我用注释写到了对应的行的后面。

根据你提供的代码还只能转换含有一个字母或

[0,9]

区间内的数字的解析式。

补充了一点栈的基本算法

执行后输出:

式子(中缀表达式):

a+b*c

新式子(后缀表达式):

a

b

c

*

+

代码使用

Linux

+

GCC

3.4.5

编译通过

July.28th,2009

14:30pm.

main()

在最下边

*/

#include

struct

_stack

{

char

__stack[1024]

int

__top

}

//

模拟栈

inline

init_stack(

struct

_stack

*

p_stack

)

//

初始化栈

{

int

i

=

0

for(

i

=

0

i

<=

1024

i++

)

p_stack->__stack[i]

=

'\0'

p_stack->__top

=

0

}

inline

void

push(

struct

_stack

*

p_stack,

char

p_op

)

//

进栈

{

p_stack->__stack[p_stack->__top]

=

p_op

(

p_stack->__top

)++

}

inline

char

pop(

struct

_stack

*

p_stack

)

//

出栈

{

(

p_stack->__top

)--

return

p_stack->__stack[p_stack->__top]

}

inline

int

precedence(

char

p_char

)

//

比较优先级

{

if(

p_char

==

'+'

||

p_char

==

'-'

)

return

1

else

if(

p_char

==

'/'

||

p_char

==

'*'

)

return

2

else

return

0

}

char

postString[1024]

char

*

infix_to_postfix(

char

*inString

)

{

int

l

=

strlen(

inString

)

struct

_stack

stack

char

p

=

'\0'

int

i

=

0

char

c

=

'\0'

l

=

strlen(

inString

)

//

l

<-

Length

of

inString

for(

i

=

0

i

<=

1024

i++

)

postString[i]

=

'\0'

//

postString

<-

empty

string

init_stack(

&stack

)

//

stack

<-

Empty

stack

for(

i

=

0

i

<=

l

i++

)

//

for

i

<-

0

to

l

{

c

=

inString[i]

//

c

<-

inString[i]

if(

(

c

>=

'0'

&&

c

<=

'9'

)||(

c

>=

'a'

&&

c

<=

'z'

)||(

c

>=

'A'

&&

c

<=

'Z'

)

)

//

if

c

is

an

operand

{

if(

postString[0]

==

'\0'

){postString[0]

=

ccontinue}

sprintf(

postString,

"%s

%c",

postString,

c

)

//

add

c

at

the

end

of

postString

}//

end

if

if(

c

==

'+'

||

c

==

'-'

||

c

==

'*'

||

c

==

'/'

)

//

if

c

is

an

operator

{

if(

stack.__top==0

||

precedence(

stack.__stack

[stack.__top]

)<

precedence(

c

)

)

//

if

stack

is

empty

OR

if

precedence(

stack_top

)<

precedence(

c

)

{

push(

&stack,

c

)

//

push

c

into

stack

}

else

{

while(

precedence(

stack.__stack

[stack.__top]

)>=

precedence(

c

)

)

//

while

precedence(

stack_top

)>=

precedence(

c

)

{

p

=

pop(

&stack

)

//

p

<-

pop

from

stack

sprintf(

postString,

"%s

%c",

postString,

p

)

//

add

p

at

the

end

of

postString

}

//

end

while

push(

&stack,

c

)

//

push

c

into

stack

}

//

end

if

}

//

end

if

}

//

end

for

while(

stack.__top

)

//

while

stack

is

not

empty

{

p

=

pop(

&stack

)

//

p

<-

pop

from

stack

sprintf(

postString,

"%s

%c",

postString,

p

)

//

add

p

at

the

end

of

postString

}

//

end

while

return

postString

}

int

main(

)

{

char

exp[]

=

"a+b*c"

char

*

p

=

0

p

=

infix_to_postfix(

exp

)

printf(

"原式子(中缀表达式):\n\t%s\n新式子(后缀表达式):\n\t%s\n",

exp,

p

)

return

0

}


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

原文地址:https://54852.com/yw/12065043.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存