void xiuf(int *k){ while( !(cin>>*k) || cin.peek()!='\n' ) { cin.clear(); cin.ignore( numeric_limits<streamsize>::max(), '\n' ); cout << "输入数据错误,请重新输入:" << endl; } }
时间: 2024-03-29 22:35:54 浏览: 14
这段代码定义了一个函数xiuf,函数参数为一个指向整型变量的指针k。
函数的作用是从标准输入流(cin)中读取一个整数,并将该整数存储到k所指向的变量中。如果输入的不是整数或者输入的整数后面还有其他字符,则清空输入流并忽略掉输入流中的所有字符,然后输出错误提示信息并要求重新输入。这样可以确保输入的数据格式正确。
相关问题
完善void _0_1::_1(){ int a; cin>>s.num[N]; cin>>s.name; cin>>s.sex; cin>>s.score1; cin>>s.score2; cin>>s.score3; i.v.push_back(s); cin>>a; if(a==1){return _1();} return ; } void __1::_2(){ int a=3; vector<_>::iterator p=i.v.begin(
void _0_1::_1() {
int a;
student s;
cin >> s.num[N];
cin >> s.name;
cin >> s.sex;
cin >> s.score1;
cin >> s.score2;
cin >> s.score3;
i.v.push_back(s);
cout << "是否继续添加学生信息?\n1. 继续\n2. 返回上一级菜单\n";
cin >> a;
if (a == 1) {
return _1();
}
return;
}
void __1::_2() {
int a = 3;
vector<_>::iterator p = i.v.begin();
cout << "请输入要删除的学生学号:";
string num;
cin >> num;
for (; p != i.v.end(); p++) {
if (p->num == num) {
i.v.erase(p);
a = 1;
break;
}
}
if (a == 1) {
cout << "删除成功!" << endl;
} else {
cout << "未找到该学生记录!" << endl;
}
system("pause");
}
这里给出了两个修改后的函数 `_1` 和 `_2`,可能还需要对程序中的一些变量或类进行调整。
在 `_1` 中,我们添加了一个 `student` 类型的变量 `s`,用于存储新添加的学生信息。我们在输入学生信息后,将 `s` 插入到 `vector` 容器 `i.v` 的末尾,并询问用户是否继续添加学生信息。如果用户输入了 1,那么继续调用 `_1` 函数进行添加;否则返回上一级菜单。
在 `_2` 中,我们添加了一个 `vector<_>::iterator` 类型的迭代器 `p`,用于遍历 `i.v` 容器中的学生信息。我们先让用户输入要删除的学生的学号,然后在容器中查找是否有符合条件的学生记录。如果找到了,就通过 `vector` 容器的 `erase()` 方法将该学生记录从容器中删除,并输出删除成功信息;否则输出未找到该学生记录的提示信息。
#include<iostream> using namespace std; /* * Definition of ListNode */ class ListNode { public: int val; ListNode *next; ListNode(int val) { this->val = val; this->next = NULL; } }; class Solution { public: ListNode* buildList(int* arr,int n) { ListNode *head=new ListNode,*val,*cul=*head; for(int j=0;j<n;j++){ val=new ListNode; cul->next=val; cul->ListNode(*(arr+i)); cul=val; } return head; } ListNode* reverse(head) { ListNode* hh,*left=head,*right; hh=head->next; right=hh->next; hh->next=NULL; while(right!=NULL){ left=hh; hh=right; right=right->next; hh->next=left; } return hh; } void printList(head) { while(head!=NULL){ cout<<head->val<<' '; head=head->next; } } }; int main() { int n; cin>>n; int* arr = new int[n]; for(int i=0;i<n;i++) cin>>arr[i]; Solution obj; ListNode* head = obj.buildList(arr,n); head = obj.reverse(head); obj.printList(head); delete[] arr; return 0; }这段代码错哪了,怎么改
这段代码有以下几个问题:
1. 在 `buildList` 函数中,第一行的 `*head` 是一个未初始化的指针,会导致程序崩溃。应该将其改为 `head = new ListNode`,即创建一个新的节点并将其地址赋给 `head`。
2. 在 `buildList` 函数中,第 5 行的 `cul=*head` 是错误的,应该将其改为 `cul=head`,即将 `cul` 初始化为 `head`。
3. 在 `reverse` 函数中,第 2 行应该指定 `head` 的类型为 `ListNode*`,即 `ListNode* reverse(ListNode* head)`。
4. 在 `reverse` 函数中,第 4 行的 `right=rigth->next` 中的 `rigth` 拼写错误,应该改为 `right=right->next`。
5. 在 `printList` 函数中,第一行应该指定 `head` 的类型为 `ListNode*`,即 `void printList(ListNode* head)`。
下面是修改后的代码:
```
#include <iostream>
using namespace std;
/* Definition of ListNode */
class ListNode {
public:
int val;
ListNode* next;
ListNode(int val) {
this->val = val;
this->next = NULL;
}
};
class Solution {
public:
ListNode* buildList(int* arr, int n) {
ListNode* head = new ListNode(0);
ListNode* val, * cul = head;
for (int j = 0; j < n; j++) {
val = new ListNode(*(arr + j));
cul->next = val;
cul = val;
}
return head->next;
}
ListNode* reverse(ListNode* head) {
ListNode* hh, * left = head, * right;
hh = head->next;
right = hh->next;
hh->next = NULL;
while (right != NULL) {
left = hh;
hh = right;
right = right->next;
hh->next = left;
}
return hh;
}
void printList(ListNode* head) {
while (head != NULL) {
cout << head->val << ' ';
head = head->next;
}
}
};
int main() {
int n;
cin >> n;
int* arr = new int[n];
for (int i = 0; i < n; i++)
cin >> arr[i];
Solution obj;
ListNode* head = obj.buildList(arr, n);
head = obj.reverse(head);
obj.printList(head);
delete[] arr;
return 0;
}
```