Skip to content

Instantly share code, notes, and snippets.

@fhs
Created November 24, 2011 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fhs/1392132 to your computer and use it in GitHub Desktop.
Save fhs/1392132 to your computer and use it in GitHub Desktop.
Assignment #3
#include <iostream>
#include <string>
using namespace std;
const int NAccounts = 3;
string Usernames[NAccounts] = {
"alice",
"bob",
"eve",
};
string Passwords[NAccounts] = {
"secret1",
"secret2",
"secret3",
};
inline double sq(double x) { return x*x; }
inline int min(int a, int b) {
if(a < b)
return a;
return b;
}
void
draweye(int w, int h)
{
int x, y;
double w1, h1;
w1 = w/2.0;
h1 = h/2.0;
for(y = h1; y >= -h1; y--){
for(x = -w1; x <= w1; x++){
if(sq(x) + sq(y) >= sq(h1)
&& sq(x)*sq(h1) + sq(y)*sq(w1) <= sq(h1)*sq(w1))
cout << '.';
else
cout << ' ';
}
cout << endl;
}
}
inline void
swap(int &a, int &b)
{
int tmp;
tmp = b;
b = a;
a = tmp;
}
void
min3(int v[], int n)
{
int i, j, imin;
if(n <= 3)
return;
for(i = 0; i < 3; i++){
imin = i;
for(j = i+1; j < n; j++){
if(v[j] < v[imin])
imin = j;
}
swap(v[i], v[imin]);
}
}
bool
validuser(void)
{
int i, j, id;
string s;
id = -1;
i = 0;
while(i < 3 && id < 0){
cout << "Username: ";
cin >> s;
for(j = 0; j < NAccounts; j++){
if(s == Usernames[j])
id = j;
}
i++;
}
if(id < 0){
return false;
}
for(i = 0; i < 3; i++){
cout << "Password: ";
cin >> s;
if(s == Passwords[id])
return true;
}
return false;
}
int
main(void)
{
int choice, again, w, h, n, i, v[20];
cout << "Welcome!" << endl;
if(!validuser()){
cout << "sorry, bye!" << endl;
return 0;
}
for(;;){
cout << "[1=eye, 2=smallest three]: ";
cin >> choice;
switch(choice){
case 1:
cout << "height: ";
cin >> h;
cout << "width: ";
cin >> w;
if((10 < h && h < 30) && (15 < w && w < 35) && w > h)
draweye(w, h);
break;
case 2:
do{
cout << "How many numbers [<20]? ";
cin >> n;
}while(0 > n || n >= 20);
cout << "Enter the numbers: ";
for(i = 0; i < n; i++)
cin >> v[i];
min3(v, n);
cout << "Smallest 3: ";
for(i = 0; i < min(n, 3); i++)
cout << v[i] << " ";
cout << endl;
break;
default:
cout << "invalid input" << endl;
break;
}
cout << "continue? [0=no, 1=yes]: ";
cin >> again;
if(again == 0)
break;
}
cout << "thanks bye" << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment