尝试AS的手机连接

news/2025/3/16 22:00:59/文章来源:https://www.cnblogs.com/fanxn/p/18775799

1.我尝试进行手机连接电脑进行debug,但是我发现我找到的教程做到一半和我的手机配置后者是AS版本不同,不知道为什么IDE会报错不能连接手机。尝试了1.5h,令人烦躁。之后计划多创建几个项目作为备份使用,防止在创建项目时浪费很多时间。在这个过程中,我了解到了虚拟设备对内存的威胁,中间有几次我的程序运行到一半卡退了。还有就是,在运行项目时,一定要先build,再运行!
2.完成了石家庄站地铁的手机端收票计算。
在过程中,我发现站点信息输入错误,逐一进行了校验。之后,MainActivity.java的换乘逻辑错误,换乘时多加了一站。还有,在涉及换乘的站点计算时,换乘后的站点计算消失了,这样就会导致平白无故地少了几站,导致金额算错。下面是我修改上面问题之后的代码:
MainActivity.java

package com.example.myapplication3;import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;import java.util.*;public class MainActivity extends AppCompatActivity {private Spinner startStation, endStation;private EditText ticketQuantity;private Button buyButton;private TextView resultText;private Map<String, List<String>> lines;private Map<String, String> stationToLine;@SuppressLint("MissingInflatedId")@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);startStation = findViewById(R.id.startStation);endStation = findViewById(R.id.endStation);ticketQuantity = findViewById(R.id.ticketQuantity);buyButton = findViewById(R.id.buyButton);resultText = findViewById(R.id.resultText);// 初始化地铁线路和站点initializeLinesAndStations();// 石家庄地铁站点列表,包含线路信息List<String> stationsWithLines = new ArrayList<>();for (Map.Entry<String, String> entry : stationToLine.entrySet()) {stationsWithLines.add(entry.getKey() + " (" + entry.getValue() + ")");}ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, stationsWithLines);adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);startStation.setAdapter(adapter);endStation.setAdapter(adapter);buyButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {calculateAndDisplayTicketPrice();}});}private void initializeLinesAndStations() {lines = new HashMap<>();stationToLine = new HashMap<>();// 1号线List<String> line1 = Arrays.asList("西王", "时光街", "长城桥", "和平医院", "烈士陵园", "新百广场", "解放广场", "平安大街", "北国商城", "博物馆", "体育场", "北宋", "谈固", "朝晖桥", "白佛", "留村", "火炬广场", "石家庄东站", "南村", "洨河大桥", "西庄", "东庄", "会展中心", "商务中心", "园博园", "福泽");lines.put("1号线", line1);for (String station : line1) {stationToLine.put(station, "1号线");}// 2号线List<String> line2 = Arrays.asList("柳辛庄", "庄窠·铁道大学", "义堂", "建和桥", "长安公园", "北国商城", "裕华路", "槐中路", "欧韵公园", "元村", "石家庄站", "塔坛", "仓丰路留村", "南位", "嘉华路");lines.put("2号线", line2);for (String station : line2) {stationToLine.put(station, "2号线");}// 3号线List<String> line3 = Arrays.asList("西三庄", "高柱", "柏林庄", "市庄", "市二中", "新百广场", "东里", "西三教", "石家庄站", "汇通路", "孙村", "塔冢", "东王", "南王", "位同", "东二环南路", "西仰陵", "中仰陵", "南豆", "太行南大街", "乐乡");lines.put("3号线", line3);for (String station : line3) {stationToLine.put(station, "3号线");}}private void calculateAndDisplayTicketPrice() {String startWithLine = startStation.getSelectedItem().toString();String endWithLine = endStation.getSelectedItem().toString();// 提取站点名称(去掉线路信息)String start = startWithLine.split(" \\(")[0];String end = endWithLine.split(" \\(")[0];String quantityStr = ticketQuantity.getText().toString();if (quantityStr.isEmpty()) {resultText.setText("请输入购票数量");return;}int quantity = Integer.parseInt(quantityStr);int distance = calculateShortestPath(start, end);int price = calculatePrice(distance);int totalPrice = price * quantity;resultText.setText("购票成功!总金额: " + totalPrice + "元");}private int calculateShortestPath(String start, String end) {if (!stationToLine.containsKey(start) || !stationToLine.containsKey(end)) {return -1;}Map<String, Integer> distances = new HashMap<>();Map<String, String> lineUsed = new HashMap<>();for (String station : stationToLine.keySet()) {distances.put(station, Integer.MAX_VALUE);}distances.put(start, 0);lineUsed.put(start, stationToLine.get(start));PriorityQueue<String> queue = new PriorityQueue<>(Comparator.comparingInt(distances::get));queue.add(start);while (!queue.isEmpty()) {String current = queue.poll();if (current.equals(end)) {break;}String currentLine = lineUsed.get(current);List<String> currentLineStations = lines.get(currentLine);// 找到当前站点在线路中的位置int currentIndex = currentLineStations.indexOf(current);if (currentIndex == -1) {continue; // 如果当前站点不在当前线路中,跳过}// 向前遍历(向线路的下一站)if (currentIndex < currentLineStations.size() - 1) {String nextStation = currentLineStations.get(currentIndex + 1);int newDistance = distances.get(current) + 1;if (newDistance < distances.get(nextStation)) {distances.put(nextStation, newDistance);lineUsed.put(nextStation, currentLine);queue.add(nextStation);}}// 向后遍历(向线路的上一站)if (currentIndex > 0) {String prevStation = currentLineStations.get(currentIndex - 1);int newDistance = distances.get(current) + 1;if (newDistance < distances.get(prevStation)) {distances.put(prevStation, newDistance);lineUsed.put(prevStation, currentLine);queue.add(prevStation);}}// 考虑换乘for (String line : lines.keySet()) {if (lines.get(line).contains(current) && !line.equals(currentLine)) {// 换乘增加1站int newDistance = distances.get(current) + 1;List<String> transferLineStations = lines.get(line);int transferIndex = transferLineStations.indexOf(current);if (transferIndex == -1) {continue; // 如果当前站点不在换乘线路中,跳过}// 向前遍历(向换乘线路的下一站)if (transferIndex < transferLineStations.size() - 1) {String nextTransferStation = transferLineStations.get(transferIndex + 1);if (newDistance < distances.get(nextTransferStation)) {distances.put(nextTransferStation, newDistance);lineUsed.put(nextTransferStation, line);queue.add(nextTransferStation);}}// 向后遍历(向换乘线路的上一站)if (transferIndex > 0) {String prevTransferStation = transferLineStations.get(transferIndex - 1);if (newDistance < distances.get(prevTransferStation)) {distances.put(prevTransferStation, newDistance);lineUsed.put(prevTransferStation, line);queue.add(prevTransferStation);}}}}}return distances.get(end);}private int calculatePrice(int distance) {// 每3站收费1元,不足3站按1元收费return (distance + 2) / 3;}
}

