for (i = 5; i >= 0; i--) { if (s[i].count == 0) { strcpy(s[i].num, s[i+1].num); strcpy(s[i].goods, s[i+1].goods); strcpy(s[i].cosumer, s[i+1].cosumer); s[i].price = s[i+1].price; s[i].count = s[i+1].count; } } s[5].num[0] = '\0'; s[5].goods[0] = '\0'; s[5].cosumer[0] = '\0'; s[5].price = 0; s[5].count = 0; printf("请输入您需要购买的物资名称:"); scanf("%s", v); printf("请输入您的姓名:"); scanf("%s", x); for (i = 0; i < 6; i++) { if (strcmp(v, s[i].goods) == 0) { if (strcmp(x, s[i].cosumer) == 0) { printf("请输入您需要购买入库物资的数量:"); scanf("%d", &b); s[i].count += b; } else { printf("请输入您需要购买入库物资的数量:"); scanf("%d", &b); s[i+1].count = b; strcpy(s[i+1].goods, s[i].goods); s[i+1].price = s[i].price; strcpy(s[i+1].num, "007"); strcpy(s[i+1].cosumer, x); } } }
时间: 2024-04-27 09:22:19 浏览: 14
这是一段 C 语言代码,主要实现了一个简单的物资采购管理系统。首先,在一个长度为 6 的结构体数组 s 中,将数量为 0 的元素向前移动一个位置,然后将最后一个元素置空。接着,程序会要求用户输入需要购买的物资名称和用户姓名,并遍历整个数组,如果找到了相应的物资名称和用户姓名,则将其数量加上用户输入的数量;如果没有找到相应的物资名称和用户姓名,则将其添加到数组中,并且数量为用户输入的数量。最后,该代码没有返回任何值,只是对结构体数组 s 进行了修改。
相关问题
修改下列代码错误#include<bits/stdc++.h> using namespace std; char s[13][20]={0}; struct ArcNode { int adjest; ArcNode *next; }; typedef struct { int vertex; int count; ArcNode *firstedge; } VNode; class AdjGraph { private: int VertexNum; int ArcNum; public: VNode adjlist[100]; AdjGraph(int a[],int n,int e); ~AdjGraph(); }; AdjGraph::AdjGraph(int a[],int n,int e) { int i,j,k; VertexNum=n; ArcNum=e; for(i=1;i<=VertexNum;i++) { adjlist[i].vertex=a[i]; adjlist[i].firstedge=NULL; } int q; for(k=0;k<ArcNum;k++) { char s2[20]={0}; char s3[20]={0}; char s0[20]={0}; cin>>s2; cin>>s3; for(q=1;q<=n;q++) { s0=s[q]; if(strcmp(s0,s2)==0) i=q; if(strcmp(s0,s3)==0) j=q; } ArcNode *s=new ArcNode; s->adjest=j; s->next=adjlist[i].firstedge; adjlist[i].firstedge=s; } } AdjGraph::~AdjGraph() { } void TopSort(AdjGraph *G,int n) { int i,j,l=0; int b[100]={0}; int St[100],top=-1; ArcNode p; for (i=1;i<=n;i++) G->adjlist[i].count=0; for (i=1;i<=n;i++) { p=G->adjlist[i].firstedge; while (p!=NULL) { G->adjlist[p->adjest].count++; p=p->next; } } for (i=n;i>0;i--) if (G->adjlist[i].count==0) { top++; St[top]=i; } while (top>-1) { i=St[top];top--; b[l]=i; l++; p=G->adjlist[i].firstedge; while (p!=NULL) { j=p->adjest; G->adjlist[j].count--; if (G->adjlist[j].count==0) { top++; St[top]=j; } p=p->next; //找下一个邻接点 } } if(l!=n) { cout<<"False"; } else { for(i=0;i<l;i++) { cout<<s[b[i]]<<" "; } } } int main() { int n,e,i; ArcNode p; cin>>n>>e; char s1[20]={0}; for(i=1;i<=n;i++) { cin>>s1; s[i]=s1; } int a[100]={0}; for(i=1;i<=n;i++) { a[i]=i; } AdjGraph A(a,n,e); / for(i=1;i<=n;i++) { cout<<A.adjlist[i].vertex<<"--->"; p=A.adjlist[i].firstedge; while(p!=NULL) { cout<<p->adjest<<"--->"; p=p->next; } cout<<endl; }/ AdjGraph *G=&A; TopSort(G,n); }
这段代码存在多处错误,包括:
1. 包含了不必要的头文件 `<bits/stdc++.h>`,应该只包含需要的头文件。
2. `char s[13][20]={0}` 中,应该使用 `'\0'` 表示空字符,而不是 `0`。
3. `ArcNode *s=new ArcNode` 中,应该使用 `ArcNode* s=new ArcNode`。
4. `p->adjest` 中,应该使用 `p->adjvex`。
5. `//找下一个邻接点` 应该换行。
以下是修改后的代码:
```cpp
#include <iostream>
#include <cstring>
using namespace std;
char s[13][20] = {'\0'};
struct ArcNode {
int adjvex;
ArcNode* next;
};
struct VNode {
int vertex;
int count;
ArcNode* firstedge;
};
class AdjGraph {
private:
int VertexNum;
int ArcNum;
public:
VNode adjlist[100];
AdjGraph(int a[], int n, int e);
~AdjGraph();
};
AdjGraph::AdjGraph(int a[], int n, int e) {
int i, j, k;
VertexNum = n;
ArcNum = e;
for (i = 1; i <= VertexNum; i++) {
adjlist[i].vertex = a[i];
adjlist[i].firstedge = NULL;
}
int q;
for (k = 0; k < ArcNum; k++) {
char s2[20] = {'\0'};
char s3[20] = {'\0'};
char s0[20] = {'\0'};
cin >> s2;
cin >> s3;
for (q = 1; q <= n; q++) {
strcpy(s0, s[q]);
if (strcmp(s0, s2) == 0) i = q;
if (strcmp(s0, s3) == 0) j = q;
}
ArcNode* s = new ArcNode;
s->adjvex = j;
s->next = adjlist[i].firstedge;
adjlist[i].firstedge = s;
}
}
AdjGraph::~AdjGraph() {}
void TopSort(AdjGraph* G, int n) {
int i, j, l = 0;
int b[100] = {0};
int St[100], top = -1;
ArcNode* p;
for (i = 1; i <= n; i++) G->adjlist[i].count = 0;
for (i = 1; i <= n; i++) {
p = G->adjlist[i].firstedge;
while (p != NULL) {
G->adjlist[p->adjvex].count++;
p = p->next;
}
}
for (i = n; i > 0; i--)
if (G->adjlist[i].count == 0) {
top++;
St[top] = i;
}
while (top > -1) {
i = St[top];
top--;
b[l] = i;
l++;
p = G->adjlist[i].firstedge;
while (p != NULL) {
j = p->adjvex;
G->adjlist[j].count--;
if (G->adjlist[j].count == 0) {
top++;
St[top] = j;
}
p = p->next; //找下一个邻接点
}
}
if (l != n) {
cout << "False";
} else {
for (i = 0; i < l; i++) {
cout << s[b[i]] << " ";
}
}
}
int main() {
int n, e, i;
ArcNode* p;
cin >> n >> e;
char s1[20] = {'\0'};
for (i = 1; i <= n; i++) {
cin >> s1;
strcpy(s[i], s1);
}
int a[100] = {0};
for (i = 1; i <= n; i++) {
a[i] = i;
}
AdjGraph A(a, n, e);
// for(i=1;i<=n;i++) {
// cout<<A.adjlist[i].vertex<<"--->";
// p=A.adjlist[i].firstedge;
// while(p!=NULL) {
// cout<<p->adjvex<<"--->";
// p=p->next;
// }
// cout<<endl;
// }
AdjGraph* G = &A;
TopSort(G, n);
return 0;
}
```
检查下列代码错误#include<bits/stdc++.h> using namespace std; char s[13][20]={'\0'}; struct ArcNode { int adjest; ArcNode *next; }; typedef struct { int vertex; int count; ArcNode firstedge; } VNode; class AdjGraph { private: int VertexNum; int ArcNum; public: VNode adjlist[100]; AdjGraph(int a[],int n,int e); ~AdjGraph(); }; AdjGraph::AdjGraph(int a[],int n,int e) { int i,j,k; VertexNum=n; ArcNum=e; for(i=1;i<=VertexNum;i++) { adjlist[i].vertex=a[i]; adjlist[i].firstedge=NULL; } int q; for(k=0;k<ArcNum;k++) { char s2[20]={'\0'}; char s3[20]={'\0'}; char s0[20]={'\0'}; cin>>s2; cin>>s3; for(q=1;q<=n;q++) { strcpy(s0,s[q]);/**/ if(strcmp(s0,s2)==0) i=q; if(strcmp(s0,s3)==0) j=q; } ArcNode s=new ArcNode; s->adjest=j; s->next=adjlist[i].firstedge; adjlist[i].firstedge=s; } } AdjGraph::~AdjGraph() { } void TopSort(AdjGraph G,int n) { int i,j,l=0; int b[100]={0}; int top=-1; stack<int> s; ArcNode p; for (i=1;i<=n;i++) G->adjlist[i].count=0; for (i=1;i<=n;i++) { p=G->adjlist[i].firstedge; while (p!=NULL) { G->adjlist[p->adjest].count++; p=p->next; } } for (i=n;i>0;i--) if (G->adjlist[i].count==0) { s.push(i); } while (top>-1) { i=s.top(); s.pop(); b[l]=i; l++; p=G->adjlist[i].firstedge; while (p!=NULL) { j=p->adjest; G->adjlist[j].count--; if (G->adjlist[j].count==0) { s.push(j); } p=p->next; //找下一个邻接点 } } if(l!=n) { cout<<"False"; } else { for(i=0;i<l;i++) { cout<<s[b[i]]; if(i!=n-1) cout<<endl; } } } int main() { int n,e,i; ArcNode p; cin>>n>>e; char s1[20]={'\0'}; for(i=1;i<=n;i++) { cin>>s1; strcpy(s[i],s1); } int a[100]={0}; for(i=1;i<=n;i++) { a[i]=i; } AdjGraph A(a,n,e); / for(i=1;i<=n;i++) { cout<<A.adjlist[i].vertex<<"--->"; p=A.adjlist[i].firstedge; while(p!=NULL) { cout<<p->adjest<<"--->"; p=p->next; } cout<<endl; } */ AdjGraph *G=&A; TopSort(G,n); return 0; }
这段代码存在以下错误:
1. 头文件包含了 `#include<bits/stdc++.h>`,这是不标准的写法。应该只包含需要使用的头文件,例如可能需要包含 `<iostream>`、`<cstring>`、`<stack>`等等。
2. 在定义 `char s[13][20]` 数组时,使用了单引号,这是字符类型的写法,应该使用双引号表示字符串,例如 `char s[13][20] = {"\0"}`。
3. 在 `TopSort` 函数中,`AdjGraph G` 的类型应改为指针类型,即 `AdjGraph *G`,因为 `A` 是在 `main` 函数中定义的,是个局部变量,在函数执行完之后就会被销毁,而 `TopSort` 函数需要使用 `A` 中的数据,所以应该将其传入函数中。
4. 在 `TopSort` 函数中,有多处使用了 `->` 运算符,但是 `G` 是一个指针,应该使用 `*G` 来访问其指向的对象。
5. 在 `TopSort` 函数中,使用了 `stack<int> s` 定义了一个栈,但是在后面使用的时候没有使用 `s`,而是使用了 `G->adjlist`,这是错误的。
6. 在 `TopSort` 函数中,最后一行应该返回 `0`,而不是 `return l;`。
相关推荐
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)