Skip to content
Open

HW #3

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
28 changes: 28 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\MinGW\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
27 changes: 27 additions & 0 deletions HomeWorks/HW1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <iostream>
using namespace std;

int main01()
{
int a, b, c;
cout << "a=";
cin >> a;
cout << "b=";
cin >> b;
/* //A
c=a;
a=b;
b=c;*/

/* //B
a+=b;
b=a-b;
a-=b;*/

/* //C
a*=b;
b=a/b;
a/=b;*/
return 0;

}
137 changes: 137 additions & 0 deletions HomeWorks/HW13.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#include <iostream>
using namespace std;

#define Element int

struct ArrayStack
{
Element* data;
int n;
int sp;
};

void arrStackCreate(ArrayStack*& stack, int n)
{
stack = new ArrayStack();
stack->data = new int[n];
stack->n = n;
stack->sp = -1;
for (int i = 0; i < n; i++)
{
stack->data[i] = 0;
}
}


bool arrStackPush(ArrayStack* stack, Element element)
{
bool result = (stack->sp < stack->n - 1);
if (result)
{
stack->sp++;
stack->data[stack->sp] = element;
}
return result;
}


bool arrStackPushs(ArrayStack* stack, Element* element, int n)
{
bool result = true;
for (int i = 0; result && (i < n); i++)
{
result = arrStackPush(stack, element[i]);
}
if (!result)
{
cout << "ERROR: Stack Overflow" << endl;
}
return result;
}

bool arrStackIsEmpty(ArrayStack* stack)
{
return (stack->sp < 0);
}

Element* arrStackPop(ArrayStack* stack, bool* result = NULL)
{
Element* element = NULL;
bool status = arrStackIsEmpty(stack);
if (result == NULL)
{
result = &status;
}
else
{
*result = status;
}
if (!*result)
{
element = &stack->data[stack->sp];
stack->sp--;
}
return element;
}

void arrStackDelete(ArrayStack* stack)
{
delete[] stack->data;
delete stack;
}

void arrStackPrint(ArrayStack* stack, const char* Message)
{
cout << "\nStack: " << Message << endl;
cout << "n = " << stack->n << endl;
cout << "sp = " << stack->sp << endl;
for (int i = 0; i <= stack->sp; i++)
{
cout << "element[" << i << "] = " << stack->data[i] << endl;
}
}
void arrPrintStack(ArrayStack* stack) {
int size = stack->n;
if (size == 0) {
cout << "Stack is empty" << endl;
return;
}
for (int i = 0; i < size; i++) {
cout << stack->data[i] << " ";
}
cout << endl;
}

int main()
{
int n1 = 12, n2 = 10, n3 = n1 + n2;
ArrayStack* stack1 = NULL;
ArrayStack* stack2 = NULL;
ArrayStack* stack3 = NULL;
Element* elemArray1 = new Element[n1]{ 2, 2, 3, 4, 3, 6, 7, 1, 9, 10,66,101 };
Element* elemArray2 = new Element[n2]{ 7, 2, 4, 4, 5, 6, 7, 8, 9, 10 };
arrStackCreate(stack1, n1);
arrStackCreate(stack2, n2);
arrStackCreate(stack3, n3);
arrStackPushs(stack1, elemArray1, n1);
arrStackPushs(stack2, elemArray2, n2);
arrStackPushs(stack3, elemArray1, n1);
arrStackPushs(stack3, elemArray2, n2);
//arrStackPrint(stack1, "After Add Array of Elements");
cout << "stack 1 = ";
arrPrintStack(stack1);
cout << "stack 2 = ";
arrPrintStack(stack2);
cout << "stack 3 = ";
while (!arrStackIsEmpty(stack3))
{
cout << *arrStackPop(stack3)<< " ";
}
cout << endl;
arrStackDelete(stack1);
arrStackDelete(stack2);
arrStackDelete(stack3);
//
cout << "\ndone" << endl;
return 0;
}
59 changes: 59 additions & 0 deletions HomeWorks/HW14.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <iostream>
using namespace std;

class Queue {
int size;
int* queue;

public:
Queue() {
size = 0;
queue = new int[100];
}
void add(int data) {
queue[size] = data;
size++;
}
void remove() {
if (size == 0) {
cout << "Queue is empty" << endl;
return;
}
else {
for (int i = 0; i < size - 1; i++) {
queue[i] = queue[i + 1];
}
size--;
}
}
void print() {
if (size == 0) {
cout << "Queue is empty" << endl;
return;
}
for (int i = 0; i < size; i++) {
cout << queue[i] << " <- ";
}
cout << endl;
}
Queue operator+(Queue& obj) {
Queue q3;
q3.size = this->size;
q3.queue = this->queue;
for (int i = 0; i < obj.size; i++) {
q3.add(obj.queue[i]);
}
return q3;
}
};

int main() {
Queue q1;
q1.add(42); q1.add(2); q1.add(8); q1.add(1);
Queue q2;
q2.add(3); q2.add(66); q2.add(128); q2.add(5); q2.add(16); q2.add(2222);
Queue q3 = q1 + q2;
q3.print();

return 0;
}
16 changes: 16 additions & 0 deletions HomeWorks/HW2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <iostream>
using namespace std;

int main02(){
int year;
cout << "year : ";
cin >> year;
if(year%4==0){
if(year%100==0 && year%400==0) cout << "A leap year\n";
else if(year%100!=0) cout << "A leap year\n";
else cout << "Is not a leap year\n";
}
else cout << "Is not a leap year\n";
system("PAUSE");
return 0;
}
27 changes: 27 additions & 0 deletions HomeWorks/HW3_1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <iostream>
using namespace std;

int main03()
{
int n, sum = 1, temp_sum = 0, count = 0, l = 1;
cout << "n=";
cin >> n;
for (int i = 1; i <= n; i++) {
if (count == l) {
sum *= temp_sum;
cout << "tem_sum1 = " << temp_sum << " i=" << i << endl;
temp_sum = 0;
count = 0;
l++;
}
if (i == n) {
temp_sum += i;
sum *= temp_sum;
cout << "tem_sum2 = " << temp_sum << " i=" << i << endl;
}
count++;
temp_sum += i;
cout << "tem_sum4 = " << temp_sum << " i=" << i<< endl;
}
cout << "sum=" << sum;
}
26 changes: 26 additions & 0 deletions HomeWorks/HW3_2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <iostream>
using namespace std;

int fact(int n);

int main04()
{
int n, sum = 0, factor_sum, count = 0, l = 1;
cout << "n=";
cin >> n;
for (int i = 1; i <= n; i++) {
factor_sum = fact(i);
sum += factor_sum;
}
cout << "sum=" << sum;

}

int fact(int n) {
int fact_sum = 1;
for (int i = 1; i <= n; i++) {
fact_sum *= i;
}
return fact_sum;

}
26 changes: 26 additions & 0 deletions HomeWorks/HW3_3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <iostream>
using namespace std;

int fact(int n,int l);

int main05()
{
unsigned long long int n, sum = 0;
cout << "n=";
cin >> n;
for (int i = 1; i <= n; i++) {
cout << "sum= " << sum << endl;
sum += fact(i*i,i);
}

cout << "sum=" << sum;
return 0;
}

int fact(int n,int l) {
int fact_sum = 1;
for (int i = l; i <= n; i++) {
fact_sum *= i;
}
return fact_sum;
}
Loading