爱睡觉的鱼
新手上路
- 注册
- 2002-12-09
- 消息
- 705
- 荣誉分数
- 2
- 声望点数
- 0
刚学C++
怎么调试都有问题。
回家了手头又没编译器,明早就要交了~
谁帮我弄好我请客吃麦当劳啊~~
谢谢咯~:thanks:
怎么调试都有问题。
回家了手头又没编译器,明早就要交了~
谁帮我弄好我请客吃麦当劳啊~~
谢谢咯~:thanks:
#include <iostream>
using std::cout;
using std::endl;
#include <ctime>
#include <cstdlib>
enum Months { JAN = 1, FEB, MAR, APR, MAY, JUN,
JUL, AUG, SEP, OCT, NOV, DEC };
void generateDate( int &, int &, int );
void printDate( Months, int, int );
int validDate( int, int );
int main()
{
int month = 1;
int day = 1;
int year = 1900;
srand( time( 0 ) );
printDate( month, day, year );
year = getYear( year );
getMonth();
day = getDay();
if ( validDate( month, day, year ) == true )
printDate( month, day, year );
return 0;
} // end main
// return month
int getMonth()
{
Month myMonth = rand() % 12 + 1;
return myMonth;
} // end getMonth
// return year
int getYear( int aYear )
{
return rand() % 101 + 1900;
} // end getYear
// return day
void getDay()
{
return rand() % 31 + 1;
} // end getDay
// output date
void printDate( Months month, int day, year );
{
switch ( month ) {
case JAN:
cout << "January " << day << ", " << year << endl;
case FEB:
cout << "February " << day << ", " << year << endl;
break;
case MAR:
cout << "March " << day << ", " << year << endl;
break;
case APR:
cout << "April " << day << ", " << year << endl;
break;
case MAY:
cout << "May " << day << ", " << year << endl;
break;
case JUN:
cout << "June " << day << ", " << year << endl;
break;
case JUL:
cout << "July " << day << ", " << year << endl;
break;
case AUG:
cout << "August " << day << ", " << year << endl;
break;
case SEP:
cout << "September " << day << ", " << year << endl;
break;
case OCT:
cout << "October " << day << ", " << year << endl;
break;
case NOV:
cout << "November " << day << ", " << year << endl;
break;
case DEC:
cout << "December " << day << ", " << year << endl;
break;
default:
cout << "invalid month\n";
} // end switch
} // end printDate
// check for validDate
bool validDate( int month, int day, int year )
{
int month;
int day;
int year;
if ( year < 1900 || year > 2001 )
return false;
else if ( month < 1 || month > 12 )
return false;
else if ( day < 1 || day > 31 )
return false;
else if ( day == 31 && month == APR || month == JUN
|| month == SEP || month == NOV )
return false;
else if ( month == 2 && day > 28 )
return false;
} // end validDate
#include <iostream>
using std::cout;
using std::endl;
#include <iomanip>
using std::setw;
#include <ctime>
const int NUM_GRADES = 10;
const int NUM_SUDENTS = 3;
int findHighest( int );
int findLowest( int * );
void printDatabase( const int [][], const char [][ 20 ] );
int main()
{
int student1[ NUM_GRADES ] = { 0 };
int student2[ NUM_GRADES ] = { 76, 89, 81, 42, 66, 93, 104,
91, 71, 85, 105 };
int student3[ NUM_GRADES ] = { 65, 69, 91, 89, 82, 93, 72,
76, 79, 99 };
char names[ NUM_SUDENTS ][ 20 ] = { "Bob", "John", "Joe" };
int database[ NUM_SUDENTS ][ NUM_GRADES ];
int i = 0;
srand( time( 0 ) );
// initialize student1
for ( i = 0; i < NUM_GRADES; i++ )
student1[ NUM_GRADES ] = rand() % 50 + 50;
// initialize database
for ( i = 1; i < NUM_GRADES; i++ ) {
database[ 0 ][ i ] = student1[ i ];
database[ 1 ][ i ] = student2[ i ];
database[ 2 ][ i ] = student3[ i ];
} // end for
printDatabase( database, studentNames );
for ( i = 0; i < NUM_SUDENTS; i++ ) {
cout << studentNames[ i ] << "'s highest grade is: "
<< findHighest( student1 ) << endl
<< studentNames[ i ] << "'s lowest grade is: "
<< findLowest( database[ i ] ) << endl;
} // end for
return 0;
} // end main
// determine largest grade
int findHighest( int )
{
int highest = a[ 0 ];
for ( int i = 1; i <= NUM_GRADES; i++ )
if ( a[ i ] > highest )
highest = a[ i ];
return highest;
} // end function findHighest
// determine lowest grade
int findLowest( int a[] )
{
int lowest = a[ 0 ];
for ( int i = 1; i < NUM_GRADES; i++ )
if ( a[ i ] < lowest )
lowest = a[ i ];
return lowest;
} // end lowestGrade
// output data
void printDatabase( int a[][ NUM_GRADES ], char names[][ 20 ] )
{
cout << "Here is the grade database\n\n"
<< setw( 10 ) << "Name";
for ( int n = 1; n <= NUM_GRADES; n++ )
cout << setw( 4 ) << n;
cout << endl;
for ( int i = 0; i < NUM_SUDENTS; i++ ) {
cout << setw( 10 ) << names[ i ];
for ( int j = 0; j < NUM_GRADES; j++ )
cout << setw( 4 ) << a[ i, j ];
cout << endl;
} // end for
cout << endl;
} // end printDatabase
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <cstring>
void initialize( char [], int * );
void input( const char [], int & );
void print( const char *, const int );
void growOlder( const char [], int * );
bool comparePeople( const char *, const int *,
const char *, const int * );
int main()
{
char name1[ 25 ];
char name2[ 25 ];
int age1;
int age2;
initialize( name1, &age1 );
initialize( name2, &age2 );
print( name1, *age1 );
print( name2, *age2 );
input( name1, age1 );
input( name2, age2 );
print( &name1, &age1Ptr );
print( &name2, &age1Ptr );
growOlder( name2, age2 );
if ( comparePeople( name1, &age1, name2, &age2 ) )
cout << "Both people have the same name and age"
<< endl;
return 0;
} // end main
// function input definition
void input( const char name[], int &age )
{
cout << "Enter a name: ";
cin >> name;
cout << "Enter an age: ";
cin >> age;
cout << endl;
} // end function input
// function initialize definition
void initialize( char name[], int *age )
{
name = "";
age = 0;
} // end function initialize
// function print definition
void print( const char name[], const int age )
{
cout << "The value stored in variable name is: "
<< name << endl
<< "The value stored in variable age is: "
<< age << endl << endl;
} // end function print
// function growOlder definition
void growOlder( const char name[], int *age )
{
cout << name << " has grown one year older\n\n";
*age++;
} // end function growOlder
// function comparePeople definition
bool comparePeople( const char *name1, const int *age1,
const char *name2, const int *age2 )
{
return ( age1 == age2 && strcmp( name1, name2 ) );
} // end function comparePeople