仅使用printf&scanf的有趣练习

时间:2022-06-01 18:17:58

I'm running out of ideas for my "Beginning C" class, and the only topics I've discussed so far are Data types, Variables & the printf & scanf functions.

我的“Beginning C”课程的想法已经用完了,到目前为止我讨论的唯一主题是数据类型,变量和printf&scanf函数。

My last quizzes involved simple formulas (area of a circle, volume of a cube..) enclosed inside the printfs..

我最后的测验涉及封装在printfs内部的简单公式(圆形区域,立方体的体积......)。

eg. printf("volume = %d",length * width * height);

例如。 printf(“volume =%d”,length * width * height);

I'm looking for something more interesting using only printfs and scanfs :(

我正在寻找一些更有趣的东西,只使用printfs和scanfs :(

Update~ Just to clarify: My students don't know how to use conditional statements and iterative statements yet. It's only week 2, and this is their very first programming class :'( And yes, they can only retrieve the input, then modify it in some way before they output it in the console. I can play with escape sequences (create a box asterisks) or format specifiers (a 3 precision division operation) but that's it..

更新〜只是为了澄清:我的学生还不知道如何使用条件语句和迭代语句。它只是第2周,这是他们的第一个编程类:'(是的,它们只能检索输入,然后在它们在控制台中输出它之前以某种方式修改它。我可以使用转义序列(创建一个盒子) asterisks)或格式说明符(3精度除法运算),但就是这样......

4 个解决方案

#1


  • Write a program that asks the user their name, age, and favorite letter of the alphabet.
  • 编写一个程序,询问用户他们的姓名,年龄和最喜欢的字母。

  • Print a single sentence containing all the information.
  • 打印包含所有信息的单个句子。

  • Bonus: Print a sentence containing the first letter of their name.
  • 奖励:打印包含其姓名首字母的句子。

Answer:

char name[50];
int age;
char letter;

printf( "\nWhat is your name? " );
scanf( "%s", name );

printf( "\nHow old are you? " );
scanf( "%d", &age );

printf( "\nWhat is your favorite letter of the alphabet? " );
scanf( " %c", &letter ); //skipping whitespace

printf( "\n\n-------------------\n\n");
printf( "Your name is %s, you are %d years old, and your favorite letter of the alphabet is %c.\n", name, age, letter );

printf( "The first letter of your name is %c", name[0] );

If you'd like some math in there, ask them questions like:

如果您想在那里学习数学,请问他们如下问题:

  • Write a program that asks the user how many weeks until christmas, then print out how many seconds there are until christmas (given the number of weeks entered only).
  • 编写一个程序,询问用户圣诞节前几周,然后打印出圣诞节前的秒数(仅给出输入的周数)。

#2


I have used a unit converter previously.

我之前使用过单位转换器。

  • Convert temperatures to and from Celsius, Fahrenheit, and Kelvin.
  • 将温度转换为摄氏温度,华氏温度和开尔文温度。

  • Change the base as mentioned by Alex.
  • 更改Alex提到的基数。

  • Cooking weights and measures has lots of interesting conversions.
  • 烹饪重量和措施有很多有趣的转换。

#3


How about a base-converter. In other words, prompt for a number in decimal and a number system (say, 2 for binary or 16 for hex) and then output the number in that system.

基础转换器怎么样?换句话说,提示输入十进制数和一个数字系统(例如,2表示二进制或16表示十六进制),然后输出该系统中的数字。

#4


Using only printfs and scanfs, and nothing more? You can only retrieve input and output the input if that's the case.

仅使用printfs和scanfs,仅此而已?如果是这种情况,您只能检索输入并输出输入。

I don't think that's what you meant though...

我不认为这就是你的意思......

Math/Algorithm Related

  • Generate/Output the first N prime numbers
  • 生成/输出前N个素数

  • Calculate factorial(n) recursively/iteratively
  • 递归地/迭代地计算阶乘(n)

  • Use C as a tool to Find a formula for sum of alternating sums up to positive N
  • 使用C作为工具来查找交替总和达到正N的公式

Game Related

  • Write a simple character based pong game (single player)
  • 写一个简单的基于角色的乒乓球比赛(单人)

  • Write "nibbles" a.k.a "snake" (char based)
  • 写“小块”a.k.a“蛇”(基于字符)

#1


  • Write a program that asks the user their name, age, and favorite letter of the alphabet.
  • 编写一个程序,询问用户他们的姓名,年龄和最喜欢的字母。

  • Print a single sentence containing all the information.
  • 打印包含所有信息的单个句子。

  • Bonus: Print a sentence containing the first letter of their name.
  • 奖励:打印包含其姓名首字母的句子。

Answer:

char name[50];
int age;
char letter;

printf( "\nWhat is your name? " );
scanf( "%s", name );

printf( "\nHow old are you? " );
scanf( "%d", &age );

printf( "\nWhat is your favorite letter of the alphabet? " );
scanf( " %c", &letter ); //skipping whitespace

printf( "\n\n-------------------\n\n");
printf( "Your name is %s, you are %d years old, and your favorite letter of the alphabet is %c.\n", name, age, letter );

printf( "The first letter of your name is %c", name[0] );

If you'd like some math in there, ask them questions like:

如果您想在那里学习数学,请问他们如下问题:

  • Write a program that asks the user how many weeks until christmas, then print out how many seconds there are until christmas (given the number of weeks entered only).
  • 编写一个程序,询问用户圣诞节前几周,然后打印出圣诞节前的秒数(仅给出输入的周数)。

#2


I have used a unit converter previously.

我之前使用过单位转换器。

  • Convert temperatures to and from Celsius, Fahrenheit, and Kelvin.
  • 将温度转换为摄氏温度,华氏温度和开尔文温度。

  • Change the base as mentioned by Alex.
  • 更改Alex提到的基数。

  • Cooking weights and measures has lots of interesting conversions.
  • 烹饪重量和措施有很多有趣的转换。

#3


How about a base-converter. In other words, prompt for a number in decimal and a number system (say, 2 for binary or 16 for hex) and then output the number in that system.

基础转换器怎么样?换句话说,提示输入十进制数和一个数字系统(例如,2表示二进制或16表示十六进制),然后输出该系统中的数字。

#4


Using only printfs and scanfs, and nothing more? You can only retrieve input and output the input if that's the case.

仅使用printfs和scanfs,仅此而已?如果是这种情况,您只能检索输入并输出输入。

I don't think that's what you meant though...

我不认为这就是你的意思......

Math/Algorithm Related

  • Generate/Output the first N prime numbers
  • 生成/输出前N个素数

  • Calculate factorial(n) recursively/iteratively
  • 递归地/迭代地计算阶乘(n)

  • Use C as a tool to Find a formula for sum of alternating sums up to positive N
  • 使用C作为工具来查找交替总和达到正N的公式

Game Related

  • Write a simple character based pong game (single player)
  • 写一个简单的基于角色的乒乓球比赛(单人)

  • Write "nibbles" a.k.a "snake" (char based)
  • 写“小块”a.k.a“蛇”(基于字符)