2021-12-16 10:59来源:m.sf1369.com作者:宇宇
读入文件ex.c,并把其中所有的小写字母改为大写字母,其余字符不变,在屏幕上输出。
#include<stdio.h>
main()
{
FILE *fp;
char ch;
if((fp=fopen(ex.c,r))==NULL)
{
printf(Cannot open file strike any key exit!);
getch();
exit(1);
}
ch=fgetc(fp);
while (ch!=EOF)
{
if(ch>='a'&&ch<='z')ch-=32;
putchar(ch);
ch=fgetc(fp);
}
fclose(fp);
}
public class mainClass {
public mainClass() {
}
public static void main(String [] args){
double x = 0;
double y = 0;
try{
File f = new File(D:/test.txt);
BufferedReader br = new BufferedReader(new FileReader(f)) ;
String s = null;
int i = 1;
double node1_x,node1_y,node2_x,node2_y;
while( ( s = br.readLine()) != null){
System.out.println(第+i+行:);
node1_x = Double.parseDouble(s.substring(s.indexOf('(')+1,s.indexOf(',')));
node1_y = Double.parseDouble(s.substring(s.indexOf(',')+1,s.indexOf(')')));
System.out.println(与第一个节点的距离为:+Math.pow(Math.pow(x-node1_x,2)+Math.pow(y-node1_y,2),0.5));
node2_x = Double.parseDouble(s.substring(s.lastIndexOf('(')+1,s.lastIndexOf(',')));
node2_y = Double.parseDouble(s.substring(s.lastIndexOf(',')+1,s.lastIndexOf(')')));
System.out.println(与第二个节点的距离为:+Math.pow(Math.pow(x-node2_x,2)+Math.pow(y-node2_y,2),0.5));
i++;
}
}catch(Exception e){
e.printStackTrace();
}
}
}
假设文件中的每一行都是这样:
node1(3,4) node2(222,1.8934)
这个程序才能用,
另外记着import java.io.*;