Sei sulla pagina 1di 13

Write a program that a string from the keyboard & determines the length

of the string. Print the string in a length that is twice the field width.

#include < iostream.h >


#include < iomanip.h >
#include < string.h >
const int SIZE = 80;
int main()
{
char string[ SIZE ];
int stringLength; cout << "Enter a string: ";
cin >> string; stringLength = strlen( string );
cout << "the length of the string is " << strlen( string ) << endl; 
// print string using twice the length as field with
cout << setw( 2 * stringLength ) << string << endl;
return 0;
}
Write a program that reads in 5 integers and determines and prints the
largest and the smallest in the group

int main()
{

int num1, num2, num3, num4, num5, largest, smallest;

cout << "Enter five integers: ";

cin >> num1 >> num2 >> num3 >> num4 >> num5;

largest = num1;

smallest = num1;

if ( num1 > largest )

largest = num1;

if ( num2 > largest )

largest = num2;

if ( num3 > largest )

largest = num3;

if ( num4 > largest )

largest = num4;

if ( num5 > largest )

largest = num5;

if ( num1 < smallest )

smallest = num1;

if ( num2 < smallest )

smallest = num2;
if ( num3 < smallest )

smallest = num3;

if ( num4 < smallest )

smallest = num4;

if ( num5 < smallest )

smallest = num5;

cout << "Largest is " << largest

<< "\nSmallest is " << smallest << endl;

return 0;

}
Write a program that prints a table of a binary, octal and hexadecimal
equivalents of the decimal numbers in the range 1 to 256.

#include <iostream.h>

int main()
{
cout << "Decimal\t\tBinary\t\t\tOctal\tHexadecimal\n";

for ( int loop = 1; loop <= 256; ++loop ) {


cout << dec << loop << "\t\t";

// Output binary number


int number = loop;
cout << ( number == 256 ? '1' : '0' );
int factor = 256;

do {
cout << ( number < factor && number >= ( factor / 2 ) ? '1' : '0' );
factor /= 2;
number %= factor;
} while ( factor > 2 );

// Output octal and hexadecimal numbers


cout << '\t' << oct << loop << '\t' << hex << loop << endl;
}

return 0;
}

Or
#include <iostream>
#include <iomanip>
#include <limits>

using namespace std;

typedef unsigned int UINT;

void bin_oct_hex(UINT i, char *bin, char *oct, char *hex)


{
static const char hex_tbl[] = "0123456789ABCDEF";
int b_digits;

// zero is a special case


if (!i) {
bin[0] = oct[0] = hex[0] = '0';
bin[1] = oct[1] = hex[1] = '\0';
return;
}

// skipping leading zeroes


for (UINT mask = 1U << (numeric_limits<UINT>::digits - 1);
mask && !(mask & i); mask >>= 1);

// extracting binary digits


for (b_digits = 0; mask; mask >>= 1)
bin[b_digits++] = mask & i ? 1 : 0;
bin[b_digits] = '\0';

/*** building octal number ***/


div_t d = div(b_digits, 3);
char *src = bin, *dest = oct;

// the first octal digit may correspond to 1 to 3 binary


digits
if (d.rem) {
int tmp = *src++;
while (--d.rem)
tmp = (tmp << 1) + *src++;
*dest++ = tmp + '0';
}

// the next octal digits will correspond each to 3 binary


digits
while (--d.quot >= 0) {
*dest++ = (src[0] << 2) + (src[1] << 1) + src[2] +
'0';
src += 3;
}
*dest = '\0';

/*** bin to hex conversion ***/


d.quot = b_digits >> 2, d.rem = b_digits & 3;
src = bin, dest = hex;

// same as for octal, but the range is 1 to 4


if (d.rem) {
int tmp = *src++;
while (--d.rem)
tmp = (tmp << 1) + *src++;
*dest++ = hex_tbl[tmp];
}

// the next hex digits will correspond each to 4 binary


digits
while (--d.quot >= 0) {
*dest++ = hex_tbl[(src[0] << 3) + (src[1] << 2) +
(src[2] << 1) + src[3]];
src += 4;
}
*dest = '\0';

// finally, convert the binary array to the ASCII form


while (--b_digits >= 0)
bin[b_digits] += '0';
}

int main ()
{
const int uint_d = numeric_limits<UINT>::digits;

// some buffers used to store the conversion results


char bin[uint_d + 1];
char oct[uint_d / 3 + 2];
char hex[uint_d / 4 + 1];

cout << " Dec Hex Oct Bin\n\n";

for (UINT x = 0; !(x & 0x100); ++x) {


bin_oct_hex(x, bin, oct, hex);
cout << setw(4) << x << setw(6) << hex << setw(6) <<
oct << setw(10) << bin << endl;
}
return 0;
}

