【c语言】c程序设计--谭浩强--(第四章)--1

// 输入两个实数,按代数值由小到大的顺序输出这两个数

#include <stdio.h>

int main()
{
	int a,b;
	printf("请输入两个数:");
	scanf("%d%d",&a,&b);
	if(a > b)
		printf("由小到大:%d  %d\n",b,a);
	else
		printf("由小到大:%d  %d\n",a,b);
	return 0;
}

// 输入3个数,要求按从小到大顺序输出

#include <stdio.h>

int main()
{
	int a,b,c,t;
	printf("请输入三个数:");
	scanf("%d%d%d",&a,&b,&c);
	if(a > b)
	{
		t = a;
		a = b;
		b = t;
	}
	if(a > c)
	{
		t = a;
		a = c;
		c = t;
	}
	if(b > c)
	{
		t = b;
		b = c;
		c = t;
	}
	printf("从小到大的顺序是:%d  %d  %d\n",a,b,c);
	return 0;
}

// 输入一个字符,判断它是否为大写字母,如果是,将它转换成小写字母,如果不是不转换

#include <stdio.h>

int main()
{
	char ch;
	printf("请输入一个字符:");
	scanf("%c",&ch);
	if(ch >= 'A' && ch <= 'Z')
		ch = ch + 32;
	printf("转换成小写字母是:%c\n",ch);

	return 0;
}

// 有一函数:x < 0 ,y = -1;x = 0,y = 0;x > 0,y = 1,编程输入一个x值,要求输出对应的y

#include  <stdio.h>

int main()
{
	int x,y;
	printf("请输入x:");
	scanf("%d",&x);
	if(x > 0)
		y = 1;
	else if(x < 0)
		y = -1;
	else
		y = 0;
	printf("对应的y值是:%d\n",y);
	return 0;
}

// 要求按照考试成绩的等级输出百分制分数段,A等为85分以上,B等为70~84分,
// C等为60~69分,D等为60分以下。成绩的等级由键盘输入

#include <stdio.h>

int main()
{
	char ch;
	printf("请输入成绩的等级:");
	scanf("%c",&ch);
	switch(ch)
	{
		case 'A':
			printf("分数段是:85分以上\n");
			break;
		case 'B':
			printf("分数段是:70~84分\n");
			break;
		case 'C':
			printf("分数段是:60~69分\n");
			break;
		case 'D':
			printf("分数段是:60分以下\n");
			break;
		default:
			break;

	}
	return 0;
}

// 编写一个简单的计算器,实现加减乘除

#include <stdio.h>

int add( int a,int b )
{
	int c;
	c = a + b;
	return c;
}

int fabs( int a,int b )
{
	int c;
	c = a - b;
	return c;
}

int mul( int a,int b )
{
	int c;
	c = a * b;
	return c;
}

int div( int a,int b )
{
	int c;
	c = a / b;
	return c;
}

int main()
{
	int input = 1;
	int a,b;
	printf("***********************************************\n");
	printf("*  1、加法                      2、减法       *\n");
	printf("*  3、乘法                      4、除法       *\n");
	printf("***********************************************\n");
	while(input)
	{

		printf("请输入你要选择的操作:");
		scanf("%d",&input);
		if(input > 4)
		{
			printf("没有你想要的操作,请重新输入\n");
			return 0;
		}
		printf("请输入你要操作的两个数:");
		scanf("%d%d",&a,&b);
		switch(input)
		{
			case 1:
				printf("%d + %d = %d\n",a,b,add(a,b));
				break;
			case 2:
				printf("%d - %d = %d\n",a,b,fabs(a,b));
				break;
			case 3:
				printf("%d * %d = %d\n",a,b,mul(a,b));
				break;
			case 4:
				printf("%d / %d = %d\n",a,b,div(a,b));
				break;
			default:
				break;

		}
	}
	return 0;
}

// 运输公司对用户计算运输费用。路程越远,每吨*千米运费越低。标准如下:
// s<250---没有折扣     250<=s<500---2%折扣    500<=s<1000---5%折扣
// 1000<=s<2000---8%折扣   2000<=s<3000---10%折扣   3000<=s---15%折扣

#include <stdio.h>

