Variables and Arithmetic Expression

本文介绍了C语言中的浮点数运算特点、格式化输出方法、循环结构应用实例以及字符输入输出的基本操作。通过多个示例程序详细讲解了如何使用for循环打印温度转换表、如何进行文件复制、字符计数及单词统计等常见任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Notes from The C Programming Language

 

A decimal point in a constant indicates that it is floating point, however, so $5.0/9.0$ i not truncated because it is the ratio of two floating-point values.

 

 

printf specification

  • %3.0f says that a floating-point number is to be printed at least three characters wide, with no decimal point and no fraction dgits.
  • %6.1f describes another number that is to be printed at least six characters wide, with 1 digit after the decimal point.

Width and precision may be omitted from a specification: %6f says that the number is to be at least six characters wide; %.2f specifies two characters after the decimal point, but the width is not constrained.

  • %o for octal
  • %x for hexadecimal
  • %c for character
  • %s for character string
  • %% for % itself

 

for statement:

#include <stdio.h>

#define LOWER 0			/* lower limit of table */
#define UPPER 300		/* upper limit */
#define STEP  20		/* step size */

/* print Fahrenheit-Celsius table */
main()
{
	int fahr;
	
	for(fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP)
		printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
}

 

 

Character input and output

The standard library provides getchar() and putchar() for reading and writing one character at a time. getchar() reads the next input character from a text stream and returns that as its value:

c = getchar();

The function putchar prints a character each time:

putchar(c);

prints the contents of the integer variable c as a character, usually on the screen.

 

File copy: a program that copies its input to its output one character at a time:

#include <stdio.h>

/* copy input to output; 1st version */
main()
{
	int c;
	
	c = getchar();
	while(c != EOF)
	{
		putchar(c);
		c = getchar();
	}
}

EOF is defined in <stdio.h>.

 

As an expression has a value, which is the left hand side after the assignment, the code can be concise:

#include <stdio.h>

/* copy input to output; 2nd version */
main()
{
	int c;
	
	while((c = getchar()) != EOF)
		putchar(c);
}

 

 

The next program counts characters:

#include <stdio.h>

/* count characters in input; 1st version */
main()
{
	long nc; 
	
	nc = 0;
	while(getchar() != EOF)
		++nc;
	
	printf("%ld\n", nc);
}

long integers are at least 32-bits.

 

It may be possible to cope with even bigger numbers by using a double(double precision float).

#include <stdio.h>

/* count characters in input; 2nd version */
main()
{
	double nc;
	
	for(nc = 0; getchar() != EOF; ++nc)
		;
	printf("%.0f\n", nc);
}

printf uses %f for both float and double; %.0f suppresses printing of the decimal point and the fraction part, which is zero.

 

Counts lines:

#include <stdio.h>

/* count lines in input */
main()
{
	int c, nl;
	
	nl = 0;
	while((c = getchar()) != EOF)
	{
		if(c == '\n')
			++n1;
		printf("%d\n", nl);
	}
}

 

Word counting with loose definition that a word is any sequence of characters that does not contain blank, tab or newline. This is a bare-bones version of the UNIX program wc:

#include <stdio.h>

#define IN  1	/* inside a word */
#define OUT 0	/* outside a word*/

/* count lines, words, and characters in input*/
main()
{
	int c, nl, nw, nc, state;
	
	state = OUT;
	nl = nw = nc = 0;
	while((c = getchar()) != EOF)
	{
		++nc;
		if(c == '\n')
			++nl;
		if(c == ' ' || c == '\n' || c == '\t')
			state = OUT;
		else if(state == OUT)
		{
			state = IN;
			++nw;
		}
	}
	printf("%d %d %d\n", nl, nw, nc);
}

Every time the program encouters the first character of a word, it counts one more word.

 

Count the number of occurrences of each digit, of white space character(blank, tab, newline), and of all other characters:

#include <stdio.h>

/* count digits, white space, others */
main()
{
	int c, i, nwhite, nother;
	int ndigit[10];
	
	nwhite = nother = 0;
	for(i = 0; i < 10; ++i)
		ndigit[i] = 0;
	
	while((c = getchar()) != EOF)
	{
		if(c >= '0' && c <= '9')
			++ndigit[c - '0'];
		else if(c == ' ' || c == '\n' || c == '\t') 
			++nwhite;
		else
			++nother;
		
		printf("digits =");
		for(i = 0; i < 10; ++i)
			printf(" %d", ndigit[i]);
		printf(", white space = %d, other = %d\n", nwhite, nother);
	}
}

 

转载于:https://www.cnblogs.com/kid551/p/4268623.html

翻译下面的这段话 Hard Autograd for Algebraic Expressions 分数 100 作者 郑友怡 单位 浙江大学 The application of automatic differentiation technology in frameworks such as torch and tensorflow has greatly facilitated people's implementation and training of deep learning algorithms based on backpropagation. Now, we hope to implement an automatic differentiation program for algebraic expressions. Input Format First, input an infix expression composed of the following symbols. Operators Type Examples Notes Bracket ( ) Power ^ Multiplication & Division * / Addition & Subtraction + - Argument separator , optional, only used as argument separators in multivariate functions The above operators are arranged in order of operator precedence from top to bottom. For example, a+b^c*d will be considered the same as a + ( (b ^ c) * d ). Mathematical Functions (bonus) Function Description ln(A) log(A, B) logarithm. ln(A) represents the natural logarithm of A, and log(A, B) represents the logarithm of B based on A. cos(A) sin(A) tan(A) basic trigonometric functions. pow(A, B) exp(A) exponential functions. pow(A, B) represents the B power of A, and exp(A) represents the natural exponential of A. Operands Type Examples Notes Literal constant 2 3 0 -5 Just consider integers consisting of pure numbers and minus signs. Variable ex cosval xy xx Considering the above "mathematical functions" as reserved words, identifiers (strings of lowercase English letters) that are not reserved words are called variables. Output Method For each variable (as defined above) that appears in the expression, describe an arithmetic expression that represents the derivative of the input algebraic expression with respect to that variable, using the operators, mathematical functions, and operands defined in the input form. The output is arranged according to the lexicographical order of the variables. For each line print two strings, which are each variable and the corresponding derivative function. Separate the two strings
03-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值