C语言习题练习
1.文件每次读取字符串,排序后转存到另一文件中
- strcmp(str1,str2) 比较两个字符串大小
例如当str1 = “abc”,str2= "cba"时,返回-1;
当 str1 = “cba”,str2 = "abc"时,返回 1; - strcpy[str1,str2] 将str2的值复制到str1
fopen打开文件之后,一定需要fclose关闭文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int i = 0 , j;
char buf[100][64];
char temp[64];
FILE *in , *out;
in = fopen("file1.txt","r");
if(in == NULL){
printf("cannot open!");
exit(0);
}
while(fscanf(in,"%s",buf[i]) > 0){
for(j = i; j > 0; j--){
if(strcmp(buf[j],buf[j - 1]) < 0){
strcpy(temp,buf[j]);
strcpy(buf[j],buf[j - 1]);
strcpy(buf[j - 1],temp);
}
}
i++;
}
fclose(in);
out = fopen("file2.txt","w");
if(out == NULL){
printf("cannot open!");
exit(0);
}
for(j = 0; j < i; j++){
fprintf(out,"%s",buf[j]);
}
fclose(out);
return 0;
}
2.读取指定文件的内容,并输出,且输出的每行不得超过30个字符
void testFile2(){
int i = 0;
char buf;
FILE *fpin;
fpin = fopen("file1.txt","r");
if(fpin == NULL){
printf("cannot open!");
}
while(fscanf(fpin,"%c",&buf) > 0){
i++;
printf("%c",buf);
if(buf == '\n'){
i = 0;
}
if(i % 30 == 0){
printf("\n");
}
}
fclose(fpin);
}
3将一个字符串的从第k个字符开始全部复制到一个新的字符串,并使用指针
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
int copyString(char *a,char *b,int k);
int main()
{
int k;
char a[MAX_SIZE];
char b[MAX_SIZE];
printf("a = :\n");
gets(a);
puts(a);
printf("k = :\n");
scanf("%d",&k);
copyString(a,b,k);
puts(b);
return 0;
}
int copyString(char *a,char *b,int k){
char *p;
p = a + k - 1;
while(*p){
*b = *p;
b++;
p++;
}
*b = '\0';
return 1;
}
4.输入某天年月日,计算该天是当年第几天
void testDate(){
int i , dayNum = 0;
int month[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
Date date;
printf("year month day:\n");
scanf("%d%d%d",&date.year,&date.month,&date.day);
month[1] += JudgeYear(date.year); //判断是否时闰年,时闰年2月就+1
for(i = 0; i < date.month - 1; i++){
dayNum += month[i];
}
dayNum += date.day;
printf("dayNum = %d\n",dayNum);
}
int JudgeYear(int year){
if((year % 400 == 0) || (year % 4 == 0 && year % 100)){
return 1;
}
return 0;
}
5.求s = 1 + 2 * 3 + 4 * 5 * 6 …前n项之和
- 第 i 项 : 初始值为:i * ( i - 1 ) / 2 + 1;
- 第 i 项 : 连续的i个数字相乘
void testInputN(){
int i;
int n ,sum = 0;
printf("input n:\n");
scanf("%d",&n);
for(i = 1; i <= n; i++){
sum += calculate((i * (i - 1) / 2 + 1),i);
}
printf("sum = %d",sum);
}
int calculate(int i,int n){
int num = 1;
while(n){
num *= i;
i++;
n--;
}
return num;
}
6.求平均分数
#include <stdio.h>
#include <stdlib.h>
typedef struct Player{
int score;
int num;
}Player;
typedef struct Top3{
Player first;
Player second;
Player third;
}Top3;
void updateRank(Top3 *top,Player player);
int main()
{
Top3 top = {0};
Player player;
int i, j;
int sum, score;
int max, min;
printf("begin:\n");
for(i = 0; i < 20; i++){
min = 10; //分数不会高于10
max = 1; //分数不会低于1
sum = 0;
printf("NO.%d begin\n",i + 1);
for(j = 0; j < 8; j++){
scanf("%d",&score);
if(score < min){
min = score;
}
if(score > max){
max = score;
}
sum += score;
}
sum = sum - min - max;
player.num = i + 1;
player.score = sum;
updateRank(&top,player);
}
printf("num | score\n");
printf("%d %d\n",top.first.num,top.first.score);
printf("%d %d\n",top.second.num,top.second.score);
printf("%d %d\n",top.third.num,top.third.score);
return 0;
}
void updateRank(Top3 *top,Player player){
if(player.score > top->first.score){
top->third = top->second;
top->second = top->first;
top->first = player;
}else if(player.score > top->second.score){
top->third = top->second;
top->second = player;
}else if(player.score > top->third.score){
top->third = player;
}
}
7.查找给定字符在字符串中首次出现的位置
#define MAX_SIZE 100
void testFindChar(){
int k;
char str[MAX_SIZE];
char ch;
printf("input str:\n");
gets(str);
printf("input ch:\n");
ch = getchar();
k = FindChar(str,ch);
printf("locate = %d",k);
}
int FindChar(char *str,char ch){
int i = 0;
while(*str){
i++;
if(*str == ch)
return i;
str++;
}
return 0;
}
8.乘法口诀
void MultiTable(){
int i , j;
for(i = 1; i < 10;i++){
for(j = 1; j <= i; j++){
printf("%d * %d = %02d ",i, j ,i * j);
}
printf("\n");
}
}
9判断回文完数
void TestHWWS(){
int num = 1;
while(num){
printf("input:\n");
scanf("%d",&num);
if(JudgeHW(num) && JudgeWS(num)){
printf("%d = HWWS\n",num);
}else if(JudgeHW(num)){
printf("%d = HW\n",num);
}else if(JudgeWS(num)){
printf("%d = WS\n",num);
}
}
}
int JudgeHW(int num){
int numr = 0,temp = num;
while(temp){
numr = numr * 10 + temp % 10;
temp /= 10;
}
return (num == numr);
}
int JudgeWS(int num){
int i, sum = 0;
for(i = 1; i <= num / 2; i++){
if(num % i == 0){
sum += i;
}
}
return (num == sum);
}
10二位数组奇偶数输出
void JudgeOdevity(){
int num[3][3];
int odd[9]; //奇数
int even[9]; //偶数
int i, j;
int numOdd = 0,numEven = 0; // 奇数和偶数的数目
int numOddPrint = 0,numEvenPrint = 0; //已经打印的奇偶数数目
printf("input:\n");
for(i = 0; i < 3; i++){
for(j = 0; j < 3; j++){
scanf("%d",&num[i][j]);
if(num[i][j] % 2){
odd[numOdd++] = num[i][j];
}else{
even[numEven++] = num[i][j];
}
}
}
//如果已经输出的奇数和偶数之和小于总的数目
while(numOddPrint + numEvenPrint < 9){
printNumfuc(odd,numOdd,&numOddPrint);
printNumfuc(even,numEven,&numEvenPrint);
}
}
void printNumfuc(int num[],int length,int *printNum){
int i;
//输出3次,或者全部输出就结束循环
for(i = 0; i < 3 && *printNum < length;i++){
printf("%4d",num[i]);
(*printNum)++;
}
printf("\n");
}
11 从键盘输入英文找出最短和最长的单词
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 100
typedef struct Word{
char word[MAX_SIZE];
int length;
}Word;
void InputEngLish(){
int num; //字符数目
int flag = 0;
Word longest = {0},shortest;
shortest.length = MAX_SIZE;
Word temp;
printf("input:\n");
scanf("%s",temp.word);
temp.length = strlen(temp.word);
num = temp.length;
while(num < 3000 && strcmp(temp.word,"#") != 0){
if(strcontain(temp.word,'#')){
flag = 1;
}
if(temp.length > longest.length){
longest = temp;
}
if(temp.length < shortest.length){
shortest = temp;
}
if(flag)
break;
scanf("%s",temp.word);
if(strcontain(temp.word,'#')){
flag = 1;
}
strcontain(temp.word,'#');
temp.length = strlen(temp.word);
num += temp.length;
}
printf("num = %d\n",num);
printf("longest: ");
printf("%s\n",longest.word);
printf("shortest: ");
printf("%s\n",shortest.word);
}
int strcontain(char *str,char ch){
while(*str){
if(*str == '#'){
*str = '\0';
return 1;
}
str++;
}
return 0;
}
12输入员工信息,按薪水升序排列,找出中位数
typedef struct Salary{
char id[10];
char name[20];
float wage;
struct Salary *next;
}Salary;
typedef struct Company{
Salary *first;
int count;
}Company;
void TestSalary(){
Company company;
char id[MAX_SIZE];
char name[MAX_SIZE];
float wage;
InitCompany(&company);
printf("input:\n");
scanf("%s%s%f",id,name,&wage);
while(wage > 0){
InsertSalary(&company,id,name,wage);
scanf("%s%s%f",id,name,&wage);
}
printf("mid salary = %f\n",FindMid(company));
}
void InitCompany(Company *company){
Salary* salary = (Salary *)malloc(sizeof(Salary));
salary->next = NULL;
company->first = salary;
company->count = 0;
}
void InsertSalary(Company *company,char *id,char *name,float wage){
Salary *p , *pre, *m;
m = (Salary *)malloc(sizeof(Salary));
strcpy(m->id,id);
strcpy(m->name,name);
m->wage = wage;
p = company->first->next;
p = company->first;
while(p){
if(p->wage > m->wage){
break;
}
pre = p;
p = p->next;
}
m->next = pre->next;
pre->next = m;
company->count++;
}
float FindMid(Company company){
Salary *p;
int n = company.count / 2 - 1;
float midWage;
p = company.first->next;
while(n){
p = p->next;
n--;
}
if(company.count % 2 == 0){
midWage = (p->wage + p->next->wage) / 2;
}else{
midWage = p->next->wage;
}
return midWage;
}