布局代码:activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:padding="16dp"><Spinnerandroid:id="@+id/startStation"android:layout_width="match_parent"android:layout_height="wrap_content"android:prompt="@string/start_station_prompt" /><Spinnerandroid:id="@+id/endStation"android:layout_width="match_parent"android:layout_height="wrap_content"android:prompt="@string/end_station_prompt" /><EditTextandroid:id="@+id/ticketQuantity"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="购票数量"android:inputType="number" /><Buttonandroid:id="@+id/buyButton"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="立即购票" /><TextViewandroid:id="@+id/resultText"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="结果将显示在这里"android:textSize="18sp"android:gravity="center"android:padding="16dp" />
</LinearLayout>

3.实现单链表的中间位置插入和单链表的释放
代码如下:
对于链表释放的操作,有两种方法:指针站在自己删自己、指针站在表头依次向后删除
在这个过程中,不可避免的要生成一个临时指针进行备份。要注意的是,不能将指针p的值给临时指针,然后再释放p。因为从图上来看,p和临时指针只是指向一个相同的元素,不能代表那个元素本身。这样操作,临时指针指向的内容也会消失。还有就是,每释放一个节点,就对链表头的总数减去一。在执行这个操作时,有点类似于删除时的操作思想。

void releaseLinkList(LinkList_t* link_table) {//站在自己删自己//node_t* p = link_table->head.next;//node_t* tmp = malloc(sizeof(node_t));//while (p) {//	tmp = p->next;//	free(p);//	p = tmp;//	link_table->count--;//记得给个数相减//}//守住头结点,一直删除头结点后面的元素node_t* p = &link_table->head;//头结点的首地址node_t* tmp;while (p->next) {tmp = p->next;p->next = tmp->next;free(tmp);link_table->count--;}printf("number of node_t: %d\n", link_table->count);
}void showLinkList(const LinkList_t* link_table) {//辅助指针指向第一个元素,辅助指针不断向后指,直到遇到NULLnode_t* p = link_table->head.next;//指针类型等于指针类型,不应该多加“&”printf("link list: %d\n ", link_table->count);while (p) {printf("%d\t", p->val);p = p->next;}printf("\n");
}

在中间插入的代码:
1.注意传过来的位置pos是从0开始还是从1开始
2.判断pos传值的合法性,总不能大于链表节点的总数或者是负数
3.还是,找到待插入节点的前一个位置;此处,指针p和下标index关联起来,有助于判断p的位置
4.执行插入操作

