第一部分暂时只写主程序,目前的程序只包括了正确性检查,内存检查待完成
示例代码
#include <iostream>
#include <cstdio>
#include <algorithm>
using std::cin;
using std::cout;
using std::endl;
using std::sort;
class vector{
private:
int size;
int number;
int * data;
public:
vector(int size){
this->size=size;
number=0;
data=new int[size];
}
vector(vector &v){
size=v.size;
number=v.number;
data=new int[size];
for (int i = 0; i <number ; ++i) {
data[i]=v.data[i];
}
}
vector(){
size=5;
data=new int[size];
number=0;
}
~vector(){
//这里故意空着
}
void insert(int value){
if(number==size){
expand();
}
data[number]=value;
number++;
}
void expand(){
int *temp=new int[size];
for (int i = 0; i < size; ++i) {
temp[i]=data[i];
}
delete[] data;
data=new int[2*size];
for(int i=0;i<size;i++){
data[i]=temp[i];
}
size=size*2;
delete [] temp;
}
void show(){
for (int i = 0; i <number ; ++i) {
cout<<data[i]<<" ";
}
cout<<endl;
}
void sort_array(){
sort(data,data+number);
}
};
int main(){
freopen("testcase.in","r",stdin);
vector v1(5);
for (int i = 0; i <7 ; ++i) {
v1.insert(i);
}
v1.show();
vector v2(v1);
int input;
for (int i=0;i<5;i++){
cin>>input;
v2.insert(input);
}
v2.show();
v1.show();
v2.sort_array();
v2.show();
return 0;
}
判题代码
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
using namespace std;
int case_test();
//int mem_test();
int main(){
cout<<"case test running......"<<endl;
case_test();
cout<<"memory test running......"<<endl;
// mem_test();
return 0;
}
int case_test(){
pid_t p1=fork();
if(p1==0){//child process
int fd=open("your_vector.txt",O_RDWR | O_CLOEXEC | O_TRUNC | O_CREAT, S_IRWXU | S_IRWXG);
if(fd<0){
perror("Failed to open txt file");
exit(1);
}
dup2(fd,STDOUT_FILENO);
execlp("./your_vector.out","./your_vector.out",NULL);
perror("Execlp failed:");
exit(1);
}
else if(p1>0){//parent process
int status;
pid_t w_result=waitpid(p1,&status,0);
if(w_result==-1){
perror("Failed to wait");
exit(1);
}
pid_t p2=fork();
if(p2==0){//child process
int fd_null=open("/dev/null",O_RDWR);
dup2(fd_null,STDOUT_FILENO);
execlp("diff","diff","your_vector.txt","your_vector_solution.txt");
perror("Execlp diff failed");
exit(1);
}
else if(p2>0){
int status2;
waitpid(p2,&status2,0);
if(WIFEXITED(status2)){
if(WEXITSTATUS(status2)!=0){
cout<<"case test failed"<<endl;
}
else{
cout<<"case test passed"<<endl;
}
}
}
else{
perror("Failed to fork");
exit(1);
}
}
else{
perror("Failed to fork");
exit(1);
}
return 0;
}