POLITO COMPUTER SCIENCE lab numero tre

瞎写的POLITO COMPUTER SCIENCE lab numero due

  • Write a program following the guidelines below:
    a) Define two integer variables: int_1 and int_2
    定义两个数:int_1,int_2
    b) Define two real variables: float_1 and float_2
    定义两个浮点数: float_1,float_2
    c) Use the scanf function to acquire two real numbers and two integer numbers from the keyboard
    使用scanf来从键盘获取相应的数字。
    d) Assign the real numbers to the real variables and the integer numbers to the integer variables, respectively
    将相应的数字分配到相应的位置
    e) Use the printf function to output the values of the 4 variables on the screen in the following format:
    使用printf的格式将其按照下列标准来输出:
    i. int_1 and int_2 on the same line, each one occupying 5 spaces,
    int_1 和 int_2 在同一行,每个占5个空位
    ii. float_1 occupies at least 5 spaces and has 2 digits of precision after the decimal point
    float_1最少占5个空格切小数点后保留2个单位。
    iii. float_2 has 3 digits of precision after the decimal point.
    ** float_2小数点后保留3位 **
    备注:
    %f:不指定宽度,整数部分全部输出并输出6位小数。
    %m.nf:输出共占m列,其中有n位小数,若数值宽度小于m左端补空格。
    %-m.nf:输出共占m列,其中有n位小数,若数值宽度小于m右端补空格。
    %.nf:保留小数点后n位。
    #include<stdio.h>
    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:相同为假

真值表:

ABA and BA or BC
11110
00001
10010
01101

上面是所有的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?


  • Write a program to determine the solution of the following equation:
    写一个·C语言程序来运算下列值
    ax + bcx + dK = 0
    In particular, follow the guidelines below:
    遵循以下规则
    a) Define a constant K using #define and assign it a value as you prefer
    使用“define“来定义K的值。
    b) Define 4 integer variables called a, b, c, d corresponding to the parameters in the equation, and then define an additional real variable x
    定义4个数字“a,b,c,d”使用这四个数字来计算,同时定义一个实数“x”。
    c) Acquire the values of a, b, c, d from keyboard
    通过键盘获取“a,b,c,d”的值
    d) Calculate the value of x
    计算“x“的值
    e) Print the result on the screen.
    在屏幕上打印x的值
    #include<stdio.h>
     #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;
     }

  • I want to buy one used cell phone. The amount I want to spend is:
    -我想买一个手机,能够为其花的钱数如下
    • 100 euro as the base price
    底价100 euro
    • 40 euro extra for each of the features I’m interested
    有一个我感兴趣的功能呢可以多付40 euro
    • 20 euro less per month when the phone was possessed by the previous owner.
    前任主人每用一个月将会减少20 euro
    Realize a C program following the guidelines:
    注意你写的C语言程序需要遵循如下规则:
    a) Define the constant values that generate the price using #define
    使用define来定义一些数值
    b) Define the integer variables price, features, months and years
    定义函数: price, features, months,years;
    c) Acquire from keyboard the amount of features possessed by the phone (features) and the years for which the mobile phone was owned (years)
    通过键盘来获取相应的值( features,years)
    d) Calculate the numbers of months for which the phone is possessed (months)
    计算出手机被使用了多少个月
    e) Calculate the maximum price expendable for the phone (price)
    计算出手机的最大价值
    f) Print the result on the screen.
    将数值打印在屏幕上
#include<stdio.h>
#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<stdio.h>
     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<stdio.h>
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[max])
		{
			store = max;
		}
	}//max finder
	
	for (min = 0; min < 4;min++)
	{
		if (arr[store2] > 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;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值