作业罢了

This commit is contained in:
iridiumR 2021-10-03 10:28:37 +08:00
commit 1059058706
9 changed files with 437 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.exe
tempCodeRunnerFile.cpp

23
.vscode/c_cpp_properties.json vendored Normal file
View File

@ -0,0 +1,23 @@
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C:/Project/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
"C:/Project/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
"C:/Project/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:/Project/mingw64/bin/gcc.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-gcc-x64"
}
],
"version": 4
}

29
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,29 @@
{
// 使 IntelliSense
//
// 访: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "gcc.exe",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "C:/Project/mingw64/bin",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Project\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc.exe 生成活动文件"
}
]
}

4
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,4 @@
{
"C_Cpp.errorSquiggles": "Enabled",
"files.encoding": "gb2312"
}

27
.vscode/tasks.json vendored Normal file
View File

@ -0,0 +1,27 @@
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe 生成活动文件",
"command": "C:/Project/mingw64/bin/gcc.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:/Project/mingw64/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}

114
ex1.cpp Normal file
View File

@ -0,0 +1,114 @@
#include <stdio.h>
// #include <iostream>
#include <stdlib.h>
#include <time.h>
#define LENGTH 10000
//数组长度
#define OUTPUT
//是否输出
// #define RANDOM
//随机数
#define MODE_1
//MODE_1: 迭代
//MODE_2: 递归
int a[LENGTH];
void init()
{
#ifdef RANDOM
srand(time(NULL));
#endif
for(int i = 0 ; i < LENGTH ; i++)
#ifdef RANDOM
a[i]=1+rand()%LENGTH;
#else
a[i]=LENGTH-i;
#endif
}
void swap(int *a,int *b)
{
int c = *a;
*a = *b;
*b = c;
}
#ifdef MODE_1
void bobblesort1A(int a[],int n)
{
bool sorted = false;
A:
sorted = true;
for(int i = 1 ; i < n ; i++)
{
if(a[i-1] > a[i])
{
swap(&a[i-1],&a[i]);
sorted=false;
}
}
n--;
if(sorted==false)
goto A;
}
#elif defined MODE_2
void bobblesort1A(int a[],int n)
{
bool sorted = false;
sorted = true;
for(int i = 1 ; i < n ; i++)
{
if(a[i-1] > a[i])
{
swap(&a[i-1],&a[i]);
sorted=false;
}
}
n--;
if(n==1)
return;
bobblesort1A(a,n);
}
#endif
int main(){
init();
clock_t start_t, end_t;
double total_t;
#ifdef OUTPUT
printf("Before\n");
for(int i=0;i<LENGTH;i++)
printf("%d \n",a[i]);
#endif
start_t = clock();
bobblesort1A(a,LENGTH);
end_t = clock();
#ifdef OUTPUT
printf("After\n");
for(int i=0;i<LENGTH;i++)
printf("%d \n",a[i]);
#endif
printf("运算时间: %.4f 秒\n",((double)(end_t - start_t) / CLOCKS_PER_SEC));
system("pause");
}

189
ex2.cpp Normal file
View File

@ -0,0 +1,189 @@
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <time.h>
class Vec
{
private:
int *v;
int LEN;
int USED;
public:
Vec(int len)
{
LEN=len*2;
v=new int[LEN];
USED=9;
for(int i=0;i<10;i++)
v[i]=i+1;
for(int i=0;i<10;i++)
swap(i,rand()%10);
}
int get(int a)
{
return v[a];
}
void put(int a,int value)
{
v[a]=value;
}
void swap(int a, int b)
{
int temp = v[a];
v[a] = v[b];
v[b] = temp;
}
void expand()
{
LEN=LEN*2;
int *p=new int[LEN];
for(int i=0;i<=USED;i++)
p[i]=v[i];
delete[] v;
v=p;
}
void shrink()
{
LEN=LEN>>1;
int *p=new int[LEN];
for(int i=0;i<=USED;i++)
p[i]=v[i];
delete[] v;
v=p;
}
int insert(int locate,int value)
{
if(locate<0||locate>USED)
return 1;
USED++;
if(USED>=LEN)
expand();
for(int i=USED;i>locate;i--)
v[i]=v[i-1];
v[locate]=value;
return 0;
}
int del(int locate,int value)
{
if(locate<0||((USED-value)<0)||((locate+value-1)>USED))
return 1;
USED=USED-value;
for(int i=locate;i<=USED;i++)
v[i]=v[i+value];
if( ( (double)USED/(double)LEN )<=0.3 )
shrink();
return 0;
}
int find(int value)
{
int i = 0;
for(i;i<=USED;i++)
{
if(v[i]==value)
{
return i;
}
}
return -1;
}
void printall()
{
for(int i=0;i<USED;i++)
printf("%d\n",v[i]);
}
int getlen()
{
return LEN;
}
int getused()
{
return USED;
}
};
int main()
{
srand(time(NULL));
Vec v(10);
int temp[10]={0};
int flag;
printf("初始化,输出数组\n");
v.printall();
while(1)
{
printf("选择操作:\n1.插入\n2.删除\n3.查找\n");
scanf("%d",&flag);
switch(flag)
{
case 1:
printf("输入秩与待插入数值\n");
scanf("%d %d",&temp[0],&temp[1]);
(v.insert(temp[0],temp[1]))?(printf("非法输入\n")):(printf("输出数组\n"));
v.printall();
break;
case 2:
printf("输入秩与待删除个数\n");
scanf("%d %d",&temp[0],&temp[1]);
(v.del(temp[0],temp[1]))?(printf("非法输入\n")):(printf("输出数组\n"));
v.printall();
break;
case 3:
printf("输入要查找的数值\n");
scanf("%d",&temp[0]);
temp[2]=v.find(temp[0]);
(temp[2]==-1)?(printf("无此元素\n")):(printf("秩为%d \n",temp[2]));
break;
default:
printf("返回菜单\n");
}
flag=0;
}
return 0;
}

8
ex3.cpp Normal file
View File

@ -0,0 +1,8 @@
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("测试");
system("pause");
return 0;
}

41
workspace.code-workspace Normal file
View File

@ -0,0 +1,41 @@
{
"folders": [
{
"path": "."
}
],
"settings": {
"files.associations": {
"ctime": "cpp",
"iostream": "cpp",
"new": "cpp"
},
"files.encoding": "gb2312"
},
"launch": {
"version": "0.2.0",
"configurations": [
{
"name": "gcc.exe - 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "C:/Project/mingw64/bin",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Project\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc.exe 生成活动文件"
}
]
}
}