if(strcmp(s[i].startcity,x[0].startcity)==0&&strcmp(s[i].arrivecity,x[0].arrivecity)==0)
时间: 2024-04-07 10:29:07 浏览: 67
这也是一个条件语句,它的意思是:如果s[i]的startcity属性与x[0]的startcity属性相等,并且s[i]的arrivecity属性与x[0]的arrivecity属性相等,那么就执行接下来的语句。其中,s[i]和x[0]都是结构体或对象,.startcity和.arrivecity分别是它们的属性,strcmp同样是用来比较两个字符串是否相等的函数。如果两个字符串相等,strcmp返回0。
相关问题
if(strcmp(s[i].startcity,name1)==0&&strcmp(s[i].arrivecity,name2)==0)
这是一个C语言中的字符串比较语句,用于判断两个字符串是否相等。其中,strcmp()函数返回0表示两个字符串相等,否则返回非零值。在这个语句中,s是一个结构体数组,startcity和arrivecity是结构体中的两个字符串变量,name1和name2是需要比较的字符串。如果两个字符串都相等,则条件成立。
将此c++代码转换为c语言代码#include<iostream> #include<cstdlib> #include<cstdio> #include<stdio.h> #include<string.h> using namespace std; #define OK 1 #define ERROR 0 #define OVERFLOW - 2 #define MAXSIZE 100 typedef int Status; typedef int SElemType; typedef struct { SElemType *base; SElemType *top; int stacksize; } SqStack; Status InitStack(SqStack &s) { s.base = new SElemType[MAXSIZE]; if(!s.base) exit(OVERFLOW); s.top = s.base; s.stacksize = MAXSIZE; return OK; } void DestroyStack(SqStack &s) { delete []s.base; s.base = s.top = NULL; s.stacksize = MAXSIZE; } Status Push(SqStack &s, int x) { if((s.top-s.base)==s.stacksize)return ERROR; *s.top=x; s.top++; return OK; } int Pop(SqStack &s) { int x; if(s.base==s.top)return ERROR; s.top--; x=*s.top; return x; } void PrintStack(SqStack s) { for(SElemType *top = s.top - 1; top >= s.base; top--) { cout << (*top); if(top != s.base) cout << ' '; } cout << endl; } int main() { SqStack s; char op[10]; int x,y,temp,sum,len,i; InitStack(s); while(scanf("%s",op)&&strcmp(op,"@")) { if(!strcmp(op," ")) { scanf("%s",op); } else if(strcmp(op,"/")&&strcmp(op,"*")&&strcmp(op,"+")&&strcmp(op,"-")) { temp=1,sum=0; len=strlen(op); for(i=len-1;i>=0;i--) { sum=sum+(op[i]-'0')*temp; temp*=10; } Push(s,sum); } else if(!strcmp(op,"+")) { x=Pop(s); y=Pop(s); Push(s,y+x); } else if(!strcmp(op,"-")) { x=Pop(s); y=Pop(s); Push(s,y-x); } else if(!strcmp(op,"/")) { x=Pop(s); y=Pop(s); Push(s,y/x); } else if(!strcmp(op,"*")) { x=Pop(s); y=Pop(s); Push(s,y*x); } } PrintStack(s); DestroyStack(s); return 0; }
以下是将该代码转换为 C 语言代码后的结果:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXSIZE 100
typedef int Status;
typedef int SElemType;
typedef struct {
SElemType *base;
SElemType *top;
int stacksize;
} SqStack;
Status InitStack(SqStack *s) {
s->base = (SElemType *) malloc(MAXSIZE * sizeof(SElemType));
if (!s->base) {
exit(OVERFLOW);
}
s->top = s->base;
s->stacksize = MAXSIZE;
return OK;
}
void DestroyStack(SqStack *s) {
free(s->base);
s->base = s->top = NULL;
s->stacksize = MAXSIZE;
}
Status Push(SqStack *s, int x) {
if ((s->top - s->base) == s->stacksize) {
return ERROR;
}
*(s->top) = x;
(s->top)++;
return OK;
}
int Pop(SqStack *s) {
int x;
if (s->base == s->top) {
return ERROR;
}
(s->top)--;
x = *(s->top);
return x;
}
void PrintStack(SqStack s) {
for (SElemType *top = s.top - 1; top >= s.base; top--) {
printf("%d", *top);
if (top != s.base) {
printf(" ");
}
}
printf("\n");
}
int main() {
SqStack s;
char op[10];
int x, y, temp, sum, len, i;
InitStack(&s);
while (scanf("%s", op) && strcmp(op, "@")) {
if (!strcmp(op, " ")) {
scanf("%s", op);
} else if (strcmp(op, "/") && strcmp(op, "*") && strcmp(op, "+") && strcmp(op, "-")) {
temp = 1;
sum = 0;
len = strlen(op);
for (i = len - 1; i >= 0; i--) {
sum = sum + (op[i] - '0') * temp;
temp *= 10;
}
Push(&s, sum);
} else if (!strcmp(op, "+")) {
x = Pop(&s);
y = Pop(&s);
Push(&s, y + x);
} else if (!strcmp(op, "-")) {
x = Pop(&s);
y = Pop(&s);
Push(&s, y - x);
} else if (!strcmp(op, "/")) {
x = Pop(&s);
y = Pop(&s);
Push(&s, y / x);
} else if (!strcmp(op, "*")) {
x = Pop(&s);
y = Pop(&s);
Push(&s, y * x);
}
}
PrintStack(s);
DestroyStack(&s);
return 0;
}
```
主要的改动如下:
- 使用 `<stdio.h>` 和 `<stdlib.h>` 头文件代替 `<iostream>` 和 `<cstdlib>` 头文件;
- 去掉 `using namespace std;`;
- 将 `typedef struct` 改为 `typedef`;
- 将 `SqStack &` 改为 `SqStack *`,并在函数内使用指针访问结构体成员;
- 将 `cout` 改为 `printf`;
- 将 `strcmp` 改为 `strcmp` 函数;
- 将 `new` 和 `delete` 改为 `malloc` 和 `free`。
阅读全文