strtok 与 fgets, atoi

#include<bits/stdc++.h>
using namespace std;
int main()
{
    char str[100];
    char  *token;
    gets(str);
   /// fgets(str,100,stdin);  字符串长度会多加1 ,算上回车了
    cout<<strlen(str);
    //cout<<str<<endl;
    token=strtok(str," ");
    while(token!=NULL)
    {
        int number=atoi(token);
        printf("%d\n",number);
         token=strtok(NULL," ");   ///指针的移动,移动到下一个分割子字符串的地址位置
    }
}

#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include "lib.h" void findBestItems(int index, int currentValue, int currentWeight, int *itemWeights, int *itemValues, int maxCapacity, int totalItems, int &highestValue, int *optimalItems, int **bestItemsList, int &bestItemCount, int *currentItems) { if (index == totalItems) { if (currentValue > highestValue) { highestValue = currentValue; bestItemCount = 1; std::memcpy(optimalItems, currentItems, sizeof(int) * totalItems); std::memcpy(bestItemsList[0], currentItems, sizeof(int) * totalItems); } else if (currentValue == highestValue) { std::memcpy(bestItemsList[bestItemCount], currentItems, sizeof(int) * totalItems); bestItemCount++; } return; } if (currentWeight + itemWeights[index] <= maxCapacity) { currentItems[index] = index + 1; findBestItems(index + 1, currentValue + itemValues[index], currentWeight + itemWeights[index], itemWeights, itemValues, maxCapacity, totalItems, highestValue, optimalItems, bestItemsList, bestItemCount, currentItems); } currentItems[index] = 0; findBestItems(index + 1, currentValue, currentWeight, itemWeights, itemValues, maxCapacity, totalItems, highestValue, optimalItems, bestItemsList, bestItemCount, currentItems); } Solution solveKnapsack(char *inputFile) { FILE *inputStream = std::fopen(inputFile, "r"); if (inputStream == NULL) { std::cout << "Error open file\n"; std::exit(1); } int maxCapacity; std::fscanf(inputStream, "%d", &maxCapacity); char inputLine[256]; std::fgets(inputLine, sizeof(inputLine), inputStream); std::fgets(inputLine, sizeof(inputLine), inputStream); char *item = std::strtok(inputLine, ","); int totalItems = 0; int itemValues[256]; while (item != NULL) { itemValues[totalItems] = std::atoi(item); item = std::strtok(NULL, ","); totalItems++; } std::fgets(inputLine, sizeof(inputLine), inputStream); item = std::strtok(inputLine, ","); int itemWeights[256]; for (int i = 0; i < totalItems; i++) { itemWeights[i] = std::atoi(item); item = std::strtok(NULL, ","); } std::fclose(inputStream); int highestValue = 0; int optimalItems[256]; int **bestItemsList = new int *[256]; for (int i = 0; i < 256; i++) { bestItemsList[i] = new int[totalItems]; } int bestItemCount = 0; int currentItems[256]; findBestItems(0, 0, 0, itemWeights, itemValues, maxCapacity, totalItems, highestValue, optimalItems, bestItemsList, bestItemCount, currentItems); Solution solution = {highestValue, bestItemsList, optimalItems, totalItems, bestItemCount}; return solution; }请帮我把这个cpp文件转换为c文件
05-22
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #define MAX_LINE_LEN 1024 #define MAX_DATA_POINTS 1024 enum { SENSOR_TYPE_YULV = 0, SENSOR_TYPE_DIANDAO, SENSOR_TYPE_PH, SENSOR_TYPE_ORP, SENSOR_TYPE_ZHOUDU, NUM_SENSOR_TYPES }; typedef struct { int point_id; int sensor_type; float value; } data_point_t; data_point_t data_points[MAX_DATA_POINTS]; int num_data_points = 0; char *sensor_type_names[NUM_SENSOR_TYPES] = { "余氯", "电导率", "PH", "ORP", "浊度" }; void save_data_points() { FILE *fp = fopen("C:\\Users\\pc\\Desktop\\test.txt", "w"); if (fp == NULL) { printf("保存数据失败\n"); return; } fprintf(fp, "检测点 传感器 数值\n"); for (int i = 0; i < num_data_points; i++) { data_point_t *p = &data_points[i]; fprintf(fp, "%d (%d) %.2f\n", p->point_id, p->sensor_type, p->value); } fclose(fp); printf("数据已保存\n"); } void load_data_points() { FILE *fp = fopen("C:\\Users\\pc\\Desktop\\test.txt", "r"); if (fp == NULL) { printf("没有找到数据文件\n"); return; } char line[MAX_LINE_LEN]; while (fgets(line, MAX_LINE_LEN, fp) != NULL) { char *fields[3]; int num_fields = 0; char *tok = strtok(line, ","); while (tok != NULL) { fields[num_fields++] = tok; tok = strtok(NULL, ","); } if (num_fields != 3) { printf("数据文件格式错误\n"); fclose(fp); return; } int point_id = atoi(fields[0]); int sensor_type = atoi(fields[1]); float value = atof(fields[2]); data_point_t *p = &data_points[num_data_points++]; p->point_id = point_id; p->sensor_type = sensor_type; p->value = value; } fclose(fp); printf("数据已加载,共%d条\n", num_data_points); }
05-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值