int insertLinkListPos(LinkList_t* link_table, int pos, Element_t val) {//pos:[0...]if (pos < 0 || pos > link_table->count) {//可以等于,放到最后printf("insert pos invalid!\n");return -1;}node_t* p = &link_table->head;/*for (int i = 0; i < pos-1; i++) {p = p->next;}*///找到索引pos-1的首地址int index = -1;while (p&&index<pos-1) {p = p->next;index++;}node_t* new_node = malloc(sizeof(node_t));new_node->val = val;new_node->next = p->next;p->next = new_node;link_table->count++;return 0;}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/900009.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

20242313 2024-2025-2 《Python程序设计》实验一报告

20242313 2024-2025-2 《Python程序设计》实验一报告 课程:《Python程序设计》 班级:2423 姓名:曾海鹏 学号:20242313 实验教师:王志强 实验日期:2025年3月16日 必修/选修:公选课 1.实验内容 1.熟悉Python开发环境; 2.练习Python运行、调试技能;(编写书中的程序,并…

nn.Embedding()函数详解

nn.Embedding()函数详解 nn.Embedding()函数:随机初始化词向量,词向量在正态分布N(0,1)中随机取值 输入: torch.nn.Embedding(num_embeddings, embedding_dim, padding_idx=None, max_norm=None, norm_type=2.0, scale_grad_by_freq=False, sparse=False, _weight=None) num…

htb Authority

端口扫描 nmap -sC -sV -p- -Pn -T4 10.10.11.222 Starting Nmap 7.92 ( https://nmap.org ) at 2024-10-04 19:42 CST Nmap scan report for 10.10.11.222 (10.10.11.222) Host is up (0.40s latency). Not shown: 65506 closed tcp ports (reset) PORT STATE SERVICE …

蓝桥杯14届省B

蓝桥杯14届省赛B组A:int a[105]; int day[]={0,31,28,31,30,31,30,31,31,30,31,30,31};//记录每个月有多少天 set<int> st;//记录不重复的日期void check(int mm,int dd){if (mm>12||mm<1||dd<1||dd>day[mm]) return;else st.insert(mm*100+dd);//st存日期 …

docker 安装 oracle database 问题记录

pre本地docker (WSL)安装运行 Oracle1. 镜像处理参考链接:https://www.cnblogs.com/wuchangsoft/p/18344847 oracle 镜像获取:https://container-registry.oracle.com/ords/f?p=113:10:::::: (Oracle官网,由于部分问题导致直接pull无法拉取) 阿里云,参考链接里有个个人19…

20242103 实验一《Python程序设计》实验报告

20242103 《Python程序设计》实验1报告 课程:《Python程序设计》 班级: 2421 姓名: 李雨虓 学号:20242103 实验教师:王志强 实验日期:2025年3月12日 必修/选修: 公选课 1.实验内容: 1.熟悉Python开发环境; 2.练习Python运行、调试技能;(编写书中的程序,并进行调试…

20241313 2024-2025-2 《Python程序设计》实验一报告

20241313 2024-2025-2 《Python程序设计》实验一报告 课程:《Python程序设计》 班级: 2413 姓名: 刘鸣宇 学号:20241313 实验教师:王志强 实验日期:2025年3月12日 必修/选修: 公选课 1.实验内容 1.熟悉Python开发环境; 2.练习Python运行、调试技能;(编写书中的程序…

mutatingwebhook的简单实例

一. k8s集群准备 这里不再赘述k8s集群搭建。主要注意参数:kubectl get po kube-apiserver-server -n kube-system -o yaml | grep plugin 预期结果为:- --enable-admission-plugins=NodeRestriction,MutatingAdmissionWebhook,ValidatingAdmissionWebhook 至少要拥有两个参数…

Tauri新手向 - 基于LSB隐写的shellcode加载器

此篇是记录自己初次学习tauri开发工具,包含遇到的一些问题以及基本的知识,也给想上手rust tauri的师傅们一些小小的参考。此项目为保持免杀性暂不开源,希望各位师傅多多支持,反响可以的话后续会放出代码大家一起交流学习。ShadowMeld - 基于图像隐写技术的载荷生成框架 通过…

P2341 [USACO03FALL / HAOI2006] 受欢迎的牛 G(缩点)

P2341 [USACO03FALL / HAOI2006] 受欢迎的牛 G 题目背景 本题测试数据已修复。 题目描述 每头奶牛都梦想成为牛棚里的明星。被所有奶牛喜欢的奶牛就是一头明星奶牛。所有奶牛都是自恋狂,每头奶牛总是喜欢自己的。奶牛之间的“喜欢”是可以传递的——如果 \(A\) 喜欢 \(B\),\(…