解释这段代码//增加信息 sy *add() { sy *head,*p,*q; head=p=(sy *)malloc(sizeof(sy)); int b; printf("请问你想输入几人信息\n"); scanf("%d",&b); for(i=0;i<b;i++) { q=(sy *)malloc(sizeof(sy)); printf("请输入员工编号:"); scanf("%d",&q->num); printf("请输入员工姓名:"); scanf("%s",q->name); printf("请输入该员工入职年月:"); scanf("%s",q->time); printf("请输入员工岗位工资:"); scanf("%f",&q->salary); printf("请输入员工奖金:"); scanf("%f",&q->bonus); printf("请输入员工扣款:"); scanf("%f",&q->withhold); //计算实发工资---实发工资=岗位工资+奖金-扣款 q->net_payroll=q->salary+q->bonus-q->withhold; //更新是否删除的标识 q->FLAG=0; p->next=q; p=q; printf("\n"); } p->next=NULL; return head; }
时间: 2024-04-27 07:23:05 浏览: 17
这段代码定义了一个名为add()的函数,用于向程序中的员工工资链表中增加一批新的员工工资信息。该函数返回链表的头指针。函数内部首先定义了三个指针变量:head、p、q,其中head和p指向链表的头节点,q是用于新建节点的临时指针。然后使用malloc()函数分配了一块内存空间,用于存储头节点的信息,并将p指向该节点。接着,函数会询问用户要输入几个员工的信息,并将这个数字存储在变量b中。接下来使用for循环,循环次数为输入的员工数b。在循环中,首先使用malloc()函数分配一块内存空间,用于存储新的员工工资信息,并将q指向该节点。然后通过scanf()函数读取用户输入的员工信息,包括员工编号、姓名、入职年月、岗位工资、奖金和扣款。接着计算实发工资,并将其存储在节点的net_payroll字段中。最后将节点插入到链表中,并将p指向新的节点。循环结束后,将最后一个节点的next指针设置为NULL,并返回链表的头指针head。函数的作用是向链表中增加新的员工工资信息。
相关问题
基于flex/bison对SysY语⾔进行语法分析 代码
下面是一个基于Flex/Bison对SysY语言进行词法分析和语法分析的示例代码:
Flex文件(sysy.l):
```c
%{
#include "sysy.tab.h"
%}
%%
"int" { return INT; }
"if" { return IF; }
"else" { return ELSE; }
"while" { return WHILE; }
"return" { return RETURN; }
"or" { return OR_OP; }
"and" { return AND_OP; }
"==" { return EQ_OP; }
"!=" { return NE_OP; }
"<=" { return LE_OP; }
">=" { return GE_OP; }
"+" { return '+'; }
"-" { return '-'; }
"*" { return '*'; }
"/" { return '/'; }
"%" { return '%'; }
"!" { return '!'; }
"=" { return '='; }
"<" { return '<'; }
">" { return '>';}
";" { return ';'; }
"," { return ','; }
"(" { return '('; }
")" { return ')'; }
"{" { return '{'; }
"}" { return '}'; }
[0-9]+ { yylval.num = atoi(yytext); return NUM; }
[a-zA-Z][a-zA-Z0-9]* { yylval.id = strdup(yytext); return ID; }
[ \t\n] { /* ignore whitespace */ }
. { fprintf(stderr, "Unknown character: %s\n", yytext); }
%%
int yywrap() {
return 1;
}
```
Bison文件(sysy.y):
```c
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define YYSTYPE_IS_DECLARED
#define YYSTYPE int
#include "sysy.tab.h"
extern int yylex();
extern int yylineno;
extern char* yytext;
void yyerror(const char *msg);
%}
%token INT ID IF ELSE WHILE RETURN
%token <num> NUM
%left OR_OP
%left AND_OP
%left EQ_OP NE_OP
%left '<' '>' LE_OP GE_OP
%left '+' '-'
%left '*' '/' '%'
%right '!'
%right '='
%left UMINUS
%%
program:
function_list
;
function_list:
function
| function_list function
;
function:
type_specifier ID '(' parameter_list ')' compound_stmt
;
type_specifier:
INT
;
parameter_list:
parameter_decl
| parameter_list ',' parameter_decl
;
parameter_decl:
type_specifier ID
;
compound_stmt:
'{' local_declarations statement_list '}'
;
local_declarations:
/* empty */
| local_declarations var_declaration
;
var_declaration:
type_specifier ID ';'
;
statement_list:
/* empty */
| statement_list statement
;
statement:
expression_stmt
| compound_stmt
| selection_stmt
| iteration_stmt
| return_stmt
;
expression_stmt:
expression ';'
| ';'
;
selection_stmt:
IF '(' expression ')' statement
| IF '(' expression ')' statement ELSE statement
;
iteration_stmt:
WHILE '(' expression ')' statement
;
return_stmt:
RETURN ';'
| RETURN expression ';'
;
expression:
assignment_expression
;
assignment_expression:
logical_or_expression
| unary_expression '=' assignment_expression
;
logical_or_expression:
logical_and_expression
| logical_or_expression OR_OP logical_and_expression
;
logical_and_expression:
equality_expression
| logical_and_expression AND_OP equality_expression
;
equality_expression:
relational_expression
| equality_expression EQ_OP relational_expression
| equality_expression NE_OP relational_expression
;
relational_expression:
additive_expression
| relational_expression '<' additive_expression
| relational_expression '>' additive_expression
| relational_expression LE_OP additive_expression
| relational_expression GE_OP additive_expression
;
additive_expression:
multiplicative_expression
| additive_expression '+' multiplicative_expression
| additive_expression '-' multiplicative_expression
;
multiplicative_expression:
unary_expression
| multiplicative_expression '*' unary_expression
| multiplicative_expression '/' unary_expression
| multiplicative_expression '%' unary_expression
;
unary_expression:
postfix_expression
| '-' unary_expression %prec UMINUS
| '!' unary_expression
;
postfix_expression:
primary_expression
| ID '(' argument_list ')'
;
primary_expression:
ID
| NUM
| '(' expression ')'
;
argument_list:
/* empty */
| expression
| argument_list ',' expression
;
%%
int main(int argc, char **argv) {
yyparse();
return 0;
}
void yyerror(const char *msg) {
fprintf(stderr, "Syntax error at line %d: %s\n", yylineno, msg);
exit(1);
}
```
在main函数中调用yyparse函数,就可以启动语法分析器进行语法分析。当语法分析器遇到错误时,会通过yyerror函数输出错误信息并退出程序。
查找字符串“aksyyegj”中sy个数//代码
很高兴回答你的问题。在字符串“aksyyegj”中,sy的数量为1。以下是相应的代码:
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "aksyyegj";
int count = ;
for (int i = ; i < strlen(str) - 1; i++) {
if (str[i] == 's' && str[i+1] == 'y') {
count++;
}
}
printf("字符串中sy的数量为:%d\n", count);
return ;
}
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)