int main()
{
	float p,w,zp,z;
	int x,s;
	printf("请输入单价,重量,距离:");
	scanf("%f%f%d",&p,&w,&s);
	if( s >= 3000 )
		x = 12;
	else
		x = s / 250;
	switch(x)
	{
		case 0:	  z = 0; break;
		case 1:   z = 2; break;
		case 2:          break;
		case 3:   z = 5; break;
		case 4:		     break;
		case 5:          break;
		case 6:          break;
		case 7:   z = 8; break;
		case 8:          break;
		case 9:          break;
		case 10:         break;
		case 11:  z = 10;break;
		case 12:  z = 15;break;
		default:break;
	}
	zp = p * w * s * (1 - z / 100);
	printf("总运费是:%10.2f\n",zp);
	return 0;
}

时间: 2024-07-31 12:25:44

【c语言】c程序设计--谭浩强--(第四章)--1的相关文章

c++面向对象程序设计 课后题 答案 谭浩强 第四章

c++面向对象程序设计课后题答案 谭浩强 第四章 1: #include <iostream> using namespace std; class Complex {public: Complex(){real=0;imag=0;} Complex(double r,double i){real=r;imag=i;} double get_real(); double get_imag(); void display(); private: double real; double imag;

c++面向对象程序设计 谭浩强 第三章答案

2: #include <iostream> using namespace std; class Date {public: Date(int,int,int); Date(int,int); Date(int); Date(); void display(); private: int month; int day; int year; }; Date::Date(int m,int d,int y):month(m),day(d),year(y) { } Date::Date(int m

【c语言】c程序设计--谭浩强--(第一章)

闲来无事把谭浩强的书里边的题敲了一遍,,纪念一哈,,纯手工 // 在屏幕上输出 This is a c program. #include <stdio.h> int main() { printf("This is a c program.\n"); return 0; } // 求两个整数之和 #include <stdio.h> int main() { int a,b,c; printf("请输入两个整数:\n"); scanf(&q

【c语言】c程序设计--谭浩强--(第二章)--2

// 有两个瓶子a和b,分别盛放醋和酱油,要求将它们互换 #include <stdio.h> int main() { int a,b,c; printf("请输入两个整数:"); scanf("%d%d",&a,&b); c = a; a = b; b = c; printf("交换后的结果是:%d %d\n",a,b); return 0; } // 依次将10个数输入,要求输出其中最大的数 #include &

c++面向对象程序设计 谭浩强 第五章答案

1: #include <iostream> using namespace std; class Student {public: void get_value() {cin>>num>>name>>sex;} void display( ) {cout<<"num: "<<num<<endl; cout<<"name: "<<name<<end

c++面向对象程序设计 谭浩强 第一章答案

c++面向对象程序设计 谭浩强 答案 第一章 第1章  C++?的初步知识 1.请根据你的了解,叙述C++?的特点.C++?对C有哪些发展? [解] 略. 2.一个C++的程序是由哪几部分构成的?其中的每一部分起什么作用? [解] 略. 3.从拿到一个任务到得到最终结果,一般要经过几个步骤? [解] 略. 4.请说明编辑.编译.连接的作用.在编译后得到的目标文件为什么不能直接运行? [解] 编译是以源程序文件为单位进行的,而一个完整的程序可能包含若干个程序文件,在分别对它们编译之后,得到若干个目

c++面向对象程序设计 谭浩强 第二章答案

第二章 类体内定义成员函数 #include <iostream> using namespace std; class Time { public: void set_time(); void show_time(); private:                    //成员改为公用的 int hour; int minute; int sec; }; void Time::set_time()          //在main函数之前定义 { cin>>hour; ci

C语言学习笔记---谭浩强

前段时间有机会去面试了一次,真是备受“打击”(其实是启发),总的来说就是让我意识到了学习工具和学习技术的区别.所以最近在看一些数据结构和算法,操作系统,python中的并行编程与异步编程等东西.然而数据结构那些本来是在看python下的,但是python实在包装了太多的东西而大多的经典教材都是基于C语言的(自然是这样,C语言能够操作直接访问物理地址,能够进行位(bit)操作的特点就决定了.)所以就决定重新学一遍C语言,先重温下谭浩强老师的书籍记录下要点: 一:语言: 机器语言:计算机直接识别和接

【c语言】c程序设计--谭浩强--(第三章)--2

// 假如我国国民生产总值的年增长率为9%,计算10年后我国国民生产总值与现在相比增长多少百分比 // p = ( 1 + r )^n r--年增长率 n--年数 p--与现在相比的倍数 #include <stdio.h> #include <math.h> int main() { double r = 0.09; double p; int n = 10; p = pow( ( 1 + r ) , n ); printf("10年后增长了 %f 倍\n",