编写程序outch,程序读入一行字符,根据命令行中的参数进行不同的输出。若有以下命令行:out -2

编写程序outch,程序读入一行字符,根据命令行中的参数进行不同的输出。若有以下命令行:out -2,第1张

#include <stdlib.h>

#include <stdio.h>

#include <string.h>

int main(int argc, char *argv[])

{

char buffer1[1024]

char buffer2[100]

int  buffer1_len

int  buffer2_len

int  out_len

printf("Please input string:")

gets(buffer1)

printf("Please input command(usage: out -2): ")

gets(buffer2)

buffer1_len = strlen(buffer1)

buffer2_len = strlen(buffer2)

if ( buffer2_len < 6

|| strncmp(buffer2, "out ", 4) != 0

|| (buffer2[4] != '+' && buffer2[4] != '-')

|| (out_len = atoi(buffer2 + 4)) == 0

|| fabs(out_len) > buffer1_len )

{

printf("error input, will exit.\n")

return 0

}

if ( fabs(out_len) == buffer1_len )

{

printf("result: %s\n", buffer1)

}

else if ( buffer1_len + out_len < buffer1_len )

{

printf("result: %s\n", buffer1 + buffer1_len + out_len)

}

else

{

buffer1[out_len] = '\0'

printf("result: %s\n", buffer1)

}

return 0

}

输出结果:1234

这是一个字符整数的程序:从头开始,依次取出字符串中的字符,当前整数向前进一位(x10),并将取出的字符作为个位数添加到整数的末尾。其中,s++表示字符串指针向后移,取下一个字符;*s-'0',当前字符跟

'0’

ASCII

编码之差,就是当前字符字面显示的数字(比如

'4'-'0’=4,实际上是

ASCII

编码之差 52-48=4 )

完整的程序:

#include

<stdio.h>

int

str_to_num(char

*s)

{

int

k=0

while(*s)

k

=

k*10

+

*s++

-

'0'

return

k

}

int

main()

{

char

*s

=

"1234"

printf("string

value: %s,

number

value:

%d\n",

s,

str_to_num(s))

getchar()

return

0

}

第2题

#include<stdio.h>

void main()

{

char *p[7]={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"}

int i

printf("please input a number:\n")

scanf("%d",&i)

switch(i)

{case 1:printf("Monday\n")break

case 2:printf("Tuesday\n")break

case 3:printf("Wednesday\n")break

case 4:printf("Thursday\n")break

case 5:printf("Friday\n")break

case 6:printf("Saturday\n")break

case 7:printf("Sunday\n")break

default:break

}

}

运行结果是:

please input a number:

3

Wednesday

第3题:

#include<stdio.h>

struct student

{ char no[5]

char name[10]

int score[4]

double average

}

void readrec(struct student s[5])

{

int i

printf("please input 5 students'information\n")

for(i=0i<5i++)

{s[i].average=0.0

scanf("%s%s%d%d%d%d",s[i].no,s[i].name,&s[i].score[0],&s[i].score[1],&s[i].score[2],&s[i].score[3])

s[i].average=(s[i].score[0]+s[i].score[1]+s[i].score[2]+s[i].score[3])/4.0

}

}

void writerec(struct student s[5])

{

int j

for(j=0j<5j++)

{printf("%s %s %d %d %d %d %f",s[j].no,s[j].name,s[j].score[0],s[j].score[1],s[j].score[2],s[j].score[3],s[j].average)

printf("\n")}

}

void main()

{struct student s[5]

readrec(s)

printf("these 5 students'information are:\n")

writerec(s)

}

运行结果是:

please input 5 students'information

1061 lili 76 75 74 78

1062 wangsui 80 87 84 86

1063 huangsi 76 74 80 91

1064 chengong 82 87 89 90

1094 huwei 90 100 64 80

these 5 students'information are:

1061 lili 76 75 74 78 75.750000

1062 wangsui 80 87 84 86 84.250000

1063 huangsi 76 74 80 91 80.250000

1064 chengong 82 87 89 90 87.000000

1094 huwei 90 100 64 80 83.500000

第4题不会

第5题

#ifndef TRUE

#define TRUE 1

#endif

#ifndef FALSE

#define FALSE 0

#endif

#include<stdio.h>

void main()

{

FILE *fp

char ch,s[1024]

int i

fp=fopen("/home/ccx/mydocument/filename","wb+")

printf("请输入10个字符串,以‘#'结束!\n")

ch=getchar()

while(ch!='#')

{fputc(ch,fp)

ch=getchar()

}

fclose(fp)

printf("你所输入的字符串是\n")

fp=fopen("/home/ccx/mydocument/filename","r")

while(TRUE)

{memset(s,0,sizeof(s))

if(fgets(s,1000,fp)==FALSE) break

printf("%s",s)

printf("\n")

}

fclose(fp)

}

运行结果是:

请输入10个字符串,以‘#'结束!

hahsg xhajkf ahaua dhaak xhaj23 shka8 ./alsj shasif shjkx akaj#

你所输入的字符串是

hahsg xhajkf ahaua dhaak xhaj23 shka8 ./alsj shasif shjkx akaj


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/7919061.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-11
下一篇 2023-04-11

发表评论

登录后才能评论

评论列表(0条)

保存