Create a gist now

Instantly share code, notes, and snippets.

@matchey /1.cpp
Last active Jul 4, 2017

What would you like to do?
L11_0627 pointer2 example code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const char* month_db[] = {
"January",
"Feburuary",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
NULL};
int name2num(char* name)
{
int i=0;
for(i=0; month_db[i]; i++){
if(!strcmp(month_db[i], name)) break;
}
return i+1;
}
const char* num2name(int num)
{
return month_db[num-1];
}
void print(char strin[])
{
int num = atoi(strin);
if(0<num && num<=12){
printf("%s\n", num2name(num));
}else{
num = name2num(strin);
if(num<=12){
printf("%d\n", num);
}else{
puts("入力が誤っています.");
}
}
}
int main()
{
char strin[128];
printf("input: ");
scanf("%s", strin);
print(strin);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
void load(char name[][128], int score[][3])
{
const char fname[] = "as_test_result.csv";
FILE* fp;
if((fp=fopen(fname,"r"))==NULL){
printf("Open Error¥n");
exit(1);
}
int i = 0;
for(; fscanf(fp, "%[^,],%d,%d,%d\n", name[i], &score[i][0], &score[i][1], &score[i][2]) != EOF;
i++){
// printf("%s, %d, %d, %d\n", name[i], score[i][0], score[i][1], score[i][2]);
}
score[i][0] = -1;
fclose(fp);
}
void calc_sum(int score[][3], int sum[][2])
{
int i = 0;
for(i=0; score[i][0]+1; i++){
sum[i][0] = (score[i][0] + score[i][1] + score[i][2]);
sum[i][1] = i;
}
sum[i][0] = -1;
}
void write_csv(char name[][128], int sum[][2])
{
const char fname[] = "test_rank.csv";
FILE* fp;
if((fp=fopen(fname,"w"))==NULL){
printf("Open Error¥n");
exit(1);
}
int total = 0;
int i = 0;
for(; sum[i][0]+1; i++){
fprintf(fp, "%s,%d\n", name[sum[i][1]], sum[i][0]);
total += sum[i][0];
}
fprintf(fp, "average,%.2f\n", 1.0*total/i);
fclose(fp);
}
void sort(char name[][128], int sum[][2])
{
for(int i=0; sum[i][0]+1; i++){
for(int j=0; sum[j][0]+1; j++){
if(sum[i][0] > sum[j][0]){
int tmp = sum[i][1];
sum[i][1] = sum[j][1];
sum[j][1] = tmp;
int tmp1 = sum[i][0];
sum[i][0] = sum[j][0];
sum[j][0] = tmp1;
}
}
}
}
int main()
{
char name[512][128];
int score[512][3];
int sum[512][2];
load(name, score);
calc_sum(score, sum);
sort(name, sum);
write_csv(name, sum);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef double Matrix[4][4];
Matrix A = { 1, 5, 0, 0,
2, 6, 0, 0,
3, 7, 0, 111,
4, 8, 0, 0 };
void write_csv()
{
FILE* fp = NULL;
const char fname[] = "myfile.bin";
// 書き込みモードでバイナリファイルを開く
if((fp=fopen(fname,"wb"))==NULL){
printf("Open Error¥n");
exit(1);
}
int n = 16; //個数
if(fwrite(&A, sizeof(double), n, fp)!= n){
printf("write error¥n");
exit(1);
}
fclose(fp);
}
void load(Matrix &m)
{
// const char fname[] = "myfile.bin";
const char fname[] = "as_matrix_image.bin";
FILE* fp;
if((fp=fopen(fname,"rb"))==NULL){
printf("Open Error¥n");
exit(1);
}
int n = 16;
if(fread(m, sizeof(double), n, fp) != n){
printf("read error¥n");
exit(1);
}
fclose(fp);
}
int main()
{
//write_csv();
Matrix m;
load(m);
for(int i=0; i<4; i++){
for(int j=0; j<4; j++){
printf("%.1f, ", m[i][j]);
}
puts("");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment