📄 lower_triangle.cpp
字号:
#include"Lower_triangle.h"
#include<iomanip.h>
Lower_triangle::Lower_triangle(int s)
{
if (s > max_side) s = max_side;
if (s < 0) s = 0;
side = s;
set_access(s);
}
void Lower_triangle::set_access(int s)
{
for (int i = 0; i < s; i++)
access[i] = (i * i + i) / 2;
}
int Lower_triangle::get(int i, int j)
{
if (j > i) {
int temp = j;
j = i;
i = temp;
}
if (i >= side || j < 0) return 0;
return entry[access[i] + j];
}
void Lower_triangle::put(int i, int j, int value)
{
if (j > i) {
int temp = j;
j = i;
i = temp;
}
if (i >= side || j < 0) return;
entry[access[i] + j] = value;
}
void Lower_triangle::print()
/*
Post: display a lower triangular table
*/
{
int i, j;
for (i = 0; i < side; i++) {
for (j = 0; j <= i; j++)
cout << get(i,j) <<" ";
for (j = i + 1; j < side; j++)
cout <<" 0 ";
cout << endl;
}
}
void Lower_triangle::read()
/*
Post: the entries of a lower triangular table are read
from standard input
*/
{
int i, j;
do {
cout << "How may rows? " << flush;
cin >> side;
set_access(side);
} while (side > max_side || side < 0);
for (i = 0; i < side; i++) {
cout << "\nEnter row " << i << " :" << flush;
for (j = 0; j <= i; j++) {
int value;
cin >> value;
put(i, j, value);
}
}
}
bool Lower_triangle::test()
/*
Post: tests the triangle rule
*/
{
bool okay = true;
int a, b, c;
int ab, ac, bc;
a = 0;
while (okay && a < side) {
c = 0;
while (okay && c <= a) {
b = 0;
ac = get(a, c);
while (okay && b < side) { // test with intermediate cities
ab = get(a, b);
bc = get(b, c);
okay = (ac <= ab + bc); // test triangle rule
b++;
}
c++;
}
a++;
}
if (okay) {
cout << "The triangle inequality holds." << endl;
return true;
}
cout<< "The Triangle inequality fails for entries "
<< a - 1 << " " << c - 1 << " with intermediate location "
<< b - 1 << endl;
return false;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -