C语言上机练习

  • 格式:doc
  • 大小:173.50 KB
  • 文档页数:17

(1)已知某公司员工的保底薪水为500,某月所接工程的利润profit(整数)与利润提成的关系如下(计量单位:元):profit≤1000没有提成;1000<profit≤2000提成10%;2000<profit≤5000提成15%;5000<profit≤10000提成20%;10000<profit提成25%。

请根据输入的利润计算员工的薪水。

编程可用素材:printf("Input profit: ")...、printf("\nsalary=...\n"...。

#include <stdio.h>int main(void){double base = 500, salary;int profit;printf("Input profit:");scanf("%d", &profit);if (profit <= 1000){salary = base;}else if (profit <= 2000){salary = base + profit * 0.1;}else if (profit <= 5000){salary = base + profit * 0.15;}else if (profit <= 10000){salary = base + profit * 0.2;}else{salary = base + profit * 0.25;}printf("salary=%.2f\n", salary);return 0;}(2)用scanf输入10个整数(采用int数据类型),计算所有正数的和、负数的和以及10个数的和。

编程可用素材:printf("Input 10 integers.\n")...、printf("\nzhengshu=...,fushu=...,all=...\n"...。

#include <stdio.h>int main(void){int x, zhengshu = 0, fushu = 0, count = 0;printf("Input 10 integers.\n");do{scanf("%d", &x);count++;if (x > 0){zhengshu = zhengshu + x;}else if (x < 0){fushu = fushu + x;}} while (count < 10);printf("zhengshu=%d,fushu=%d,all=%d\n", zhengshu, fushu, zhengshu + fushu);return 0;}(3)从键盘上读入一行字符,在屏幕上输出该行字符的长度及内容(先输出长度,后输出内容)。

注意:(1)以回车表示行结束且回车不计入输入内容。

若读入过程中发生错误或遇到文件结束,则也表示行输入结束。

(2)若用户输入时输入了很多字符,则仅读入前100个字符。

(3)不能使用库函数gets、fgets、strlen或使用同名的变量、函数、单词。

(4)编程可用素材:printf("input a string: ")...、printf("\nThe string lenth is: ...、printf("\nThe string is: ...。

#include <stdio.h>int main(void){char str[101];int ch, i = 0, length = 0;printf("input a string: ");ch = fgetc(stdin);while (((char)ch != '\n') && (ch != EOF)){length++;if (length <= 100){str[i] = (char)ch;i++;ch = fgetc(stdin);}else{break;}}str[i] = '\0';printf("\nThe string lenth is: %d", i);printf("\nThe string is: %s\n", str);return 0;}(4)输出n行星号,每行5个*星号。

编程可用素材:printf("please input n: ")...。

#include <stdio.h>int main(void){int n, i, j;printf("please input n: ");scanf("%d", &n);for (i=0; i<n; i++){for (j=0; j<5; j++){printf("* ");}printf("\n");}return 0;}(5)输入3行3列的矩阵,输出所有元素的累加和。

编程可用素材:printf("Please input the 3x3 Matrix:\n"...、printf("\nsum=...\n"...。

程序的运行效果应类似地如图1所示,图1中的1 2 34 5 67 8 9#include <stdio.h>int main(void){int i, j, sum = 0, arr[3][3];for (i=0; i<3; i++){for (j=0; j<3; j++){scanf("%d", &arr[i][j]);}}for (i=0; i<3; i++){for (j=0; j<3; j++){sum = sum + arr[i][j];}}printf("sum=%d\n", sum);return 0;}(6)从键盘输入一个正三角形的边长(整型),计算该三角形的面积和周长。

注:根据“海伦-秦九韶”公式,area=√p(p-a)(p-b)(p-c),其中p=(a+b+c)/2、a,b,c为三角形的3条边长。

#include<stdio.h>#include<math.h>int main(void){int a,l;double p,s,q;printf("Intput a side of triangle: ");scanf("%d",&a);p=(3*a)/2;q=p*(p-a)*(p-a)*(p-a);s=sqrt(q);l=3*a;printf("the aera of triangle is %.2f,the cirle of triangle is %d\n",s,l);return 0;}(7)从键盘输入3个整数,输出绝对值最大的数。

编程可用素材:printf("Input 3 numbers: ")...、printf("\nThe number with maximum absolute value is ....\n"...。

#include <stdio.h>#include <math.h>int main(void){int x, y, z, max;printf("Input 3 numbers: ");scanf("%d,%d,%d", &x, &y, &z);if (abs(x) > abs(y)){max = abs(x);}else{max = abs(y);}if (abs(z) > max){max = abs(z);}printf("The number with maxinum absolute value is ");if (max == abs(x)){printf("%d.\n", x);}else if (max == abs(y)){printf("%d.\n", y);}else if (max == abs(z)){printf("%d.\n", z);}return 0;}(8)现有两个一维数组(各含5个整型元素)设为A、B,从键盘分别输入数据给这两个数组。

计算A数组正序位置与B数组逆序对应位置积的和。

编程可用素材:printf("\n\nInput A: ")...、printf("Input B: ")...、printf("sum=...\n"...。

#include <stdio.h>int main(void){int i, j, sum = 0, A[5], B[5];printf("Input A:");for (i = 0; i < 5; i++){scanf("%d", &A[i]);}printf("Input B:");for (i = 0; i < 5; i++){scanf("%d", &B[i]);}for (i = 0, j = 4; i < 5; i++, j--){sum = sum + A[i] * B[j];}printf("sum=%d\n", sum);return 0;}(9)从键盘输入一行可带空格的字符串(约定:字符数≤127字节),按逆序输出该字符串。

注意,程序中不能使用库函数strrev或使用同名的变量、函数、单词。

编程可用素材:printf("Input a string: ")...、printf("\nThe result is: ")...。

#include <stdio.h>#include <string.h>int main(void){char str[100];int i, len;printf("Input a string: ");gets(str);len = strlen(str);printf("The result is: ");for (i=len-1; i>=0; i--)printf("%c", str[i]);}return 0;}(10)编写一程序P7-744.C实现以下功能从键盘输入一个一百分制成绩,如果不在0~100范围内,则要求重新输入数据,直到输入的数据在0~100范围内。