瞎写的POLITO COMPUTER SCIENCE lab numero due
#include
void main()
{
float float_1, float_2;
/*define int */
int int_1, int_2;
/*define float*/
printf("pls int the values !\n");
scanf("%d,%d,%f,%f", &int_1, &int_2, &float_1, &float_2);
printf("%5d%5d\n%5.2f\n%.3f\n", int_1, int_2, float_1, float_2);
/*take up 5 space :%5d*/
return 0;
}
Define and initialize the integer variables A, B, and C
(for example: A=3, B=5 or A=7,B=7). If they follow the following instruction:
定义并且设定一个初始的值
C = (A==B)
which is the value of C? Repeat the experiments using the following relational operators:
C的值是多少?再用下面的式子试试:
!= , <= , >=,
(!=:不等于;<=:小于等于;>=:大于等于)
Further insight 1:
calculate and visualize (using the printf unction) the value of C in the following equation,(A=0,B=0, A=0 B=1, A=1 B=0, and A=1 B=1):
通过以下方程式计算c的值
(当:A=0,B=0;A=0 B=1; A=1 B=0;A=1 B=1):
C = ( (A && B) || (!B) ) && (!A)
C = ( (A 与 B) 或 (!B) ) 与 (!A)
“&&”——And:一假全假,全真才真 (1*1=1)
“||”——Or:同假为假,一真全真 (1+1=1)
Xor:相同为假
真值表:
A | B | A and B | A or B | C |
---|---|---|---|---|
1 | 1 | 1 | 1 | 0 |
0 | 0 | 0 | 0 | 1 |
1 | 0 | 0 | 1 | 0 |
0 | 1 | 1 | 0 | 1 |
上面是所有的A和不同的情况时C的值,这道题在都灵理工大学C语言考试的时候会在小题里出现(运算或且非门)同时要求你画出真值表。
Further insight 2:
define the integer variables A, B, C, and X and initialize them with appropriate values, so to verify whether the expression
C = A < X < B
in C language corresponds to the mathematical relation (X is between A and B).
Which is the correct way to express the mathematical relation in C language?
#include
#define K 10
void main()
{
int a, b, c, d;
float x;
printf("pls int the values of a,b,c,d\n");
scanf("%d,%d,%d,%d", &a, &b, &c, &d);
x = -d*K / (a + b*c);
printf("the x is euqal to %f", x);
return 0;
}
#include
#define basic_price 100
void main()
{
int price, features, months, years;
printf("pls int the features and how long did the previous owner possessed!\n");
printf("features:");
scanf("%d", &features);
printf("year:");
scanf("%d", &years);
months = 12 * years;
price = basic_price + 40 * features + months * -20;
printf("the celluare is worth for about %d\n", price);
return 0;
}
Write a program that calculates the average value of two integers. The program will:
写一个程序来计算两个数字的平均值
a. Sum the two values (positive or negative) into a properly defined variable
把这两个数字(正数或者负数)保存在一个已经定义的函数里
b. Calculate the arithmetic mean value
计算出算数意义上的和
c. Print the result on the screen.
打印在屏幕上
#include
void main()
{
int a, b,temp;
float average;
fflush(stdin);
printf("pls int the values\n");
scanf("%d,%d", &a, &b);
temp=a+b;
average = (float)temp / 2;
printf("%f", average);
return ;
}
** Write a program that acquires 4 integer values, positive and lower than 1000. The program will:**
写一个C语言程序,输入4个小于1000的正值,并且满足:
a. Verify that the values belong to the defined interval [0, 1000). On the contrary, it must assign 0 to the value and print an error message.
判断所输入的4个数字是否在[0,1000)之间,如果不是的话使输出值为0并且输出错误信息。
b. Calculate the maximum difference between the acquired values (in absolute value)
计算所获取的数值中的最大差(最大的数字减去最小的数字,所得到的数字必须是正数)
c. Print the maximum difference result on the screen.
将最大差值打印出来。
For example, if the program receives 25, 115, 380, 213, it shall print the value 355,
which correspond to the difference between 380 and 25.
#include
void main()
{
int arr[4],count,max,min;
int store2 = 0;
int store = 0;
printf("pls int 4 values!\n");
for (count=0;count<4 ;count++)
{
scanf("%d", &arr[count]);
if (arr[count] < 1000 && arr[count])
{
continue;//如果得到的值在[0,1000)内就继续.
}
else
{
printf(" this value is not the value we want! ");
return 0;//返回一个0.
}
}//如果成功的扫描到4组数据进入下一个阶段.
for (max = 0; max < 4;max++)
{
if (arr[store] arr[min])
{
store2 = min;
}
}//min finder
printf("the max is %d and the min is %d.the maximum difference is %d !\n",arr[store],arr[store2], arr[store]- arr[store2]);
return;