程序集合

3.1

#include <stdio.h>

 

int main(int argc, char *argv[])

{

enum {RED, YELLOW, AMBER=YELLOW, GREEN};

printf("%d, %d, %d, %d\n", RED, YELLOW, AMBER, GREEN);

return 0;

}

3.2

#include <iostream>

 

int main(int argc, char *argv[])

{

int a = 5;

int *pt = &a;

int &ar = a;

++(*pt);

++ar;

 

printf("a: %d\n", a);

 

printf("0xf3f2: %d\n", 0xf3f2);

printf("0437: %d\n", 0437);

printf("a: %d\n", 'a');

 

return 0;

}

3.3

#include <iostream>

using namespace std;

 

int main(int argc, char *argv[])

{

char word[100];

int i = 0, start = 0;

cout << "Enter word: ";

 

char tmp;

while((tmp  = cin.get())){

if(tmp == '-' ||  tmp == ' '){

cout << "[" << i << "] ";

for(int j = 0; j <= i; ++j)

cout << word[j];

i = 0;

cout << endl;

if(tmp == ' ')

break;

} else{

++i;

word[i] = tmp;

}

}

 

return 0;

}

3.4

#include <iostream>

#include <string>

using namespace std;

 

int main(int argc, char *argv[])

{

typedef string StringArr [10];

StringArr mystring;

string max_string = "";

 

cout << "Input 10 strings:\n";

for(int i = 0; i < 10; ++i){

cin >> mystring[i];

if(max_string.size() < mystring[i].size())

max_string = mystring[i];

cout << max_string << endl;

return 0;

}

 

4.1

 

#include <iostream>

using namespace std;

 

void input(int &a, int &b){

cout << "Enter two integer: ";

cin >> a;

cin >> b;

}

 

int main(int argc, char *argv[])

{

int a, b;

input(a, b);

cout << a+b << endl;

return 0;

}

 

4.2

fac.h

#include <iostream.h>

using namespace std;

 

int fac_recur(int n){

if(n < 0)

return -1;

if(n == 0 || n == 1)

return 1;

return n*fac_recur(n-1);

}

 

int fac_loop(int n){

if(n < 0)

return -1;

int res = 1;

for(int i = 2; i <= n; ++i){

res *= i;

}

return res;

}

main.c

#include <iostream>

#include "output.h"

#include "factorial.h"

using namespace std;

 

int main(int argc, char *argv[])

{

string myname = "Li";

output(3.14);

output("hi there");

output(myname);

output("hi there", 3);

output("hi there", 3, 2);

 

cout << "Enter an interger: ";

int n;

cin >> n;

cout << "Factorial of " << n << " is " << fac_recur(n) << ", " << fac_loop(n);

cout << endl;

return 0;

}

 

out.h

#include <iostream.h>

using namespace std;

 

void output(int a){

cout << "Integer parameter: " << a << endl;

}

void output(char ch){

cout << "Char parameter: " << ch << endl;

}

void output(double f){

cout << "Double parameter: " << f << endl;

}

void output(long l){

cout << "Long parameter: " << l << endl;

}

void output(string str){

cout << "String parameter: " << str << endl;

}

void output(char *char_arr){

cout << "Char Array parameter: " << char_arr << endl;

}

void output(string str, int start){

// Judge if the start is larger than length of str.

int len = str.length();

for(int i = start-1; i < len; ++i){

cout << str.at(i);

}

cout << endl;

}

void output(string str, int start, int len){

// Judge if the start and (start+len) are larger than length of str 

int str_len = str.length();

if(len == -1) 

len = str_len;

for(int i = 0; i < len; ++i){

--start;

cout << str.at(start);

}

cout << endl;

}

7.4

#include <iostream>

#include <cstring>

 

int main(int argc, char *argv[])

{

char *t1 = "t1";

const char *t2 = "t2";

char *const t3 = "t3";

const char *const t4 = "t4";

 

//t1 = t2;

//t1 = t3;

//t1 = t4;

//

//t2 = t1;

//t2 = t3;

//t2 = t4;

//

//t3 = t1;

//t3 = t2;

//t3 = t4;

//

//t4 = t1;

//t4 = t2;

//t4 = t3;

 

strcpy(t1, t2);

strcpy(t1, t3);

strcpy(t1, t4);

 

strcpy(t2, t1);

strcpy(t2, t3);

strcpy(t2, t4);

 

strcpy(t3, t1);

strcpy(t3, t2);

strcpy(t3, t4);

 

strcpy(t4, t1);

strcpy(t4, t2);

strcpy(t4, t3);

return 0;

}

8.1

#include <iostream>

#include <string>

#include <cstring>

using namespace std;

 

int main(int argc, char *argv[])

{

string word;

cout << "Enter a word: ";

cin >> word;

 

const char * cword = word.c_str();

while(*cword){

cout << *cword++;

}

cout << endl;

return 0;

}

 

8.2

mystring.h

 

namespace my {

int strcmp(const char *l, const char *r);

int strlen(const char *s);

char *strcat(char *l, const char *r);

char *strcpy(char *l, const char *r);

char *toupper(char *s);

}

mystring.c

#include "mystring.h"

namespace my {

int strcmp(const char *l, const char *r){

char lch, rch;

int cmp;

do{

lch = *l++;

rch = *r++;

cmp = lch - rch;

} while((cmp == 0) && (lch != '\0'));

return cmp;

}

int strlen(const char *s){

int i = 0;

char ch;

while((ch = *s++) != '\0')

++i;

return i;

}

char *strcat(char *l, const char *r){

char *res = l;

while(*l != '\0')

l++;

 

while((*l++ = *r++) != '\0'){

}

return res;

}

char *strcpy(char *l, const char *r){

char *res = l;

while((*l++ = *r++) != '\0'){

}

return res;

}

char *toupper(char *s){

char *res = s;

while(*s != '\0'){

if(*s >= 'a' && *s <= 'z'){

*s -= 32;

}

s++;

}

return res;

}

}

main.c

#include <iostream>

#include "mystring.h"

using namespace std;

 

void mystring_test(){

char *str1 = new char[100];

char *str2 = new char[100];

char *str3 = new char[100];

 

my::strcpy(str1, "limp");

my::strcpy(str2, "limp");

my::strcpy(str3, "st");

 

cout << "Compare " << str1 << " , " << str2 << ": " << my::strcmp(str1, str2) << endl;

 

cout << "Length of " << str1 << " is " << my::strlen(str1) << endl;

cout << "Concatenate of " << str1 << " , " << str3 << " is ";

cout << my::strcat(str1, str3) << endl;

 

cout << "To upper of " << str1 << " is ";

cout << my::toupper(str1) << endl;

 

delete[] str1;

delete[] str2;

delete[] str3;

}

 

int compare_pwd(){

char *pwd1 = new char[100];

char *pwd2 = new char[100];

char *pwd3 = new char[100];

char *str = "HASTA LA VISTA";

char *blank = " ";

cout << "Enter password 1: ";

cin >> pwd1;

cout << "Enter password 2: ";

cin >> pwd2;

cout << "Enter password 3: ";

cin >> pwd3;

 

char *pwd = my::strcat(my::strcat(pwd1, blank), pwd2);

my::strcat(my::strcat(pwd, blank), pwd3);

my::toupper(pwd);

cout << "The password you enter : " << pwd << endl;

 

int res = my::strcmp(pwd, str);

delete[] pwd1;

delete[] pwd2;

delete[] pwd3;

return res;

}

int main(int argc, char *argv[])

{

//mystring_test();

int res;

while(true){

if(compare_pwd() == 0){

cout << "You may go for coffee ;-\n";

break;

} else{

cout << "Wrong password! \n";

}

return 0;

}

3.5

#include <iostream>

 

using namespace std;

 

#define SIZE 3

 

void switch_player(char &player) {

player = (player == 'X' ? 'O' : 'X'); 

}

 

void print_board(char *board, int size) {

for (int col = 0; col < size; col++) {

for (int row = 0; row < size; row++) {

cout << board[col * size + row] << ' ';

}

 

cout << endl;

}

}

 

int main() {

char player = 'X';

char board[SIZE * SIZE];

int move_x, move_y;

 

// fill board with '?'

for (int i = 0; i < SIZE * SIZE; ++i)

board[i] = '?';

 

while (1) {

print_board(board, SIZE);

 

cout << player << " move: ";

cin >> move_x >> move_y;

 

if (move_x >= SIZE || move_y >= SIZE) continue;

 

board[move_x * SIZE + move_y] = player;

switch_player(player);

}

 

return 0;

}

 

4.3

#include <iostream>

 

using namespace std;

 

unsigned int rotate (unsigned int s, unsigned int r) {

return s << r;

}

 

int main() {

unsigned int s, r;

 

cout << "input two values: ";

cin >> s >> r;

 

cout << "original: " << s << endl;

cout << hex << "rotated: "<< rotate(s, r) << endl;

 

return 0;

}

4.4

 

#include <iostream>

 

using namespace std;

 

int main() {

int power = 0, result = 1;

while (result <<= 1, ++power < 10);

 

return 0;

}

 

4.5

 

#include <iostream>

 

using namespace std;

 

#define SIZE 3

 

void switch_player(char &player) {

player = (player == 'X' ? 'O' : 'X'); 

}

 

void print_board(char *board, int size) {

for (int col = 0; col < size; col++) {

for (int row = 0; row < size; row++) {

cout << board[col * size + row] << ' ';

}

 

cout << endl;

}

}

 

bool check_end(char *board, int size, char &winner) {

// check col

for (int row = 0; row < size; ++row) {

int col = 1;

for (; col < size; ++col) {

if (board[row * size + col] != board[row * size + col - 1]) break;

if (board[row * size + col] == '?') break;

}

if (col == size) {

winner = board[row * size + col - 1];

return true;

}

}

 

// check row

for (int col = 0; col < size; ++col) {

int row = 1;

for (; row < size; ++row) {

if (board[row * size + col] != board[(row - 1) * size + col]) break;

if (board[row * size + col] == '?') break;

}

if (row == size) {

winner = board[(row - 1) * size + col];

return true;

}

}

 

//check cross

int index = 1;

for (; index < size; ++index) {

if (board[index * size + index] != board[(index - 1) * size + index - 1]) break;

if (board[index * size + index] == '?') break;

}

if (index == size) {

winner = board[0];

return true;

}

 

index = 1;

for (; index < size; ++index) {

if (board[index * size + size - index - 1] != board[(index - 1) * size + size - index]) break;

if (board[index * size + size - index - 1] == '?') break;

}

if (index == size) {

winner = board[size - 1];

return true;

}

 

return false;

}

 

int main() {

char player = 'X', winner = '?';

char board[SIZE * SIZE];

int move_x, move_y;

 

// fill board with '?'

for (int i = 0; i < SIZE * SIZE; ++i)

board[i] = '?';

 

while (!check_end(board, SIZE, winner)) {

print_board(board, SIZE);

 

cout << player << " move: ";

cin >> move_x >> move_y;

 

if (move_x >= SIZE || move_y >= SIZE) continue;

 

board[move_x * SIZE + move_y] = player;

switch_player(player);

}

 

if (winner == '?') cout << "Draw!" << endl;

else cout << winner << " win!";

 

return 0;

}

 

56.2

#include <iostream>

 

using namespace std;

 

int calculate(int first, int second, char op) {

switch (op) {

case '+':

return first + second;

case '-':

return first - second;

case '*':

return first * second;

case '/':

return first / second;

}

 

return 0;

}

 

int main () {

int first, second;

char op;

 

while (true) {

cout << "Please input two operands: ";

cin >> first >> second;

cout << "Please input an operator: ";

cin >> op;

 

int result;

string strOP;

switch (op) {

case '+':

result = first + second;

strOP = "Adding";

break;

case '*':

result = first * second;

strOP = "Multiplying";

break;

}

 

cout << strOP.c_str() << " " << first << " and " << second << " = " << result << endl;

}

 

return 0;

}

 

56.4

 

#include <iostream>

#include <stack>

 

using namespace std;

 

int main() {

stack<char> record;

 

// get all inputs end with '\n'

char character;

while (cin.get(character)) {

if (character == '\n') break;

 

record.push(character);

}

 

// output in reverse order

while (!record.empty()) {

cout.put(record.top());

record.pop();

}

 

return 0;

}

 

 

56.5

#include <iostream>

#include <string>

#include <sstream>

 

using namespace std;

 

int main() {

string input;

 

cin >> input;

stringstream  inputStream(input);

 

string max_string = "";

string tmp;

while (inputStream >> tmp) {

if (tmp.length() >= max_string.length()) max_string = tmp;

}

 

cout << "max string: " << max_string << "(" << max_string.length() << ")" << endl;

 

return 0;

}

 

你可能感兴趣的:(集合)