请高手们给我写几个小程序(最初级的)交作业,明天要交作业,多谢

2024-11-23 07:42:13
推荐回答(2个)
回答1:

//01 0_TO_100.C
#include

void main ()
{
int value = 0;

while (value <= 100)
{
printf("%d\n", value);
value++;
}
}

//02 3_MSGS.C
#include

void main ()
{
printf ("1001 ");
printf ("C and C++ ");
printf ("Tips!");
}

//03 BIT_AND.C

#include

void main ()
{
printf("0 & 0 is %d\n", 0 & 0);
printf("0 & 1 is %d\n", 0 & 1);
printf("1 & 1 is %d\n", 1 & 1);
printf("1 & 2 is %d\n", 1 & 2);
printf("15 & 127 is %d\n", 15 & 127);
}

//04 BIT_INV.C

#include

void main ()
{
int value = 0xFF;

printf("The inverse of %X is %X\n", value, ~value);
}

//05 BIT_OR.C

#include

void main ()
{
printf("0 | 0 is %d\n", 0 | 0);
printf("0 | 1 is %d\n", 0 | 1);
printf("1 | 1 is %d\n", 1 | 1);
printf("1 | 2 is %d\n", 1 | 2);
printf("128 | 127 is %d\n", 128 | 127);
}

//06 BIT_XOR.C

#include

void main ()
{
printf("0 ^ 0 is %d\n", 0 ^ 0);
printf("0 ^ 1 is %d\n", 0 ^ 1);
printf("1 ^ 1 is %d\n", 1 ^ 1);
printf("1 ^ 2 is %d\n", 1 ^ 2);
printf("15 ^ 127 is %d\n", 15 ^ 127);
}

//07 CHAR_OUT.C

#include

void main ()
{
printf("The letter is %c\n", 'A');
printf("The letter is %c\n", 65);
}

//08 COMMENT.C

/* Purpose: Illustrates the use of comments in a C program.*/

#include

void main ()
{
printf ("1001 C & C++ Tips!"); /* Display a message*/
}

//09 CON_VOWL.C

#include

void main()
{
char letter;

int vowel_count = 0;
int consonant_count = 0;

for (letter = 'A'; letter <= 'Z'; letter++)
switch (letter) {
case 'A':
case 'E':
case 'I':
case 'O':
case 'U': vowel_count++;
break;
default: consonant_count++;
};

printf("The number of vowels is %d\n", vowel_count);
printf("The number of vowels is %d\n", consonant_count);
}

//10 DO_MENU.C

#include
#include
#include
#include

void main()
{
char letter;

do {
printf("A Display directory listing\n");
printf("B Display disk information\n");
printf("C Change system date\n");
printf("Q Quit\n");
printf("Choice: ");

letter = getch();
letter = toupper(letter);

if (letter == 'A')
system("DIR");
else if (letter == 'B')
system("CHKDSK");
else if (letter == 'C')
system("DATE");
}
while (letter != 'Q');
}

//11 EQL_NEQL.C

#include

void main ()
{
int age = 21;
int height = 73;

if (age == 21)
printf("User's age is 21\n");

if (age != 21)
printf("User's age is not 21\n");

if (height == 73)
printf("User's height is 73\n");

if (height != 73)
printf("User's height is not 73\n");
}

//12 EXP_FMT.C

#include

void main ()
{
float value = 1.23456;

printf ("%12.1e\n", value);
printf ("%12.3e\n", value);
printf ("%12.5e\n", value);
}

//13 EXP_OUT.C

#include

void main ()
{
float pi = 3.14159;
float radius = 2.0031;

printf("The circle's area is %e\n", 2 * pi * radius);
printf("The circle's area is %E\n", 2 * pi * radius);
}

//14 FLOATOUT.C

#include

void main ()
{
float price = 525.75;
float sales_tax = 0.06;

printf("The item cost is %f\n", price);
printf("Sales tax on the item is %f\n", price * sales_tax);
}

//15 FLT_FMT.C

#include

void main ()
{
float value = 1.23456;

printf ("%8.1f\n", value);
printf ("%8.3f\n", value);
printf ("%8.5f\n", value);
}

//16 FLT_PT.C

#include

void main ()
{
printf("Displaying 0.1234 yields %g\n", 0.1234);

printf("Displaying 0.00001234 yields %g\n", 0.00001234);
}

//17 FOR_2VAR.C

#include

void main()
{
int i, j;

for (i = 0, j = 100; i <= 100; i++, j++)
printf("i = %d j = %d\n", i, j);
}

//18 FOR_DIFF.C

#include

void main ()
{
int counter;

for (counter = -100; counter <= 100; counter += 5)
printf("%d ", counter);

printf("\nStarting second loop\n");

for (counter = 100; counter >= -100; counter -= 25)
printf("%d ", counter);
}

//19 FOR_DOWN.C

#include

void main ()
{
int counter;

for (counter = 5; counter >= 1; counter--)
printf("%d ", counter);

printf("\nStarting second loop\n");

for (counter = 10; counter >= 1; counter--)
printf("%d ", counter);

printf("\nStarting third loop\n");

for (counter = 0; counter >= 1; counter--)
printf("%d ", counter);
}

//20 FOR_MORE.C

#include

void main ()
{
char letter;
float percent;

for (letter = 'A'; letter <= 'Z'; letter++)
putchar(letter);

for (letter = 'z'; letter >= 'a'; letter--)
putchar(letter);

putchar('\n');

for (percent = 0.0; percent < 1.0; percent += 0.1)
printf("%3.1f\n", percent);
}

还有很多~

回答2:

去"编程中国"的论坛里面看看,里面关于各种语言都有比较详细的例题分析,总结的非常好.
www.bc-cn.net