Date: December 30th, 2009
Time: 14:10-16:00
Open book; turn off computer & mobile phone
// Range of an unsigned short integer
#include <iostream>
using std::cout;
int main()
{
unsigned short f=1;
unsigned short i=1;
for (i=1; i<14; i++)
{
f *= i;
cout << f << "\n";
}
return 0;
}
#include <iostream>
using std::cout;
int sum(int arg1 = 10, int arg2 = 20, int arg3 = 30, int arg4 = 40);
int main()
{
cout << sum(5, 5) << "\n";
return 0;
}
int sum(int arg1, int arg2, int arg3, int arg4)
{
return arg1 + arg2 + arg3 + arg4;
}
// Ex6_01a.cpp
#include <iostream>
using std::cout;
using std::endl;
long sum(long, long);
long product(long, long);
int main(void)
{
long (*pdo_it) (long, long);
pdo_it = product;
cout << pdo_it(3,5) << endl;
pdo_it = sum;
cout << pdo_it(3,5) << endl;
return 0;
}
long product(long a, long b)
{
return a*b;
}
long sum(long a, long b)
{
return a+b;
}
// The conditional operator
#include <iostream>
using std::cout;
int odd(int i)
{
int j = i % 2;
return (i=1)?1:0;
}
int main(void)
{
for (int k=1; k<=2; k++)
if (odd(k))
cout << "Odd\n";
else
cout << "Even\n";
return 0;
}
// Comparing two strings
#include <iostream>
using std::cout;
int main()
{
char* p1="Q";
char* p2 = "QUIT";
bool flag = true;
while (flag && *p1 != '\0')
{
if (*p1 != *p2)
flag = false;
p1++; p2++;
}
if (flag)
cout << "The two strings are the same.\n";
else
cout << "The two strings are different.\n";
}
// List_program
#include <iostream>
using std::cout;
int main()
{
const int MAX_LINE=10;
const int STATEMENT_WIDTH=80;
int numbers[MAX_LINE]={ 0 }; // Line number
int next[MAX_LINE] = { 0 }; // Index of the next line
char statements[MAX_LINE][STATEMENT_WIDTH];
int nLines = 0; // how many lines are kept in memory
strcpy(statements[nLines], "A=10");
numbers[nLines++] = 10;
strcpy(statements[nLines], "B=20");
numbers[nLines++] = 20;
strcpy(statements[nLines], "C=A+B");
numbers[nLines++] = 30;
// List_program
for (int i=0; i<nLines; i++)
{
cout << numbers[i] << " ";
cout << statements[i] << "\n";
}
cout << sizeof statements / sizeof statements[0] << "\n";
return 0;
}
// A program to implement a calculator
#include <iostream>
#include <cstdlib>
#include <cctype>
using std::cin;
using std::cout;
using std::endl;
void eatspaces(char* str);
int expr(char* str);
int number(char* str, int& index);
int main()
{
char buffer[] = "45 - 5 -4";
eatspaces(buffer);
cout << strlen(buffer) << endl;
cout << buffer << " = " << expr(buffer) << endl;
}
// Function to eliminate spaces from a string
void eatspaces(char* str)
{
int i = 0;
int j = 0;
while ((*(str + i) = *(str + j++)) != '\0')
if (*(str+i) != ' ')
i++;
return;
}
// Function to evaluate an arithmetic expression
int expr(char* str)
{
int value = 0;
int index = 0;
value = number(str, ++index);
for (;;)
{
switch (*(str + index++))
{
case '\0':
return value;
case '+':
value += number(str, ++index);
break;
case '-':
value -= number(str, ++index);
break;
default:
cout << endl
<< "Arrrgh!*#!! There's an error"
<< endl;
exit(1);
}
}
}
// Function to recognize a number in a string
int number(char* str, int& index)
{
int value = 0;
while(isdigit(*(str + index)))
value = 10*value + (*(str + index++) - '0');
return value;
}