Write a C++ program to sort the numbers by using pointers

#include <iostream> 

using std::cin; 
using std::cout; 
using std::endl; 

int main() 

double* firstNumber = NULL; 
cout << endl << "Enter first number: "; 
cin >> *firstNumber; 

double* secondNumber = NULL; 


cout << endl << "Enter second number: "; 
cin >> *secondNumber; 

if (*firstNumber > *secondNumber) 



cout << endl << *firstNumber << " > " << *secondNumber << endl; 

else if (*firstNumber < *secondNumber) 

cout << endl << *firstNumber << " < " << *secondNumber << endl;

else 

cout << endl << *firstNumber << " = " << *secondNumber << endl;

system("PAUSE"); 
return 0; 
}

works only on microsoft compiler


Write a C++ program with the employee class that makes the two
employees.

include<iostream>

class employee{

char name;

int age;

employee(char name1, int age1){

name= name1;

age-age1;

Void main(){

Employee e1,e2;

Cout<<Emplyees created.

}
Write a program to compare two files containing same type of records
using overload operators.

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int functionCounter(int count_N, int number);

int main()
{
// Create ostream object that writes to file outfile
ofstream fout ("step2out.txt");

//Declare and initialize variables


int count_N=1, number=1;

//Call functionCounter
number = functionCounter(count_N, number);

//Construct array
string *A;
A = new string[number];

//Read the words in the file as strings


ifstream fin ("step2.txt");
for(int i = 0; i < number; i++)
fin >> A[i];

//Tell user the file is written to 'step1out.txt'


cout << "The file 'step2out.txt' is written to your folder." << endl;

return 0;
}

//FUNCTION HERE--------------------------------------
int functionCounter(int count_N, int number)
//-------------------------------------------------
//Prompt user to enter the name of the file to be read
{ string fileName;
cout << "Enter name of your dictionary file: ";
cin >> fileName;

//Open the file using the input from the user


ifstream fin (fileName.c_str());

//While the file is open count the number of words and print the file in reverse form
if (fin.is_open())
{
//Define and initialize variables
int count_N=0;
string n;

//Read in string until file is finished


while(fin >> n)
{
count_N++;
}
number= count_N;
return number;
}

return -1;
}
hint I'm supposed to use an english dictionary file "dictionary.txt" to compare to an article
(another text file), find matches and show a counter of each time that word appears in the article. 

Or
/* here is a very basic program to compare files - chamkila! */
#include 
#include 
#include 

#define ANSI 
#ifdef ANSI /* ANSI compatible version */
#include 
void closeFiles(FILE* fh, ...);
#else /* UNIX compatible version */
#include 
void closeFiles(va_list);
#endif

int compareFiles(char[], char[]);


void printUsage(void);
void displayError(void);

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


{
int rc = errno;
if (argc < 3)
{
printUsage();
return -1;
}
rc = compareFiles(argv[1], argv[2]);
printf("%s: %dn", rc ? "failed" : "success");
return 0;
}

int compareFiles(char arg1[], char arg2[])


{
FILE* fh1 = NULL;
FILE* fh2 = NULL;
char buf1[BUFSIZ] = {''};
char buf2[BUFSIZ] = {''};

if (strlen(arg1) <= 0 || strlen(arg2) <= 0)


{
printUsage();
return -1;
}
if (NULL == (fh1 = fopen(arg1, "r")) || NULL == (fh2 = fopen(arg2, "r"))) 
{
displayError();
return -1;
}

while (!feof(fh1) && !feof(fh2))


{
fgets(buf1, BUFSIZ - 1, fh1);
fgets(buf2, BUFSIZ - 1, fh2);
if (0 != strcmp(buf1, buf2))
{
printf("file '%s' and file '%s' don't matchn", arg1, arg2);
closeFiles(fh1, fh2);
return -1;
}
else
{
continue;
}
}
printf("file '%s' and file '%s' matchn", arg1, arg2);
closeFiles(fh1, fh2);
return 0;
}

void printUsage(void)
{
printf("usage: fcmp.exe , nn");
}

void displayError()
{
printf("error: %sn", strerror(errno));
return;
}

#ifdef ANSI /* ANSI compatible version */


void closeFiles(FILE* fh, ...)
{
FILE* f = NULL;
va_list argptr;

va_start(argptr, fh);
f = va_arg(argptr, FILE*);
if (NULL != f)
{
fclose(f);
}
va_end (argptr);

return;
}
#else /* UNIX compatible version must use old-style definition. */
void closeFiles(va_alist)
{
FILE* f = NULL;
va_list argptr;

va_start(argptr);
f = va_arg(argptr, FILE*);
if (NULL != f)
{
fclose(f);
}
va_end (argptr);
return;
}

Potrebbero piacerti anche