Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions C++_level1/queque.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include <iostream>
using namespace std;
typedef int Data_type;

struct Linked_list
{
Data_type data;
Linked_list * next;
};

class Queue
{
public:
Queue() : head(0),tail(0),num(0) {}
void append (Data_type date);
Data_type pop();
bool is_full() { return num >= Max_num; }
bool is_empty() { return num <= 0; }
private:
enum { Max_num = 100 };
Linked_list *head,*tail;
int num;
};


void Queue::append(Data_type data)
{
if(is_full())
{
cerr << "Fail to append, Queue is full!\n";
return;
}
if(is_empty())
{
head = tail = new Linked_list;
}
else
{
tail = tail->next = new Linked_list;
}
tail->data = data;
tail->next = 0;
num++;
}

Data_type Queue::pop()
{
if(is_empty())
{
cerr << "Fail to pop, Queue is empty!\n";
return -1;
}
Data_type data = head->data;
Linked_list *tmp = head;
head = head->next;
delete tmp;
num--;
return data;
}



int main()
{
Queue q;
q.append(1);
q.append(2);
q.append(3);
int data = q.pop();
cout << noskipws << data << endl;
data = q.pop();
cout << noskipws << data << endl;
data = q.pop();
cout << noskipws << data << endl;
data = q.pop();
cout << noskipws << data << endl;

for(int i = 0; i < 100; i++)
{
q.append(i);
}
q.append(1);
q.append(1);
return 0;
}


60 changes: 60 additions & 0 deletions C++_level1/stack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <iostream>
using namespace std;
typedef int Data_type;
class Stack
{
public:
Stack() : i(-1) {}
void push(Data_type data)
{
if(is_full())
{
cerr << "Fail to push, Stack is full!\n";
return;
}
arr[++i] = data;
}
Data_type pop()
{
if(is_empty())
{
cerr << "Fail to pop, Stack is empty\n";
return (-1);
}
return arr[i--];
}
bool is_full() { return (i >= Size - 1); }
bool is_empty() { return (i < 0); }
private:
enum { Size = 100 };
int i;
Data_type arr[Size];
};


int main()
{

Stack s;
s.push(1);
s.push(2);
s.push(3);
int data = s.pop();
cout << noskipws << data << endl;
data = s.pop();
cout << noskipws << data << endl;
data = s.pop();
cout << noskipws << data << endl;
data = s.pop();
cout << noskipws << data << endl;

for(int i = 0; i < 100; i++)
{
s.push(i);
}
s.push(1);
s.push(1);
return 0;
}


28 changes: 28 additions & 0 deletions level1/0runningLetter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <stdio.h>
#include <unistd.h>
#define COLUMN 80

int main(){
int blank,location;

for (location = 1; location <=COLUMN ; location++){
for (blank=1; blank < location; blank++){
printf(" ");
}
putchar('A');
usleep(2000000);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

定义常量

system("clear");
}
for (location = location-1; location >=1; location--){
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

闻到了重复的味道,请消除之

for (blank=1; blank < location; blank++){
printf(" ");
}
putchar('A');
usleep(200000);
system("clear");
}
return 0;
}



12 changes: 12 additions & 0 deletions level1/Diophantus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <stdio.h>

int main()
{
int x;
for(x=1; x<150; x++){
if(x/6 + x/12 + x/7 + 5 + x/2 + 4 ==x)
break;
}
printf("The father had lived for %d years.\n",x);
return 0;
}
30 changes: 30 additions & 0 deletions level1/Goldbach.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int isPrime(int x)
{
int i;
int dlm = sqrt((double)x);

for (i=2; i<=dlm; i++)
{ if (x%i==0) break; }
if (x==1) return 0;
else if (x==2 || x==3 || i>dlm)
{ return 1; }
else return 0;
return 0;
}

int main()
{
int i,j;

for (i=4; i<=100; i=i+2)
{ for (j=2; j<=i/2; j++)
{ if (isPrime(j) && isPrime(i-j))
{ printf("\n%d = prime%d + prime%d.",i,j,i-j);
break; } } }
return 0;
}

22 changes: 22 additions & 0 deletions level1/allPrime.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <stdio.h>
#include <math.h>
#include <time.h>

int main()
{
int i,j;
long begin,end;

begin = time(NULL);
printf("\nThe primes are:\n%-7d",2);
for (i=3; i<1000; i=i+2){
for (j=3; j<=(int)sqrt(i); j=j+2)
if (i%j == 0) break;
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

加大括号

if (j > (int)sqrt(i) ) printf("%-7d",i);
}
end = time(NULL);
printf("\nThe begin time is: %d.",begin);
printf("\nThe end time is: %d.",end);
printf("\nThe total time is: %lf seconds.",(end-begin)/1000000.0);
return 0;
}
7 changes: 7 additions & 0 deletions level1/hanoi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
void(

int mian()
{

23 changes: 23 additions & 0 deletions level1/isPrime.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <stdio.h>
#include <math.h>

int main()
{
int i,num;

printf("\nPlease enter a number: ");
scanf("%d",&num);
if (num == 2)
printf("\nIt's a prime.\n");
else{
for (i=2; i<=sqrt(num); i=i+2){
if (num % i==0){
printf("\nIt's not a prime.\n");
break;
}
}
if (i > sqrt(num))
printf("\nIt's a prime.\n");
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

大括号

}
return 0;
}
14 changes: 14 additions & 0 deletions level1/narcissus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <stdio.h>

int main()
{
int i,ones,tens,hundreds;
for (i=100; i<1000; i++){
ones = i % 10;
tens = i/10 %10;
hundreds = i/100;
if (ones*ones*ones + tens*tens*tens + hundreds*hundreds*hundreds == i)
printf("%7d\n",i);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

大括号

}
return 0;
}
26 changes: 26 additions & 0 deletions level1/runningLetter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <stdio.h>
#include <unistd.h>
#define COLUMN 80
#define PAUSE 100000

int main(){
int blank,location=1,back=0;

do
{ for (blank=1; blank<location; blank++)
{ printf(" "); }
puts("A");
usleep(PAUSE);
system("clear");
if (location==COLUMN) back=1;
if (!back) location++;
else location--;
}
while (location<=COLUMN && location>=1);

return 0;

}



30 changes: 30 additions & 0 deletions level1_2/Goldbach.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int isPrime(int x)
{
int i;
int dlm = sqrt((double)x);

for (i=2; i<=dlm; i++)
{ if (x%i==0) break; }
if (x==1) return 0;
else if (x==2 || x==3 || i>dlm)
{ return 1; }
else return 0;
return 0;
}

int main()
{
int i,j;

for (i=4; i<=100; i=i+2)
{ for (j=2; j<=i/2; j++)
{ if (isPrime(j) && isPrime(i-j))
{ printf("\n%d = prime%d + prime%d.",i,j,i-j);
break; } } }
return 0;
}

Binary file added level1_2/a.out
Binary file not shown.
Binary file added level1_2/encryptDecrept
Binary file not shown.
Loading